You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
1.5 KiB
61 lines
1.5 KiB
![]()
1 month ago
|
import storage from '../storage/index.js';
|
||
|
import useUserStore from '@/jtools/store/user';
|
||
|
|
||
|
//把配置项单独处理
|
||
|
let server_url = ' '; // 请求地址
|
||
|
let token = ' '; // 凭证
|
||
|
|
||
|
server_url = import.meta.env.VITE_APP_BASE_API; //环境配置
|
||
|
function service(options = {}) {
|
||
|
storage.get('token') && (token = storage.get('token'));
|
||
|
options.url = `${server_url}${options.url}`;
|
||
|
if (!options.noToken) {
|
||
|
if (!!options.publicApi) {
|
||
|
if (token.trim()) {
|
||
|
options.header = {
|
||
|
Authorization: `${token}`
|
||
|
};
|
||
|
}
|
||
|
} else if (!token.trim()) {
|
||
|
uni.navigateTo({
|
||
|
url: '/pages/me/login'
|
||
|
});
|
||
|
} else {
|
||
|
options.header = {
|
||
|
Authorization: `${token}`
|
||
|
};
|
||
|
}
|
||
|
}
|
||
|
return new Promise((resolved, rejected) => {
|
||
|
//成功
|
||
|
options.success = res => {
|
||
|
if (res.data.code == 'E403') {
|
||
|
// 未登录
|
||
|
uni.showToast({
|
||
|
title: res?.data?.message || '请重新登录',
|
||
|
icon: 'none'
|
||
|
});
|
||
|
useUserStore().logoutWithoutToken();
|
||
|
//请求成功
|
||
|
resolved(res.data);
|
||
|
} else if (res.data.code != '0000' && res.data.code != '4001' && res.data.code != 200) {
|
||
|
uni.hideLoading();
|
||
|
uni.showToast({
|
||
|
title: res?.data?.message || '访问出错',
|
||
|
icon: 'none'
|
||
|
});
|
||
|
resolved(res.data);
|
||
|
} else {
|
||
|
//请求成功
|
||
|
resolved(res.data);
|
||
|
}
|
||
|
};
|
||
|
//错误
|
||
|
options.fail = err => {
|
||
|
rejected(err); //错误
|
||
|
};
|
||
|
uni.request(options);
|
||
|
});
|
||
|
}
|
||
|
export default service;
|