init
This commit is contained in:
85
common/http/interface.js
Normal file
85
common/http/interface.js
Normal file
@@ -0,0 +1,85 @@
|
||||
import _config from './config'; // 导入私有配置
|
||||
|
||||
export default function $http(options) {
|
||||
options.url = _config.url + options.url;
|
||||
if (options.type) _config.header['Content-Type'] = options.type;
|
||||
if (uni.getStorageSync("tourism_token")) {
|
||||
_config.header.Authorization = 'Bearer ' + uni.getStorageSync("tourism_token");
|
||||
} else _config.header.Authorization = '';
|
||||
return new Promise((resolve, reject) => {
|
||||
// 进行url字符串拼接
|
||||
// 拦截请求
|
||||
_config.complete = (response) => {
|
||||
// request請求访问成功
|
||||
if (response.statusCode === 200 || response.statusCode === 403) {
|
||||
// 接口请求成功
|
||||
if([401,403,404,405].includes(response.data.code)){
|
||||
_page_error(response.data.code , response.data.msg, options.url);
|
||||
}else{
|
||||
if(response.data.code!=200){
|
||||
if(response.data.msg && (response.data.msg.indexOf("用户不存在,请重新登录")!=-1 || response.data.msg.indexOf("请重新登录后进行操作")!=-1)){
|
||||
uni.removeStorageSync("tourism_token");
|
||||
uni.reLaunch({
|
||||
url:"/pages/index/index"
|
||||
})
|
||||
}else{
|
||||
_page_error(response.data.code , response.data.msg, options.url);
|
||||
}
|
||||
resolve(response.data);
|
||||
}else{
|
||||
resolve(response.data);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// 处理catch 请求,不在本页面之外处理,统一在这里处理
|
||||
if (options.handle) {
|
||||
reject(response)
|
||||
} else {
|
||||
try {
|
||||
Promise.reject(response).catch(err => {
|
||||
// console.error(err);
|
||||
_page_error(response.statusCode , response.errMsg, options.url);
|
||||
});
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
// 开始请求
|
||||
uni.request(Object.assign({}, _config, options));
|
||||
})
|
||||
}
|
||||
// request 錯誤
|
||||
function _page_error(err, msg, response) {
|
||||
switch (err) {
|
||||
case 401:
|
||||
// 错误码404的处理方式
|
||||
uni.showToast({ title: '登录已过期,请重新登录', icon: 'none', mask: true })
|
||||
uni.removeStorageSync("tourism_token");
|
||||
setTimeout(() => {
|
||||
uni.reLaunch({
|
||||
url: "/pages/login/login"
|
||||
})
|
||||
}, 1500)
|
||||
break;
|
||||
case 403:
|
||||
uni.showToast({
|
||||
title: "您没有权限,请联系管理员",
|
||||
icon: "none",
|
||||
mask: true
|
||||
})
|
||||
break;
|
||||
case 404:
|
||||
uni.showToast({ title: '请求页面不存在,请联系管理员', icon: 'none', mask: true })
|
||||
// 错误码404的处理方式
|
||||
break;
|
||||
case 405:
|
||||
console.error("错误的请求")
|
||||
break;
|
||||
case 500:
|
||||
if (response.indexOf('/api/userCollect/HandVerification') == -1 && response.indexOf('/api/device/handheld/toVerification') == -1 && response.indexOf('/api/userCollect/toVerification') == -1) uni.showToast({ title: msg, icon: 'none', mask: true });
|
||||
break;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user