This commit is contained in:
@@ -1,5 +1,13 @@
|
||||
import { BASE_URL } from './env.js';
|
||||
|
||||
const showErrorToast = (message) => {
|
||||
uni.showToast({
|
||||
title: message || '请求失败,请稍后重试',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
};
|
||||
|
||||
const request = (options) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
const token = uni.getStorageSync('token') || '';
|
||||
@@ -14,39 +22,36 @@ const request = (options) => {
|
||||
...options.header
|
||||
},
|
||||
success: (res) => {
|
||||
const responseData = res.data || {};
|
||||
const { code, msg, data } = responseData;
|
||||
|
||||
if (res.statusCode === 401) {
|
||||
uni.removeStorageSync('token');
|
||||
uni.removeStorageSync('identifier');
|
||||
uni.showToast({
|
||||
title: '登录已过期,请重新登录',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
showErrorToast(msg || '登录已过期,请重新登录');
|
||||
setTimeout(() => {
|
||||
uni.reLaunch({ url: '/pages/login/login' });
|
||||
}, 2000);
|
||||
reject(res.data);
|
||||
reject(responseData);
|
||||
return;
|
||||
}
|
||||
|
||||
const { code, msg, data } = res.data;
|
||||
|
||||
if (code === 0 || code === 200) {
|
||||
if (code === 0) {
|
||||
resolve(data);
|
||||
} else {
|
||||
reject(res.data);
|
||||
return;
|
||||
}
|
||||
|
||||
if (options.showError !== false) {
|
||||
showErrorToast(msg);
|
||||
}
|
||||
reject(responseData);
|
||||
},
|
||||
fail: (err) => {
|
||||
uni.showToast({
|
||||
title: '网络请求失败',
|
||||
icon: 'none',
|
||||
duration: 2000
|
||||
});
|
||||
showErrorToast('网络请求失败');
|
||||
reject(err);
|
||||
}
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
export default request;
|
||||
export default request;
|
||||
|
||||
Reference in New Issue
Block a user