msg
All checks were successful
构建并部署前端到生产环境 / build-and-deploy (push) Successful in 48s

This commit is contained in:
sexygoat
2026-04-29 17:48:54 +08:00
parent d679cad55f
commit cebe545440

View File

@@ -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;