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