fix: 修复鸿蒙元服务TabBar跳转失效问题

1.  修复多处页面onTabItemTap事件,使用uni.reLaunch兜底鸿蒙下switchTab静默失败的问题
2.  全局混入onTabItemTap兜底未手动处理的页面
3.  优化路由跳转逻辑,针对鸿蒙环境增加switchTab失败兜底和超时强制跳转
4.  优化可选链调用和空值判断,提升代码健壮性
This commit is contained in:
2026-07-06 16:48:38 +08:00
parent 9815f09c1f
commit aa7d967422
6 changed files with 120 additions and 20 deletions

58
main.js
View File

@@ -14,22 +14,22 @@ import globalConfig from './common/js/golbalConfig.js';
import {
uniStorage
} from './common/js/storage.js'
import configExternal from './common/js/config-external.js'
import PrivacyManager from './common/js/privacy.js'
import splashState from './common/js/splash-state.js'
Vue.prototype.$store = store //挂在vue
Vue.config.productionTip = false
Vue.prototype.$util = Util;
Vue.prototype.$api = Http;
Vue.prototype.$langConfig = Lang; //语言包对象
Vue.prototype.$lang = Lang.lang; //解析语言包
Vue.prototype.$config = Config;
import configExternal from './common/js/config-external.js'
import PrivacyManager from './common/js/privacy.js'
import splashState from './common/js/splash-state.js'
Vue.prototype.$store = store //挂在vue
Vue.config.productionTip = false
Vue.prototype.$util = Util;
Vue.prototype.$api = Http;
Vue.prototype.$langConfig = Lang; //语言包对象
Vue.prototype.$lang = Lang.lang; //解析语言包
Vue.prototype.$config = Config;
Vue.prototype.$privacy = PrivacyManager; //全局隐私管理器
@@ -52,6 +52,32 @@ Vue.mixin({
}
});
// 鸿蒙元服务环境: 全局接管 onTabItemTap, 兜底系统 TabBar 点击
// 背景: 鸿蒙下 uni.switchTab 对系统 TabBar 点击静默失败
// 通过全局 mixin 覆盖所有未显式定义 onTabItemTap 的页面(子包动态 tab 页等),
// 显式定义 onTabItemTap 的页面(如 pages/empty/index.vue、index.js)优先级高于此 mixin
// #ifdef MP-HARMONY
Vue.mixin({
onTabItemTap(e) {
console.log('[TabBar][HARMONY] global onTabItemTap fired:', e);
if (!e || !e.pagePath) return;
const pages = getCurrentPages();
const currentPage = pages[pages.length - 1];
const currentPath = currentPage ? '/' + currentPage.route : '';
const targetPath = e.pagePath.startsWith('/') ? e.pagePath : '/' + e.pagePath;
console.log('[TabBar][HARMONY] onTabItemTap currentPath=', currentPath, 'targetPath=', targetPath);
// 已经在目标页则不重复跳转
if (currentPath === targetPath) return;
// 用干净路径(不带 query), 避免 HarmonyOS 对带 query 的 tabBar 跳转有兼容性问题
uni.reLaunch({
url: targetPath,
success: () => console.log('[TabBar][HARMONY] global reLaunch success:', targetPath),
fail: (err) => console.error('[TabBar][HARMONY] global reLaunch fail:', targetPath, err)
});
}
});
// #endif
App.mpType = 'app';
// 重写存储,增加前缀