diff --git a/App.vue b/App.vue index c02d13d..89be1ca 100644 --- a/App.vue +++ b/App.vue @@ -3,8 +3,14 @@ import appUpdate from '@/components/appUpdate/appUpdate.js' // #endif import { jwtDecode } from 'jwt-decode' +// 导入路由拦截器 +import routeInterceptor from '@/utils/routeInterceptor.js' + export default { onLaunch: function () { + // 初始化路由拦截器 + routeInterceptor.init(); + console.log('%c%s', 'color:red', 'onLaunch---') uni.showLoading({ title: '加载中' }) // uni.setStorageSync("token", ""); diff --git a/pages/index/index.vue b/pages/index/index.vue index 2794530..fcd40ca 100644 --- a/pages/index/index.vue +++ b/pages/index/index.vue @@ -136,11 +136,28 @@ export default { // this.getPlanByDate(); }, - onShow() { - this.onDateChange({ date: formatDate(new Date()), isToday: true }); + onLoad() { + 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: { + handlePageShow() { + // console.log("首页刷新", new Date().toLocaleString()); + this.onDateChange({ date: formatDate(new Date()), isToday: true }); + }, testFn() { let u = navigator.userAgent; let isAndroid = u.indexOf("Android") > -1 || u.indexOf("Adr") > -1; //android diff --git a/utils/routeInterceptor.js b/utils/routeInterceptor.js new file mode 100644 index 0000000..56ff2dd --- /dev/null +++ b/utils/routeInterceptor.js @@ -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, +};