修改小程序
This commit is contained in:
@@ -1,115 +1,225 @@
|
||||
// 请求工具类
|
||||
const auth = require('./auth.js')
|
||||
const apiConfig = require('../config/api.js');
|
||||
|
||||
const request = (options) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 添加认证头
|
||||
const token = auth.getToken()
|
||||
if (token) {
|
||||
options.header = {
|
||||
...options.header,
|
||||
'Authorization': `Bearer ${token}`
|
||||
/**
|
||||
* 封装的HTTP请求工具
|
||||
*/
|
||||
class Request {
|
||||
constructor() {
|
||||
this.baseURL = apiConfig.baseURL;
|
||||
this.timeout = apiConfig.timeout;
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送HTTP请求
|
||||
* @param {Object} options 请求配置
|
||||
* @returns {Promise} 请求结果
|
||||
*/
|
||||
request(options) {
|
||||
return new Promise((resolve, reject) => {
|
||||
// 获取存储的token
|
||||
const token = wx.getStorageSync('token');
|
||||
|
||||
// 构建请求头
|
||||
const header = {
|
||||
'Content-Type': 'application/json',
|
||||
...options.header
|
||||
};
|
||||
|
||||
// 如果有token,添加到请求头
|
||||
if (token) {
|
||||
header['Authorization'] = `Bearer ${token}`;
|
||||
}
|
||||
}
|
||||
|
||||
// 添加基础URL
|
||||
const app = getApp()
|
||||
if (!options.url.startsWith('http')) {
|
||||
options.url = app.globalData.baseUrl + options.url
|
||||
}
|
||||
|
||||
// 添加默认配置
|
||||
const config = {
|
||||
timeout: 10000,
|
||||
dataType: 'json',
|
||||
responseType: 'text',
|
||||
...options
|
||||
}
|
||||
|
||||
console.log('发起请求:', config)
|
||||
|
||||
wx.request({
|
||||
...config,
|
||||
success: (res) => {
|
||||
console.log('请求成功:', res)
|
||||
|
||||
// 检查响应状态
|
||||
if (res.statusCode === 200) {
|
||||
const { code, message, data } = res.data
|
||||
|
||||
// 构建完整URL
|
||||
const url = options.url.startsWith('http')
|
||||
? options.url
|
||||
: `${this.baseURL}${options.url}`;
|
||||
|
||||
// 发送请求
|
||||
wx.request({
|
||||
url,
|
||||
method: options.method || 'GET',
|
||||
data: options.data || {},
|
||||
header,
|
||||
timeout: this.timeout,
|
||||
success: (res) => {
|
||||
console.log('API请求成功:', url, res);
|
||||
|
||||
if (code === 200) {
|
||||
resolve(data)
|
||||
} else if (code === 401) {
|
||||
// token过期,清除本地存储并跳转登录
|
||||
auth.clearToken()
|
||||
wx.reLaunch({
|
||||
url: '/pages/login/login'
|
||||
})
|
||||
reject(new Error(message || '认证失败'))
|
||||
// 处理HTTP状态码
|
||||
if (res.statusCode >= 200 && res.statusCode < 300) {
|
||||
// 处理业务状态码
|
||||
if (res.data && res.data.code !== undefined) {
|
||||
if (res.data.code === 200 || res.data.code === 0) {
|
||||
resolve(res.data);
|
||||
} else {
|
||||
// 业务错误
|
||||
this.handleBusinessError(res.data);
|
||||
reject(res.data);
|
||||
}
|
||||
} else {
|
||||
// 没有业务状态码,直接返回数据
|
||||
resolve(res.data);
|
||||
}
|
||||
} else {
|
||||
reject(new Error(message || '请求失败'))
|
||||
// HTTP错误
|
||||
this.handleHttpError(res);
|
||||
reject(res);
|
||||
}
|
||||
} else {
|
||||
reject(new Error(`请求失败: ${res.statusCode}`))
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('API请求失败:', url, err);
|
||||
this.handleNetworkError(err);
|
||||
reject(err);
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
console.error('请求失败:', err)
|
||||
reject(new Error(err.errMsg || '网络请求失败'))
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* GET请求
|
||||
*/
|
||||
get(url, data = {}, options = {}) {
|
||||
return this.request({
|
||||
url,
|
||||
method: 'GET',
|
||||
data,
|
||||
...options
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* POST请求
|
||||
*/
|
||||
post(url, data = {}, options = {}) {
|
||||
return this.request({
|
||||
url,
|
||||
method: 'POST',
|
||||
data,
|
||||
...options
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* PUT请求
|
||||
*/
|
||||
put(url, data = {}, options = {}) {
|
||||
return this.request({
|
||||
url,
|
||||
method: 'PUT',
|
||||
data,
|
||||
...options
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* DELETE请求
|
||||
*/
|
||||
delete(url, data = {}, options = {}) {
|
||||
return this.request({
|
||||
url,
|
||||
method: 'DELETE',
|
||||
data,
|
||||
...options
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理业务错误
|
||||
*/
|
||||
handleBusinessError(data) {
|
||||
const message = data.message || '请求失败';
|
||||
|
||||
// 特殊错误码处理
|
||||
switch (data.code) {
|
||||
case 401:
|
||||
// 未授权,清除token并跳转登录
|
||||
wx.removeStorageSync('token');
|
||||
wx.removeStorageSync('userInfo');
|
||||
wx.showToast({
|
||||
title: '登录已过期,请重新登录',
|
||||
icon: 'none'
|
||||
});
|
||||
setTimeout(() => {
|
||||
wx.reLaunch({
|
||||
url: '/pages/login/login'
|
||||
});
|
||||
}, 1500);
|
||||
break;
|
||||
case 403:
|
||||
wx.showToast({
|
||||
title: '权限不足',
|
||||
icon: 'none'
|
||||
});
|
||||
break;
|
||||
default:
|
||||
wx.showToast({
|
||||
title: message,
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理HTTP错误
|
||||
*/
|
||||
handleHttpError(res) {
|
||||
let message = '网络请求失败';
|
||||
|
||||
switch (res.statusCode) {
|
||||
case 400:
|
||||
message = '请求参数错误';
|
||||
break;
|
||||
case 401:
|
||||
message = '未授权访问';
|
||||
break;
|
||||
case 403:
|
||||
message = '禁止访问';
|
||||
break;
|
||||
case 404:
|
||||
message = '请求地址不存在';
|
||||
break;
|
||||
case 500:
|
||||
message = '服务器内部错误';
|
||||
break;
|
||||
case 502:
|
||||
message = '网关错误';
|
||||
break;
|
||||
case 503:
|
||||
message = '服务不可用';
|
||||
break;
|
||||
case 504:
|
||||
message = '网关超时';
|
||||
break;
|
||||
}
|
||||
|
||||
wx.showToast({
|
||||
title: message,
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理网络错误
|
||||
*/
|
||||
handleNetworkError(err) {
|
||||
let message = '网络连接失败';
|
||||
|
||||
if (err.errMsg) {
|
||||
if (err.errMsg.includes('timeout')) {
|
||||
message = '请求超时';
|
||||
} else if (err.errMsg.includes('fail')) {
|
||||
message = '网络连接失败';
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
// GET请求
|
||||
const get = (url, params = {}) => {
|
||||
const queryString = Object.keys(params)
|
||||
.map(key => `${encodeURIComponent(key)}=${encodeURIComponent(params[key])}`)
|
||||
.join('&')
|
||||
|
||||
const fullUrl = queryString ? `${url}?${queryString}` : url
|
||||
|
||||
return request({
|
||||
url: fullUrl,
|
||||
method: 'GET'
|
||||
})
|
||||
}
|
||||
|
||||
// POST请求
|
||||
const post = (url, data = {}) => {
|
||||
return request({
|
||||
url,
|
||||
method: 'POST',
|
||||
data,
|
||||
header: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
|
||||
wx.showToast({
|
||||
title: message,
|
||||
icon: 'none'
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
// PUT请求
|
||||
const put = (url, data = {}) => {
|
||||
return request({
|
||||
url,
|
||||
method: 'PUT',
|
||||
data,
|
||||
header: {
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
})
|
||||
}
|
||||
// 创建实例
|
||||
const request = new Request();
|
||||
|
||||
// DELETE请求
|
||||
const del = (url) => {
|
||||
return request({
|
||||
url,
|
||||
method: 'DELETE'
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
get,
|
||||
post,
|
||||
put,
|
||||
delete: del
|
||||
}
|
||||
module.exports = request;
|
||||
Reference in New Issue
Block a user