2020-08-13 08:12:57 +00:00
|
|
|
|
import {
|
|
|
|
|
HTTP_REQUEST_URL,
|
|
|
|
|
HEADER,
|
|
|
|
|
TOKENNAME,
|
|
|
|
|
HEADERPARAMS
|
|
|
|
|
} from '@/config/app';
|
|
|
|
|
import {
|
|
|
|
|
toLogin,
|
|
|
|
|
checkLogin
|
|
|
|
|
} from '../libs/login';
|
|
|
|
|
import store from '../store';
|
2023-10-16 07:06:58 +00:00
|
|
|
|
import {getTerminal} from "./util";
|
2020-08-13 08:12:57 +00:00
|
|
|
|
|
2023-07-08 04:43:29 +00:00
|
|
|
|
// TODO 芋艿:临时解决 uniapp 在小程序,undefined 会被 tostring 的问题
|
|
|
|
|
function deleteUndefinedProperties(obj) {
|
|
|
|
|
for (let key in obj) {
|
|
|
|
|
if (obj.hasOwnProperty(key)) {
|
|
|
|
|
if (typeof obj[key] === 'object' && obj[key] !== null) {
|
|
|
|
|
deleteUndefinedProperties(obj[key]); // 递归调用,处理嵌套的对象
|
|
|
|
|
} else if (obj[key] === undefined) {
|
|
|
|
|
delete obj[key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-13 08:12:57 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 发送请求
|
|
|
|
|
*/
|
|
|
|
|
function baseRequest(url, method, data, {
|
|
|
|
|
noAuth = false,
|
|
|
|
|
noVerify = false
|
|
|
|
|
}, params) {
|
|
|
|
|
let Url = HTTP_REQUEST_URL,header = HEADER
|
|
|
|
|
if (params != undefined) {
|
|
|
|
|
header = HEADERPARAMS;
|
|
|
|
|
}
|
|
|
|
|
if (!noAuth) {
|
|
|
|
|
//登录过期自动登录
|
|
|
|
|
if (!store.state.app.token && !checkLogin()) {
|
2023-10-07 14:54:08 +00:00
|
|
|
|
// debugger
|
2020-08-13 08:12:57 +00:00
|
|
|
|
toLogin();
|
|
|
|
|
return Promise.reject({
|
2020-09-15 08:13:25 +00:00
|
|
|
|
msg: '未登录'
|
2020-08-13 08:12:57 +00:00
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-06 11:12:07 +00:00
|
|
|
|
|
2023-07-08 04:43:29 +00:00
|
|
|
|
deleteUndefinedProperties(data)
|
|
|
|
|
|
2023-06-06 11:12:07 +00:00
|
|
|
|
// TODO 补个 header 多租户
|
|
|
|
|
if (url.indexOf('app-api') >= 0) {
|
|
|
|
|
header = {
|
|
|
|
|
...header
|
|
|
|
|
}
|
|
|
|
|
header['tenant-id'] = 1
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-16 07:06:58 +00:00
|
|
|
|
// 终端
|
|
|
|
|
header['terminal'] = getTerminal()
|
|
|
|
|
|
2023-10-05 18:10:56 +00:00
|
|
|
|
if (store.state.app.token) {
|
|
|
|
|
// header[TOKENNAME] = store.state.app.token;
|
|
|
|
|
header['Authorization'] = 'Bearer ' + store.state.app.token;
|
|
|
|
|
}
|
2020-08-13 08:12:57 +00:00
|
|
|
|
return new Promise((reslove, reject) => {
|
|
|
|
|
uni.request({
|
2023-07-08 04:43:29 +00:00
|
|
|
|
// url: url.indexOf('app-api') < 0 ? Url + '/api/front/' + url
|
|
|
|
|
url: url.indexOf('app-api') < 0 ? Url + '/api/front/' + url
|
2023-08-13 12:13:00 +00:00
|
|
|
|
// : 'http://yunai.natapp1.cc/' + url, // TODO 芋艿:搞个 url 的配置
|
|
|
|
|
: 'http://127.0.0.1:48080/' + url, // TODO 芋艿:搞个 url 的配置
|
2023-09-03 13:21:54 +00:00
|
|
|
|
// : 'http://api-dashboard.yudao.iocoder.cn/' + url, // TODO 芋艿:搞个 url 的配置
|
2023-08-13 12:13:00 +00:00
|
|
|
|
method: method || 'GET',
|
2020-08-13 08:12:57 +00:00
|
|
|
|
header: header,
|
|
|
|
|
data: data || {},
|
|
|
|
|
success: (res) => {
|
|
|
|
|
if (noVerify)
|
|
|
|
|
reslove(res.data, res);
|
2023-06-06 11:12:07 +00:00
|
|
|
|
else if (res.data.code === 200 || res.data.code === 0)
|
2020-08-13 08:12:57 +00:00
|
|
|
|
reslove(res.data, res);
|
|
|
|
|
else if ([410000, 410001, 410002, 401].indexOf(res.data.code) !== -1) {
|
|
|
|
|
toLogin();
|
|
|
|
|
reject(res.data);
|
|
|
|
|
} else
|
2023-09-11 08:30:12 +00:00
|
|
|
|
reject(res.data.msg || res.data.message || '系统错误');
|
2020-08-13 08:12:57 +00: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;
|