477 lines
14 KiB
Vue
477 lines
14 KiB
Vue
<!--
|
||
features/equipment-acceptance/views/EquipmentAcceptanceConfigPage.vue
|
||
用途:设备验收预警配置页。按升级轮次编辑天数、第 4 轮开关和三个固定岗位。
|
||
依赖:equipment-acceptance.api、Element Plus、UserPickerDialog、userPicker.utils、vue-i18n。
|
||
逻辑:由 Chat 作业面的「预警配置」Tab 嵌入。样式和 ElConfigProvider 由作业面提供。
|
||
同一岗位在多个轮次里共用一个人。发送时刻不在本页,由排程 Cron 配置。非管理员只能查看。
|
||
-->
|
||
<script setup lang="ts">
|
||
import { computed, onMounted, ref } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import {
|
||
ElButton,
|
||
ElCard,
|
||
ElCheckbox,
|
||
ElInputNumber,
|
||
ElLoading,
|
||
} from 'element-plus';
|
||
import { BusinessError } from '@/shared/api/client';
|
||
import UserPickerDialog from '@/shared/components/UserPickerDialog.vue';
|
||
import { useMessage } from '@/shared/composables/useMessage';
|
||
import { userPickerLabel } from '@/shared/utils/userPicker.utils';
|
||
import type { UserPickerPerson } from '@/shared/types/userPicker.types';
|
||
import {
|
||
getEquipmentAcceptanceConfig,
|
||
saveEquipmentAcceptanceConfig,
|
||
} from '../api/equipment-acceptance.api';
|
||
|
||
const props = defineProps<{
|
||
editable: boolean;
|
||
}>();
|
||
|
||
type RoleKey = 'buyer_manager' | 'coo' | 'executive';
|
||
type RoundLine =
|
||
| { kind: 'plain'; tag: 'ruleTo' | 'ruleCc'; text: string; note?: string }
|
||
| { kind: 'roles'; tag: 'ruleTo' | 'ruleCc'; roles: RoleKey[] };
|
||
|
||
const ROUNDS: Array<{ index: number; lines: RoundLine[] }> = [
|
||
{
|
||
index: 0,
|
||
lines: [
|
||
{ kind: 'plain', tag: 'ruleTo', text: 'ruleTo1' },
|
||
{ kind: 'plain', tag: 'ruleCc', text: 'ruleCc1', note: 'roundFixedNote' },
|
||
],
|
||
},
|
||
{
|
||
index: 1,
|
||
lines: [
|
||
{ kind: 'plain', tag: 'ruleTo', text: 'ruleTo2' },
|
||
{ kind: 'roles', tag: 'ruleCc', roles: ['coo', 'buyer_manager'] },
|
||
],
|
||
},
|
||
{
|
||
index: 2,
|
||
lines: [
|
||
{ kind: 'roles', tag: 'ruleTo', roles: ['coo'] },
|
||
{ kind: 'roles', tag: 'ruleCc', roles: ['buyer_manager'] },
|
||
],
|
||
},
|
||
{
|
||
index: 3,
|
||
lines: [
|
||
{ kind: 'roles', tag: 'ruleTo', roles: ['executive'] },
|
||
{ kind: 'roles', tag: 'ruleCc', roles: ['coo', 'buyer_manager'] },
|
||
],
|
||
},
|
||
];
|
||
|
||
const { t } = useI18n();
|
||
const msg = useMessage();
|
||
const vLoading = ElLoading.directive;
|
||
const loading = ref(false);
|
||
const saving = ref(false);
|
||
const days = ref<number[]>([30, 37, 44, 51]);
|
||
const round4Enabled = ref(false);
|
||
const roleIds = ref<Record<RoleKey, number | null>>({
|
||
buyer_manager: null,
|
||
coo: null,
|
||
executive: null,
|
||
});
|
||
const candidates = ref<UserPickerPerson[]>([]);
|
||
const knownUsers = ref<UserPickerPerson[]>([]);
|
||
const pickerOpen = ref(false);
|
||
const pickerRole = ref<RoleKey | null>(null);
|
||
|
||
const people = computed(() => {
|
||
const map = new Map<number, UserPickerPerson>();
|
||
for (const user of candidates.value) map.set(user.id, user);
|
||
for (const user of knownUsers.value) map.set(user.id, user);
|
||
return map;
|
||
});
|
||
|
||
const pickerSelectedIds = computed(() => {
|
||
const id = pickerRole.value ? roleIds.value[pickerRole.value] : null;
|
||
return id ? [id] : [];
|
||
});
|
||
|
||
/**
|
||
* 读取当前配置。管理员响应里包含候选人。
|
||
* 核心逻辑:失败时保留页面上的默认天数,并用统一错误提示。
|
||
*/
|
||
async function load(): Promise<void> {
|
||
loading.value = true;
|
||
try {
|
||
const result = await getEquipmentAcceptanceConfig();
|
||
days.value = result.days?.length === 4 ? [...result.days] : [30, 37, 44, 51];
|
||
round4Enabled.value = result.round_4_enabled === true;
|
||
roleIds.value = {
|
||
buyer_manager: result.buyer_manager_user_id ?? null,
|
||
coo: result.coo_user_id ?? null,
|
||
executive: result.executive_user_id ?? null,
|
||
};
|
||
candidates.value = result.users || [];
|
||
knownUsers.value = result.selected_users || [];
|
||
} catch (error) {
|
||
msg.error(error instanceof BusinessError ? error.message : t('admin.equipmentAcceptance.configLoadFailed'));
|
||
} finally {
|
||
loading.value = false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 打开某个岗位的选人弹窗。每个岗位只保留最后选中的一人。
|
||
* @param role - buyer_manager、coo、executive
|
||
*/
|
||
function openPicker(role: RoleKey): void {
|
||
if (!props.editable) return;
|
||
pickerRole.value = role;
|
||
pickerOpen.value = true;
|
||
}
|
||
|
||
/**
|
||
* 把弹窗结果写回当前岗位。选了多人时只留最后一人。
|
||
* @param ids - 弹窗确定时的用户主键
|
||
*/
|
||
function applyPicker(ids: number[]): void {
|
||
const role = pickerRole.value;
|
||
if (!role) return;
|
||
roleIds.value[role] = ids[ids.length - 1] ?? null;
|
||
pickerRole.value = null;
|
||
}
|
||
|
||
/**
|
||
* 清空一个岗位,保存后表示该岗位未配置。
|
||
* @param role - 岗位键
|
||
*/
|
||
function clearRole(role: RoleKey): void {
|
||
roleIds.value[role] = null;
|
||
}
|
||
|
||
/**
|
||
* 保存天数、第 4 轮开关和三个岗位。天数须逐档增大。
|
||
* 核心逻辑:先在页面拦住空值和倒序,服务端再校验用户是否存在。
|
||
*/
|
||
async function save(): Promise<void> {
|
||
if (!props.editable || saving.value) return;
|
||
const nextDays = days.value.map((day) => Number(day));
|
||
const [first, second, third, fourth] = nextDays;
|
||
const increases = first !== undefined && second !== undefined && third !== undefined && fourth !== undefined
|
||
&& first < second && second < third && third < fourth;
|
||
if (nextDays.some((day) => !Number.isInteger(day) || day < 1) || !increases) {
|
||
msg.error(t('admin.equipmentAcceptance.configDaysInvalid'));
|
||
return;
|
||
}
|
||
saving.value = true;
|
||
try {
|
||
const result = await saveEquipmentAcceptanceConfig({
|
||
days: nextDays,
|
||
round4Enabled: round4Enabled.value,
|
||
buyerManagerUserId: roleIds.value.buyer_manager,
|
||
cooUserId: roleIds.value.coo,
|
||
executiveUserId: roleIds.value.executive,
|
||
});
|
||
days.value = [...result.days];
|
||
round4Enabled.value = result.round_4_enabled === true;
|
||
knownUsers.value = result.selected_users || [];
|
||
candidates.value = result.users || [];
|
||
msg.success(t('admin.equipmentAcceptance.configSaved'));
|
||
} catch (error) {
|
||
msg.error(error instanceof BusinessError ? error.message : t('admin.equipmentAcceptance.configSaveFailed'));
|
||
} finally {
|
||
saving.value = false;
|
||
}
|
||
}
|
||
|
||
/**
|
||
* 取出某个岗位已选的人。没有选择时为空。
|
||
* @param role - buyer_manager、coo、executive
|
||
* @returns 选人组件里的用户,或空
|
||
*/
|
||
function rolePerson(role: RoleKey): UserPickerPerson | undefined {
|
||
const id = roleIds.value[role];
|
||
if (!id) return undefined;
|
||
return people.value.get(id);
|
||
}
|
||
|
||
/**
|
||
* 芯片上的姓名。没有选择时返回空串。
|
||
* @param role - 岗位键
|
||
* @returns 展示名
|
||
*/
|
||
function roleName(role: RoleKey): string {
|
||
const person = rolePerson(role);
|
||
if (!person) return '';
|
||
return userPickerLabel(person).replace(/([^)]*)$/, '') || userPickerLabel(person);
|
||
}
|
||
|
||
/**
|
||
* 姓名首字,画在圆形头像里。
|
||
* @param role - 岗位键
|
||
* @returns 一个字符
|
||
*/
|
||
function roleInitial(role: RoleKey): string {
|
||
const name = roleName(role).trim();
|
||
return name.slice(0, 1) || '?';
|
||
}
|
||
|
||
/**
|
||
* 芯片第二行:工号和岗位名。
|
||
* @param role - 岗位键
|
||
* @returns 例如 26030025 · COO
|
||
*/
|
||
function roleMeta(role: RoleKey): string {
|
||
const person = rolePerson(role);
|
||
const roleText = t(`admin.equipmentAcceptance.role.${role}`);
|
||
const employeeId = String(person?.employee_id || person?.username || '').trim();
|
||
return employeeId ? `${employeeId} · ${roleText}` : roleText;
|
||
}
|
||
|
||
onMounted(() => {
|
||
void load();
|
||
});
|
||
</script>
|
||
|
||
<template>
|
||
<section v-loading="loading" class="ea-scroll">
|
||
<p class="ea-lead">{{ t('admin.equipmentAcceptance.configLead') }}</p>
|
||
<ElCard v-if="!loading" shadow="never" class="ea-config-card">
|
||
<template #header>
|
||
<h2 class="ea-card-title">{{ t('admin.equipmentAcceptance.configSheetTitle') }}</h2>
|
||
<p class="ea-lead">{{ t('admin.equipmentAcceptance.configSheetLead') }}</p>
|
||
</template>
|
||
<article
|
||
v-for="round in ROUNDS"
|
||
:key="round.index"
|
||
class="ea-round"
|
||
:class="round.index === ROUNDS.length - 1 ? 'ea-round--last' : ''"
|
||
>
|
||
<div class="ea-round__when">
|
||
<b>{{ t('admin.equipmentAcceptance.roundName', { n: round.index + 1 }) }}</b>
|
||
<label class="ea-days">
|
||
{{ t('admin.equipmentAcceptance.overPrefix') }}
|
||
<ElInputNumber
|
||
v-model="days[round.index]"
|
||
:min="1"
|
||
:step="1"
|
||
:precision="0"
|
||
:disabled="!editable"
|
||
controls-position="right"
|
||
/>
|
||
{{ t('admin.equipmentAcceptance.dayUnit') }}
|
||
</label>
|
||
<template v-if="round.index === 3">
|
||
<ElCheckbox v-model="round4Enabled" :disabled="!editable">
|
||
{{ t('admin.equipmentAcceptance.round4Enabled') }}
|
||
</ElCheckbox>
|
||
<p class="ea-lead">{{ t('admin.equipmentAcceptance.round4ShortHint') }}</p>
|
||
</template>
|
||
</div>
|
||
<div class="ea-round__who" :class="round.index === 3 && !round4Enabled ? 'is-muted' : ''">
|
||
<div v-for="(line, lineIndex) in round.lines" :key="`${round.index}-${lineIndex}`" class="ea-line">
|
||
<span class="ea-line__tag">{{ t(`admin.equipmentAcceptance.${line.tag}`) }}</span>
|
||
<div class="ea-line__body">
|
||
<template v-if="line.kind === 'plain'">
|
||
<p class="ea-line__text">{{ t(`admin.equipmentAcceptance.${line.text}`) }}</p>
|
||
<p v-if="line.note" class="ea-lead">{{ t(`admin.equipmentAcceptance.${line.note}`) }}</p>
|
||
</template>
|
||
<div v-else class="ea-people">
|
||
<div v-for="role in line.roles" :key="role" class="ea-person">
|
||
<span class="ea-person__avatar">{{ roleInitial(role) }}</span>
|
||
<span class="ea-person__text">
|
||
<strong>{{ roleName(role) || t('admin.equipmentAcceptance.roleEmpty') }}</strong>
|
||
<span>{{ roleMeta(role) }}</span>
|
||
</span>
|
||
<ElButton v-if="editable" link type="primary" @click="openPicker(role)">
|
||
{{ roleIds[role] ? t('admin.equipmentAcceptance.replaceUser') : t('admin.equipmentAcceptance.selectUser') }}
|
||
</ElButton>
|
||
<ElButton v-if="editable && roleIds[role]" link @click="clearRole(role)">
|
||
{{ t('admin.equipmentAcceptance.clearUser') }}
|
||
</ElButton>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</article>
|
||
<div class="ea-config-foot">
|
||
<span>{{ editable ? t('admin.equipmentAcceptance.configSaveHint') : t('admin.equipmentAcceptance.configReadOnly') }}</span>
|
||
<ElButton v-if="editable" type="primary" :loading="saving" @click="save">
|
||
{{ t('admin.equipmentAcceptance.saveConfig') }}
|
||
</ElButton>
|
||
</div>
|
||
</ElCard>
|
||
|
||
<UserPickerDialog
|
||
v-model="pickerOpen"
|
||
:users="candidates"
|
||
:selected-ids="pickerSelectedIds"
|
||
:title="t('admin.equipmentAcceptance.selectUser')"
|
||
:subtitle="t('admin.equipmentAcceptance.configPickOne')"
|
||
@confirm="applyPicker"
|
||
/>
|
||
</section>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.ea-scroll {
|
||
flex: 1 1 auto;
|
||
min-height: 240px;
|
||
overflow: auto;
|
||
}
|
||
|
||
.ea-lead {
|
||
margin: 0;
|
||
font-size: var(--el-font-size-extra-small);
|
||
line-height: 1.4;
|
||
color: var(--el-text-color-secondary);
|
||
}
|
||
|
||
.ea-scroll > .ea-lead {
|
||
margin-bottom: 8px;
|
||
}
|
||
|
||
.ea-config-card :deep(.el-card__header) {
|
||
padding: 12px 16px 8px;
|
||
}
|
||
|
||
.ea-config-card :deep(.el-card__body) {
|
||
padding: 0 16px 12px;
|
||
}
|
||
|
||
.ea-card-title {
|
||
margin: 0 0 4px;
|
||
font-size: var(--el-font-size-base);
|
||
font-weight: var(--el-font-weight-primary);
|
||
color: var(--el-text-color-primary);
|
||
}
|
||
|
||
.ea-round {
|
||
position: relative;
|
||
display: grid;
|
||
grid-template-columns: 200px minmax(0, 1fr);
|
||
gap: 12px 20px;
|
||
margin-left: 12px;
|
||
border-left: 2px solid var(--el-border-color);
|
||
padding: 12px 0 12px 20px;
|
||
}
|
||
|
||
.ea-round--last {
|
||
border-left-color: transparent;
|
||
}
|
||
|
||
.ea-round::before {
|
||
content: '';
|
||
position: absolute;
|
||
left: -7px;
|
||
top: 18px;
|
||
width: 12px;
|
||
height: 12px;
|
||
border: 2px solid var(--el-bg-color);
|
||
border-radius: 999px;
|
||
background: var(--el-color-primary);
|
||
box-shadow: 0 0 0 1px var(--el-border-color);
|
||
}
|
||
|
||
.ea-round__when {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 8px;
|
||
color: var(--el-text-color-secondary);
|
||
font-size: var(--el-font-size-small);
|
||
}
|
||
|
||
.ea-days {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
}
|
||
|
||
.ea-days :deep(.el-input-number) {
|
||
width: 120px;
|
||
}
|
||
|
||
.ea-round__who.is-muted {
|
||
opacity: 0.45;
|
||
}
|
||
|
||
.ea-line {
|
||
display: flex;
|
||
align-items: flex-start;
|
||
gap: 8px;
|
||
margin-bottom: 8px;
|
||
}
|
||
|
||
.ea-line__tag {
|
||
flex: none;
|
||
width: 36px;
|
||
margin-top: 6px;
|
||
font-size: var(--el-font-size-extra-small);
|
||
font-weight: var(--el-font-weight-primary);
|
||
color: var(--el-text-color-secondary);
|
||
}
|
||
|
||
.ea-line__text {
|
||
margin: 6px 0 0;
|
||
color: var(--el-text-color-primary);
|
||
font-weight: var(--el-font-weight-primary);
|
||
}
|
||
|
||
.ea-people {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
gap: 8px;
|
||
}
|
||
|
||
.ea-person {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 8px;
|
||
min-height: 32px;
|
||
padding: 4px 8px 4px 4px;
|
||
border-radius: 999px;
|
||
background: var(--el-fill-color-light);
|
||
}
|
||
|
||
.ea-person__avatar {
|
||
display: grid;
|
||
width: 24px;
|
||
height: 24px;
|
||
place-items: center;
|
||
border-radius: 999px;
|
||
background: var(--el-color-primary-light-8);
|
||
color: var(--el-color-primary);
|
||
font-size: var(--el-font-size-extra-small);
|
||
font-weight: var(--el-font-weight-primary);
|
||
}
|
||
|
||
.ea-person__text {
|
||
display: flex;
|
||
flex-direction: column;
|
||
line-height: 1.2;
|
||
color: var(--el-text-color-primary);
|
||
}
|
||
|
||
.ea-person__text span {
|
||
color: var(--el-text-color-secondary);
|
||
font-size: var(--el-font-size-extra-small);
|
||
}
|
||
|
||
.ea-config-foot {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: space-between;
|
||
gap: 12px;
|
||
margin: 4px -16px 0;
|
||
padding: 12px 16px;
|
||
border-top: 1px solid var(--el-border-color-lighter);
|
||
color: var(--el-text-color-secondary);
|
||
font-size: var(--el-font-size-extra-small);
|
||
}
|
||
|
||
@media (max-width: 720px) {
|
||
.ea-round {
|
||
grid-template-columns: 1fr;
|
||
}
|
||
}
|
||
</style>
|