WEB/src/api/print/print.ts
2026-04-18 20:52:27 +08:00

218 lines
4.9 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";
export function getList(data:any){
return request({
url: '/system',
method: 'post',
params: {
method: 'print.getList',
v,
timestamp: new Date().getTime()
},
data: data
})
}
export function getItem(id: string | number) {
const numId = Number(id)
return request({
url: '/system',
method: 'post',
params: {
method: 'print.getItem',
v,
timestamp: new Date().getTime()
},
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: new Date().getTime()
},
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: new Date().getTime()
},
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: new Date().getTime()
},
data: body
})
}
function normalizeDelIds(ids: unknown[]): number[] {
return ids
.map((i) => Number(i))
.filter((n) => Number.isFinite(n))
}
// 删除单个打印台(兼容传入 id 或 { ids: [...] })
export function delItem(idOrPayload: any) {
let payload: any = idOrPayload
if (typeof idOrPayload === 'string' || typeof idOrPayload === 'number') {
payload = { ids: normalizeDelIds([idOrPayload]) }
} else if (Array.isArray(idOrPayload)) {
payload = { ids: normalizeDelIds(idOrPayload) }
} else if (idOrPayload && Array.isArray(idOrPayload.ids)) {
payload = { ids: normalizeDelIds(idOrPayload.ids) }
}
return request({
url: '/system',
method: 'post',
params: {
method: 'print.del',
v,
timestamp: new Date().getTime()
},
data: payload
})
}
// 批量删除打印台,接受 id 数组(与后端约定为数字 id)
export function delItems(ids: Array<string | number>) {
const payload = { ids: normalizeDelIds(ids) }
return request({
url: '/system',
method: 'post',
params: {
method: 'print.del',
v,
timestamp: new Date().getTime()
},
data: payload
})
}
export function getComputer() {
return request({
url: '/system',
method: 'post',
params: {
method: 'print.getComputer',
v,
timestamp: new Date().getTime()
},
data: {}
})
}
export function getExpressList(data?: Record<string, unknown>) {
return request({
url: '/system',
method: 'post',
params: {
method: 'index.getExpressList',
v,
timestamp: new Date().getTime()
},
data: {
systemOnly: true,
...(data || {})
},
})
}
export function getComputerPrinter(id: string | number) {
const numId = Number(id)
return request({
url: '/system',
method: 'post',
params: {
method: 'print.getComputerPrinter',
v,
timestamp: new Date().getTime()
},
data: Number.isFinite(numId) ? { id: numId } : { id }
})
}
// 保存打印机与快递绑定关系(字段名与接口 print.savePrinters 一致)
export function savePrinters(data: {
defaultPrinter: string
express?: Array<Record<string, unknown>>
print_station_id?: number
}) {
const body: Record<string, unknown> = {
defaultPrinter: data.defaultPrinter,
express: data.express ?? []
}
if (data.print_station_id !== undefined && data.print_station_id !== null) {
const n = Number(data.print_station_id)
if (Number.isFinite(n)) body.print_station_id = n
}
return request({
url: '/system',
method: 'post',
params: {
method: 'print.savePrinters',
v,
timestamp: new Date().getTime()
},
data: body
})
}
// 发送打印任务
export function sendPrint(data: any) {
return request({
url: '/system',
method: 'post',
params: {
method: 'print.send',
v,
timestamp: new Date().getTime()
},
data: data
})
}