Files
crawler-plugin/frontend-vue/src/main.ts
T
huangzd1997 3987b8d7fe 用户端(四面9分批次): 三条全局错误出口、URL 白名单收口、令牌键单点、uid 短路进门禁
- 此前全树 0 处全局错误处理器:装 app.config.errorHandler + window error + unhandledrejection,
  每条都留可诊断记录、界面只出中文、处理器自身异常不重抛也不白屏(9 例判据)。
- safeHttpUrl 未覆盖的 3 处(两个品牌 Tab 与 useVersionUpdate 直装服务端 file_url)收口;
  令牌键 aiimage_auth_token 由 4 处各自定义 + 2 处内联收成唯一定义点。
- 「localStorage 有 uid 就免校验」改走 auth-gate 单点,剩余例外台账化且同口径必须能抓到
  评审点名的原写法(6 例);卸载配对 3 处补齐 + 全树判据(7 例)。
- task-status-text 把 CANCELLED 显示成"执行中"改对,CANCELED/CANCELLED 两拼写都映射(4 例)。
- 主审计补的四处修复:新判据里两处拿数组和 0 比(怎么写都红);静默 catch 的"用户可见出口"
  符号表认不出本仓 userNotice 与注入式 notify,把真报了用户的 catch 判成静默;
  新错误出口带来的 2 个 try/catch 按棘轮合同写理由进基线(296→298,silent 一格没涨)。
- 验收:node --test 820 例 0 红、npx vue-tsc --noEmit rc=0。
2026-10-09 23:09:37 +08:00

87 lines
3.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { createApp } from 'vue'
import '@/styles/main.css'
// 命令式调用的组件样式必须在这里显式引入:ElMessage / ElMessageBox 由函数调用触发,
// 不出现在任何模板里,ElementPlusResolver 的按需注入看不到它们。漏掉时弹窗没有定位
// 与遮罩样式,会渲染成普通块元素贴到文档左上角、与页头文字叠在一起。
import 'element-plus/es/components/message/style/css'
import 'element-plus/es/components/message-box/style/css'
import App from '@/App.vue'
import router from '@/router'
import { ElMessage } from 'element-plus'
import { ensureAuth } from '@/shared/auth/ensure-auth'
import { shouldBounceToHome } from '@/shared/auth/auth-gate'
import { AUTH_TOKEN_KEY, UID_KEY } from '@/shared/auth/session-keys'
import { installGlobalErrorGuard } from '@/shared/utils/app-error'
import { ensureApiSecretsLoaded, loadApiSecrets } from '@/shared/utils/api-secret-store'
import { installUserTokenSync } from '@/shared/auth/user-token-sync.ts'
import { installSubmitGuard } from '@/shared/utils/submit-guard'
// 登录态令牌同步给桌面端 Python(A1/A3):无桥环境静默跳过
installUserTokenSync()
// 提交按钮防连点:捕获阶段拦下同一按钮的连点(各工具页 .btn-run 通用)
installSubmitGuard()
const app = createApp(App)
// 全局错误处理器:Vue 渲染/生命周期抛错、顶层 window error、没人接的 Promise rejection
// 三个出口一次装好;界面只说中文(原文过 userNotice 把关,英文异常只进控制台记录)。
// 卸载出口挂在 App.vue 的 onBeforeUnmount(disposeGlobalErrorGuard)。
installGlobalErrorGuard(app, {
notify: (text) => ElMessage.error(text),
record: (line) => console.error('[app-error]', line),
})
/**
* 数富AI 前端统一入口(SPA,URL 无 .html 后缀)
*
* 原 MPA 的 22 个 html 入口 + 22 个 *-main.ts 已合并:
* 页面路由见 src/router/index.ts;登录态由路由守卫统一引导;
* 密钥门禁:服务端密钥未配置完整时全站拦截到 /setup-secrets(拉取失败软失败放行)。
*
* Element Plus 走按需引入(vite.config.ts 的 ElementPlusResolver):
* 此前 `app.use(ElementPlus)` + 全量 index.css 会把整套组件与样式打进入口
* (入口 JS 实测 1.05MB,而组件 resolver 的分包优势全部失效)。
* locale 由 App.vue 的 el-config-provider 提供。
*/
// 后台预热密钥包,减少首次进入守卫时的等待
void loadApiSecrets()
router.beforeEach(async (to) => {
if (to.name === 'login') return true
const ok = await ensureAuth()
if (!ok) return { name: 'login' }
if (to.name === 'setup-secrets') return true
// 密钥门禁:仅当服务端明确返回"未配置完整"时拦截;
// unknown(拉取失败/网络异常)一律放行,避免服务端抖动把全体用户锁死。
const secretState = await ensureApiSecretsLoaded()
if (secretState === 'incomplete') {
const query = to.fullPath && to.fullPath !== '/' ? { redirect: to.fullPath } : {}
return { name: 'setup-secrets', query }
}
return true
})
router.beforeEach((to) => {
// 已登录访问登录页 → 回首页(与 MPA 时代 login.html 展示"请先退出"一致:直接回首页);
// 但带 logout/switch 的登出导航放行。判断须看 to.query:守卫在 URL 更新前执行,
// window.location.search 仍是旧地址,会漏判 /home 点击"退出"的 router-link(=登出无效果)。
if (to.name === 'login' && shouldBounceToHome({
uid: window.localStorage.getItem(UID_KEY),
token: window.localStorage.getItem(AUTH_TOKEN_KEY),
})) {
const isLogoutNav = to.query.logout === '1' || to.query.switch === '1'
if (isLogoutNav) {
// 排查日志:登出/切号放行(清 token 逻辑在登录页 onMounted 处理)
console.log('[auth] 登出导航放行, fullPath=', to.fullPath)
} else {
return { name: 'home' }
}
}
return true
})
app.use(router).mount('#app')