OfficeApp/utils/oahttp.js

113 lines
3.0 KiB
JavaScript
Raw Normal View History

2023-07-15 17:51:20 +08:00
import {
HTTP_REQUEST_URL_THREE,
HEADER,
TOKENNAME,
} from '@/config/app';
2023-08-04 11:32:51 +08:00
import { Toast } from '../libs/uniApi';
2023-07-15 17:51:20 +08:00
// import { checkLogin } from '../libs/login';
import store from '../store';
function toLogin() {
uni.showToast({
2023-08-04 16:09:05 +08:00
title: '请先登录',
2023-07-15 17:51:20 +08:00
icon: 'none',
duration: 1000
});
}
function baseRequestTwo(url, method, data, {
noAuth = false,
noVerify = false
}) {
let Url = HTTP_REQUEST_URL_THREE,
2023-08-05 15:16:05 +08:00
header = HEADER;
2023-07-19 11:48:36 +08:00
if (!noAuth) {
2023-08-05 15:16:05 +08:00
// 已经未登录了,禁止请求
if (!store.state.config.request) return Promise.reject({
msg: '未登录'
});
//登录过期自动登录
2023-07-19 11:48:36 +08:00
if (!store.state.app.token) {
toLogin();
2023-08-05 15:16:05 +08:00
store.commit("SET_REQUEST", false);
2023-07-19 11:48:36 +08:00
return Promise.reject({
msg: '未登录'
});
}
}
2023-07-15 17:51:20 +08:00
2023-07-19 11:48:36 +08:00
// if (store.state.app.token) header[TOKENNAME] = 'Bearer ' + store.state.app.token;
if (store.state.app.token) header[TOKENNAME] = store.state.app.token;
2023-08-26 18:41:06 +08:00
// header[TOKENNAME] = 'Bearer sdjflidshjgfkbdasgjmasbgvhauuiavhkesvndkaesbvkjsdbv';
2023-07-15 17:51:20 +08:00
return new Promise((reslove, reject) => {
// uni.showLoading({
// title: '加载中'
// })
uni.request({
// url: Url + '/api/v1' + url,
url: Url + '/api' + url,
2023-07-15 17:51:20 +08:00
method: method || 'GET',
header: header,
2023-07-25 10:02:10 +08:00
data: method != 'GET' ? data || {} : {},
2023-07-19 11:48:36 +08:00
params: method == 'GET' ? data : {},
2023-07-15 17:51:20 +08:00
success: (res) => {
// uni.hideLoading()
if (noVerify)
// reslove(res.data, res);
reslove(res.data);
2023-08-05 15:16:05 +08:00
else if (res.data.code == -1) {
2023-07-21 17:17:37 +08:00
store.commit("LOGOUT");
2023-08-05 15:16:05 +08:00
store.commit("SET_REQUEST", false);
2023-07-21 17:17:37 +08:00
reject(res.data);
2023-08-05 15:16:05 +08:00
} else if (res.data.code == 0) {
// uni.hideLoading();
2023-08-04 11:32:51 +08:00
uni.showToast({
title: res.data.msg || '请检查网络',
2023-08-11 13:51:58 +08:00
icon: 'none',
2023-08-04 11:32:51 +08:00
})
reject(res.data);
2023-08-05 15:16:05 +08:00
} else if (res.data.code == 1){
store.commit("SET_REQUEST");
2023-07-15 17:51:20 +08:00
reslove(res.data);
2023-08-05 15:16:05 +08:00
}
else if (res.data.code == 200){
store.commit("SET_REQUEST");
2023-07-15 17:51:20 +08:00
reslove(res.data.data);
2023-08-05 15:16:05 +08:00
}
2023-07-15 17:51:20 +08:00
else if ([410000, 410001, 410002, 40000].indexOf(res.data.code) !== -1) {
toLogin();
reject(res.data);
} else if (res.data.code == 501) {
2023-08-04 11:32:51 +08:00
// uni.reLaunch({
// url: '/pages/error/index'
// })
2023-07-15 17:51:20 +08:00
reject(res.data);
} else {
2023-08-04 11:32:51 +08:00
// uni.hideLoading();
2023-07-15 17:51:20 +08:00
uni.showToast({
title: res.data.msg || '请检查网络',
2023-07-15 17:51:20 +08:00
icon: 'none'
})
reject(res.data.msg || '请检查网络');
2023-07-15 17:51:20 +08:00
}
},
fail: (message) => {
2023-08-04 11:32:51 +08:00
// uni.hideLoading()
2023-08-05 15:16:05 +08:00
uni.showToast({
title: '网络错误',
icon: 'none'
})
2023-07-15 17:51:20 +08:00
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;