fix: 立账凭证发票号取oma75; 优化前端懒加载
This commit is contained in:
1 parent
0e4e581f9a
commit
d7d1ad522a
22 files changed
+245
-54
No files matched your search
@@ -36,6 +36,7 @@ server {
|
||||
|
||||
# index.html 必须始终重新校验(no-cache):避免浏览器/网关缓存旧版 HTML,引用部署后已删除的旧 hash 产物,
|
||||
# 导致登录后懒加载 chunk 404,报 "Failed to fetch dynamically imported module"。
|
||||
# Chat 内 defineAsyncComponent 不走 router.onError,前端另用 vite:preloadError 做一次刷新自愈。
|
||||
location = /index.html {
|
||||
root /usr/share/nginx/html;
|
||||
add_header Cache-Control "no-cache, no-store, must-revalidate";
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
/**
|
||||
* app/router/guards.ts
|
||||
* 全局路由守卫:认证、权限、旧 URL 兼容、document.title。
|
||||
* 依赖:vue-router、config、auth.store、i18n、adminModulePath、isPublicPath、useMessage。
|
||||
* 依赖:vue-router、config、auth.store、i18n、adminModulePath、isPublicPath、useMessage、chunkLoadRecovery。
|
||||
* 逻辑:beforeEach 做登录/管理员/功能权限校验,未登录跳登录页时携带 redirect 深链参数(登录后原样回跳);
|
||||
* afterEach 将 meta.title(i18n key 或字面量)解析为浏览器标题。
|
||||
* afterEach 将 meta.title(i18n key 或字面量)解析为浏览器标题;
|
||||
* 路由懒加载 404 与 Chat defineAsyncComponent 共用一次刷新自愈。
|
||||
*/
|
||||
import type { Router } from 'vue-router';
|
||||
import { config } from '@/config';
|
||||
@@ -12,6 +13,7 @@ import { useAuthStore } from '@/features/auth/stores/auth.store';
|
||||
import { isPublicPath } from '@/shared/constants/routes';
|
||||
import { useMessage } from '@/shared/composables/useMessage';
|
||||
import { resolveSafeRedirect } from '@/shared/utils/redirect';
|
||||
import { reloadOnceOnChunkLoadError } from '@/shared/utils/chunkLoadRecovery';
|
||||
import i18n from '@/i18n';
|
||||
|
||||
/**
|
||||
@@ -38,22 +40,15 @@ function resolveAppBaseTitle(): string {
|
||||
}
|
||||
|
||||
/**
|
||||
* 懒加载 chunk 失败自愈。
|
||||
* 用途/触发时机:部署后若浏览器(或网关)缓存了旧版 index.html,会引用已删除的旧 hash 产物,
|
||||
* 登录等页面跳转时动态 import() 该 chunk 失败 → 报 "Failed to fetch dynamically imported module"
|
||||
* 并被登录流程误判为“登录失败”。此处检测到该类失败时做一次整页刷新(带 sessionStorage 防循环守卫),
|
||||
* 刷新后取到最新 index.html 与 chunk,用户即可正常进入,不再看到误导性报错。
|
||||
* 核心逻辑:仅匹配 chunk 加载失败错误;同一标签页会话内只刷新一次,避免服务器持续异常时无限刷新。
|
||||
* 路由级懒加载 chunk 失败自愈。
|
||||
* 用途/触发时机:部署后旧 index.html 或旧路由分包 404。Chat 内 defineAsyncComponent 不走 router.onError,
|
||||
* 由 main.ts 的 vite:preloadError 与 defineRecoverableAsyncComponent 覆盖。
|
||||
* 核心逻辑:委托 reloadOnceOnChunkLoadError,同一标签页只刷新一次。
|
||||
* @param router - Vue Router 实例
|
||||
*/
|
||||
function setupChunkLoadRecovery(router: Router): void {
|
||||
router.onError((error) => {
|
||||
const message = String((error as Error)?.message || '');
|
||||
if (!message.includes('Failed to fetch dynamically imported module')) return;
|
||||
const recoveryKey = 'hxops_chunk_load_recovery_once';
|
||||
if (sessionStorage.getItem(recoveryKey)) return;
|
||||
sessionStorage.setItem(recoveryKey, '1');
|
||||
window.location.reload();
|
||||
reloadOnceOnChunkLoadError(error);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -4,7 +4,7 @@
|
||||
|
||||
* 用途:对话页主区域,组合侧栏、消息列表、输入栏、货代会话 Workflow 时间线与内置浏览器。
|
||||
|
||||
* 依赖:ChatSidebar、ChatMessageList、ChatInputBar、ShippingWorkflowTimeline、ContractPaymentChatPanel、VoucherCheckChatPanel、chat.context。
|
||||
* 依赖:ChatSidebar、ChatMessageList、ChatInputBar、ShippingWorkflowTimeline、ContractPaymentChatPanel、VoucherCheckChatPanel、chunkLoadRecovery、chat.context。
|
||||
|
||||
* 逻辑:本文件只做页面结构编排;货代 Chat 仅上传/进度通知,履约写操作不在本页;
|
||||
* 右侧「本次处理」由 conversationId 驱动 Workflow projection,extractionRunNo 仅作进度装饰;
|
||||
@@ -15,7 +15,7 @@
|
||||
|
||||
<script setup lang="ts">
|
||||
|
||||
import { computed, defineAsyncComponent, provide, ref, watch } from 'vue';
|
||||
import { computed, provide, ref, watch } from 'vue';
|
||||
|
||||
import { useRoute } from 'vue-router';
|
||||
|
||||
@@ -68,16 +68,18 @@ import {
|
||||
|
||||
} from '@/features/maintenance/utils/maintenance-panel-layout.utils';
|
||||
|
||||
import { defineRecoverableAsyncComponent } from '@/shared/utils/chunkLoadRecovery';
|
||||
|
||||
/** 复核工作台按需分包:仅 order-review 模式加载,避免 Chat 首屏带上 PDF.js。 */
|
||||
const ShippingOrderReviewView = defineAsyncComponent(
|
||||
const ShippingOrderReviewView = defineRecoverableAsyncComponent(
|
||||
() => import('@/features/chat/views/ShippingOrderReviewView.vue'),
|
||||
);
|
||||
|
||||
const ContractPaymentChatPanel = defineAsyncComponent(
|
||||
const ContractPaymentChatPanel = defineRecoverableAsyncComponent(
|
||||
() => import('@/features/contract-payment/components/ContractPaymentChatPanel.vue'),
|
||||
);
|
||||
|
||||
const VoucherCheckChatPanel = defineAsyncComponent(
|
||||
const VoucherCheckChatPanel = defineRecoverableAsyncComponent(
|
||||
() => import('@/features/voucher-check/components/VoucherCheckChatPanel.vue'),
|
||||
);
|
||||
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
"delete": "Delete",
|
||||
"loading": "Loading...",
|
||||
"refresh": "Refresh",
|
||||
"chunkLoad": {
|
||||
"failed": "This page was updated. Refresh to load the latest version.",
|
||||
"refresh": "Refresh page"
|
||||
},
|
||||
"yes": "Yes",
|
||||
"no": "No",
|
||||
"app": {
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
"delete": "ลบ",
|
||||
"loading": "กำลังโหลด...",
|
||||
"refresh": "รีเฟรช",
|
||||
"chunkLoad": {
|
||||
"failed": "หน้านี้มีการอัปเดต โปรดรีเฟรชเพื่อโหลดเวอร์ชันล่าสุด",
|
||||
"refresh": "รีเฟรชหน้า"
|
||||
},
|
||||
"yes": "ใช่",
|
||||
"no": "ไม่",
|
||||
"app": {
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
"delete": "删除",
|
||||
"loading": "加载中...",
|
||||
"refresh": "刷新",
|
||||
"chunkLoad": {
|
||||
"failed": "页面资源已更新,请刷新后重试。",
|
||||
"refresh": "刷新页面"
|
||||
},
|
||||
"yes": "是",
|
||||
"no": "否",
|
||||
"app": {
|
||||
|
||||
@@ -6,6 +6,10 @@
|
||||
"delete": "刪除",
|
||||
"loading": "載入中...",
|
||||
"refresh": "刷新",
|
||||
"chunkLoad": {
|
||||
"failed": "頁面資源已更新,請重新整理後再試。",
|
||||
"refresh": "重新整理頁面"
|
||||
},
|
||||
"yes": "是",
|
||||
"no": "否",
|
||||
"app": {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
/**
|
||||
* main.ts
|
||||
* 用途:Vue 应用启动入口(pinia / router / i18n / 401 fetch 包装)。
|
||||
* 依赖:App.vue、router、i18n、config、auth sync、agentTabs。
|
||||
* 逻辑:挂载前设置 document.title;包装 fetch 以刷新 token;仅会话类 401 才派发未授权事件。
|
||||
* 依赖:App.vue、router、i18n、config、auth sync、agentTabs、chunkLoadRecovery。
|
||||
* 逻辑:挂载前设置 document.title;包装 fetch 以刷新 token;仅会话类 401 才派发未授权事件;
|
||||
* 监听 vite:preloadError,部署后旧 hash 分包 404 时刷新一次(含 Chat 懒加载作业面)。
|
||||
*/
|
||||
import { createApp } from 'vue';
|
||||
import './style.css';
|
||||
@@ -14,9 +15,11 @@ import { pinia } from './app/plugins/pinia';
|
||||
import { bindAuthStoreSync } from './features/auth/stores/auth.store';
|
||||
import { refreshAccessToken, getAuthToken, initAuthSync, isSessionUnauthorizedError } from '@/shared/utils/auth';
|
||||
import { initAgentTabSync } from '@/shared/utils/agentTabs';
|
||||
import { setupVitePreloadErrorRecovery } from '@/shared/utils/chunkLoadRecovery';
|
||||
|
||||
initAuthSync();
|
||||
initAgentTabSync();
|
||||
setupVitePreloadErrorRecovery();
|
||||
document.title = String(i18n.global.t(config.appTitleKey));
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* shared/components/AsyncChunkLoadError.vue
|
||||
* 用途:懒加载分包在一次自动刷新后仍失败时的兜底提示。
|
||||
* 依赖:vue-i18n common.chunkLoad。
|
||||
* 逻辑:由 defineRecoverableAsyncComponent 作为 errorComponent 挂上;按钮整页刷新。
|
||||
*/
|
||||
import { useI18n } from 'vue-i18n';
|
||||
|
||||
const { t } = useI18n();
|
||||
|
||||
/**
|
||||
* 用户手动刷新,重新拉取 index.html 与最新 hash 分包。
|
||||
*/
|
||||
function refreshPage(): void {
|
||||
window.location.reload();
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<section class="flex min-h-0 flex-1 flex-col items-center justify-center gap-3 px-6 py-10 text-center">
|
||||
<p class="text-sm text-muted">{{ t('common.chunkLoad.failed') }}</p>
|
||||
<button
|
||||
type="button"
|
||||
class="rounded-md bg-brand-600 px-3 py-1.5 text-sm font-medium text-white hover:bg-brand-700"
|
||||
@click="refreshPage"
|
||||
>
|
||||
{{ t('common.chunkLoad.refresh') }}
|
||||
</button>
|
||||
</section>
|
||||
</template>
|
||||
@@ -0,0 +1,40 @@
|
||||
/**
|
||||
* shared/utils/chunkLoadRecovery.test.ts
|
||||
* 用途:校验分包 404 识别与同一标签页只刷新一次。
|
||||
* 依赖:vitest;不挂载 Vue。
|
||||
* 逻辑:覆盖 Chromium 文案、非分包错误、第二次失败不再刷新。
|
||||
*/
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
import {
|
||||
CHUNK_LOAD_RECOVERY_KEY,
|
||||
isChunkLoadError,
|
||||
reloadOnceOnChunkLoadError,
|
||||
} from './chunkLoadRecovery';
|
||||
|
||||
describe('chunkLoadRecovery', () => {
|
||||
afterEach(() => {
|
||||
sessionStorage.removeItem(CHUNK_LOAD_RECOVERY_KEY);
|
||||
});
|
||||
|
||||
it('detects chromium dynamic import 404 copy', () => {
|
||||
// 断言:线上控制台那句 Failed to fetch… 必须识别为分包失败。
|
||||
expect(isChunkLoadError(new Error(
|
||||
'Failed to fetch dynamically imported module: http://example/assets/VoucherCheckChatPanel-CQCRT86N.js',
|
||||
))).toBe(true);
|
||||
});
|
||||
|
||||
it('ignores unrelated errors', () => {
|
||||
// 断言:业务异常不能触发整页刷新。
|
||||
expect(isChunkLoadError(new Error('Network timeout'))).toBe(false);
|
||||
});
|
||||
|
||||
it('reloads once then stops', () => {
|
||||
// 断言:同一会话第一次刷新,第二次交给错误 UI。
|
||||
const reload = vi.fn();
|
||||
const error = new Error('Failed to fetch dynamically imported module: ./x.js');
|
||||
expect(reloadOnceOnChunkLoadError(error, reload)).toBe(true);
|
||||
expect(reload).toHaveBeenCalledTimes(1);
|
||||
expect(reloadOnceOnChunkLoadError(error, reload)).toBe(false);
|
||||
expect(reload).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,81 @@
|
||||
/**
|
||||
* shared/utils/chunkLoadRecovery.ts
|
||||
* 用途:部署后旧 hash 分包 404 时整页刷新一次,避免懒加载页面空白。
|
||||
* 依赖:vue defineAsyncComponent、AsyncChunkLoadError。
|
||||
* 逻辑:main.ts 监听 vite:preloadError(覆盖 defineAsyncComponent);
|
||||
* 路由 onError 复用同一套一次刷新;Chat 作业面懒加载再用 onError 兜底。
|
||||
*/
|
||||
import { defineAsyncComponent, type AsyncComponentLoader, type Component } from 'vue';
|
||||
import AsyncChunkLoadError from '@/shared/components/AsyncChunkLoadError.vue';
|
||||
|
||||
export const CHUNK_LOAD_RECOVERY_KEY = 'hxops_chunk_load_recovery_once';
|
||||
|
||||
let vitePreloadRecoveryInstalled = false;
|
||||
|
||||
/**
|
||||
* 判断是否为动态 import / Vite preload 失败(旧 chunk 已被新部署删掉)。
|
||||
* 核心逻辑:匹配 Chromium / Firefox / Safari 常见文案,避免把业务异常当成分包 404。
|
||||
* @param error - 捕获到的未知错误
|
||||
* @returns 是否应按分包加载失败处理
|
||||
*/
|
||||
export function isChunkLoadError(error: unknown): boolean {
|
||||
const message = String((error as { message?: string })?.message || error || '');
|
||||
return (
|
||||
message.includes('Failed to fetch dynamically imported module')
|
||||
|| message.includes('error loading dynamically imported module')
|
||||
|| message.includes('Importing a module script failed')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 分包加载失败时刷新一次当前标签页。
|
||||
* 核心逻辑:sessionStorage 防循环;已刷新过则返回 false,交给错误 UI。
|
||||
* @param error - 捕获到的未知错误
|
||||
* @param reload - 可注入的刷新函数(测试用)
|
||||
* @returns 已发起刷新则为 true
|
||||
*/
|
||||
export function reloadOnceOnChunkLoadError(
|
||||
error: unknown,
|
||||
reload: () => void = () => window.location.reload(),
|
||||
): boolean {
|
||||
if (!isChunkLoadError(error)) return false;
|
||||
try {
|
||||
if (sessionStorage.getItem(CHUNK_LOAD_RECOVERY_KEY)) return false;
|
||||
sessionStorage.setItem(CHUNK_LOAD_RECOVERY_KEY, '1');
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
reload();
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 监听 Vite preload 失败(defineAsyncComponent 与动态 import 都会走到这里)。
|
||||
* 核心逻辑:成功发起刷新时 preventDefault,避免未捕获异常刷屏。
|
||||
*/
|
||||
export function setupVitePreloadErrorRecovery(): void {
|
||||
if (vitePreloadRecoveryInstalled || typeof window === 'undefined') return;
|
||||
vitePreloadRecoveryInstalled = true;
|
||||
window.addEventListener('vite:preloadError', (event) => {
|
||||
const payload = (event as VitePreloadErrorEvent).payload;
|
||||
if (reloadOnceOnChunkLoadError(payload || event)) {
|
||||
event.preventDefault();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Chat / 作业面按需分包:失败时走一次刷新,仍失败则展示刷新提示。
|
||||
* @param loader - 动态 import 函数
|
||||
* @returns 可在模板中使用的异步组件
|
||||
*/
|
||||
export function defineRecoverableAsyncComponent(loader: AsyncComponentLoader): Component {
|
||||
return defineAsyncComponent({
|
||||
loader,
|
||||
onError(error, _retry, fail) {
|
||||
if (reloadOnceOnChunkLoadError(error)) return;
|
||||
fail();
|
||||
},
|
||||
errorComponent: AsyncChunkLoadError,
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user