294 lines
9.4 KiB
TypeScript
294 lines
9.4 KiB
TypeScript
import type { Request, Response } from 'express'
|
|
import {
|
|
cancelQueuedPrintJob,
|
|
claimNextPrintJob,
|
|
createAssetLabelPrintJob as createAssetLabelPrintJobService,
|
|
createPrintTerminalPairCode as createPrintTerminalPairCodeService,
|
|
heartbeatPrintTerminalAgent,
|
|
getDefaultPrintTerminalReadiness,
|
|
getPrintAgentJobState,
|
|
resolveUnknownPrintJob,
|
|
isPrintServiceError,
|
|
listPrintJobs,
|
|
listPrintTerminals,
|
|
pairPrintTerminalAgent,
|
|
reportPrintAgentTestPrint,
|
|
setDefaultPrintTerminal as setDefaultPrintTerminalService,
|
|
updateClaimedPrintJobStatus,
|
|
updatePrintTerminalAdminStatus
|
|
} from '../services/printService'
|
|
import { errorResponse, successResponse } from '../utils/response'
|
|
|
|
const handlePrintServiceError = (
|
|
res: Response,
|
|
error: unknown,
|
|
fallbackMessage: string
|
|
) => {
|
|
if (isPrintServiceError(error)) {
|
|
return errorResponse(res, error.message, error.statusCode)
|
|
}
|
|
|
|
console.error('[printController]', error)
|
|
const message =
|
|
error instanceof Error && error.message ? error.message : fallbackMessage
|
|
return errorResponse(res, message)
|
|
}
|
|
|
|
const parsePositiveInt = (value: unknown) => {
|
|
const parsed = Number(value)
|
|
return Number.isInteger(parsed) && parsed > 0 ? parsed : null
|
|
}
|
|
|
|
const getCurrentUserId = (res: Response) => {
|
|
return parsePositiveInt(res.locals.user?.id)
|
|
}
|
|
|
|
const getCurrentTerminalId = (res: Response) => {
|
|
return parsePositiveInt(res.locals.printTerminal?.id)
|
|
}
|
|
|
|
export const getPrintTerminals = async (req: Request, res: Response) => {
|
|
try {
|
|
const terminals = await listPrintTerminals()
|
|
successResponse(res, { list: terminals }, '获取打印终端成功')
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '获取打印终端失败')
|
|
}
|
|
}
|
|
|
|
export const getPrintReadiness = async (_req: Request, res: Response) => {
|
|
try {
|
|
const terminal = await getDefaultPrintTerminalReadiness()
|
|
successResponse(res, { terminal }, '获取打印就绪状态成功')
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '获取打印就绪状态失败')
|
|
}
|
|
}
|
|
|
|
export const createPrintTerminalPairCode = async (req: Request, res: Response) => {
|
|
const userId = getCurrentUserId(res)
|
|
if (!userId) {
|
|
return errorResponse(res, '当前登录用户无效', 401)
|
|
}
|
|
|
|
try {
|
|
const pairCode = await createPrintTerminalPairCodeService(userId)
|
|
successResponse(res, pairCode, '已生成一次性配对码')
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '生成配对码失败')
|
|
}
|
|
}
|
|
|
|
export const setDefaultPrintTerminal = async (req: Request, res: Response) => {
|
|
const terminalId = parsePositiveInt(req.params.id)
|
|
if (!terminalId) {
|
|
return errorResponse(res, '打印终端编号无效', 422)
|
|
}
|
|
|
|
try {
|
|
const terminal = await setDefaultPrintTerminalService(terminalId)
|
|
successResponse(res, terminal, '默认打印电脑已更新')
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '设置默认打印电脑失败')
|
|
}
|
|
}
|
|
|
|
export const updatePrintTerminalStatus = async (req: Request, res: Response) => {
|
|
const terminalId = parsePositiveInt(req.params.id)
|
|
const nextStatus = typeof req.body?.status === 'string' ? req.body.status.trim() : ''
|
|
|
|
if (!terminalId) {
|
|
return errorResponse(res, '打印终端编号无效', 422)
|
|
}
|
|
|
|
if (nextStatus !== 'offline' && nextStatus !== 'disabled') {
|
|
return errorResponse(res, '仅支持启用为离线或停用终端', 422)
|
|
}
|
|
|
|
try {
|
|
const terminal = await updatePrintTerminalAdminStatus(terminalId, nextStatus)
|
|
successResponse(res, terminal, '终端状态已更新')
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '更新终端状态失败')
|
|
}
|
|
}
|
|
|
|
export const createAssetLabelPrintJob = async (req: Request, res: Response) => {
|
|
const userId = getCurrentUserId(res)
|
|
if (!userId) {
|
|
return errorResponse(res, '当前登录用户无效', 401)
|
|
}
|
|
|
|
try {
|
|
const result = await createAssetLabelPrintJobService(req.body, userId)
|
|
successResponse(res, result.job, result.queue_notice)
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '创建打印任务失败')
|
|
}
|
|
}
|
|
|
|
export const getPrintJobs = async (req: Request, res: Response) => {
|
|
try {
|
|
const page = parsePositiveInt(req.query.page) || 1
|
|
const pageSize = parsePositiveInt(req.query.pageSize) || 20
|
|
const status = typeof req.query.status === 'string' ? req.query.status : null
|
|
const result = await listPrintJobs(page, pageSize, status)
|
|
|
|
successResponse(
|
|
res,
|
|
{
|
|
list: result.list,
|
|
total: result.pagination.total,
|
|
page: result.pagination.page,
|
|
pageSize: result.pagination.pageSize
|
|
},
|
|
'获取打印任务成功'
|
|
)
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '获取打印任务失败')
|
|
}
|
|
}
|
|
|
|
export const cancelPrintJob = async (req: Request, res: Response) => {
|
|
const jobId = parsePositiveInt(req.params.id)
|
|
if (!jobId) {
|
|
return errorResponse(res, '打印任务编号无效', 422)
|
|
}
|
|
const userId = getCurrentUserId(res)
|
|
if (!userId) {
|
|
return errorResponse(res, '当前登录用户无效', 401)
|
|
}
|
|
const canManageAll = Array.isArray(res.locals.user?.permissions) &&
|
|
res.locals.user.permissions.includes('system.settings')
|
|
|
|
try {
|
|
await cancelQueuedPrintJob(jobId, userId, canManageAll)
|
|
successResponse(res, null, '打印任务已取消')
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '取消打印任务失败')
|
|
}
|
|
}
|
|
|
|
export const pairPrintAgent = async (req: Request, res: Response) => {
|
|
try {
|
|
const result = await pairPrintTerminalAgent({
|
|
code: req.body?.code,
|
|
name: req.body?.terminal_name || req.body?.name,
|
|
platform: req.body?.platform,
|
|
machine_name: req.body?.machine_name
|
|
})
|
|
|
|
successResponse(res, result, '打印终端配对成功')
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '打印终端配对失败')
|
|
}
|
|
}
|
|
|
|
export const heartbeatPrintAgent = async (req: Request, res: Response) => {
|
|
const terminalId = getCurrentTerminalId(res)
|
|
if (!terminalId) {
|
|
return errorResponse(res, '打印终端未认证', 401)
|
|
}
|
|
|
|
try {
|
|
const result = await heartbeatPrintTerminalAgent(terminalId, {
|
|
name: req.body?.terminal_name || req.body?.name,
|
|
platform: req.body?.platform,
|
|
machine_name: req.body?.machine_name,
|
|
selected_printer_name: req.body?.selected_printer_name,
|
|
printer_snapshot: req.body?.printer_snapshot,
|
|
label_profile: req.body?.label_profile,
|
|
protocol_version: req.body?.protocol_version
|
|
})
|
|
|
|
successResponse(res, result, '心跳已接收')
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '心跳上报失败')
|
|
}
|
|
}
|
|
|
|
export const claimPrintAgentJob = async (req: Request, res: Response) => {
|
|
const terminalId = getCurrentTerminalId(res)
|
|
if (!terminalId) {
|
|
return errorResponse(res, '打印终端未认证', 401)
|
|
}
|
|
|
|
try {
|
|
const result = await claimNextPrintJob(terminalId, req.body?.protocol_version)
|
|
successResponse(res, result, result.message)
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '领取打印任务失败')
|
|
}
|
|
}
|
|
|
|
export const updatePrintAgentJobStatus = async (req: Request, res: Response) => {
|
|
const terminalId = getCurrentTerminalId(res)
|
|
const jobId = parsePositiveInt(req.params.id)
|
|
|
|
if (!terminalId) {
|
|
return errorResponse(res, '打印终端未认证', 401)
|
|
}
|
|
|
|
if (!jobId) {
|
|
return errorResponse(res, '打印任务编号无效', 422)
|
|
}
|
|
|
|
try {
|
|
const result = await updateClaimedPrintJobStatus(terminalId, jobId, {
|
|
status: req.body?.status,
|
|
claim_token: req.body?.claim_token,
|
|
error_message: req.body?.error_message,
|
|
retryable: req.body?.retryable
|
|
})
|
|
|
|
successResponse(res, result.job, result.message)
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '更新打印任务状态失败')
|
|
}
|
|
}
|
|
|
|
export const readPrintAgentJobState = async (req: Request, res: Response) => {
|
|
const terminalId = getCurrentTerminalId(res)
|
|
const jobId = parsePositiveInt(req.params.id)
|
|
if (!terminalId) return errorResponse(res, '打印终端未认证', 401)
|
|
if (!jobId) return errorResponse(res, '打印任务编号无效', 422)
|
|
try {
|
|
successResponse(res, await getPrintAgentJobState(terminalId, jobId), '获取任务状态成功')
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '获取任务状态失败')
|
|
}
|
|
}
|
|
|
|
export const resolvePrintJob = async (req: Request, res: Response) => {
|
|
const userId = getCurrentUserId(res)
|
|
const jobId = parsePositiveInt(req.params.id)
|
|
if (!userId) return errorResponse(res, '当前登录用户无效', 401)
|
|
if (!jobId) return errorResponse(res, '打印任务编号无效', 422)
|
|
try {
|
|
successResponse(res, await resolveUnknownPrintJob(jobId, userId, req.body || {}), '核实结果已保存')
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '保存核实结果失败')
|
|
}
|
|
}
|
|
|
|
export const printAgentTestPrint = async (req: Request, res: Response) => {
|
|
const terminalId = getCurrentTerminalId(res)
|
|
if (!terminalId) {
|
|
return errorResponse(res, '打印终端未认证', 401)
|
|
}
|
|
|
|
try {
|
|
const result = await reportPrintAgentTestPrint(terminalId, {
|
|
selected_printer_name: req.body?.selected_printer_name,
|
|
printer_snapshot: req.body?.printer_snapshot,
|
|
label_profile: req.body?.label_profile,
|
|
success: req.body?.success,
|
|
error_message: req.body?.error_message
|
|
})
|
|
|
|
successResponse(res, result, result.message)
|
|
} catch (error) {
|
|
handlePrintServiceError(res, error, '测试打印状态上报失败')
|
|
}
|
|
}
|