feat:app嵌套h5返回键

This commit is contained in:
LHY\18810
2025-03-14 23:29:14 +08:00
parent d9ecd839db
commit ef9e1a4e48
6 changed files with 145 additions and 131 deletions

View File

@@ -12,50 +12,30 @@ import appBridge from '@/assets/js/appBridge';
import VConsole from 'vconsole';
const app = createApp(App);
// import VConsole from 'vconsole';
// let vconsole;
if (import.meta.env.VITE_APP_ENV !== 'production') {
const vconsole = new VConsole();
app.use(vconsole);
// eslint-disable-next-line no-unused-vars
// vconsole = new VConsole();
}
// 添加 TypeScript 类型声明,在文件顶部添加
declare global {
interface Window {
onAndroidBack: (() => void) | null;
appBridge?: any; // 同时添加 appBridge 类型声明
appBridge?: any;
}
}
// 隐藏/显示 header
appBridge.setHeaderShown(false);
// 设置系统状态栏明暗主题
appBridge.setStatusBarStyle('light');
router.beforeEach((to, from, next) => {
if (to.query.digitalYiliToken) {
utils.setSessionStorage('xToken', to.query.digitalYiliToken);
}
// appBridge.setTitle(to.meta.title as string);
// 设置禁止原生返回
appBridge.takeOverAndroidBack();
// 定义路由是否可以返回的判断
const routerCanGoBack = () => {
const position = router.options.history.state?.position;
return typeof position === 'number' && position > 0;
};
// 定义调用APP返回的方法
const callAppGoBack = () => {
appBridge.navigateBack(); // 使用现有的navigateBack方法替代不存在的goBack方法
};
// 添加 Android 返回按钮监听方法
window.onAndroidBack = () => {
if (routerCanGoBack()) {
router.back();
} else {
callAppGoBack();
}
};
// 设置 header 标题
appBridge.setTitle(to.meta.title as string);
next();
});
app.use(createPinia());
app.use(router);
app.mount('#app');