This commit is contained in:
Zy
2026-10-14 09:28:49 +08:00
parent ace7d969ac
commit 27d9fddccc
18 changed files with 4164 additions and 3890 deletions

View File

@@ -1,18 +1,34 @@
let baseUrl = '';
if (process.env.NODE_ENV === 'development') {
// baseUrl = 'https://wuyeapi.ckdzkj.com'; // 开发环境
baseUrl = "https://wuyeapi.bugtc.com"
baseUrl = 'https://wuyeapi.ckdzkj.com'; // 开发环境
// baseUrl = "http://192.168.0.222:8080"
// baseUrl = 'https://wuyeapi.ckdzkj.com';
// baseUrl = "https://apitest.ckdzkj.com"
// baseUrl = "https://wuyeapi.bugtc.com"
// baseUrl = 'https://during-immovable-eats.ngrok-free.dev';
} else {
baseUrl = 'https://wuyeapi.bugtc.com'; // 生产环境
baseUrl = 'https://wuyeapi.ckdzkj.com';
// baseUrl = 'https://apitest.ckdzkj.com';
// baseUrl = 'https://during-immovable-eats.ngrok-free.dev';
}
// 图片基础地址
// const imgBaseUrl = 'https://wuyeadmin.bugtc.com'
const imgBaseUrl = 'https://wuye.ckdzkj.com'
export {
baseUrl,
imgBaseUrl
};
/**
* 显示接口错误提示(自动处理长消息截断问题)
* @param {string|Object} msg - 错误消息或含 msg 字段的错误对象
*/
export function showApiError(msg) {
const message = typeof msg === 'string' ? msg : (msg.msg || msg.message || '请求失败');
uni.showToast({ title: message || '请求失败', icon: 'error', duration: 2000 });
uni.showToast({
title: message || '请求失败',
icon: 'error',
duration: 2000
});
}
export default function request(url, data = {}, method = "GET") {
@@ -36,7 +52,10 @@ export default function request(url, data = {}, method = "GET") {
// HTTP 失败
if (res.statusCode !== 200) {
reject({ msg: `请求异常 ${res.statusCode}`, code: res.statusCode });
reject({
msg: `请求异常 ${res.statusCode}`,
code: res.statusCode
});
return;
}
@@ -44,7 +63,11 @@ export default function request(url, data = {}, method = "GET") {
// token 过期 — 清除登录态,提示用户,但不强制跳转(用户可继续浏览非敏感页面)
if (resData.code === 401) {
uni.showToast({ title: '登录已过期,请重新登录', icon: 'none', duration: 2000 });
uni.showToast({
title: '登录已过期,请重新登录',
icon: 'none',
duration: 2000
});
uni.removeStorageSync('token');
uni.removeStorageSync('userId');
// 使用 navigateTo 而非 reLaunch,用户可通过返回按钮退出登录页
@@ -53,11 +76,16 @@ export default function request(url, data = {}, method = "GET") {
url: '/pageSubPack/loginSub/pwd-login',
fail: () => {
// 如果 navigateTo 失败(如页面栈满),则用 reLaunch
uni.reLaunch({ url: '/pageSubPack/loginSub/pwd-login' });
uni.reLaunch({
url: '/pageSubPack/loginSub/pwd-login'
});
}
});
}, 1500);
reject({ msg: '登录过期', code: 401 });
reject({
msg: '登录过期',
code: 401
});
return;
}
@@ -68,14 +96,26 @@ export default function request(url, data = {}, method = "GET") {
// 业务失败(code === 500 统一 showToast 展示完整 msg)
else {
if (resData.code === 500 && resData.msg) {
uni.showToast({ title: resData.msg, icon: 'error', duration: 2500 });
uni.showToast({
title: resData.msg,
icon: 'error',
duration: 2500
});
}
reject({ msg: resData.msg || '请求失败', code: resData.code, data: resData });
reject({
msg: resData.msg || '请求失败',
code: resData.code,
data: resData
});
}
},
fail: (err) => {
uni.hideLoading();
reject({ msg: '网络异常', code: -1, raw: err });
reject({
msg: '网络异常',
code: -1,
raw: err
});
}
});
});
@@ -99,15 +139,15 @@ export const upload = (filePath, url) => {
},
success: (res) => {
try {
const data = JSON.parse(res.data);
resolve(data.data || ''); // 直接返回图片地址
} catch (e) {
resolve('');
}
const data = JSON.parse(res.data);
resolve(data.data || ''); // 直接返回图片地址
} catch (e) {
resolve('');
}
},
fail: (err) => {
reject(err);
}
});
});
};
};