112 lines
3.1 KiB
JavaScript
112 lines
3.1 KiB
JavaScript
|
|
import Request from './request.js';
|
|||
|
|
import errorCode from './errorCode.js';
|
|||
|
|
let protocol = 'https'; //协议
|
|||
|
|
let baseUrl = 'app.xiaokang88.com/service/v1/'; // 域名
|
|||
|
|
export const config = {
|
|||
|
|
baseUrl: `${protocol}://${baseUrl}`
|
|||
|
|
}
|
|||
|
|
/**
|
|||
|
|
* 请求拦截器
|
|||
|
|
* @param {*} options
|
|||
|
|
*/
|
|||
|
|
const reqInterceptor = async (options) => {
|
|||
|
|
options.header = {
|
|||
|
|
...options.header
|
|||
|
|
}
|
|||
|
|
const isToken = (options.header || {}).isToken === false
|
|||
|
|
if (uni.getStorageSync('token') && !isToken) {
|
|||
|
|
options.header['authorization'] = `Bearer ${uni.getStorageSync('token')}`// 让每个请求携带自定义token 请根据实际情况自行修改
|
|||
|
|
}
|
|||
|
|
return options;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 响应拦截器
|
|||
|
|
*/
|
|||
|
|
const resInterceptor = (response, conf = {}) => {
|
|||
|
|
// 网络状态码
|
|||
|
|
const statusCode = response.statusCode|| response.errcode;
|
|||
|
|
// 响应拦截器
|
|||
|
|
if (statusCode >= 200 && statusCode < 300) {
|
|||
|
|
_responseLog(response, conf, "response 200-299")
|
|||
|
|
return response.data
|
|||
|
|
} else if (statusCode === 500) {
|
|||
|
|
uni.showToast({
|
|||
|
|
icon: 'error',
|
|||
|
|
title: response['msg'] || errorCode[statusCode]
|
|||
|
|
})
|
|||
|
|
_responseLog(response, conf, "response 500")
|
|||
|
|
// 增加一个控制字段wakaryReqToReject 使 reject的内容更加可控
|
|||
|
|
return {
|
|||
|
|
// 根据当前字段来判断是否reject
|
|||
|
|
wakaryReqToReject: true,
|
|||
|
|
// 下面可以配置其他返回信息,方便统一处理reject
|
|||
|
|
// 以下内容作为reject的返回,根据需求处理,返回具体错误信息
|
|||
|
|
msg: "服务器错误",
|
|||
|
|
res: response
|
|||
|
|
}
|
|||
|
|
} else if (statusCode === 401) {
|
|||
|
|
uni.showToast({
|
|||
|
|
icon: 'error',
|
|||
|
|
title: response['msg'] || errorCode[statusCode]
|
|||
|
|
})
|
|||
|
|
uni.removeStorageSync()
|
|||
|
|
setTimeout(()=>{
|
|||
|
|
uni.navigateTo({
|
|||
|
|
url:'/pages/login/index'
|
|||
|
|
})
|
|||
|
|
},1000)
|
|||
|
|
_responseLog(response, conf, "response 401")
|
|||
|
|
// 增加一个控制字段wakaryReqToReject 使 reject的内容更加可控
|
|||
|
|
return {
|
|||
|
|
// 根据当前字段来判断是否reject
|
|||
|
|
wakaryReqToReject: true,
|
|||
|
|
// 下面可以配置其他返回信息,方便统一处理reject
|
|||
|
|
// 以下内容作为reject的返回,根据需求处理,返回具体错误信息
|
|||
|
|
msg: response['msg'] || errorCode[statusCode],
|
|||
|
|
res: response
|
|||
|
|
}
|
|||
|
|
} else {
|
|||
|
|
console.log
|
|||
|
|
uni.showToast({
|
|||
|
|
icon: 'error',
|
|||
|
|
title: response['msg'] || errorCode[statusCode]
|
|||
|
|
})
|
|||
|
|
_responseLog(response, conf, "response 300-499");
|
|||
|
|
// 增加一个控制字段wakaryReqToReject 使 reject的内容更加可控
|
|||
|
|
return {
|
|||
|
|
// 根据当前字段来判断是否reject
|
|||
|
|
wakaryReqToReject: true,
|
|||
|
|
// 下面可以配置其他返回信息,方便统一处理reject
|
|||
|
|
// 以下内容作为reject的返回,根据需求处理,返回具体错误信息
|
|||
|
|
msg: "response 300-499",
|
|||
|
|
res: response
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* request log
|
|||
|
|
*/
|
|||
|
|
function _requestLog(req, describe = null) {
|
|||
|
|
if (process.env.NODE_ENV === 'development') {}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* response log
|
|||
|
|
*/
|
|||
|
|
function _responseLog(res, conf = {}, describe = null) {
|
|||
|
|
let _statusCode = res.statusCode;
|
|||
|
|
if (process.env.NODE_ENV === 'development') {}
|
|||
|
|
|
|||
|
|
if (_statusCode === 500) {
|
|||
|
|
// save log to server
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 请求方法封装
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
export const req = new Request(config, reqInterceptor, resInterceptor)
|