WEB/src/api/print/print.ts
2026-05-15 18:22:48 +08:00

279 lines
6.7 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import request from '@/utils/request'
const v = '1.0'
function stamp() {
return Date.now()
}
/** print.getList:page、pageSize、where(支持 id / name / computer_id / custom_barcode 等,见 print 模块) */
export type PrintListParams = {
page?: number
pageSize?: number
where?: Record<string, unknown>
}
export function getList(data?: PrintListParams) {
return request({
url: '/system',
method: 'post',
params: {
method: 'print.getList',
v,
timestamp: stamp()
},
data: data ?? {}
})
}
export function getItem(id: string | number) {
const numId = Number(id)
return request({
url: '/system',
method: 'post',
params: {
method: 'print.getItem',
v,
timestamp: stamp()
},
data: Number.isFinite(numId) ? { id: numId } : { id }
})
}
export function add(data: Record<string, unknown>) {
const body = { ...data }
if (body.computer_id !== undefined && body.computer_id !== null && body.computer_id !== '') {
const n = Number(body.computer_id)
if (Number.isFinite(n)) body.computer_id = n
}
return request({
url: '/system',
method: 'post',
params: {
method: 'print.add',
v,
timestamp: stamp()
},
data: body
})
}
export function hasName(data: { name: string; id?: string | number | null }) {
const body: Record<string, unknown> = { name: data.name }
if (data.id !== undefined && data.id !== null && data.id !== '') {
const n = Number(data.id)
if (Number.isFinite(n)) body.id = n
}
return request({
url: '/system',
method: 'post',
params: {
method: 'print.hasName',
v,
timestamp: stamp()
},
data: body
})
}
export function updateItem(data: Record<string, unknown>) {
const body = { ...data }
if (body.id !== undefined && body.id !== null && body.id !== '') {
const n = Number(body.id)
if (Number.isFinite(n)) body.id = n
}
if (body.computer_id !== undefined && body.computer_id !== null && body.computer_id !== '') {
const n = Number(body.computer_id)
if (Number.isFinite(n)) body.computer_id = n
}
return request({
url: '/system',
method: 'post',
params: {
method: 'print.updateItem',
v,
timestamp: stamp()
},
data: body
})
}
function normalizeDelIds(ids: unknown[]): number[] {
return ids.map((i) => Number(i)).filter((n) => Number.isFinite(n))
}
/** print.del:ids 必填且须为非空数组(见 print 模块 module.json) */
export function delItems(ids: Array<string | number>) {
const normalized = normalizeDelIds(ids)
if (!normalized.length) {
return Promise.reject(new Error('print.del:ids 必须为非空数组'))
}
return request({
url: '/system',
method: 'post',
params: {
method: 'print.del',
v,
timestamp: stamp()
},
data: { ids: normalized }
})
}
export function getComputer() {
return request({
url: '/system',
method: 'post',
params: {
method: 'print.getComputer',
v,
timestamp: stamp()
},
data: {}
})
}
export function getComputerPrinter(computerId: string | number) {
const numId = Number(computerId)
return request({
url: '/system',
method: 'post',
params: {
method: 'print.getComputerPrinter',
v,
timestamp: stamp()
},
data: Number.isFinite(numId) ? { id: numId } : { id: computerId }
})
}
export function getPrintResult(printId: string) {
return request({
url: '/system',
method: 'post',
params: {
method: 'print.getPrintResult',
v,
timestamp: stamp()
},
data: { printId }
})
}
export function getPrintRecordItem(id: string | number) {
const numId = Number(id)
return request({
url: '/system',
method: 'post',
params: {
method: 'print.getPrintRecordItem',
v,
timestamp: stamp()
},
data: Number.isFinite(numId) ? { id: numId } : { id }
})
}
/** customize:computerId / printer / template 等;express 路径下由服务端忽略 */
export type PrintSendCustomizeParams = {
type: 'customize'
computerId: string
count?: number
data: Record<string, unknown>
printer: string
/** 宜含 page_width、page_height、template_content */
template: Record<string, unknown>
timestamp?: string
}
/**
* express:须 printStationId、requestCode;data 须含 waveGroups(见 order.print.getWavePlatformPrintData)。
* computerId / count / printer / timestamp / template 在 express 下不适用,请求体中不发送。
*/
export type PrintSendExpressParams = {
type: 'express'
printStationId: string
requestCode: string
data: Record<string, unknown>
}
export type PrintSendParams = PrintSendCustomizeParams | PrintSendExpressParams
/** print.send 业务响应字段(成功 code 多为 200,flag 为 success) */
export type PrintSendResult = {
flag?: string
code?: number
message?: string
printId?: number
}
/** savePrinters.express 单项:首选 express_code + printer_name(兼容 express_id、name 由服务端映射) */
export type PrintSavePrintersExpressItem = {
express_code: string
printer_name: string
}
/** print.savePrinters:defaultPrinter、express、print_station_id 均为必填(见 print 模块 module.json) */
export type PrintSavePrintersParams = {
defaultPrinter: string
express: PrintSavePrintersExpressItem[] | Array<Record<string, unknown>>
print_station_id: number
}
export function savePrinters(data: PrintSavePrintersParams) {
const sid = Number(data.print_station_id)
if (!Number.isFinite(sid)) {
return Promise.reject(new Error('print.savePrinters:print_station_id 无效'))
}
return request({
url: '/system',
method: 'post',
params: {
method: 'print.savePrinters',
v,
timestamp: stamp()
},
data: {
defaultPrinter: data.defaultPrinter,
express: Array.isArray(data.express) ? data.express : [],
print_station_id: sid
}
})
}
export function sendPrint(data: PrintSendParams) {
let body: Record<string, unknown>
if (data.type === 'express') {
body = {
type: 'express',
printStationId: data.printStationId,
requestCode: data.requestCode,
data: data.data
}
} else {
body = {
type: data.type,
computerId: data.computerId,
data: data.data,
printer: data.printer,
template: data.template
}
if (data.count !== undefined && data.count !== null) {
body.count = data.count
}
if (data.timestamp !== undefined && data.timestamp !== null) {
body.timestamp = typeof data.timestamp === 'string' ? data.timestamp : String(data.timestamp)
}
}
return request({
url: '/system',
method: 'post',
params: {
method: 'print.send',
v,
timestamp: stamp()
},
data: body
})
}