fix: 首页刷新

This commit is contained in:
yangzhe 2025-05-06 10:36:52 +08:00
parent d57579844e
commit d07f1f5992
3 changed files with 113 additions and 2 deletions

View File

@ -3,8 +3,14 @@
import appUpdate from '@/components/appUpdate/appUpdate.js' import appUpdate from '@/components/appUpdate/appUpdate.js'
// #endif // #endif
import { jwtDecode } from 'jwt-decode' import { jwtDecode } from 'jwt-decode'
//
import routeInterceptor from '@/utils/routeInterceptor.js'
export default { export default {
onLaunch: function () { onLaunch: function () {
//
routeInterceptor.init();
console.log('%c%s', 'color:red', 'onLaunch---') console.log('%c%s', 'color:red', 'onLaunch---')
uni.showLoading({ title: '加载中' }) uni.showLoading({ title: '加载中' })
// uni.setStorageSync("token", ""); // uni.setStorageSync("token", "");

View File

@ -136,11 +136,28 @@ export default {
// this.getPlanByDate(); // this.getPlanByDate();
}, },
onShow() { onLoad() {
this.onDateChange({ date: formatDate(new Date()), isToday: true }); uni.$off("indexPageShow", this.handlePageShow);
//
uni.$on("indexPageShow", this.handlePageShow);
//
this.handlePageShow();
},
onUnload() {
//
uni.$off("indexPageShow", this.handlePageShow);
}, },
// onShow() {
// this.onDateChange({ date: formatDate(new Date()), isToday: true });
// },
methods: { methods: {
handlePageShow() {
// console.log("", new Date().toLocaleString());
this.onDateChange({ date: formatDate(new Date()), isToday: true });
},
testFn() { testFn() {
let u = navigator.userAgent; let u = navigator.userAgent;
let isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; //android let isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; //android

88
utils/routeInterceptor.js Normal file
View File

@ -0,0 +1,88 @@
/**
* 路由拦截器
* 用于在页面跳转时执行特定逻辑
*/
// 通过getCurrentPages获取当前页面并触发刷新
const checkCurrentPageAndRefresh = () => {
setTimeout(() => {
const pages = getCurrentPages();
if (pages.length > 0) {
const currentPage = pages[pages.length - 1];
const pagePath = currentPage.route;
console.log("当前页面路径:", pagePath);
// 如果当前页面是首页,触发刷新事件
if (pagePath === "pages/index/index") {
console.log("检测到首页,触发刷新事件");
uni.$emit("indexPageShow");
}
// 这里可以添加其他页面的刷新逻辑
// if (pagePath === 'pages/other/page') {
// uni.$emit('otherPageShow');
// }
} else {
console.log("getCurrentPages返回空数组无法获取当前页面");
}
}, 150); // 增加延迟,确保页面完全加载
};
// 拦截器配置
const interceptors = {
// 普通页面跳转
navigateTo: {
success: () => {
checkCurrentPageAndRefresh();
},
},
// 页面返回
navigateBack: {
success: () => {
checkCurrentPageAndRefresh();
},
},
// 切换 tabbar 页面
switchTab: {
success: () => {
checkCurrentPageAndRefresh();
},
},
// 重定向页面
redirectTo: {
success: () => {
checkCurrentPageAndRefresh();
},
},
// 重启应用并打开页面
reLaunch: {
success: () => {
checkCurrentPageAndRefresh();
},
},
};
// 初始化函数:注册所有拦截器
const initInterceptors = () => {
console.log("初始化路由拦截器");
// 注册各种导航方法的拦截器
uni.addInterceptor("navigateTo", interceptors.navigateTo);
uni.addInterceptor("navigateBack", interceptors.navigateBack);
uni.addInterceptor("switchTab", interceptors.switchTab);
uni.addInterceptor("redirectTo", interceptors.redirectTo);
uni.addInterceptor("reLaunch", interceptors.reLaunch);
console.log("路由拦截器注册完成");
};
// 导出一个对象,包含初始化方法和拦截器配置
export default {
init: initInterceptors,
interceptors,
};