59 lines
1.4 KiB
Vue
59 lines
1.4 KiB
Vue
<template>
|
|
<div></div>
|
|
</template>
|
|
|
|
<script lang="ts">
|
|
import { defineComponent, onMounted, onUnmounted } from 'vue';
|
|
import { useRouter, useRoute } from 'vue-router';
|
|
import appBridge from '@/assets/js/appBridge';
|
|
|
|
export default defineComponent({
|
|
name: 'AndroidBackHandler',
|
|
setup() {
|
|
const router = useRouter();
|
|
const route = useRoute();
|
|
// 检查是否可以返回
|
|
const canGoBack = () => {
|
|
// 检查是否有历史记录
|
|
if (window.history.length > 1) {
|
|
return true;
|
|
}
|
|
// 检查路由状态
|
|
const position = router.options.history.state?.position;
|
|
return typeof position === 'number' && position > 0;
|
|
};
|
|
|
|
// 检查是否是首页
|
|
const isHomePage = () => {
|
|
// 根据实际路由配置修改这里的判断逻辑
|
|
return route.path === '/' || route.path === '/home';
|
|
};
|
|
|
|
// 处理返回按钮事件
|
|
const handleBack = () => {
|
|
// 如果有历史记录,说明是从其他页面进入的
|
|
if (canGoBack()) {
|
|
router.go(-1);
|
|
return false;
|
|
} else {
|
|
appBridge.navigateBack();
|
|
}
|
|
};
|
|
|
|
onMounted(() => {
|
|
// 设置禁止原生返回
|
|
appBridge.takeOverAndroidBack();
|
|
// 添加返回按钮监听
|
|
window.onAndroidBack = handleBack;
|
|
});
|
|
|
|
onUnmounted(() => {
|
|
// 移除返回按钮监听
|
|
window.onAndroidBack = null;
|
|
});
|
|
|
|
return {};
|
|
}
|
|
});
|
|
</script>
|