Files
syops/frontend/src/shared/components/UserPickerDialog.vue
T

323 lines
11 KiB
Vue
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.
<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 shrink-0 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 overflow-hidden md:flex-row">
<aside class="max-h-40 min-h-0 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 shrink-0 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-0 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 shrink-0 items-center gap-3 border-t border-default bg-surface-soft px-5 py-3">
<!-- 已选标签限高滚动,人数再多也不撑开底栏去盖住部门和候选人 -->
<div class="flex max-h-28 min-h-0 min-w-0 flex-1 flex-wrap content-start gap-1.5 overflow-y-auto overscroll-contain pr-1">
<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>