project/utils/request1.js

76 lines
2.0 KiB
JavaScript
Raw Normal View History

2024-07-14 15:48:34 +08:00
import systemJs from './system';
// import authLogin from './autuLogin.js';
import {
HEADER,
TOKENNAME,
HTTP_REQUEST_URL
} from './../config.js';
/**
* 发送请求
*/
export default function request(api, method, data, {
noAuth = false,
noVerify = false
}) {
let Url = HTTP_REQUEST_URL,
header = HEADER;
if (uni.getStorageSync('token')) header[TOKENNAME] = 'Bearer ' + uni.getStorageSync('token');
// header[TOKENNAME] = 'Bearer ' + '2ddc11ec-7b63-44d5-a22a-18b8b451ec1d'
return new Promise((reslove, reject) => {
uni.request({
url: Url + api,
method: method || 'GET',
header: header,
data: data || {},
success: (res) => {
if (res.data.code == 0)
reslove(res.data, res);
else if (res.data.code == 200)
reslove(res.data, res);
else if (res.data.code == 402)
reslove(res.data, res);
else if (res.data.code == 401) {
uni.removeStorageSync('token')
uni.removeStorageSync('userInfo');
uni.removeStorageSync('firstEnter');
// systemJs.debounce(
// uni.showModal({
// title: '提示',
// content: '您暂未登入或者token已过期,是否前往登入?',
// // showCancel: false,//没有取消按钮的弹框
// success: function(res) {
// if (res.confirm) {
// uni.navigateTo({
// url: '/pages/account/login'
// })
// } else {
// console.log("你点击了取消");
// }
// }
// })
// , 2000)
} else if ([410000, 410001, 410002].indexOf(res.data.code) !== -1) {
return res.data.msg
} else if (res.data.code == 500) {
if(res.data.msg == null ){
res.data.msg = '系统错误'
}
reslove(res.data, res);
} else
reject(res.data.msg || '系统错误');
},
fail: (msg) => {
reject('操作失败');
}
})
});
}
['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
request[method] = (api, data, opt) => request(api, method, data, opt || {})
});