refactor event

This commit is contained in:
Timi
2026-08-06 20:53:22 +08:00
parent 1243068fc3
commit 9df6dfaf59
37 changed files with 1584 additions and 2316 deletions
+76 -153
View File
@@ -25,18 +25,18 @@
<t-cell
v-for="item in recentRecordList"
:key="item.id"
:title="getRecordEventName(item)"
:description="formatTime(item.registeredAt)"
:title="item.event?.name || '未知事件'"
:description="Time.toPassedDateTime(item.registeredAt)"
:note="item.remark || '无备注'"
arrow
hover
@click="goRecordDetail(item.id || '')"
@click="toRecordDetail(item.id || '')"
>
<template #leftIcon>
<t-image
v-if="getRecordPhotoURL(item)"
:src="getRecordPhotoURL(item)"
:alt="getRecordEventName(item)"
v-if="getEventRecordPhotoURL(item)"
:src="getEventRecordPhotoURL(item)"
:alt="item.event?.name || '未知事件'"
fit="cover"
class="record-photo"
/>
@@ -48,7 +48,7 @@
theme="primary"
:disabled="!canSubmitRegister"
:loading="submitting"
@click="submitRegister"
@click="doSubmit"
>登记</t-button>
</div>
<t-popup v-model="eventSelectorVisible" placement="bottom">
@@ -60,7 +60,7 @@
:key="item.id"
:title="item.name"
:note="item.id === selectedEventId ? '当前使用' : ''"
@click="selectEvent(item.id || '')"
@click="selectEvent(item.id)"
/>
</t-cell-group>
<div v-else class="empty-block">暂无可用登记事件</div>
@@ -81,72 +81,116 @@ import type { EventItem } from "@/types/EventItem";
import type { EventRecord } from "@/types/EventRecord";
import { permissionCodes } from "@/utils/permission";
import { getEventRecordPhotoURL } from "@/utils/eventRecord";
import { Time } from "timi-web";
import { Storage, Time } from "timi-web";
defineOptions({
name: "QuickSignInPage"
});
const router = useRouter();
const EventItemStorageKey = "quick-scan-registration-event-id";
const settingStore = useSettingStore();
const scanCode = ref("");
const scanStatus = ref("正在启动扫码预览。");
const scannerSessionKey = ref(0);
const matchedCustomer = ref<Customer>();
const recentRecordList = ref<EventRecord[]>([]);
const eventList = ref<EventItem[]>([]);
const selectedEventId = ref("");
const eventSelectorVisible = ref(false);
const submitting = ref(false);
let latestDecodedText = "";
// ---------- 登记事件 ----------
const EventItemStorageKey = "quick-scan-event-id";
const eventSelectorVisible = ref(false);
const selectedEventId = ref("");
const eventList = ref<EventItem[]>([]);
const selectedEventName = computed(() => eventList.value.find(item => item.id === selectedEventId.value)?.name || "请选择登记事件");
const canSubmitRegister = computed(() => !!matchedCustomer.value && !!selectedEventId.value);
async function loadActions(): Promise<void> {
eventList.value = await EventAPI.listValid();
const storedEventId = Storage.getString(EventItemStorageKey);
const storedAction = eventList.value.find(item => item.id === storedEventId);
selectedEventId.value = storedAction?.id || eventList.value[0]?.id || "";
if (selectedEventId.value) {
Storage.setString(EventItemStorageKey, selectedEventId);
}
}
function selectEvent(eventId?: string): void {
if (!eventId) {
return;
}
selectedEventId.value = eventId;
Storage.setString(EventItemStorageKey, selectedEventId);
eventSelectorVisible.value = false;
Toast.success("登记事件已切换");
}
onMounted(loadActions);
// ---------- 扫码 ----------
const scanCode = ref("");
const scanStatus = ref("正在启动扫码预览");
const scannerSessionKey = ref(0);
const matchedCustomer = ref<Customer>();
let latestDecodedText = "";
async function handleDecodedText(rawText: string): Promise<void> {
const normalizedText = rawText.trim();
if (!normalizedText || normalizedText === latestDecodedText) {
return;
}
latestDecodedText = normalizedText;
scanCode.value = normalizedText;
await settingStore.playScanSuccessSound();
await loadCustomer(normalizedText);
if (matchedCustomer.value) {
scanStatus.value = `已识别 ${normalizedText},可直接登记。`;
scanStatus.value = `已识别 ${normalizedText},可直接登记`;
return;
}
scanStatus.value = `已识别 ${normalizedText},但未匹配客户档案。`;
scanStatus.value = `已识别 ${normalizedText},但未匹配客户档案`;
}
function handleScanStatusChange(value: string): void {
scanStatus.value = value;
}
// ---------- 提交 ----------
// ---------- 读取客户 ----------
async function submitRegister(): Promise<void> {
if (!validateForm()) {
const recentRecordList = ref<EventRecord[]>([]);
async function loadCustomer(code: string): Promise<void> {
const customer = await CustomerAPI.findByCode(code);
if (!customer) {
matchedCustomer.value = undefined;
recentRecordList.value = [];
return;
}
matchedCustomer.value = customer;
recentRecordList.value = (await EventRecordAPI.list({
index: 0,
size: 5,
customerId: matchedCustomer.value.id!
})).list;
}
async function toRecordDetail(id: string): Promise<void> {
if (!id) {
return;
}
await router.push(`/management/registration-event-records/${id}`);
}
// ---------- 表单 ----------
const submitting = ref(false);
const canSubmitRegister = computed(() => !!matchedCustomer.value && !!selectedEventId.value);
async function doSubmit(): Promise<void> {
const customer = matchedCustomer.value;
if (!customer) {
return;
}
submitting.value = true;
try {
await CustomerAPI.quickRegister({
customerCode: customer.code || scanCode.value,
await EventRecordAPI.create({
customerId: customer.id,
eventId: selectedEventId.value,
remark: "客户快速扫码登记"
});
})
scanStatus.value = `登记成功:${customer.name || customer.code || scanCode.value}。`;
scanCode.value = "";
matchedCustomer.value = undefined;
@@ -158,127 +202,6 @@ async function submitRegister(): Promise<void> {
submitting.value = false;
}
}
function validateForm(): boolean {
if (!selectedEventId.value) {
showToast("请先选择登记事件", "warning");
return false;
}
if (!matchedCustomer.value) {
scanStatus.value = "当前编码未匹配客户,无法登记。";
showToast("请先扫码匹配客户", "warning");
return false;
}
return true;
}
async function loadCustomer(code: string): Promise<void> {
const customer = await CustomerAPI.nullableDetailByCode(code);
if (!customer) {
matchedCustomer.value = undefined;
recentRecordList.value = [];
return;
}
matchedCustomer.value = customer;
await loadRecentRecordList();
}
async function loadRecentRecordList(): Promise<void> {
const customerId = matchedCustomer.value?.id;
if (!customerId) {
recentRecordList.value = [];
return;
}
recentRecordList.value = (await EventRecordAPI.rangeList({
customerId,
beginAt: 0,
endAt: Time.toTimestamp(new Date())
}))
.sort((left, right) => (right.registeredAt || 0) - (left.registeredAt || 0))
.slice(0, 5);
}
async function goRecordDetail(id: string): Promise<void> {
if (!id) {
return;
}
await router.push(`/management/registration-event-records/${id}`);
}
function getRecordEventName(record: EventRecord): string {
return record.event?.name || "未知事件";
}
function getRecordPhotoURL(record: EventRecord): string {
return getEventRecordPhotoURL(record);
}
function formatTime(value?: number): string {
if (!value) {
return "-";
}
const date = Time.toDateObject(value);
if (!date) {
return "-";
}
return Time.formatDateTime(date);
}
// ---------- 登记事件 ----------
async function loadActions(): Promise<void> {
eventList.value = await EventAPI.enabledList();
const storedEventId = loadStoredEventId();
const storedAction = eventList.value.find(item => item.id === storedEventId);
selectedEventId.value = storedAction?.id || eventList.value[0]?.id || "";
saveStoredEventId(selectedEventId.value);
}
function selectEvent(eventId: string | number | Array<string | number>): void {
if (Array.isArray(eventId)) {
return;
}
const nextEventId = String(eventId);
if (!nextEventId) {
return;
}
selectedEventId.value = nextEventId;
saveStoredEventId(nextEventId);
eventSelectorVisible.value = false;
showToast("登记事件已切换");
}
function loadStoredEventId(): string {
if (typeof window === "undefined") {
return "";
}
return window.localStorage.getItem(EventItemStorageKey) || "";
}
function saveStoredEventId(eventId: string): void {
if (typeof window === "undefined" || !eventId) {
return;
}
window.localStorage.setItem(EventItemStorageKey, eventId);
}
function showToast(message: string, theme: "success" | "warning" = "success"): void {
Toast({
theme,
direction: "row",
message
});
}
onMounted(loadActions);
</script>
<style scoped lang="less">