Merge branch 'dev' into order-delivery-reply
# Conflicts: # .env.shared # backend/app/api/chat.py # backend/app/core/config/settings.py # backend/app/models/user.py # backend/app/services/admin/permission_service.py # docs/demand.md # frontend/src/app/router/routes/workspace.routes.ts # frontend/src/features/admin/modules/users/components/UserFormModal.vue # frontend/src/features/admin/modules/users/composables/useUsersAdmin.ts # frontend/src/features/admin/types/user.types.ts # frontend/src/features/auth/stores/auth.store.ts # frontend/src/features/user/views/UserProfilePage.vue # frontend/src/locales/en/admin.json # frontend/src/locales/en/errors.json # frontend/src/locales/th/admin.json # frontend/src/locales/th/errors.json # frontend/src/locales/zh-CN/admin.json # frontend/src/locales/zh-CN/errors.json # frontend/src/locales/zh-TW/admin.json # frontend/src/locales/zh-TW/errors.json # frontend/src/shared/utils/auth.ts
This commit is contained in:
commit
616034a4cd
541 files changed
+43805
-14767
No files matched your search
@@ -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,320 @@
|
||||
<script setup lang="ts">
|
||||
/**
|
||||
* shared/components/UserPickerDialog.vue
|
||||
* 用途:跨业务公共选人弹窗:左侧部门、右侧搜索/勾选、底部已选与确定。
|
||||
* 依赖:vue-i18n、dialog-overlay / dialog-panel、userPicker.types / userPicker.utils。
|
||||
* 逻辑:父组件传入候选人并 v-model 控制显隐;确定才回写 selectedIds,取消不改已保存选择。
|
||||
* 不自行拉用户列表,便于凭证检核、排程告警等各自提供数据源。
|
||||
*/
|
||||
import { computed, onBeforeUnmount, ref, watch } from 'vue';
|
||||
import { useI18n } from 'vue-i18n';
|
||||
import {
|
||||
USER_PICKER_ALL_DEPT,
|
||||
USER_PICKER_NONE_DEPT,
|
||||
type UserPickerPerson,
|
||||
} from '@/shared/types/userPicker.types';
|
||||
import {
|
||||
filterUserPickerPeople,
|
||||
listUserPickerDepartments,
|
||||
userPickerLabel,
|
||||
} from '@/shared/utils/userPicker.utils';
|
||||
|
||||
interface Props {
|
||||
modelValue: boolean;
|
||||
users: UserPickerPerson[];
|
||||
selectedIds: number[];
|
||||
title?: string;
|
||||
subtitle?: string;
|
||||
/** 为 true 强制展示企微标记;为 false 隐藏;缺省时有 wecom_bound 字段才展示 */
|
||||
showWecomStatus?: boolean;
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
users: () => [],
|
||||
selectedIds: () => [],
|
||||
title: '',
|
||||
subtitle: '',
|
||||
});
|
||||
|
||||
const emit = defineEmits<{
|
||||
(e: 'update:modelValue', value: boolean): void;
|
||||
(e: 'confirm', ids: number[]): void;
|
||||
(e: 'cancel'): void;
|
||||
}>();
|
||||
|
||||
const { t } = useI18n();
|
||||
const query = ref('');
|
||||
const departmentKey = ref(USER_PICKER_ALL_DEPT);
|
||||
const draftIds = ref<number[]>([]);
|
||||
|
||||
const userMap = computed(() => {
|
||||
const next = new Map<number, UserPickerPerson>();
|
||||
for (const user of props.users) next.set(user.id, user);
|
||||
return next;
|
||||
});
|
||||
|
||||
const departments = computed(() => listUserPickerDepartments(props.users));
|
||||
|
||||
const visibleUsers = computed(() =>
|
||||
filterUserPickerPeople(props.users, {
|
||||
departmentKey: departmentKey.value,
|
||||
query: query.value,
|
||||
}),
|
||||
);
|
||||
|
||||
const showWecom = computed(() => {
|
||||
if (props.showWecomStatus === true) return true;
|
||||
if (props.showWecomStatus === false) return false;
|
||||
return props.users.some((user) => typeof user.wecom_bound === 'boolean');
|
||||
});
|
||||
|
||||
const resolvedTitle = computed(() => props.title || t('common.userPicker.title'));
|
||||
const resolvedSubtitle = computed(() => {
|
||||
if (props.subtitle) return props.subtitle;
|
||||
return showWecom.value ? t('common.userPicker.subtitleWithWecom') : t('common.userPicker.subtitle');
|
||||
});
|
||||
|
||||
const visibleAllSelected = computed(() => {
|
||||
const ids = visibleUsers.value.map((user) => user.id);
|
||||
return ids.length > 0 && ids.every((id) => draftIds.value.includes(id));
|
||||
});
|
||||
|
||||
/**
|
||||
* 「全部」或有搜索时用「全选当前列表」;点了具体部门且未搜索时用「全选本部门」。
|
||||
*/
|
||||
const selectVisibleLabel = computed(() => {
|
||||
const scopedToDept =
|
||||
departmentKey.value !== USER_PICKER_ALL_DEPT && !String(query.value || '').trim();
|
||||
return t(scopedToDept ? 'common.userPicker.selectDepartment' : 'common.userPicker.selectVisible');
|
||||
});
|
||||
|
||||
/**
|
||||
* 把传入 id 收成去重正整数,打开弹窗时作为草稿。
|
||||
*/
|
||||
function normalizeIds(ids: number[]): number[] {
|
||||
const seen = new Set<number>();
|
||||
const next: number[] = [];
|
||||
for (const raw of ids || []) {
|
||||
const id = Number(raw);
|
||||
if (!Number.isInteger(id) || id <= 0 || seen.has(id)) continue;
|
||||
seen.add(id);
|
||||
next.push(id);
|
||||
}
|
||||
return next;
|
||||
}
|
||||
|
||||
/**
|
||||
* 部门筛选项展示名:全部 / 未分配 / 具名部门。
|
||||
*/
|
||||
function departmentLabel(key: string): string {
|
||||
if (key === USER_PICKER_ALL_DEPT) return t('common.userPicker.allDepartments');
|
||||
if (key === USER_PICKER_NONE_DEPT) return t('common.userPicker.unknownDepartment');
|
||||
return key;
|
||||
}
|
||||
|
||||
/**
|
||||
* 人员第二行:工号 · 部门。
|
||||
*/
|
||||
function personMeta(person: UserPickerPerson): string {
|
||||
const emp = String(person.employee_id || '').trim();
|
||||
const dept = String(person.department || '').trim() || t('common.userPicker.unknownDepartment');
|
||||
return [emp, dept].filter(Boolean).join(' · ');
|
||||
}
|
||||
|
||||
/**
|
||||
* 勾选或取消单个候选人,保留点击顺序。
|
||||
*/
|
||||
function toggleUser(userId: number): void {
|
||||
if (draftIds.value.includes(userId)) {
|
||||
draftIds.value = draftIds.value.filter((id) => id !== userId);
|
||||
return;
|
||||
}
|
||||
draftIds.value = [...draftIds.value, userId];
|
||||
}
|
||||
|
||||
/**
|
||||
* 全选或取消当前可见列表(受部门与搜索约束)。
|
||||
*/
|
||||
function toggleVisibleSelection(): void {
|
||||
const ids = visibleUsers.value.map((user) => user.id);
|
||||
if (!ids.length) return;
|
||||
if (visibleAllSelected.value) {
|
||||
const drop = new Set(ids);
|
||||
draftIds.value = draftIds.value.filter((id) => !drop.has(id));
|
||||
return;
|
||||
}
|
||||
const seen = new Set(draftIds.value);
|
||||
draftIds.value = [...draftIds.value, ...ids.filter((id) => !seen.has(id))];
|
||||
}
|
||||
|
||||
function closeDialog(): void {
|
||||
emit('update:modelValue', false);
|
||||
}
|
||||
|
||||
function handleCancel(): void {
|
||||
emit('cancel');
|
||||
closeDialog();
|
||||
}
|
||||
|
||||
/**
|
||||
* 把草稿 id 回写给父组件后再关闭。
|
||||
*/
|
||||
function handleConfirm(): void {
|
||||
emit('confirm', [...draftIds.value]);
|
||||
closeDialog();
|
||||
}
|
||||
|
||||
function handleEscKey(event: KeyboardEvent): void {
|
||||
if (event.key === 'Escape' && props.modelValue) handleCancel();
|
||||
}
|
||||
|
||||
watch(
|
||||
() => props.modelValue,
|
||||
(visible) => {
|
||||
if (visible) {
|
||||
draftIds.value = normalizeIds(props.selectedIds);
|
||||
departmentKey.value = USER_PICKER_ALL_DEPT;
|
||||
query.value = '';
|
||||
document.addEventListener('keydown', handleEscKey);
|
||||
return;
|
||||
}
|
||||
document.removeEventListener('keydown', handleEscKey);
|
||||
},
|
||||
{ immediate: true },
|
||||
);
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
document.removeEventListener('keydown', handleEscKey);
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<Teleport to="body">
|
||||
<div
|
||||
v-if="modelValue"
|
||||
class="dialog-overlay z-[120] items-center"
|
||||
@click.self="handleCancel"
|
||||
>
|
||||
<div
|
||||
class="dialog-panel flex max-h-[min(620px,calc(100vh-40px))] w-full max-w-[760px] overflow-hidden"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
>
|
||||
<div class="flex items-start justify-between gap-3 border-b border-default px-5 py-4">
|
||||
<div class="min-w-0">
|
||||
<h3 class="text-base font-semibold text-primary">{{ resolvedTitle }}</h3>
|
||||
<p class="mt-1 text-xs text-muted">{{ resolvedSubtitle }}</p>
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
class="shrink-0 rounded-lg px-1.5 text-xl leading-none text-muted hover:bg-surface-soft hover:text-primary"
|
||||
:aria-label="t('common.close')"
|
||||
@click="handleCancel"
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<div class="flex min-h-0 flex-1 flex-col md:flex-row">
|
||||
<aside class="max-h-40 shrink-0 overflow-auto border-b border-default bg-surface-soft md:max-h-none md:w-[200px] md:border-b-0 md:border-r">
|
||||
<button
|
||||
v-for="dept in departments"
|
||||
:key="dept.key"
|
||||
type="button"
|
||||
class="flex w-full items-center justify-between border-l-4 px-3 py-2.5 text-left text-sm text-secondary"
|
||||
:class="departmentKey === dept.key
|
||||
? 'border-brand-600 bg-surface font-semibold text-brand-600'
|
||||
: 'border-transparent hover:bg-surface'"
|
||||
@click="departmentKey = dept.key"
|
||||
>
|
||||
<span>{{ departmentLabel(dept.key) }}</span>
|
||||
<span class="text-xs font-medium text-muted">{{ dept.count }}</span>
|
||||
</button>
|
||||
</aside>
|
||||
|
||||
<section class="flex min-h-0 min-w-0 flex-1 flex-col">
|
||||
<div class="flex items-center gap-2 border-b border-default px-3 py-2.5">
|
||||
<input
|
||||
v-model="query"
|
||||
type="search"
|
||||
class="input-field min-w-0 flex-1"
|
||||
:placeholder="t('common.userPicker.searchPlaceholder')"
|
||||
/>
|
||||
<button
|
||||
type="button"
|
||||
class="shrink-0 whitespace-nowrap text-xs font-semibold text-brand-600 hover:text-brand-700 disabled:opacity-40"
|
||||
:disabled="visibleUsers.length === 0"
|
||||
@click="toggleVisibleSelection"
|
||||
>
|
||||
{{ selectVisibleLabel }}
|
||||
</button>
|
||||
</div>
|
||||
<div class="min-h-[220px] flex-1 overflow-auto">
|
||||
<button
|
||||
v-for="user in visibleUsers"
|
||||
:key="user.id"
|
||||
type="button"
|
||||
class="flex w-full items-center gap-2.5 border-b border-default px-3 py-2.5 text-left hover:bg-surface-soft"
|
||||
:class="draftIds.includes(user.id) ? 'bg-blue-50/70' : 'bg-surface'"
|
||||
@click="toggleUser(user.id)"
|
||||
>
|
||||
<input
|
||||
type="checkbox"
|
||||
class="pointer-events-none h-3.5 w-3.5 rounded text-brand-600"
|
||||
:checked="draftIds.includes(user.id)"
|
||||
tabindex="-1"
|
||||
readonly
|
||||
/>
|
||||
<span class="min-w-0 flex-1">
|
||||
<span class="block text-sm font-medium text-primary">{{ userPickerLabel(user) }}</span>
|
||||
<span class="block text-xs text-muted">{{ personMeta(user) }}</span>
|
||||
</span>
|
||||
<span
|
||||
v-if="showWecom && user.wecom_bound"
|
||||
class="shrink-0 text-[11px] font-semibold text-emerald-700"
|
||||
>
|
||||
{{ t('common.userPicker.wecomBound') }}
|
||||
</span>
|
||||
<span
|
||||
v-else-if="showWecom"
|
||||
class="shrink-0 text-[11px] font-semibold text-amber-700"
|
||||
>
|
||||
{{ t('common.userPicker.wecomUnbound') }}
|
||||
</span>
|
||||
</button>
|
||||
<div v-if="visibleUsers.length === 0" class="px-3 py-10 text-center text-sm text-muted">
|
||||
{{ t('common.userPicker.empty') }}
|
||||
</div>
|
||||
</div>
|
||||
</section>
|
||||
</div>
|
||||
|
||||
<div class="flex items-center gap-3 border-t border-default bg-surface-soft px-5 py-3">
|
||||
<div class="flex min-w-0 flex-1 flex-wrap gap-1.5">
|
||||
<span
|
||||
v-for="userId in draftIds"
|
||||
:key="userId"
|
||||
class="inline-flex items-center gap-1 rounded-lg bg-blue-50 px-2 py-0.5 text-xs font-medium text-blue-700"
|
||||
>
|
||||
{{ userPickerLabel(userMap.get(userId), userId) }}
|
||||
<button
|
||||
type="button"
|
||||
class="text-blue-400 hover:text-blue-600"
|
||||
@click="toggleUser(userId)"
|
||||
>✕</button>
|
||||
</span>
|
||||
<span v-if="draftIds.length === 0" class="text-sm text-muted">
|
||||
{{ t('common.userPicker.noneSelected') }}
|
||||
</span>
|
||||
</div>
|
||||
<button type="button" class="btn-secondary btn-sm shrink-0" @click="handleCancel">
|
||||
{{ t('common.cancel') }}
|
||||
</button>
|
||||
<button type="button" class="btn-primary btn-sm shrink-0" @click="handleConfirm">
|
||||
{{ t('common.userPicker.confirmCount', { count: draftIds.length }) }}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</Teleport>
|
||||
</template>
|
||||
@@ -0,0 +1,17 @@
|
||||
/**
|
||||
* shared/types/userPicker.types.ts
|
||||
* 用途:公共选人弹窗的人员数据结构,供各业务传入自己的候选人列表。
|
||||
* 依赖:无。
|
||||
* 逻辑:UserPickerDialog / userPicker.utils 共用;wecom_bound 可选,缺省时不展示企微标记。
|
||||
*/
|
||||
export interface UserPickerPerson {
|
||||
id: number;
|
||||
username: string;
|
||||
real_name?: string | null;
|
||||
employee_id?: string | null;
|
||||
department?: string | null;
|
||||
wecom_bound?: boolean;
|
||||
}
|
||||
|
||||
export const USER_PICKER_ALL_DEPT = '__all__';
|
||||
export const USER_PICKER_NONE_DEPT = '__none__';
|
||||
@@ -10,6 +10,8 @@ export const AGENT_CODE_GENERIC = 'generic';
|
||||
export const AGENT_CODE_SHIPPING_NOTICE = 'shipping_notice';
|
||||
export const AGENT_CODE_FILE_CHECK = 'file_check';
|
||||
export const AGENT_CODE_PROCUREMENT_CONTRACT_REVIEW = 'procurement_contract_review';
|
||||
export const AGENT_CODE_CONTRACT_PAYMENT = 'contract_payment';
|
||||
export const AGENT_CODE_VOUCHER_CHECK = 'voucher_check';
|
||||
export const AGENT_CODE_IN_TRANSIT_TRANSFER = 'in_transit_transfer';
|
||||
|
||||
export interface AgentRegistryCapabilities {
|
||||
@@ -237,6 +239,16 @@ export function isProcurementContractAgent(agent: AgentLike): boolean {
|
||||
return resolveAgentCode(agent) === AGENT_CODE_PROCUREMENT_CONTRACT_REVIEW;
|
||||
}
|
||||
|
||||
/** 是否为合同付款预警 Agent(agent_code=contract_payment)。 */
|
||||
export function isContractPaymentAgent(agent: AgentLike): boolean {
|
||||
return resolveAgentCode(agent) === AGENT_CODE_CONTRACT_PAYMENT;
|
||||
}
|
||||
|
||||
/** 是否为立账凭证检核 Agent(agent_code=voucher_check)。 */
|
||||
export function isVoucherCheckAgent(agent: AgentLike): boolean {
|
||||
return resolveAgentCode(agent) === AGENT_CODE_VOUCHER_CHECK;
|
||||
}
|
||||
|
||||
/** 是否启用本地请假表单(后端 leave_feature_enabled)。 */
|
||||
export function canUseLeaveFeature(agent: AgentLike): boolean {
|
||||
return Boolean(agent?.leave_feature_enabled);
|
||||
|
||||
@@ -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,
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
/**
|
||||
* shared/utils/pdfjs.ts
|
||||
* 用途:统一加载 pdfjs-dist,并以正确 MIME 启动 module Worker。
|
||||
* 依赖:pdfjs-dist、Vite `?url` 产物。
|
||||
* 逻辑:nginx 曾把 .mjs 以 application/octet-stream + immutable 长期缓存;
|
||||
* Chrome 对 module Worker 硬性校验 MIME,硬刷新也常仍命中错误缓存。
|
||||
* 因此用 cache:'reload' 拉最新字节,再以 text/javascript Blob 建 Worker,
|
||||
* 通过 GlobalWorkerOptions.workerPort 交给 pdf.js 5(始终 type:'module')。
|
||||
*/
|
||||
type PdfJsModule = typeof import('pdfjs-dist');
|
||||
|
||||
let pdfjsLoader: Promise<PdfJsModule> | null = null;
|
||||
|
||||
/**
|
||||
* 动态加载 PDF.js,并保证 Worker 以 JavaScript MIME 启动。
|
||||
*/
|
||||
export async function loadPdfJs(): Promise<PdfJsModule> {
|
||||
if (!pdfjsLoader) {
|
||||
pdfjsLoader = (async () => {
|
||||
const [pdfjs, workerMod] = await Promise.all([
|
||||
import('pdfjs-dist'),
|
||||
import('pdfjs-dist/build/pdf.worker.min.mjs?url'),
|
||||
]);
|
||||
const workerUrl = String(workerMod.default || '');
|
||||
if (!workerUrl) {
|
||||
throw new Error('PDF.js worker URL is unavailable');
|
||||
}
|
||||
const response = await fetch(workerUrl, { cache: 'reload' });
|
||||
if (!response.ok) {
|
||||
throw new Error(`PDF worker HTTP ${response.status}`);
|
||||
}
|
||||
const source = await response.text();
|
||||
const blobUrl = URL.createObjectURL(new Blob([source], { type: 'text/javascript' }));
|
||||
const worker = new Worker(blobUrl, { type: 'module' });
|
||||
pdfjs.GlobalWorkerOptions.workerPort = worker;
|
||||
pdfjs.GlobalWorkerOptions.workerSrc = workerUrl;
|
||||
return pdfjs;
|
||||
})();
|
||||
}
|
||||
return pdfjsLoader;
|
||||
}
|
||||
|
||||
/** 测试用:重置单例。 */
|
||||
export function resetPdfJsLoaderForTests(): void {
|
||||
pdfjsLoader = null;
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* shared/utils/userPicker.utils.test.ts
|
||||
* 用途:锁定公共选人弹窗的部门分组与筛选规则。
|
||||
*/
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
USER_PICKER_ALL_DEPT,
|
||||
USER_PICKER_NONE_DEPT,
|
||||
type UserPickerPerson,
|
||||
} from '@/shared/types/userPicker.types';
|
||||
import {
|
||||
filterUserPickerPeople,
|
||||
listUserPickerDepartments,
|
||||
userPickerLabel,
|
||||
} from './userPicker.utils';
|
||||
|
||||
const people: UserPickerPerson[] = [
|
||||
{ id: 1, username: 'a', real_name: '钟淑惠', employee_id: '0100004', department: '财务部' },
|
||||
{ id: 2, username: 'b', real_name: '成秋兰', employee_id: '0203036', department: '关务部' },
|
||||
{ id: 3, username: 'c', real_name: '刘江', employee_id: '07090745', department: ' ' },
|
||||
];
|
||||
|
||||
describe('userPickerLabel', () => {
|
||||
it('prefers real name and appends employee id', () => {
|
||||
expect(userPickerLabel(people[0])).toBe('钟淑惠(0100004)');
|
||||
expect(userPickerLabel(undefined, 9)).toBe('9');
|
||||
});
|
||||
});
|
||||
|
||||
describe('listUserPickerDepartments', () => {
|
||||
it('puts all first, named departments next, and unnamed last', () => {
|
||||
expect(listUserPickerDepartments(people)).toEqual([
|
||||
{ key: USER_PICKER_ALL_DEPT, count: 3 },
|
||||
{ key: '财务部', count: 1 },
|
||||
{ key: '关务部', count: 1 },
|
||||
{ key: USER_PICKER_NONE_DEPT, count: 1 },
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('filterUserPickerPeople', () => {
|
||||
it('filters by department and keyword', () => {
|
||||
expect(filterUserPickerPeople(people, { departmentKey: '财务部' }).map((row) => row.id)).toEqual([1]);
|
||||
expect(filterUserPickerPeople(people, { query: '0203' }).map((row) => row.id)).toEqual([2]);
|
||||
expect(filterUserPickerPeople(people, { departmentKey: USER_PICKER_NONE_DEPT }).map((row) => row.id)).toEqual([3]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,80 @@
|
||||
/**
|
||||
* shared/utils/userPicker.utils.ts
|
||||
* 用途:公共选人弹窗的展示名、部门分组与筛选。
|
||||
* 依赖:userPicker.types。
|
||||
* 逻辑:UserPickerDialog 调用;部门为空归入 USER_PICKER_NONE_DEPT。
|
||||
*/
|
||||
import {
|
||||
USER_PICKER_ALL_DEPT,
|
||||
USER_PICKER_NONE_DEPT,
|
||||
type UserPickerPerson,
|
||||
} from '@/shared/types/userPicker.types';
|
||||
|
||||
export interface UserPickerDepartment {
|
||||
key: string;
|
||||
count: number;
|
||||
}
|
||||
|
||||
/**
|
||||
* 人员展示名:实名优先,附工号。
|
||||
*/
|
||||
export function userPickerLabel(person?: UserPickerPerson | null, fallbackId?: number): string {
|
||||
if (!person) return String(fallbackId || '');
|
||||
const name = String(person.real_name || person.username || '').trim();
|
||||
const emp = String(person.employee_id || '').trim();
|
||||
return emp ? `${name}(${emp})` : name || String(person.id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 把用户部门归一成筛选键:空部门用 USER_PICKER_NONE_DEPT。
|
||||
*/
|
||||
export function userPickerDepartmentKey(person: UserPickerPerson): string {
|
||||
return String(person.department || '').trim() || USER_PICKER_NONE_DEPT;
|
||||
}
|
||||
|
||||
/**
|
||||
* 统计部门列表:全部在前,具名部门按名称排序,未分配部门垫底。
|
||||
*/
|
||||
export function listUserPickerDepartments(users: UserPickerPerson[]): UserPickerDepartment[] {
|
||||
const counts = new Map<string, number>();
|
||||
for (const user of users) {
|
||||
const key = userPickerDepartmentKey(user);
|
||||
counts.set(key, (counts.get(key) || 0) + 1);
|
||||
}
|
||||
const named = [...counts.keys()]
|
||||
.filter((key) => key !== USER_PICKER_NONE_DEPT)
|
||||
.sort((a, b) => a.localeCompare(b, 'zh-CN'));
|
||||
const items: UserPickerDepartment[] = [
|
||||
{ key: USER_PICKER_ALL_DEPT, count: users.length },
|
||||
...named.map((key) => ({ key, count: counts.get(key) || 0 })),
|
||||
];
|
||||
const unnamed = counts.get(USER_PICKER_NONE_DEPT) || 0;
|
||||
if (unnamed) items.push({ key: USER_PICKER_NONE_DEPT, count: unnamed });
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
* 按部门与关键字筛选候选人。
|
||||
* @param options.departmentKey - USER_PICKER_ALL_DEPT 表示不限部门
|
||||
* @param options.query - 匹配姓名、用户名、工号、部门
|
||||
*/
|
||||
export function filterUserPickerPeople(
|
||||
users: UserPickerPerson[],
|
||||
options?: { departmentKey?: string; query?: string },
|
||||
): UserPickerPerson[] {
|
||||
const departmentKey = options?.departmentKey || USER_PICKER_ALL_DEPT;
|
||||
const keyword = String(options?.query || '').trim().toLowerCase();
|
||||
return users.filter((user) => {
|
||||
if (departmentKey !== USER_PICKER_ALL_DEPT && userPickerDepartmentKey(user) !== departmentKey) {
|
||||
return false;
|
||||
}
|
||||
if (!keyword) return true;
|
||||
const hay = [
|
||||
user.real_name,
|
||||
user.username,
|
||||
user.employee_id,
|
||||
user.department,
|
||||
].join(' ').toLowerCase();
|
||||
return hay.includes(keyword);
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user