细节优化

This commit is contained in:
kron
2025-07-28 15:05:19 +08:00
parent e116c8d22f
commit 955abbb115
7 changed files with 98 additions and 12 deletions

View File

@@ -10,6 +10,7 @@ function request(method, url, data = {}) {
method,
header,
data,
timeout: 10000,
success: (res) => {
if (res.data) {
const { code, data, message } = res.data;
@@ -55,16 +56,66 @@ function request(method, url, data = {}) {
}
},
fail: (err) => {
handleRequestError(err, url);
reject(err);
uni.showToast({
title: "接口调用失败",
icon: "none",
});
},
});
});
}
// 统一的错误处理函数
function handleRequestError(err, url) {
console.log('请求失败:', { err, url });
// 根据错误类型显示不同提示
if (err.errMsg) {
if (err.errMsg.includes('timeout')) {
showCustomToast("请求超时,请稍后重试", "timeout");
} else if (err.errMsg.includes('fail')) {
// 检查网络状态
uni.getNetworkType({
success: (res) => {
if (res.networkType === 'none') {
showCustomToast("网络连接已断开,请检查网络设置", "network");
} else {
showCustomToast("服务器连接失败,请稍后重试", "server");
}
},
fail: () => {
showCustomToast("网络异常,请检查网络连接", "unknown");
}
});
} else {
showCustomToast("请求失败,请稍后重试", "general");
}
} else {
showCustomToast("网络异常,请稍后重试", "unknown");
}
}
// 自定义提示函数
function showCustomToast(message, type) {
const config = {
title: message,
icon: "none",
duration: 3000
};
// 根据错误类型可以添加不同的处理逻辑
switch (type) {
case 'timeout':
config.duration = 4000; // 超时提示显示更久
break;
case 'network':
config.duration = 5000; // 网络问题提示显示更久
break;
default:
break;
}
uni.showToast(config);
}
// 获取全局配置
export const getAppConfig = () => {
return request("GET", "/index/appConfig");