diff --git a/app/src/main/ipc/register-handlers.ts b/app/src/main/ipc/register-handlers.ts index 0b6d8b9..9145dc3 100644 --- a/app/src/main/ipc/register-handlers.ts +++ b/app/src/main/ipc/register-handlers.ts @@ -104,8 +104,7 @@ export function registerIpcHandlers(): void { const snapshot: PrinterStatusSnapshot = { ribbonType: cached?.ribbonType ?? '—', statusText: parsed.statusText, - serialNo: cached?.serialNo ?? '—', - printedCount: cached?.printedCount ?? 0 + serialNo: cached?.serialNo ?? '—' } configStore.set('lastPrinterStatus', snapshot) return ok({ statusText: parsed.statusText, statusCode: code }) diff --git a/app/src/renderer/src/components/AppHeader.vue b/app/src/renderer/src/components/AppHeader.vue index ba55641..23bd153 100644 --- a/app/src/renderer/src/components/AppHeader.vue +++ b/app/src/renderer/src/components/AppHeader.vue @@ -10,7 +10,6 @@ >状态: {{ status.statusText }} 序列号: {{ status.serialNo }} - 已发行: {{ status.printedCount }}
@@ -26,7 +25,7 @@ import { computed, onMounted } from 'vue' import { useConfigStore } from '@/stores/config' import { useAppStore } from '@/stores/app' -import { refreshPrinterHeader } from '@/composables/usePrinterStatus' +import { refreshLiveStatus } from '@/composables/usePrinterStatus' defineProps<{ mode?: string }>() @@ -35,12 +34,12 @@ const appStore = useAppStore() const status = computed(() => configStore.printer) onMounted(() => { - if (appStore.initialized) void refreshPrinterHeader(configStore) + if (appStore.initialized) void refreshLiveStatus() }) const statusTone = computed(() => { const t = status.value.statusText - if (t.includes('未初始化') || t.includes('未连接')) return 'c-status-warn' + if (t.includes('未初始化') || t.includes('未连接') || t.includes('失败')) return 'c-status-warn' return '' }) diff --git a/app/src/renderer/src/composables/useAppBootstrap.ts b/app/src/renderer/src/composables/useAppBootstrap.ts index 3129aea..d035b61 100644 --- a/app/src/renderer/src/composables/useAppBootstrap.ts +++ b/app/src/renderer/src/composables/useAppBootstrap.ts @@ -1,6 +1,6 @@ import { onMounted } from 'vue' import { notify } from '@/composables/useNotify' -import { applyPrinterPayload, refreshPrinterFullInfo } from '@/composables/usePrinterStatus' +import { refreshPrinterAfterInit } from '@/composables/usePrinterStatus' import { configGet, dllInit, dllRejectAvailable } from '@/api/cardsoon' import { useAppStore } from '@/stores/app' import { useConfigStore } from '@/stores/config' @@ -43,7 +43,7 @@ export function useAppBootstrap(): void { appStore.setInitialized(true) placeholderStatus(configStore, '就绪') await syncRejectApi() - window.setTimeout(() => void refreshPrinterFullInfo(configStore), 1500) + window.setTimeout(() => void refreshPrinterAfterInit(), 1500) return } @@ -62,7 +62,7 @@ export function useAppBootstrap(): void { if (initMeta?.warning) notify.warning(initMeta.warning) placeholderStatus(configStore, initMeta?.warning ? '未连接打印机' : '就绪') await syncRejectApi() - window.setTimeout(() => void refreshPrinterFullInfo(configStore), 1500) + window.setTimeout(() => void refreshPrinterAfterInit(), 1500) } onMounted(() => { diff --git a/app/src/renderer/src/composables/usePrinterStatus.ts b/app/src/renderer/src/composables/usePrinterStatus.ts index 7104ede..c3517ea 100644 --- a/app/src/renderer/src/composables/usePrinterStatus.ts +++ b/app/src/renderer/src/composables/usePrinterStatus.ts @@ -1,68 +1,41 @@ import { dllPrinterInfo, dllPrinterStatus, parsePrinterInfo } from '@/api/cardsoon' import { useConfigStore } from '@/stores/config' -import type { PrinterStatusDisplay } from '@/types/printer' -function isFullPrinterPayload(data: Record): boolean { - return ( - data.snapshot != null || - data.printerList != null || - data.serial_no != null || - data.SerialNo != null - ) -} - -export function applyPrinterPayload( - configStore: ReturnType, - data: Record -): void { - const snapshot = data.snapshot as PrinterStatusDisplay | undefined - if (snapshot) { - configStore.setPrinter(snapshot) - return - } - if (isFullPrinterPayload(data)) { - configStore.setPrinter(parsePrinterInfo(data)) - return - } - if (typeof data.statusText === 'string') { - configStore.setPrinter({ - ...configStore.printer, - statusText: data.statusText - }) - return - } - if (data.fromCache) { - configStore.setPrinter({ - ribbonType: String(data.ribbonType ?? configStore.printer.ribbonType), - statusText: String(data.statusText ?? configStore.printer.statusText), - serialNo: String(data.serialNo ?? configStore.printer.serialNo), - printedCount: Number(data.printedCount ?? configStore.printer.printedCount) - }) - } -} - -export async function refreshPrinterHeader( - configStore: ReturnType -): Promise { +/** SAPI_PrinterCheckstatus:仅刷新 Header 状态文案 */ +export async function refreshLiveStatus(): Promise { + const store = useConfigStore() try { const r = await dllPrinterStatus() - if (r.ok && r.data) { - applyPrinterPayload(configStore, r.data as Record) + if (r.ok && r.data?.statusText) { + store.setPrinter({ ...store.printer, statusText: r.data.statusText }) } } catch { /* 无打印机时不阻塞 */ } } -export async function refreshPrinterFullInfo( - configStore: ReturnType -): Promise { +/** GetPrinterInfoEx:仅刷新色带、序列号(启动时一次) */ +export async function refreshPrinterInfo(): Promise { + const store = useConfigStore() try { const info = await dllPrinterInfo() - if (info.ok && info.data) { - applyPrinterPayload(configStore, info.data) - } + if (!info.ok || !info.data) return + const data = info.data as Record + const parsed = data.snapshot + ? (data.snapshot as typeof store.printer) + : parsePrinterInfo(data) + store.setPrinter({ + ...store.printer, + ribbonType: parsed.ribbonType, + serialNo: parsed.serialNo + }) } catch { /* 无打印机时不阻塞 */ } } + +/** 启动后:先拉静态信息,再查实时状态 */ +export async function refreshPrinterAfterInit(): Promise { + await refreshPrinterInfo() + await refreshLiveStatus() +} diff --git a/app/src/renderer/src/types/printer.ts b/app/src/renderer/src/types/printer.ts index 9a3ce19..fe91418 100644 --- a/app/src/renderer/src/types/printer.ts +++ b/app/src/renderer/src/types/printer.ts @@ -2,12 +2,10 @@ export interface PrinterStatusDisplay { ribbonType: string statusText: string serialNo: string - printedCount: number } export const defaultPrinterStatus: PrinterStatusDisplay = { ribbonType: '—', statusText: '—', - serialNo: '—', - printedCount: 0 + serialNo: '—' } diff --git a/app/src/renderer/src/views/DistributeRunningView.vue b/app/src/renderer/src/views/DistributeRunningView.vue index f870c7c..556f4dc 100644 --- a/app/src/renderer/src/views/DistributeRunningView.vue +++ b/app/src/renderer/src/views/DistributeRunningView.vue @@ -82,6 +82,7 @@ import { computed, onMounted, onUnmounted, ref } from 'vue' import { useRoute, useRouter } from 'vue-router' import { notify } from '@/composables/useNotify' +import { refreshLiveStatus } from '@/composables/usePrinterStatus' import AppShell from '@/layouts/AppShell.vue' import AppHeader from '@/components/AppHeader.vue' import AppFooter from '@/components/AppFooter.vue' @@ -259,6 +260,7 @@ async function enterWaitPhase(next: 'completed' | 'failed', errorText = ''): Pro appStore.setMode(sessionMode) lastCardPosition = -1 await startCardPositionWatch(sessionMode) + void refreshLiveStatus() } async function enterFailedPhase(errorText = ''): Promise { @@ -321,6 +323,7 @@ async function resubmitTask(): Promise { usbAwaitNewCycle = true collectHint.value = usbTaskStatusHint(USB_TASK_PREPARING) unsubUsb = onUsbPollTick((payload) => applyUsbProgress(payload as UsbPollPayload)) + void refreshLiveStatus() return } @@ -349,6 +352,7 @@ async function resubmitTask(): Promise { return } unsubJob = onJobPollTick((payload) => applyJobProgress(payload as JobPollPayload)) + void refreshLiveStatus() } catch (e) { await backToWait(String(e)) } finally { @@ -460,6 +464,7 @@ onMounted(async () => { setProgress(0) collectHint.value = usbTaskStatusHint(USB_TASK_PREPARING) unsubUsb = onUsbPollTick((payload) => applyUsbProgress(payload as UsbPollPayload)) + void refreshLiveStatus() return } @@ -476,6 +481,7 @@ onMounted(async () => { return } unsubJob = onJobPollTick((payload) => applyJobProgress(payload as JobPollPayload)) + void refreshLiveStatus() }) onUnmounted(() => { diff --git a/app/src/renderer/src/views/HomeView.vue b/app/src/renderer/src/views/HomeView.vue index c95ba85..6af2ef2 100644 --- a/app/src/renderer/src/views/HomeView.vue +++ b/app/src/renderer/src/views/HomeView.vue @@ -52,7 +52,7 @@ import { computed } from 'vue' import { useRouter } from 'vue-router' import { notify, notifyRequireInit } from '@/composables/useNotify' -import { refreshPrinterHeader } from '@/composables/usePrinterStatus' +import { refreshLiveStatus } from '@/composables/usePrinterStatus' import AppShell from '@/layouts/AppShell.vue' import AppHeader from '@/components/AppHeader.vue' import AppFooter from '@/components/AppFooter.vue' @@ -77,7 +77,7 @@ async function onReset(): Promise { const r = await dllPrinterReset() if (r.ok) { notify.success('已发送重置指令') - await refreshPrinterHeader(configStore) + await refreshLiveStatus() } else notify.error(r.message || '重置失败') } @@ -88,8 +88,10 @@ async function onReject(): Promise { return } const r = await dllPrinterReject() - if (r.ok) notify.success('已废弃卡片') - else notify.error(r.message || '操作失败') + if (r.ok) { + notify.success('已废弃卡片') + await refreshLiveStatus() + } else notify.error(r.message || '操作失败') } async function onTemplate(): Promise { diff --git a/app/src/shared/printer-info.ts b/app/src/shared/printer-info.ts index e4e1e5f..439fc77 100644 --- a/app/src/shared/printer-info.ts +++ b/app/src/shared/printer-info.ts @@ -2,7 +2,6 @@ export interface PrinterStatusSnapshot { ribbonType: string statusText: string serialNo: string - printedCount: number } const PRINTER_STATUS_MAP: Record = { @@ -30,6 +29,7 @@ function normalizeStatus(raw: unknown): string { return PRINTER_STATUS_MAP[s] ?? s } +/** SAPI_PrinterCheckstatus 返回值 → 展示文案 */ export function statusTextFromCheckstatus(code: number): { ok: boolean statusText: string @@ -59,36 +59,18 @@ function snapshotFromRecord(row: Record): PrinterStatusSnapshot 'PrinterSerial', 'PrinterName' ]) - const statusRaw = pickFirst(row, [ - 'printer_status', - 'PrinterStatus', - 'PrinterType', - 'Status' - ]) const ribbon = pickFirst(row, ['ribbon_type', 'RibbonType', 'RibbonAmount']) - const printed = pickFirst(row, ['printed_count', 'PrintedCount', 'PrintCount', 'printedCount']) - - let statusText = normalizeStatus(statusRaw) - if (statusText === '—') { - const remain = row.RibbonRemain ?? row.RemainCount - const capacity = row.RibbonCapacity ?? row.Capacity ?? row.MaxCount - if (remain != null && capacity != null) { - statusText = `${remain}/${capacity}` - } - } return { ribbonType: String(ribbon ?? '—'), - statusText, - serialNo: String(serial ?? '—'), - printedCount: Number(printed ?? 0) + statusText: '—', + serialNo: String(serial ?? '—') } } export function parsePrinterInfoFromDll(json: Record): PrinterStatusSnapshot { const flatSerial = pickFirst(json, ['serial_no', 'SerialNo', 'serialNo', 'szPrinterSerial']) - const flatStatus = pickFirst(json, ['printer_status', 'PrinterStatus']) - if (flatSerial != null || flatStatus != null) { + if (flatSerial != null) { return snapshotFromRecord(json) }