去除无用的load-manager

This commit is contained in:
WindowBird 2025-09-16 16:20:50 +08:00
parent f09e59c16c
commit 04b12ae621
5 changed files with 18 additions and 314 deletions

View File

@ -3,10 +3,6 @@ import App from "./App";
import cookies from "weapp-cookie";
import uView from "uview-ui";
import Vuex from "vuex";
// 必须在导入 store 之前注册 Vuex
Vue.use(Vuex);
// http接口API集中管理引入部分
// import httpApi from '@/common/http.api.js'
import createStore from "./store/index.js";
@ -21,9 +17,11 @@ import md5 from "js-md5";
import tabbar from "@/components/tab-bar/tab-bar.vue";
import httpConfig from "@/common/http.config.js";
// 初始化全局loading管理器
import { initGlobalLoadingManager } from "@/utils/request.js";
import PageScrollMixin from "./mixins/page-scroll-mixin.js";
// 必须在导入 store 之前注册 Vuex
Vue.use(Vuex);
// import customizeAnswer from'@/components/customizeAnswer/customizeAnswer.vue'
// Vue.use('customizeAnswer',customizeAnswer)
Vue.use("tab-bar", tabbar);
@ -52,7 +50,6 @@ const app = new Vue({
Vue.use(httpConfig, app);
initGlobalLoadingManager();
// Vue.use(httpApi, app)
// #ifdef MP-WEIXIN
Vue.prototype.wxLogin = async function () {

View File

@ -85,7 +85,7 @@
<script>
import { navigateToPage } from "@/utils/router.js";
import { wxLogin } from "@/api/auth/auth.js";
import { AutoLoadingManager, forceHideLoading } from "@/utils/request.js";
import { getPrivacyPolicy, getServiceTerms } from "@/api/article/article.js";
export default {
@ -108,17 +108,8 @@ export default {
scrollTop: 0, //
};
},
onLoad() {
// loading
this.pageLoading = new AutoLoadingManager();
},
onUnload() {
// loading
forceHideLoading();
if (this.pageLoading) {
this.pageLoading.destroy();
}
},
onLoad() {},
onUnload() {},
methods: {
userType(num) {
this.chooseType = false;
@ -249,7 +240,6 @@ export default {
if (this.pageLoading) {
this.pageLoading.hide();
}
forceHideLoading();
if (res.code == 200) {
console.log(res, "resres");
@ -265,7 +255,7 @@ export default {
if (this.pageLoading) {
this.pageLoading.hide();
}
forceHideLoading();
// 使
navigateToPage("index");
}, 1500);
@ -282,7 +272,6 @@ export default {
if (this.pageLoading) {
this.pageLoading.hide();
}
forceHideLoading();
console.error("登录失败:", error);
uni.showToast({

View File

@ -8,7 +8,7 @@
<image :src="userInfo.avatar" class="avatar" mode="aspectFill" />
</view>
<view class="user-details">
<text class="phone-number">{{ userInfo.phone }}</text>
<text class="phone-number">{{ userInfo.nickName }}</text>
<view class="verification-status">
<text class="status-text">{{ realNameStatus }}</text>
</view>
@ -53,8 +53,8 @@ export default {
loading: false,
//
userInfo: {
phone: "666****6666",
nickName: "",
isRealName: "",
avatar: "",
},
//
@ -100,7 +100,7 @@ export default {
if (response.code === 200) {
//
this.userInfo = {
phone: response.data.phonenumber || "666****6666",
nickName: response.data.nickName,
isRealName: response.data.isReal || "false",
avatar: response.data.avatar,
};
@ -137,7 +137,7 @@ export default {
},
computed: {
realNameStatus() {
return this.userInfo.isRealName ? "已实名" : "未实名";
return this.userInfo.isRealName === "true" ? "已实名" : "未实名";
},
},
};

View File

@ -1,243 +0,0 @@
/**
* 全局自动Loading管理器
* 提供智能的Loading状态管理支持并发请求计数和超时保护
*/
// 环境配置
const ENV_CONFIG = {
develop: {
loadingText: "加载中~",
loadingTime: 100,
},
trial: {
loadingText: "加载中~",
loadingTime: 100,
},
release: {
loadingText: "加载中~",
loadingTime: 100,
},
};
// 获取当前环境配置
const getCurrentConfig = () => {
try {
const { envVersion } = wx.getAccountInfoSync().miniProgram;
console.log("当前环境:", envVersion);
return ENV_CONFIG[envVersion] || ENV_CONFIG.release;
} catch (error) {
console.warn("获取环境失败,默认使用正式环境:", error);
return ENV_CONFIG.release;
}
};
const config = getCurrentConfig();
// 全局状态管理
let isLoading = false;
let loadingTimer = null;
let loadingCount = 0; // 请求计数器
/**
* 设置loading超时自动清除
*/
const setLoadingTimeout = () => {
if (loadingTimer) {
clearTimeout(loadingTimer);
}
loadingTimer = setTimeout(() => {
console.warn("Loading超时强制清除");
forceHideLoading();
}, 30000); // 30秒超时
};
/**
* 显示加载状态
* @param {string} text - 加载提示文字
*/
function showLoading(text = config.loadingText) {
try {
loadingCount++;
if (!isLoading) {
isLoading = true;
uni.showLoading({
title: text,
mask: true,
});
// 设置超时清除
setLoadingTimeout();
console.log("显示Loading:", text);
}
} catch (error) {
console.warn("显示loading失败:", error);
}
}
/**
* 隐藏加载状态
*/
function hideLoading() {
try {
loadingCount--;
if (loadingCount > 0) {
console.log(
"还有其他请求在进行不隐藏Loading, 剩余请求数:",
loadingCount,
);
return; // 还有其他请求在进行
}
if (isLoading) {
isLoading = false;
// 清除超时定时器
if (loadingTimer) {
clearTimeout(loadingTimer);
loadingTimer = null;
}
uni.hideLoading();
console.log("隐藏Loading");
}
} catch (error) {
console.warn("隐藏loading失败:", error);
}
}
/**
* 强制隐藏loading
*/
export function forceHideLoading() {
try {
isLoading = false;
loadingCount = 0;
if (loadingTimer) {
clearTimeout(loadingTimer);
loadingTimer = null;
}
uni.hideLoading();
console.log("强制隐藏Loading");
} catch (error) {
console.warn("强制隐藏loading失败:", error);
}
}
/**
* 获取当前loading状态
* @returns {Object} loading状态信息
*/
export function getLoadingStatus() {
return {
isLoading,
loadingCount,
hasTimer: !!loadingTimer,
};
}
/**
* 设置loading配置
* @param {Object} newConfig - 新的配置
*/
export function setLoadingConfig(newConfig) {
Object.assign(config, newConfig);
console.log("更新Loading配置:", config);
}
/**
* 获取loading配置
* @returns {Object} 当前配置
*/
export function getLoadingConfig() {
return { ...config };
}
/**
* 显示loading带延迟
* @param {string} text - 加载提示文字
* @param {number} delay - 延迟时间毫秒
*/
export function showLoadingWithDelay(
text = config.loadingText,
delay = config.loadingTime,
) {
setTimeout(() => {
showLoading(text);
}, delay);
}
/**
* 隐藏loading带延迟
* @param {number} delay - 延迟时间毫秒
*/
export function hideLoadingWithDelay(delay = 500) {
setTimeout(() => {
hideLoading();
}, delay);
}
/**
* 初始化全局loading管理器
*/
export function initGlobalLoadingManager() {
// 监听页面显示事件
uni.$on("page-show", () => {
console.log("页面显示检查loading状态");
// 页面显示时检查loading状态如果超时则清除
if (isLoading && loadingTimer) {
const remainingTime =
30000 - (Date.now() - (loadingTimer._startTime || Date.now()));
if (remainingTime <= 0) {
console.warn("页面显示时发现超时loading强制清除");
forceHideLoading();
}
}
});
// 监听页面隐藏事件
uni.$on("page-hide", () => {
console.log("页面隐藏");
});
// 监听应用显示事件
uni.$on("app-show", () => {
console.log("应用显示");
});
// 监听应用隐藏事件
uni.$on("app-hide", () => {
console.log("应用隐藏清除loading");
forceHideLoading();
});
console.log("全局loading管理器已初始化");
}
/**
* 简化的自动loading管理类
*/
export class AutoLoadingManager {
constructor() {
this.isActive = false;
}
// 显示loading可选
show(text = "加载中...") {
this.isActive = true;
// 这里可以选择是否显示额外的loading
// 默认情况下所有API调用都会自动显示loading
}
// 隐藏loading可选
hide() {
this.isActive = false;
// 这里可以选择是否手动隐藏loading
// 默认情况下API调用完成后会自动隐藏loading
}
// 销毁管理器
destroy() {
this.isActive = false;
// 页面卸载时可以选择是否强制清除loading
// forceHideLoading()
}
}
// 导出内部方法供request.js使用
export { showLoading, hideLoading, config };

View File

@ -1,17 +1,5 @@
// 统一请求工具
import { getAppId, getTempToken, shouldUseTempToken } from "@/config/dev.js";
import {
AutoLoadingManager,
forceHideLoading,
getLoadingConfig,
getLoadingStatus,
hideLoading,
hideLoadingWithDelay,
initGlobalLoadingManager,
setLoadingConfig,
showLoading,
showLoadingWithDelay,
} from "./loading-manager.js";
// 环境配置
const ENV_CONFIG = {
@ -109,11 +97,13 @@ function handleResponseError(res, reject, options = {}) {
title: "请求的资源不存在",
action: () => {},
},
500: {
title: "服务器错误",
action: () => {},
},
};
console.log("response:", res);
const error = errorMap[res.data.code] || {
const error = errorMap[res.statusCode] || {
title: res.data?.msg || "请求失败",
action: () => {},
};
@ -176,12 +166,9 @@ export function request(options = {}) {
timeout: options.timeout || 60000, // 默认60秒超时
success: (res) => {
// 隐藏加载状态
if (options.showLoading !== false) {
hideLoading();
}
// 请求成功处理
if (res.code === 200 || res.data.code === 200) {
if (res.statusCode === 200) {
resolve(res.data);
} else {
// 处理错误响应
@ -190,9 +177,6 @@ export function request(options = {}) {
},
fail: (err) => {
// 隐藏加载状态
if (options.showLoading !== false) {
hideLoading();
}
// 请求失败处理
console.error("请求失败:", {
@ -395,29 +379,6 @@ export function getToken() {
return uni.getStorageSync("token");
}
// 导出loading相关函数作为统一入口
export {
// 基础loading函数
showLoading,
hideLoading,
forceHideLoading,
// 高级loading函数
showLoadingWithDelay,
hideLoadingWithDelay,
// 状态和配置管理
getLoadingStatus,
setLoadingConfig,
getLoadingConfig,
// 全局初始化
initGlobalLoadingManager,
// 自动loading管理器类
AutoLoadingManager,
};
// Loading管理相关函数已从loading-manager.js导入
// 默认导出request函数方便API文件导入