Binary file not shown.
|
Before Width: | Height: | Size: 57 KiB |
+103
-132
@@ -1,142 +1,113 @@
|
|||||||
import { getStorageFun, TOKEN_NAME } from "./auth.js";
|
import { getStorageFun, TOKEN_NAME } from './auth.js';
|
||||||
import { BASE_URL, Authorization } from "@/utils/config.js";
|
import { BASE_URL, Authorization } from '@/utils/config.js'
|
||||||
|
|
||||||
// utils/http.js (修改后的版本)
|
// utils/http.js (修改后的版本)
|
||||||
// const baseUrl = 'http://192.168.0.108:8087';
|
// const baseUrl = 'http://192.168.0.108:8087';
|
||||||
|
|
||||||
function request(options) {
|
function request(options) {
|
||||||
const {
|
const { url, method = 'GET', data = {}, headers = {}, isShowLoading = true } = options;
|
||||||
url,
|
|
||||||
method = "GET",
|
|
||||||
data = {},
|
|
||||||
headers = {},
|
|
||||||
isShowLoading = true,
|
|
||||||
} = options;
|
|
||||||
|
|
||||||
// const token = uni.getStorageSync(tokenKey); // 获取token, 根据实际情况调整获取方式
|
// const token = uni.getStorageSync(tokenKey); // 获取token, 根据实际情况调整获取方式
|
||||||
const token = getStorageFun(TOKEN_NAME); // 获取token, 根据实际情况调整获取方式
|
const token = getStorageFun(TOKEN_NAME); // 获取token, 根据实际情况调整获取方式
|
||||||
let defaultHeaders = {
|
let defaultHeaders = {
|
||||||
Authorization: Authorization,
|
"Authorization": Authorization,
|
||||||
"HSM-AUTH": token ? token : "", // 如果存在token, 则添加到请求头中
|
'HSM-AUTH': token ? token : '', // 如果存在token, 则添加到请求头中
|
||||||
"content-type": ["post", "POST"].includes(method)
|
'content-type': ['post', 'POST'].includes(method) ? 'application/x-www-form-urlencoded' : 'application/json',
|
||||||
? "application/x-www-form-urlencoded"
|
};
|
||||||
: "application/json",
|
if (options.cha) {
|
||||||
};
|
defaultHeaders['content-type'] = 'application/json'
|
||||||
if (options.cha) {
|
}
|
||||||
defaultHeaders["content-type"] = "application/json";
|
if (token) {
|
||||||
}
|
defaultHeaders['HSM-AUTH'] = token;
|
||||||
if (token) {
|
}
|
||||||
defaultHeaders["HSM-AUTH"] = token;
|
return new Promise((resolve, reject) => {
|
||||||
}
|
if (isShowLoading) {
|
||||||
return new Promise((resolve, reject) => {
|
// 请求之前做一些事
|
||||||
if (isShowLoading) {
|
uni.showLoading({
|
||||||
// 请求之前做一些事:显示自定义GIF加载
|
title: "加载中...",
|
||||||
const loadingDiv = document.createElement("div");
|
})
|
||||||
loadingDiv.id = "custom-loading";
|
}
|
||||||
loadingDiv.style.cssText = `
|
uni.request({
|
||||||
position: fixed;
|
url: BASE_URL + url,
|
||||||
top: 50%;
|
method,
|
||||||
left: 50%;
|
data,
|
||||||
transform: translate(-50%, -50%);
|
header: {
|
||||||
width: 300rpx;
|
...defaultHeaders,
|
||||||
height: 300rpx;
|
...headers, // 使用展开运算符合并默认头和自定义头
|
||||||
background: rgba(0, 0, 0, 0.3);
|
},
|
||||||
z-index: 9999;
|
success: (res) => {
|
||||||
border-radius: 16px;
|
if (isShowLoading) {
|
||||||
`;
|
// 请求之前做一些事
|
||||||
const img = new Image();
|
uni.hideLoading();
|
||||||
img.src = "/static/common/loding.gif"; // 使用指定路径的GIF图片
|
}
|
||||||
img.style.width = "80px"; // 调整GIF尺寸
|
|
||||||
img.style.height = "80px";
|
|
||||||
loadingDiv.appendChild(img);
|
|
||||||
document.body.appendChild(loadingDiv);
|
|
||||||
}
|
|
||||||
uni.request({
|
|
||||||
url: BASE_URL + url,
|
|
||||||
method,
|
|
||||||
data,
|
|
||||||
header: {
|
|
||||||
...defaultHeaders,
|
|
||||||
...headers, // 使用展开运算符合并默认头和自定义头
|
|
||||||
},
|
|
||||||
success: (res) => {
|
|
||||||
if (isShowLoading) {
|
|
||||||
// 请求完成:隐藏自定义GIF加载
|
|
||||||
const loadingDiv = document.getElementById("custom-loading");
|
|
||||||
if (loadingDiv && document.body.contains(loadingDiv)) {
|
|
||||||
document.body.removeChild(loadingDiv);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
let data = res.data;
|
let data = res.data;
|
||||||
if (data && typeof data === "string") {
|
if (data && typeof (data) === "string") {
|
||||||
data = JSON.parse(data);
|
data = JSON.parse(data);
|
||||||
}
|
}
|
||||||
if (data.status === 500) {
|
if (data.status === 500) {
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: "系统异常,请稍后再试",
|
title: "系统异常,请稍后再试",
|
||||||
icon: "none", // 可选 success/loading/none
|
icon: 'none', // 可选 success/loading/none
|
||||||
duration: 2000, // 持续时长,单位ms
|
duration: 2000, // 持续时长,单位ms
|
||||||
mask: false, // 是否显示透明蒙层,防止触摸穿透
|
mask: false, // 是否显示透明蒙层,防止触摸穿透
|
||||||
});
|
});
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (data.bizcode === 100) {
|
if (data.bizcode === 100) {
|
||||||
resolve(data); // 成功时返回数据
|
resolve(data); // 成功时返回数据
|
||||||
} else if (data.bizcode === 201) {
|
} else if (data.bizcode === 201) {
|
||||||
if ([201, 200].includes(data.errcode)) {
|
if ([201, 200].includes(data.errcode)) {
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: "登录失效,请重新登录",
|
title: '登录失效,请重新登录',
|
||||||
icon: "none", // 可选 success/loading/none
|
icon: 'none', // 可选 success/loading/none
|
||||||
duration: 2000, // 持续时长,单位ms
|
duration: 2000, // 持续时长,单位ms
|
||||||
mask: false, // 是否显示透明蒙层,防止触摸穿透
|
mask: false, // 是否显示透明蒙层,防止触摸穿透
|
||||||
position: "middle", // 位置,可选 top/middle/bottom
|
position: 'middle' // 位置,可选 top/middle/bottom
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: data.msg,
|
title: data.msg,
|
||||||
icon: "none", // 可选 success/loading/none
|
icon: 'none', // 可选 success/loading/none
|
||||||
duration: 2000, // 持续时长,单位ms
|
duration: 2000, // 持续时长,单位ms
|
||||||
mask: false, // 是否显示透明蒙层,防止触摸穿透
|
mask: false, // 是否显示透明蒙层,防止触摸穿透
|
||||||
position: "middle", // 位置,可选 top/middle/bottom
|
position: 'middle' // 位置,可选 top/middle/bottom
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
/**
|
/**
|
||||||
* 205:已经有账号,返回登录页面
|
* 205:已经有账号,返回登录页面
|
||||||
* 200 or 201:token失效,返回登录页面
|
* 200 or 201:token失效,返回登录页面
|
||||||
*/
|
*/
|
||||||
if ([201, 200, 205].includes(data.errcode)) {
|
if ([201, 200, 205].includes(data.errcode)) {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
uni.navigateTo({
|
uni.navigateTo({
|
||||||
url: "/pages/login_package/login/login",
|
url: '/pages/login_package/login/login'
|
||||||
});
|
})
|
||||||
}, 2000);
|
}, 2000);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
title: data.msg || "系统错误,请稍后再试",
|
title: data.msg || "系统错误,请稍后再试",
|
||||||
icon: "none", // 可选 success/loading/none
|
icon: 'none', // 可选 success/loading/none
|
||||||
duration: 2000, // 持续时长,单位ms
|
duration: 2000, // 持续时长,单位ms
|
||||||
});
|
});
|
||||||
reject(data); // 其他错误情况返回错误信息
|
reject(data); // 其他错误情况返回错误信息
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
fail: (error) => {
|
fail: (error) => {
|
||||||
if (isShowLoading) {
|
if (isShowLoading) {
|
||||||
const loadingDiv = document.getElementById("custom-loading");
|
// 请求之前做一些事
|
||||||
if (loadingDiv && document.body.contains(loadingDiv)) {
|
uni.hideLoading();
|
||||||
document.body.removeChild(loadingDiv);
|
}
|
||||||
}
|
uni.showToast({
|
||||||
}
|
title: "服务器异常,稍后再试",
|
||||||
uni.showToast({
|
icon: 'none', // 可选 success/loading/none
|
||||||
title: "服务器异常,稍后再试",
|
duration: 2000, // 持续时长,单位ms
|
||||||
icon: "none", // 可选 success/loading/none
|
});
|
||||||
duration: 2000, // 持续时长,单位ms
|
// reject(error); // 请求失败时返回错误信息
|
||||||
});
|
},
|
||||||
// reject(error); // 请求失败时返回错误信息
|
});
|
||||||
},
|
});
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default request;
|
export default request;
|
||||||
|
|||||||
Reference in New Issue
Block a user