Files
gao-customer-h5/src/pages/event/EventRecordDetail.vue
T
2026-11-02 23:12:05 +08:00

517 lines
15 KiB
Vue

<template>
<div v-can="permissionCodes.eventRecord.read" class="tui-page-layout tui-navbar event-record-detail">
<t-navbar
fixed
:z-index="20"
title="登记事件记录详情"
left-arrow
@left-click="goBack"
>
<template #right>
<t-button
v-if="canUpdateRecord"
@click="toEdit"
theme="primary"
variant="text"
size="small"
content="编辑"
/>
</template>
</t-navbar>
<main class="tui-page-content scroll column">
<customer-profile-card
v-entrance-transition
v-if="record?.customer"
class="customer-profile"
:customer="record.customer"
@click="toCustomerDetail"
/>
<t-cell-group v-entrance-transition v-if="record" title="登记信息" theme="card">
<t-cell
title="登记事件"
:note="record.event?.name || '未知事件'"
:arrow="canReadEvent && !!(record.event?.id || record.eventId)"
:hover="canReadEvent && !!(record.event?.id || record.eventId)"
@click="toEventDetail"
/>
<t-cell title="业务类型" :note="getEventBizTypeText(record.event?.bizType)" />
<t-cell v-if="record.numberValue !== undefined && record.numberValue !== null" title="事件数值" :note="getNumberValueText(record)" />
<t-cell v-if="record.pointValue !== undefined && record.pointValue !== null" title="提交积分值" :note="getPointValueText(record.pointValue, pointLedgerChangeType)" />
<t-cell v-if="record.earnedPointValue !== undefined && record.earnedPointValue !== null" title="本次积分变化" :note="getPointValueText(record.earnedPointValue, pointLedgerChangeType)" />
<t-cell title="登记时间">
<template #note>
<time-label :timestamp="record.registeredAt" underline />
</template>
</t-cell>
<t-cell title="登记人" :note="Text.display(record.operator?.nick || record.operator?.name)" />
<t-cell title="备注" :note="Text.display(record.remark)" />
</t-cell-group>
<t-cell-group
v-if="record?.verificationStatus && record.verificationStatus !== 'NOT_REQUIRED'"
title="核销信息"
theme="card"
v-entrance-transition
>
<t-cell title="核销状态" :note="getVerificationStatusText(record.verificationStatus)" />
<t-cell v-if="record.verificationStatus === 'VERIFIED'" title="核销人" :note="Text.display(record.verifier?.nick || record.verifier?.name)" />
<t-cell v-if="record.verificationStatus === 'VERIFIED'" title="核销时间">
<template #note>
<time-label :timestamp="record.verifiedAt" underline />
</template>
</t-cell>
</t-cell-group>
<card-container
v-if="recordOptionEvent"
description="登记选项"
v-entrance-transition
>
<event-option-selector
class="option-selector"
:model-value="recordOptionSelectionMap"
:event-list="[recordOptionEvent]"
:selected-event-id-list="recordOptionEventIdList"
readonly
/>
</card-container>
<t-button
v-if="canVerifyRecord && record?.verificationStatus === 'PENDING'"
class="cell-button bg-orange white"
variant="text"
:loading="isVerifying"
@click="confirmVerifyRecord"
>
核销登记记录
</t-button>
<card-container v-if="record" v-entrance-transition>
<div class="attachment">
<attachment-selector :items="recordPhotoItemList" title="登记照片" readonly>
<template #viewer-actions="{ item, index, close }">
<attachment-viewer-actions
v-if="canUpdateRecord"
:attach-type="item.attachType"
:busy="isPhotoSaving"
@change-type="changePhotoType(index, $event, close)"
@remove="removePhoto(index, close)"
/>
</template>
</attachment-selector>
</div>
</card-container>
<t-button
v-if="record && canDeleteRecord"
class="cell-button"
theme="danger"
:loading="isDeleting"
:disabled="isDeleting"
content="删除登记记录"
v-entrance-transition
@click="confirmRemoveRecord"
/>
<t-empty v-if="!record" :description="isLoading ? '正在加载登记事件记录' : '登记事件记录不存在'" />
</main>
<event-record-verify-dialog
v-model:visible="verifyDialogVisible"
:record="record"
:can-update="canUpdateRecord"
:submit="submitVerifyRecord"
@verified="handleVerifySuccess"
/>
</div>
</template>
<script setup lang="ts">
import { DialogPlugin } from "tdesign-mobile-vue";
import { useExpandToMaster, usePane } from "timi-tdesign-mobile";
import { Text, TimeLabel } from "timi-web";
import { type Attachment, useUserStore } from "timi-web-api";
import EventRecordAPI from "@/api/EventRecordAPI";
import CustomerProfileCard from "@/components/CustomerProfileCard.vue";
import EventOptionSelector from "@/components/EventOptionSelector.vue";
import EventRecordVerifyDialog from "@/components/EventRecordVerifyDialog.vue";
import AttachmentViewerActions from "@/components/attachmentSelector/AttachmentViewerActions.vue";
import AttachmentSelector from "@/components/attachmentSelector/AttachmentSelector.vue";
import type { PhotoItem } from "@/types/Photo";
import type { PointLedgerChangeType } from "@/types/Point";
import { getAttachmentReadURL } from "@/utils/attachment";
import { buildEventOptionSelectionMap } from "@/utils/eventOption";
import { getEventBizTypeText, isRetiredEvent } from "@/utils/event";
import { getErrorMessage } from "@/utils/error";
import { formatEventNumberValue } from "@/utils/eventValue";
import { Message } from "@/utils/message";
import { permissionCodes } from "@/utils/permission";
import type { EventRecord } from "@/types/EventRecord";
defineOptions({
name: "EventRecordDetail"
});
const props = withDefaults(defineProps<{
/** 指定记录 ID 时忽略路由参数,用于平板主从分栏的内联渲染。 */
recordId?: string;
}>(), {
recordId: ""
});
const emit = defineEmits<{
/** 内联在右栏时请求切换到编辑视图 */
edit: [];
}>();
const route = useRoute();
const router = useRouter();
const userStore = useUserStore();
function resolveId(): string {
return props.recordId || String(route.params.id || "");
}
// 竖屏整页进来后转成横屏:回到列表页并恢复右栏选中,而不是继续停在整页详情
useExpandToMaster({
path: "/management/registration-event-records",
id: () => resolveId()
});
/** 非空说明当前被内联在主从分栏的右栏里 */
const pane = usePane();
// ---------- 记录数据 ----------
const record = ref<EventRecord>();
const isLoading = ref(false);
const isVerifying = ref(false);
const isDeleting = ref(false);
const isPhotoSaving = ref(false);
const verifyDialogVisible = ref(false);
const isRetiredRecord = computed(() => isRetiredEvent(record.value?.event));
const canUpdateRecord = computed(() => !!record.value && !isRetiredRecord.value && userStore.hasPermissions(permissionCodes.eventRecord.update));
const canVerifyRecord = computed(() => !!record.value && !isRetiredRecord.value && userStore.hasPermissions(permissionCodes.eventRecord.verify));
const canDeleteRecord = computed(() => !!record.value && !isRetiredRecord.value && userStore.hasPermissions(permissionCodes.eventRecord.delete));
const canReadEvent = computed(() => userStore.hasPermissions(permissionCodes.event.read));
const recordPhotoItemList = computed(() => (record.value?.attachmentList || [])
.filter(item => !!item.id)
.map(item => ({
attachment: item,
attachType: item.attachType || "PHOTO",
previewUrl: getAttachmentReadURL(item.id || ""),
url: getAttachmentReadURL(item.id || "", 64)
})));
async function submitPhotoItems(photoItems: PhotoItem[]): Promise<void> {
const currentRecord = record.value;
if (!currentRecord?.id || isRetiredRecord.value) {
throw new Error("登记记录不存在");
}
const attachmentList = buildPhotoAttachmentList(photoItems);
await EventRecordAPI.update({
id: currentRecord.id,
remark: currentRecord.remark || "",
attachmentList
});
record.value = {
...currentRecord,
attachmentList
};
}
async function applyPhotoItems(photoItems: PhotoItem[], close: () => void): Promise<void> {
if (isPhotoSaving.value) {
return;
}
isPhotoSaving.value = true;
try {
await submitPhotoItems(photoItems);
close();
} catch (error) {
Message.error(getErrorMessage(error, "登记照片修改失败"));
} finally {
isPhotoSaving.value = false;
}
}
async function changePhotoType(index: number, attachType: string, close: () => void): Promise<void> {
if (!recordPhotoItemList.value[index] || recordPhotoItemList.value[index].attachType === attachType) {
return;
}
await applyPhotoItems(recordPhotoItemList.value.map((item, itemIndex) => itemIndex === index ? { ...item, attachType } : item), close);
}
async function removePhoto(index: number, close: () => void): Promise<void> {
if (!recordPhotoItemList.value[index]) {
return;
}
await applyPhotoItems(recordPhotoItemList.value.filter((_, itemIndex) => itemIndex !== index), close);
}
function buildPhotoAttachmentList(photoItems: PhotoItem[]): Attachment[] {
return photoItems.flatMap(item => {
if (item.attachment) {
return [{
...item.attachment,
attachType: item.attachType || item.attachment.attachType || "PHOTO"
}];
}
if (item.tempFileId) {
return [{
attachType: item.attachType || "PHOTO",
tempFileId: item.tempFileId
}];
}
return [];
});
}
function getNumberValueText(value: EventRecord): string {
return formatEventNumberValue(value.event, value.numberValue);
}
function getVerificationStatusText(status?: EventRecord["verificationStatus"]): string {
if (status === "PENDING") {
return "待核销";
}
if (status === "VERIFIED") {
return "已核销";
}
if (status === "NOT_REQUIRED") {
return "无需核销";
}
return "未知";
}
const pointLedgerChangeType = computed(() => record.value?.changeType || parsePointLedgerChangeType(route.query.changeType));
const recordOptionEvent = computed(() => {
const event = record.value?.event;
return event?.id && event.optionGroupList?.some(group => group.optionList?.length) ? event : undefined;
});
const recordOptionEventIdList = computed(() => recordOptionEvent.value?.id ? [String(recordOptionEvent.value.id)] : []);
const recordOptionSelectionMap = computed(() => buildEventOptionSelectionMap(
recordOptionEventIdList.value[0] || "",
record.value?.selectedOptionGroupList
));
function getPointValueText(value?: number | null, changeType?: PointLedgerChangeType): string {
if (value === undefined || value === null) {
return "-";
}
if (changeType === "CONSUME") {
return `消耗 ${Math.abs(value)} 分`;
}
if (changeType) {
return `获得 ${Math.abs(value)} 分`;
}
return `${value} 分`;
}
function parsePointLedgerChangeType(value: unknown): PointLedgerChangeType | undefined {
const text = Array.isArray(value) ? String(value[0] || "") : String(value || "");
return ["EARN", "CONSUME", "REVOKE", "ADJUST"].includes(text) ? text as PointLedgerChangeType : undefined;
}
// ---------- 页面数据 ----------
async function loadRecord(): Promise<void> {
const recordId = resolveId();
if (!recordId) {
record.value = undefined;
return;
}
isLoading.value = true;
try {
record.value = await EventRecordAPI.detail(recordId);
} catch (err) {
record.value = undefined;
console.warn("登记事件记录加载失败", err);
} finally {
isLoading.value = false;
}
}
async function confirmVerifyRecord(): Promise<void> {
const currentRecord = record.value;
if (!currentRecord?.id || currentRecord.verificationStatus !== "PENDING" || isVerifying.value) {
return;
}
verifyDialogVisible.value = true;
}
async function submitVerifyRecord(record: EventRecord, attachmentList: Attachment[]): Promise<void> {
const id = record.id;
if (!id || isRetiredRecord.value) {
throw new Error("登记事件记录不存在");
}
isVerifying.value = true;
try {
if (canUpdateRecord.value) {
await EventRecordAPI.update({
id,
remark: record.remark || "",
attachmentList
});
}
await EventRecordAPI.verify(id);
} finally {
isVerifying.value = false;
}
}
async function handleVerifySuccess(): Promise<void> {
verifyDialogVisible.value = false;
if (record.value?.id) {
record.value = await EventRecordAPI.detail(record.value.id);
}
}
// ---------- 删除 ----------
function confirmRemoveRecord(): void {
const currentRecord = record.value;
if (!currentRecord?.id || !canDeleteRecord.value || isDeleting.value) {
return;
}
const customerName = currentRecord.customer?.name || "未知客户";
const eventName = currentRecord.event?.name || "未知事件";
const recordId = currentRecord.id;
const dialog = DialogPlugin.confirm({
title: "删除登记记录",
content: `确定删除 ${customerName} 的 ${eventName} 记录吗?删除后无法恢复。`,
cancelBtn: "取消",
confirmBtn: "删除",
onCancel() {
dialog.destroy();
},
async onConfirm() {
dialog.destroy();
await removeRecord(recordId);
}
});
}
async function removeRecord(id: string): Promise<void> {
if (isDeleting.value || isRetiredRecord.value) {
return;
}
isDeleting.value = true;
try {
await EventRecordAPI.remove(id);
Message.success("登记记录已删除");
if (pane) {
pane.close();
return;
}
await router.replace({ name: "EventRecordList" });
} catch (error) {
const message = getErrorMessage(error, "登记记录删除失败");
if (message.includes("积分余额不足") || message.includes("余额不足")) {
Message.warning("积分余额不足,请先人工调整积分后再删除登记记录");
return;
}
Message.error(message);
} finally {
isDeleting.value = false;
}
}
// ---------- 导航 ----------
async function toEdit(): Promise<void> {
const recordId = resolveId();
if (!recordId || !canUpdateRecord.value) {
return;
}
// 分栏时右栏直接切到编辑视图,不跳路由,列表上下文保留
if (pane) {
emit("edit");
return;
}
await router.push(`/management/registration-event-records/${recordId}/edit`);
}
function goBack(): void {
if (pane) {
pane.close();
return;
}
void router.back();
}
async function toCustomerDetail(): Promise<void> {
const customerId = record.value?.customer?.id;
if (!customerId) {
return;
}
await router.push({
name: "CustomerDetail",
params: { id: customerId },
query: { transitionDepth: String(getCurrentRouteDepth() + 1) }
});
}
async function toEventDetail(): Promise<void> {
const eventId = record.value?.event?.id || record.value?.eventId;
if (!canReadEvent.value || !eventId) {
return;
}
await router.push({
name: "EventDetail",
params: { id: eventId },
query: { transitionDepth: String(getCurrentRouteDepth() + 1) }
});
}
function getCurrentRouteDepth(): number {
const transitionDepth = Array.isArray(route.query.transitionDepth)
? route.query.transitionDepth[0]
: route.query.transitionDepth;
const depth = Number(transitionDepth);
if (transitionDepth && Number.isInteger(depth) && 0 <= depth && depth <= 20) {
return depth;
}
return Number(route.meta.depth ?? 0);
}
watch(() => [props.recordId, route.params.id], async () => {
await loadRecord();
}, { immediate: true });
</script>
<style scoped lang="less">
.event-record-detail {
.customer-profile {
margin: 0 1rem;
}
.attachment {
padding: 1rem;
}
.option-selector {
padding-left: 1rem;
}
}
:global(html.theme-dark) {
.registration-record-detail {
.photo-panel {
background: rgba(15, 23, 42, .88);
box-shadow: 0 .75rem 2rem rgba(2, 6, 23, .32);
}
}
}
</style>