79 lines
2.9 KiB
Vue
79 lines
2.9 KiB
Vue
<script>
|
||
import { getToken } from '@/core/utils/auth'
|
||
import { ensureEntry } from '@/core/utils/auth-guard'
|
||
|
||
let bootstrapping = false
|
||
let validatedToken = ''
|
||
// H5(history 模式)下,整页直接刷新/打开 packMode 等“非入口页”时 uni-app 不创建页面视图(白屏)。
|
||
// 该状态仅本次整页启动时自愈一次(页面缺失则回跳入口),避免重复触发。
|
||
let h5WhiteHealDone = false
|
||
|
||
export default {
|
||
onLaunch() {
|
||
this.bootstrapAuth()
|
||
},
|
||
onShow() {
|
||
this.bootstrapAuth()
|
||
this.h5WhiteScreenHeal()
|
||
},
|
||
methods: {
|
||
async bootstrapAuth() {
|
||
if (bootstrapping) {
|
||
return
|
||
}
|
||
|
||
const token = getToken()
|
||
const forceValidate = !!token && token !== validatedToken
|
||
|
||
bootstrapping = true
|
||
try {
|
||
const result = await ensureEntry(this.$store, forceValidate)
|
||
validatedToken = result.loggedIn ? token : ''
|
||
} finally {
|
||
bootstrapping = false
|
||
}
|
||
},
|
||
// H5 白屏自愈:manifest h5 路由为 history 模式时,若应用是“整页加载”方式
|
||
// 直接从 packMode 等非 tab、非入口页启动,uni-app 不会渲染出页面视图(uni-page),
|
||
// 且该会话内后续客户端跳转同样失效,页面会一直白屏(刷新也无法恢复)。
|
||
// 这里在启动后检测:uni-app 外壳已挂载但 uni-page 缺失 → 判定为白屏态,
|
||
// 整页回跳根路径让应用从入口正常启动,避免停留在白屏。真机/App 端不参与编译。
|
||
h5WhiteScreenHeal() {
|
||
// #ifdef H5
|
||
if (h5WhiteHealDone) return
|
||
h5WhiteHealDone = true
|
||
if (typeof window === 'undefined' || !window.location) return
|
||
const bootPath = window.location.pathname || '/'
|
||
// 入口页(根/登录页)本身可正常渲染,无需处理
|
||
if (bootPath === '/' || bootPath === '/pages/login/index') return
|
||
setTimeout(() => {
|
||
// 外壳 uni-app 已存在但页面视图 uni-page 缺失 → 白屏,回跳入口
|
||
const shell = document.querySelector('uni-app')
|
||
const page = document.querySelector('uni-page')
|
||
if (shell && !page) {
|
||
console.warn('[App] H5 深链接整页启动未渲染页面视图(白屏),自动回跳入口页:', bootPath)
|
||
window.location.replace('/')
|
||
}
|
||
}, 1500)
|
||
// #endif
|
||
}
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style lang="scss">
|
||
/* 每个页面公共css */
|
||
@import "@/uni_modules/uview-ui/index.scss";
|
||
@import "common/demo.scss";
|
||
|
||
.status_bar {
|
||
height: var(--status-bar-height);
|
||
background-color: #FFFFFF;
|
||
}
|
||
.normar_container{
|
||
height:100vh;
|
||
display:flex;
|
||
flex-direction: column;
|
||
background-color: #f8f8f8;
|
||
}
|
||
</style> |