OA/utils/logistics.js

118 lines
2.8 KiB
JavaScript
Raw Permalink Normal View History

2023-11-05 00:11:08 +08:00
import {
HTTP_REQUEST_URL_TWO,
HEADER,
TOKENNAME,
} from '@/config/app';
import {
Toast
} from '../libs/uniApi';
// import { checkLogin } from '../libs/login';
import store from '../store';
function toLogin() {
uni.showToast({
title: '请先登录',
icon: 'none',
duration: 1000
});
}
function baseRequestTwo(url, method, data, {
noAuth = false,
noVerify = false
}) {
let Url = HTTP_REQUEST_URL_TWO,
header = HEADER;
if (!noAuth) {
// 已经未登录了,禁止请求
if (!store.state.config.request) return Promise.reject({
msg: '未登录'
});
//登录过期自动登录
if (!store.state.app.token) {
toLogin();
store.commit("SET_REQUEST", false);
return Promise.reject({
msg: '未登录'
});
}
}
// if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
if (store.state.app.token) header[TOKENNAME] = store.state.app.token;
// header[TOKENNAME] = 'Bearer sdjflidshjgfkbdasgjmasbgvhauuiavhkesvndkaesbvkjsdbv';
return new Promise((reslove, reject) => {
// uni.showLoading({
// title: '加载中'
// })
uni.request({
// url: Url + '/api/v1' + url,
url: Url + '/api' + url,
method: method || 'GET',
header: {
...header
},
data: method != 'GET' ? data || {} : {},
params: method == 'GET' ? data : {},
success: (res) => {
// uni.hideLoading()
if (noVerify)
// reslove(res.data, res);
reslove(res.data);
else if (res.data.code == -1) {
store.commit("LOGOUT");
store.commit("SET_REQUEST", false);
reject(res.data);
} else if (res.data.code == 0) {
// uni.hideLoading();
if (res.data.show != 1 && res.data.msg != '无登录信息') {
uni.showToast({
title: res.data.msg || '请检查网络',
icon: 'none',
})
}
reject(res.data);
} else if (res.data.code == 1) {
store.commit("SET_REQUEST");
reslove(res.data);
} else if (res.data.code == 200) {
store.commit("SET_REQUEST");
reslove(res.data.data);
} else if ([410000, 410001, 410002, 40000].indexOf(res.data.code) !== -1) {
toLogin();
reject(res.data);
} else if (res.data.code == 501) {
// uni.reLaunch({
// url: '/pages/error/index'
// })
reject(res.data);
} else {
// uni.hideLoading();
uni.showToast({
title: res.data.msg || '请检查网络',
icon: 'none'
})
reject(res.data.msg || '请检查网络');
}
},
fail: (message) => {
// uni.hideLoading()
uni.showToast({
title: '网络错误',
icon: 'none'
})
reject('请求失败');
}
})
});
}
const oahttp = {};
['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
oahttp[method] = (api, data, opt) => baseRequestTwo(api, method, data, opt || {})
});
export default oahttp;