2025-03-11 09:45:29 +08:00
|
|
|
|
import _config from './config'; // 导入私有配置
|
2025-03-11 17:57:17 +08:00
|
|
|
|
// 这里service是自己做的本地信息存储封装的一个配置文件,方便管理使用
|
2025-03-11 09:45:29 +08:00
|
|
|
|
import service from './service'; // 这里是之前项目配置本地存储账号信息的,不需要可以删除
|
|
|
|
|
|
|
|
|
|
export default function $http(options) {
|
2025-03-11 17:57:17 +08:00
|
|
|
|
// 进行url字符串拼接,_config.url是再config中配置要请求的域名或者id+端口号这样方便管理,
|
|
|
|
|
// options.url是index中请求配置的,完美拼接
|
|
|
|
|
options.url = _config.url + options.url;
|
|
|
|
|
console.log(options.url)
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
// 拦截请求
|
2025-03-14 16:36:24 +08:00
|
|
|
|
// _config.header.Authorization=service.getToken()//这里是调用本地存储配置文件的方法不需要可以改下面这种方式
|
|
|
|
|
let token = uni.getStorageSync('token')
|
|
|
|
|
console.log('token',service.getToken(),uni.getStorageSync('token') )
|
2025-03-11 17:57:17 +08:00
|
|
|
|
// if(service.getStorageExpires('utn')) {
|
|
|
|
|
// _config.header.Authorization = service.getStorageExpires('utn')
|
|
|
|
|
// service.setStorageExpires('utn', _config.header.Authorization, 30)
|
|
|
|
|
// console.log('Authorization',_config.header.Authorization )
|
|
|
|
|
// }else {
|
|
|
|
|
// service.getStorageExpires('token') && uni.removeStorageSync("token")
|
|
|
|
|
// uni.redirectTo({
|
|
|
|
|
// url: "/pages/login/index"
|
|
|
|
|
// })
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
_config.header['Content-type'] = 'application/json;charset=utf-8'
|
2025-03-14 16:36:24 +08:00
|
|
|
|
_config.header['Authorization'] = "Bearer " + token
|
2025-03-11 17:57:17 +08:00
|
|
|
|
_config.timeout = 600000
|
|
|
|
|
// _config.header['Content-type'] = 'application/x-www-form-urlencoded;charset=utf-8'
|
|
|
|
|
//_config.header.Authorization=uni.getStorageSync(STATE_KEY);
|
|
|
|
|
_config.complete = (response) => {
|
|
|
|
|
console.log(response)
|
|
|
|
|
// 登录失效这边后台是返回403看情况
|
|
|
|
|
if(response.data.code === 403){
|
|
|
|
|
//返回登录界面
|
|
|
|
|
uni.navigateTo({
|
|
|
|
|
url:'/pages/login/login'
|
|
|
|
|
})
|
|
|
|
|
uni.showToast({
|
|
|
|
|
icon: 'none',
|
|
|
|
|
title: '登录已失效'
|
|
|
|
|
});
|
|
|
|
|
//清空token
|
|
|
|
|
service.addToken('')
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// request請求访问成功 这里返回的时候response.data才是请求返回的结果errorCode
|
|
|
|
|
if(response.data.code === 200){
|
|
|
|
|
resolve(response.data)
|
|
|
|
|
}else{
|
|
|
|
|
// 接口请求失败
|
|
|
|
|
resolve(response.data)
|
|
|
|
|
//console.log(response.data)
|
|
|
|
|
// 处理catch 请求,不在本页面之外处理,统一在这里处理
|
|
|
|
|
if(options.handle){
|
|
|
|
|
reject(response)
|
|
|
|
|
}else{
|
|
|
|
|
try {
|
|
|
|
|
Promise.reject(response).catch(err => {
|
|
|
|
|
//console.error(err);
|
|
|
|
|
_page_error(response.statusCode || response.errMsg);
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.log(e)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// 开始请求
|
|
|
|
|
uni.request(Object.assign({}, _config, options));
|
|
|
|
|
})
|
2025-03-11 09:45:29 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 接口錯誤
|
|
|
|
|
function _error(err, msg = '') {
|
2025-03-11 17:57:17 +08:00
|
|
|
|
switch (err) {
|
|
|
|
|
case 400:
|
|
|
|
|
console.error(msg)
|
|
|
|
|
// 错误码400的处理方式
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-03-11 09:45:29 +08:00
|
|
|
|
}
|
|
|
|
|
// request 錯誤
|
|
|
|
|
function _page_error(err) {
|
2025-03-11 17:57:17 +08:00
|
|
|
|
switch (err) {
|
|
|
|
|
case 401:
|
|
|
|
|
// 错误码404的处理方式
|
|
|
|
|
console.error("请求背拒绝")
|
|
|
|
|
break;
|
|
|
|
|
case 404:
|
|
|
|
|
// 错误码404的处理方式
|
|
|
|
|
console.error("没有找到页面")
|
|
|
|
|
break;
|
|
|
|
|
case 405:
|
|
|
|
|
console.error("错误的请求")
|
|
|
|
|
break;
|
|
|
|
|
}
|
2025-03-11 09:45:29 +08:00
|
|
|
|
}
|