Files
SoonMachine/app/src/renderer/src/composables/useAppBootstrap.ts
T
24kycj 574cb353e8 完善分发与收集任务全流程
- RestJobEx 对齐 task_id,path_file 提交目录路径
- 运行页进度仅跟接口轮询,失败态停留 page3 UI
- 完成态卡位续做,USB/任务轮询生命周期优化
- 拆分 DLL 加载、任务 staging 与提交前校验
- 移除 mock 与冗余样式,补充 native 依赖

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-06-01 09:43:24 +08:00

76 lines
2.4 KiB
TypeScript

import { onMounted } from 'vue'
import { notify } from '@/composables/useNotify'
import { applyPrinterPayload, refreshPrinterHeader } from '@/composables/usePrinterStatus'
import { configGet, dllInit, dllRejectAvailable } from '@/api/cardsoon'
import { useAppStore } from '@/stores/app'
import { useConfigStore } from '@/stores/config'
let bootstrapped = false
function placeholderStatus(configStore: ReturnType<typeof useConfigStore>, text: string): void {
if (configStore.printer.statusText === '—') {
configStore.setPrinter({ ...configStore.printer, statusText: text })
}
}
export function useAppBootstrap(): void {
const appStore = useAppStore()
const configStore = useConfigStore()
async function hydrateFromConfig() {
const cfg = await configGet()
if (cfg.ok && cfg.data?.lastPrinterStatus) {
configStore.setPrinter(cfg.data.lastPrinterStatus)
}
if (cfg.ok && cfg.data?.sharedDir) {
configStore.setSharedDir(cfg.data.sharedDir)
}
return cfg
}
async function syncRejectApi(): Promise<void> {
try {
const rej = await dllRejectAvailable()
if (rej.ok && rej.data) configStore.rejectApiAvailable = rej.data.available
} catch {
/* optional API */
}
}
async function bootstrap(): Promise<void> {
const cfg = await hydrateFromConfig()
if (cfg.ok && cfg.data?.dllInitialized) {
appStore.setInitialized(true)
placeholderStatus(configStore, '就绪')
await syncRejectApi()
window.setTimeout(() => void refreshPrinterHeader(configStore), 1500)
return
}
const sharedDir = cfg.data?.sharedDir || ''
configStore.setSharedDir(sharedDir)
const init = await dllInit({ sharedDir })
if (!init.ok) {
appStore.setInitialized(false, init.message || 'Init 失败')
configStore.setPrinter({ ...configStore.printer, statusText: '初始化失败' })
notify.error(init.message || '初始化失败,请检查任务目录权限')
return
}
appStore.setInitialized(true)
const initMeta = init.data as { warning?: string } | undefined
if (initMeta?.warning) notify.warning(initMeta.warning)
placeholderStatus(configStore, initMeta?.warning ? '未连接打印机' : '就绪')
await syncRejectApi()
window.setTimeout(() => void refreshPrinterHeader(configStore), 1500)
}
onMounted(() => {
if (bootstrapped) return
bootstrapped = true
window.setTimeout(() => {
void bootstrap()
}, 100)
})
}