front/src/main.ts
zzh 5010213539
All checks were successful
Build and Deploy Front / deploy (push) Successful in 4m3s
feat: 单号截取扫描加速、登录验证码与 SPA 刷新回退
新增单号规则截取模式与本地扫描引擎;登录图形验证码;吉祥物空态;开发态从 Router 自动回退 PC/H5 刷新直链。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-11-14 19:33:27 +08:00

159 lines
4.4 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 ArcoVue from '@arco-design/web-vue';
import ArcoVueIcon from '@arco-design/web-vue/es/icon';
// eslint-disable-next-line import/no-unresolved
import 'uno.css';
import { installGlobalMessageDefaults } from '@/utils/message';
import router from './router';
import store, { useAppStore } from './store';
import i18n, { initPreferredLocale, ensureFullLocaleMessages } from './locale';
import directive from './directive';
import globalProperties from './plugins/globalProperties';
// 全局 Message:可手动关闭;错误/警告默认 8s
installGlobalMessageDefaults();
import {
bindHeavyPluginsContext,
ensureHeavyPlugins,
routeNeedsHeavyPlugins,
} from './plugins/heavy-plugins';
import { initAuthKeepAlive } from '@/utils/auth-keepalive';
import { installWeChatH5HistoryGuard } from '@/utils/device';
import { installChunkReloadHandlers, reloadOnChunkLoadError } from '@/utils/chunkReload';
import { initGa } from '@/utils/ga';
import { setAuthToken } from './api/rbac';
import { useRbacUserStore } from '@/store/modules/rbac-user';
import { applyRbacUserToUserStore } from '@/utils/enter-system';
import App from './App.vue';
import '@/assets/style/global.less';
import '@/styles/list-action.less';
import '@/styles/app-button.less';
import '@/styles/dark-mode.less';
if (import.meta.env.DEV) {
void import('./mock');
}
/** Login / public pages: no VXE/HT on critical path */
const LIGHT_ENTRY_PATHS = [
'/',
'/admin',
'/login',
'/register',
'/track/query',
'/wechat/register',
'/wechat/bind',
'/h5',
];
/** Home uses Arco (+ optional echarts); do not block on VXE/HT */
function isHomePath(path: string) {
return path === '/home' || path.startsWith('/home/');
}
/** H5/微信/查件:纯 Arco,后台预热 VXE/HT 会注入全局 CSS 造成登录页闪乱 */
function isH5LikePath(path: string) {
const normalized = path.split('?')[0] || '/';
return (
normalized === '/h5' ||
normalized.startsWith('/h5/') ||
normalized.startsWith('/wechat/') ||
normalized === '/track/query'
);
}
function isLightEntry(path: string) {
const normalized = path.split('?')[0] || '/';
return LIGHT_ENTRY_PATHS.some(
(p) => normalized === p || normalized.startsWith(`${p}/`),
);
}
const app = createApp(App);
app.use(ArcoVue, {});
app.use(ArcoVueIcon);
app.use(router);
installChunkReloadHandlers();
router.onError((error) => {
if (reloadOnChunkLoadError(error)) return;
throw error;
});
app.use(store);
useAppStore().hydrateTheme();
app.use(i18n);
app.use(directive);
app.use(globalProperties);
bindHeavyPluginsContext(app, i18n);
const rbacAccessToken =
localStorage.getItem('rbac_access_token') ||
localStorage.getItem('rbac_token');
if (rbacAccessToken) {
setAuthToken(rbacAccessToken);
}
router.beforeEach((to) => {
if (!isLightEntry(to.path)) {
void ensureFullLocaleMessages();
}
// 只预热、不堵导航;业务页由 page-layout 等就绪后再挂载
if (routeNeedsHeavyPlugins(to.path)) {
void ensureHeavyPlugins();
}
});
async function bootstrap() {
const entryPath =
typeof window !== 'undefined' ? window.location.pathname : '/';
const light = isLightEntry(entryPath);
const rbacUserStore = useRbacUserStore();
// F5:本地身份先灌进 userStore;缺 menus 时与 i18n 并行预取
if (rbacUserStore.isLoggedIn && rbacUserStore.userInfo?.id) {
applyRbacUserToUserStore(rbacUserStore.userInfo);
}
if (!light) {
rbacUserStore.prefetchCurrentUser();
}
await initPreferredLocale();
// Business / home refresh: need full i18n before paint
if (!light) {
await ensureFullLocaleMessages();
}
if (routeNeedsHeavyPlugins(entryPath)) {
await ensureHeavyPlugins();
}
app.mount('#app');
;(window as any).__APP_MOUNTED__ = true;
installWeChatH5HistoryGuard(router);
initAuthKeepAlive();
// GA4:有 VITE_GA_MEASUREMENT_ID 才加载;地域/设备等自动采集
initGa();
// 登录页勿预热 VXE/Element:体积大且会注入全局 CSS,和首屏抢带宽
if (isHomePath(entryPath) && !isH5LikePath(entryPath) && !light) {
void ensureHeavyPlugins();
}
// 等路由就绪 + 浏览器完成首帧绘制后再淡出 boot,避免 mount 瞬间替换抖动
try {
await router.isReady();
} catch {
/* ignore */
}
await new Promise<void>((resolve) => {
requestAnimationFrame(() => {
requestAnimationFrame(() => resolve());
});
});
;(window as any).__dismissAppBoot?.();
}
void bootstrap();