feat:性能优化
This commit is contained in:
+27
-15
@@ -4,6 +4,27 @@ import { BASE_URL, Authorization } from "@/utils/config.js";
|
||||
// utils/http.js (修改后的版本)
|
||||
// const baseUrl = 'http://192.168.0.108:8087';
|
||||
|
||||
let requestCount = 0;
|
||||
|
||||
function showLoading() {
|
||||
if (requestCount === 0) {
|
||||
uni.showLoading({
|
||||
title: "加载中...",
|
||||
mask: false
|
||||
});
|
||||
}
|
||||
requestCount++;
|
||||
}
|
||||
|
||||
function hideLoading() {
|
||||
if (requestCount > 0) {
|
||||
requestCount--;
|
||||
}
|
||||
if (requestCount === 0) {
|
||||
uni.hideLoading();
|
||||
}
|
||||
}
|
||||
|
||||
function request(options) {
|
||||
const {
|
||||
url,
|
||||
@@ -13,11 +34,10 @@ function request(options) {
|
||||
isShowLoading = true,
|
||||
} = options;
|
||||
|
||||
// const token = uni.getStorageSync(tokenKey); // 获取token, 根据实际情况调整获取方式
|
||||
const token = getStorageFun(TOKEN_NAME); // 获取token, 根据实际情况调整获取方式
|
||||
const token = getStorageFun(TOKEN_NAME);
|
||||
let defaultHeaders = {
|
||||
Authorization: Authorization,
|
||||
"HSM-AUTH": token ? token : "", // 如果存在token, 则添加到请求头中
|
||||
"HSM-AUTH": token ? token : "",
|
||||
"content-type": ["post", "POST"].includes(method)
|
||||
? "application/x-www-form-urlencoded"
|
||||
: "application/json",
|
||||
@@ -30,13 +50,7 @@ function request(options) {
|
||||
}
|
||||
return new Promise((resolve, reject) => {
|
||||
if (isShowLoading) {
|
||||
// 请求之前做一些事
|
||||
uni.showToast({
|
||||
title: "加载中...",
|
||||
image: "https://static.tbmall.xin/static/loading.gif",
|
||||
// image: "/static/common/loading.gif", // 本地图片路径
|
||||
position: "middle", // 可选:top/middle/bottom
|
||||
});
|
||||
showLoading();
|
||||
}
|
||||
uni.request({
|
||||
url: BASE_URL + url,
|
||||
@@ -44,12 +58,11 @@ function request(options) {
|
||||
data,
|
||||
header: {
|
||||
...defaultHeaders,
|
||||
...headers, // 使用展开运算符合并默认头和自定义头
|
||||
...headers,
|
||||
},
|
||||
success: (res) => {
|
||||
if (isShowLoading) {
|
||||
// 请求之前做一些事
|
||||
uni.hideToast();
|
||||
hideLoading();
|
||||
}
|
||||
|
||||
let data = res.data;
|
||||
@@ -114,8 +127,7 @@ function request(options) {
|
||||
fail: (error) => {
|
||||
console.log("错误-2", url);
|
||||
if (isShowLoading) {
|
||||
// 请求之前做一些事
|
||||
uni.hideToast();
|
||||
hideLoading();
|
||||
}
|
||||
uni.showToast({
|
||||
title: "服务器异常,稍后再试",
|
||||
|
||||
Reference in New Issue
Block a user