Files
frontend-app/utils/request.js
T

114 lines
3.5 KiB
JavaScript
Raw Normal View History

2025-08-06 20:25:41 +08:00
import { getStorageFun, TOKEN_NAME } from './auth.js';
import { BASE_URL, Authorization } from '@/utils/config.js'
2025-08-05 15:25:23 +08:00
// utils/http.js (修改后的版本)
// const baseUrl = 'http://192.168.0.108:8087';
function request(options) {
2025-08-06 20:25:41 +08:00
const { url, method = 'GET', data = {}, headers = {}, isShowLoading = true } = options;
2025-08-23 14:25:27 +08:00
2025-08-05 15:25:23 +08:00
// const token = uni.getStorageSync(tokenKey); // 获取token, 根据实际情况调整获取方式
const token = getStorageFun(TOKEN_NAME); // 获取token, 根据实际情况调整获取方式
2025-08-16 01:03:25 +08:00
let defaultHeaders = {
2025-08-06 20:25:41 +08:00
"Authorization": Authorization,
2025-08-05 15:25:23 +08:00
'HSM-AUTH': token ? token : '', // 如果存在token, 则添加到请求头中
2025-08-06 20:25:41 +08:00
'content-type': ['post', 'POST'].includes(method) ? 'application/x-www-form-urlencoded' : 'application/json',
2025-08-05 15:25:23 +08:00
};
2025-08-23 14:25:27 +08:00
if (options.cha) {
defaultHeaders['content-type'] = 'application/json'
2025-08-16 01:03:25 +08:00
}
2025-08-06 20:25:41 +08:00
if (token) {
2025-08-05 15:25:23 +08:00
defaultHeaders['HSM-AUTH'] = token;
}
return new Promise((resolve, reject) => {
2025-08-06 20:25:41 +08:00
if (isShowLoading) {
2025-08-05 15:25:23 +08:00
// 请求之前做一些事
uni.showLoading({
title: "加载中...",
})
}
uni.request({
url: BASE_URL + url,
method,
data,
header: {
...defaultHeaders,
...headers, // 使用展开运算符合并默认头和自定义头
},
success: (res) => {
2025-08-06 20:25:41 +08:00
if (isShowLoading) {
2025-08-05 15:25:23 +08:00
// 请求之前做一些事
uni.hideLoading();
}
2025-08-06 20:25:41 +08:00
2025-08-05 15:25:23 +08:00
let data = res.data;
2025-08-06 20:25:41 +08:00
if (data && typeof (data) === "string") {
2025-08-05 15:25:23 +08:00
data = JSON.parse(data);
}
2025-08-06 20:25:41 +08:00
if (data.status === 500) {
2025-08-05 15:25:23 +08:00
uni.showToast({
title: "系统异常,请稍后再试",
icon: 'none', // 可选 success/loading/none
duration: 2000, // 持续时长,单位ms
mask: false, // 是否显示透明蒙层,防止触摸穿透
});
return;
}
if (data.bizcode === 100) {
resolve(data); // 成功时返回数据
} else if (data.bizcode === 201) {
2025-08-06 20:25:41 +08:00
if ([201, 200].includes(data.errcode)) {
2025-08-05 15:25:23 +08:00
uni.showToast({
title: '登录失效,请重新登录',
icon: 'none', // 可选 success/loading/none
duration: 2000, // 持续时长,单位ms
mask: false, // 是否显示透明蒙层,防止触摸穿透
position: 'middle' // 位置,可选 top/middle/bottom
});
2025-08-06 20:25:41 +08:00
} else {
2025-08-05 15:25:23 +08:00
uni.showToast({
title: data.msg,
icon: 'none', // 可选 success/loading/none
duration: 2000, // 持续时长,单位ms
mask: false, // 是否显示透明蒙层,防止触摸穿透
position: 'middle' // 位置,可选 top/middle/bottom
});
}
/**
* 205:已经有账号,返回登录页面
* 200 or 201:token失效,返回登录页面
*/
2025-08-06 20:25:41 +08:00
if ([201, 200, 205].includes(data.errcode)) {
2025-08-05 15:25:23 +08:00
setTimeout(() => {
uni.navigateTo({
2025-08-06 20:25:41 +08:00
url: '/pages/login_package/login/login'
2025-08-05 15:25:23 +08:00
})
}, 2000);
}
} else {
uni.showToast({
title: data.msg,
icon: 'none', // 可选 success/loading/none
duration: 2000, // 持续时长,单位ms
});
reject(data); // 其他错误情况返回错误信息
}
},
fail: (error) => {
2025-08-06 20:25:41 +08:00
if (isShowLoading) {
2025-08-05 15:25:23 +08:00
// 请求之前做一些事
uni.hideLoading();
}
uni.showToast({
title: "服务器异常,稍后再试",
icon: 'none', // 可选 success/loading/none
duration: 2000, // 持续时长,单位ms
});
// reject(error); // 请求失败时返回错误信息
},
});
});
}
export default request;