去除无用的load-manager
This commit is contained in:
parent
f09e59c16c
commit
04b12ae621
9
main.js
9
main.js
|
|
@ -3,10 +3,6 @@ import App from "./App";
|
||||||
import cookies from "weapp-cookie";
|
import cookies from "weapp-cookie";
|
||||||
import uView from "uview-ui";
|
import uView from "uview-ui";
|
||||||
import Vuex from "vuex";
|
import Vuex from "vuex";
|
||||||
|
|
||||||
// 必须在导入 store 之前注册 Vuex
|
|
||||||
Vue.use(Vuex);
|
|
||||||
|
|
||||||
// http接口API集中管理引入部分
|
// http接口API集中管理引入部分
|
||||||
// import httpApi from '@/common/http.api.js'
|
// import httpApi from '@/common/http.api.js'
|
||||||
import createStore from "./store/index.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 tabbar from "@/components/tab-bar/tab-bar.vue";
|
||||||
import httpConfig from "@/common/http.config.js";
|
import httpConfig from "@/common/http.config.js";
|
||||||
// 初始化全局loading管理器
|
// 初始化全局loading管理器
|
||||||
import { initGlobalLoadingManager } from "@/utils/request.js";
|
|
||||||
import PageScrollMixin from "./mixins/page-scroll-mixin.js";
|
import PageScrollMixin from "./mixins/page-scroll-mixin.js";
|
||||||
|
|
||||||
|
// 必须在导入 store 之前注册 Vuex
|
||||||
|
Vue.use(Vuex);
|
||||||
|
|
||||||
// import customizeAnswer from'@/components/customizeAnswer/customizeAnswer.vue'
|
// import customizeAnswer from'@/components/customizeAnswer/customizeAnswer.vue'
|
||||||
// Vue.use('customizeAnswer',customizeAnswer)
|
// Vue.use('customizeAnswer',customizeAnswer)
|
||||||
Vue.use("tab-bar", tabbar);
|
Vue.use("tab-bar", tabbar);
|
||||||
|
|
@ -52,7 +50,6 @@ const app = new Vue({
|
||||||
|
|
||||||
Vue.use(httpConfig, app);
|
Vue.use(httpConfig, app);
|
||||||
|
|
||||||
initGlobalLoadingManager();
|
|
||||||
// Vue.use(httpApi, app)
|
// Vue.use(httpApi, app)
|
||||||
// #ifdef MP-WEIXIN
|
// #ifdef MP-WEIXIN
|
||||||
Vue.prototype.wxLogin = async function () {
|
Vue.prototype.wxLogin = async function () {
|
||||||
|
|
|
||||||
|
|
@ -85,7 +85,7 @@
|
||||||
<script>
|
<script>
|
||||||
import { navigateToPage } from "@/utils/router.js";
|
import { navigateToPage } from "@/utils/router.js";
|
||||||
import { wxLogin } from "@/api/auth/auth.js";
|
import { wxLogin } from "@/api/auth/auth.js";
|
||||||
import { AutoLoadingManager, forceHideLoading } from "@/utils/request.js";
|
|
||||||
import { getPrivacyPolicy, getServiceTerms } from "@/api/article/article.js";
|
import { getPrivacyPolicy, getServiceTerms } from "@/api/article/article.js";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
|
|
@ -108,17 +108,8 @@ export default {
|
||||||
scrollTop: 0, // 滚动位置
|
scrollTop: 0, // 滚动位置
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
onLoad() {
|
onLoad() {},
|
||||||
// 初始化页面loading管理器
|
onUnload() {},
|
||||||
this.pageLoading = new AutoLoadingManager();
|
|
||||||
},
|
|
||||||
onUnload() {
|
|
||||||
// 页面卸载时清除loading
|
|
||||||
forceHideLoading();
|
|
||||||
if (this.pageLoading) {
|
|
||||||
this.pageLoading.destroy();
|
|
||||||
}
|
|
||||||
},
|
|
||||||
methods: {
|
methods: {
|
||||||
userType(num) {
|
userType(num) {
|
||||||
this.chooseType = false;
|
this.chooseType = false;
|
||||||
|
|
@ -249,7 +240,6 @@ export default {
|
||||||
if (this.pageLoading) {
|
if (this.pageLoading) {
|
||||||
this.pageLoading.hide();
|
this.pageLoading.hide();
|
||||||
}
|
}
|
||||||
forceHideLoading();
|
|
||||||
|
|
||||||
if (res.code == 200) {
|
if (res.code == 200) {
|
||||||
console.log(res, "resres");
|
console.log(res, "resres");
|
||||||
|
|
@ -265,7 +255,7 @@ export default {
|
||||||
if (this.pageLoading) {
|
if (this.pageLoading) {
|
||||||
this.pageLoading.hide();
|
this.pageLoading.hide();
|
||||||
}
|
}
|
||||||
forceHideLoading();
|
|
||||||
// 使用路由仓库跳转到首页
|
// 使用路由仓库跳转到首页
|
||||||
navigateToPage("index");
|
navigateToPage("index");
|
||||||
}, 1500);
|
}, 1500);
|
||||||
|
|
@ -282,7 +272,6 @@ export default {
|
||||||
if (this.pageLoading) {
|
if (this.pageLoading) {
|
||||||
this.pageLoading.hide();
|
this.pageLoading.hide();
|
||||||
}
|
}
|
||||||
forceHideLoading();
|
|
||||||
|
|
||||||
console.error("登录失败:", error);
|
console.error("登录失败:", error);
|
||||||
uni.showToast({
|
uni.showToast({
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@
|
||||||
<image :src="userInfo.avatar" class="avatar" mode="aspectFill" />
|
<image :src="userInfo.avatar" class="avatar" mode="aspectFill" />
|
||||||
</view>
|
</view>
|
||||||
<view class="user-details">
|
<view class="user-details">
|
||||||
<text class="phone-number">{{ userInfo.phone }}</text>
|
<text class="phone-number">{{ userInfo.nickName }}</text>
|
||||||
<view class="verification-status">
|
<view class="verification-status">
|
||||||
<text class="status-text">{{ realNameStatus }}</text>
|
<text class="status-text">{{ realNameStatus }}</text>
|
||||||
</view>
|
</view>
|
||||||
|
|
@ -53,8 +53,8 @@ export default {
|
||||||
loading: false,
|
loading: false,
|
||||||
// 用户信息
|
// 用户信息
|
||||||
userInfo: {
|
userInfo: {
|
||||||
phone: "666****6666",
|
nickName: "",
|
||||||
|
isRealName: "",
|
||||||
avatar: "",
|
avatar: "",
|
||||||
},
|
},
|
||||||
// 菜单项数据
|
// 菜单项数据
|
||||||
|
|
@ -100,7 +100,7 @@ export default {
|
||||||
if (response.code === 200) {
|
if (response.code === 200) {
|
||||||
// 更新用户信息
|
// 更新用户信息
|
||||||
this.userInfo = {
|
this.userInfo = {
|
||||||
phone: response.data.phonenumber || "666****6666",
|
nickName: response.data.nickName,
|
||||||
isRealName: response.data.isReal || "false",
|
isRealName: response.data.isReal || "false",
|
||||||
avatar: response.data.avatar,
|
avatar: response.data.avatar,
|
||||||
};
|
};
|
||||||
|
|
@ -137,7 +137,7 @@ export default {
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
realNameStatus() {
|
realNameStatus() {
|
||||||
return this.userInfo.isRealName ? "已实名" : "未实名";
|
return this.userInfo.isRealName === "true" ? "已实名" : "未实名";
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -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 };
|
|
||||||
|
|
@ -1,17 +1,5 @@
|
||||||
// 统一请求工具
|
// 统一请求工具
|
||||||
import { getAppId, getTempToken, shouldUseTempToken } from "@/config/dev.js";
|
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 = {
|
const ENV_CONFIG = {
|
||||||
|
|
@ -109,11 +97,13 @@ function handleResponseError(res, reject, options = {}) {
|
||||||
title: "请求的资源不存在",
|
title: "请求的资源不存在",
|
||||||
action: () => {},
|
action: () => {},
|
||||||
},
|
},
|
||||||
|
500: {
|
||||||
|
title: "服务器错误",
|
||||||
|
action: () => {},
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
console.log("response:", res);
|
const error = errorMap[res.statusCode] || {
|
||||||
|
|
||||||
const error = errorMap[res.data.code] || {
|
|
||||||
title: res.data?.msg || "请求失败",
|
title: res.data?.msg || "请求失败",
|
||||||
action: () => {},
|
action: () => {},
|
||||||
};
|
};
|
||||||
|
|
@ -176,12 +166,9 @@ export function request(options = {}) {
|
||||||
timeout: options.timeout || 60000, // 默认60秒超时
|
timeout: options.timeout || 60000, // 默认60秒超时
|
||||||
success: (res) => {
|
success: (res) => {
|
||||||
// 隐藏加载状态
|
// 隐藏加载状态
|
||||||
if (options.showLoading !== false) {
|
|
||||||
hideLoading();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 请求成功处理
|
// 请求成功处理
|
||||||
if (res.code === 200 || res.data.code === 200) {
|
if (res.statusCode === 200) {
|
||||||
resolve(res.data);
|
resolve(res.data);
|
||||||
} else {
|
} else {
|
||||||
// 处理错误响应
|
// 处理错误响应
|
||||||
|
|
@ -190,9 +177,6 @@ export function request(options = {}) {
|
||||||
},
|
},
|
||||||
fail: (err) => {
|
fail: (err) => {
|
||||||
// 隐藏加载状态
|
// 隐藏加载状态
|
||||||
if (options.showLoading !== false) {
|
|
||||||
hideLoading();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 请求失败处理
|
// 请求失败处理
|
||||||
console.error("请求失败:", {
|
console.error("请求失败:", {
|
||||||
|
|
@ -395,29 +379,6 @@ export function getToken() {
|
||||||
return uni.getStorageSync("token");
|
return uni.getStorageSync("token");
|
||||||
}
|
}
|
||||||
|
|
||||||
// 导出loading相关函数,作为统一入口
|
|
||||||
export {
|
|
||||||
// 基础loading函数
|
|
||||||
showLoading,
|
|
||||||
hideLoading,
|
|
||||||
forceHideLoading,
|
|
||||||
|
|
||||||
// 高级loading函数
|
|
||||||
showLoadingWithDelay,
|
|
||||||
hideLoadingWithDelay,
|
|
||||||
|
|
||||||
// 状态和配置管理
|
|
||||||
getLoadingStatus,
|
|
||||||
setLoadingConfig,
|
|
||||||
getLoadingConfig,
|
|
||||||
|
|
||||||
// 全局初始化
|
|
||||||
initGlobalLoadingManager,
|
|
||||||
|
|
||||||
// 自动loading管理器类
|
|
||||||
AutoLoadingManager,
|
|
||||||
};
|
|
||||||
|
|
||||||
// Loading管理相关函数已从loading-manager.js导入
|
// Loading管理相关函数已从loading-manager.js导入
|
||||||
|
|
||||||
// 默认导出request函数,方便API文件导入
|
// 默认导出request函数,方便API文件导入
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user