cyywl_server/yudao-ui-app/utils/request.js

69 lines
1.4 KiB
JavaScript
Raw Normal View History

2023-05-15 10:12:22 +08:00
import {
HTTP_REQUEST_URL,
HEADER,
TOKENNAME,
HEADERPARAMS
} from '@/config/app';
import {
toLogin,
checkLogin
} from '../libs/login';
import store from '../store';
/**
* 发送请求
*/
function baseRequest(url, method, data, {
noAuth = false,
noVerify = false
}, params) {
let Url = HTTP_REQUEST_URL,header = HEADER
2023-05-15 18:19:03 +08:00
// if (params != undefined) {
// header = HEADERPARAMS;
// }
header['tenant-id'] = 1
2023-05-15 10:12:22 +08:00
if (!noAuth) {
//登录过期自动登录
if (!store.state.app.token && !checkLogin()) {
toLogin();
return Promise.reject({
msg: '未登录'
});
}
}
2023-05-16 18:00:52 +08:00
if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
2023-05-15 10:12:22 +08:00
return new Promise((reslove, reject) => {
uni.request({
2023-05-15 18:19:03 +08:00
url: Url + '/app-api/' + url,
2023-05-15 10:12:22 +08:00
method: method || 'GET',
header: header,
data: data || {},
success: (res) => {
if (noVerify)
reslove(res.data, res);
2023-05-15 18:19:03 +08:00
else if (res.data.code == 0)
2023-05-15 10:12:22 +08:00
reslove(res.data, res);
else if ([410000, 410001, 410002, 401].indexOf(res.data.code) !== -1) {
toLogin();
reject(res.data);
} else
2023-05-15 18:19:03 +08:00
reject(res.data.msg || '系统错误');
2023-05-15 10:12:22 +08:00
},
fail: (msg) => {
reject('请求失败');
}
})
});
}
const request = {};
['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
request[method] = (api, data, opt, params) => baseRequest(api, method, data, opt || {}, params)
});
export default request;