OfficeApp/utils/oahttp.js

127 lines
3.0 KiB
JavaScript
Raw Permalink Normal View History

2023-07-15 17:51:20 +08:00
import {
2023-11-13 17:59:22 +08:00
HTTP_REQUEST_URL_THREE,
HEADER,
TOKENNAME,
2023-07-15 17:51:20 +08:00
} from '@/config/app';
2023-08-31 10:44:45 +08:00
import {
2023-11-13 17:59:22 +08:00
Toast
2023-08-31 10:44:45 +08:00
} from '../libs/uniApi';
2023-07-15 17:51:20 +08:00
// import { checkLogin } from '../libs/login';
import store from '../store';
function toLogin() {
2023-11-13 17:59:22 +08:00
uni.showToast({
title: '请先登录',
icon: 'none',
duration: 1000
});
2023-07-15 17:51:20 +08:00
}
function baseRequestTwo(url, method, data, {
2023-11-13 17:59:22 +08:00
noAuth = false,
noVerify = false,
onReLogin = false
2023-07-15 17:51:20 +08:00
}) {
2023-11-13 17:59:22 +08:00
let Url = HTTP_REQUEST_URL_THREE,
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: '未登录'
});
}
}
2023-08-31 10:44:45 +08:00
2023-11-13 17:59:22 +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-07-15 17:51:20 +08:00
2023-11-13 17:59:22 +08:00
// 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) => {
if (noVerify)
reslove(res.data);
else if (res.data.code == -1) {
if (onReLogin) {
store.commit('LOGOUT');
return reject();
}
// 如果登录超时,自动重新登录并且继续发送请求
store.dispatch("RE_LOGIN", {
url: url,
method: method,
data: data,
opt: {
noAuth,
noVerify
}
}).then((e) => {
reslove(e);
}).catch((err) => {
reject(res.data);
})
// store.commit("SET_REQUEST", false);
} else if (res.data.code == 0) {
if (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) {
reject(res.data);
} else {
uni.showToast({
title: res.data.msg || '请检查网络',
icon: 'none'
})
reject(res.data.msg || '请检查网络');
}
},
fail: (message) => {
// uni.hideLoading()
uni.showToast({
title: '网络错误',
icon: 'none'
})
reject('请求失败');
}
})
});
2023-07-15 17:51:20 +08:00
}
const oahttp = {};
['options', 'get', 'post', 'put', 'head', 'delete', 'trace', 'connect'].forEach((method) => {
2023-11-13 17:59:22 +08:00
oahttp[method] = (api, data, opt) => baseRequestTwo(api, method, data, opt || {})
2023-07-15 17:51:20 +08:00
});
export default oahttp;