1018 lines
32 KiB
Vue
1018 lines
32 KiB
Vue
<template>
|
||
<div class="print-station-info">
|
||
<div class="print-station-info__main">
|
||
<el-form
|
||
:model="form"
|
||
ref="formRef"
|
||
label-width="128px"
|
||
label-position="right"
|
||
:disabled="loading"
|
||
class="info-form"
|
||
>
|
||
<section class="info-section">
|
||
<h3 class="info-section__title">基础信息</h3>
|
||
<div class="info-section__grid">
|
||
<el-form-item label="打印台名称" prop="name">
|
||
<el-input v-model="form.name" placeholder="打印台名称" clearable />
|
||
</el-form-item>
|
||
<el-form-item label="自定义条码" prop="custom_barcode">
|
||
<el-input v-model="form.custom_barcode" placeholder="可选" clearable />
|
||
</el-form-item>
|
||
<el-form-item label="打印电脑" prop="computer_id" class="info-form__item-span2">
|
||
<el-select
|
||
v-model="form.computer_id"
|
||
placeholder="请选择打印电脑"
|
||
clearable
|
||
filterable
|
||
class="info-form__control-wide"
|
||
@visible-change="onComputerVisibleChange"
|
||
>
|
||
<el-option v-for="item in computerOptions" :key="item.value" :label="item.label" :value="item.value" />
|
||
</el-select>
|
||
</el-form-item>
|
||
</div>
|
||
</section>
|
||
|
||
<section class="info-section">
|
||
<h3 class="info-section__title">打印机与快递</h3>
|
||
<el-form-item label="默认打印机" prop="printer_id">
|
||
<el-select
|
||
v-model="form.printer_id"
|
||
placeholder="请先选择电脑,再选本机打印机"
|
||
clearable
|
||
filterable
|
||
class="info-form__control-wide"
|
||
@visible-change="onPrinterVisibleChange"
|
||
>
|
||
<el-option v-for="item in printerOptions" :key="item.value" :label="item.label" :value="item.value" />
|
||
</el-select>
|
||
<!-- <div class="field-desc">未在下方单独配置的快递,将使用此处默认打印机出单。</div> -->
|
||
</el-form-item>
|
||
|
||
<el-form-item label="快递绑定" class="info-form__item-block">
|
||
<div class="array-editor">
|
||
<div class="array-table">
|
||
<div class="array-table__head">
|
||
<div class="array-table__cell array-table__cell--express">快递公司</div>
|
||
<div class="array-table__cell array-table__cell--printer">使用打印机</div>
|
||
<div class="array-table__op">操作</div>
|
||
</div>
|
||
<div
|
||
v-for="(bindRow, bindIdx) in expressBindRows"
|
||
:key="`express-bind-${bindIdx}`"
|
||
class="array-table__row"
|
||
>
|
||
<div class="array-table__cell array-table__cell--express">
|
||
<el-select
|
||
v-model="bindRow.expressId"
|
||
filterable
|
||
clearable
|
||
placeholder="请选择快递"
|
||
@visible-change="onExpressVisibleChange"
|
||
>
|
||
<el-option
|
||
v-for="item in expressOptions"
|
||
:key="`ex-${bindIdx}-${item.value}`"
|
||
:label="item.label"
|
||
:value="item.value"
|
||
/>
|
||
</el-select>
|
||
</div>
|
||
<div class="array-table__cell array-table__cell--printer">
|
||
<el-select
|
||
v-model="bindRow.printerName"
|
||
filterable
|
||
clearable
|
||
placeholder="同默认打印机"
|
||
:disabled="!form.computer_id"
|
||
>
|
||
<el-option
|
||
v-for="item in printerOptions"
|
||
:key="`pr-${bindIdx}-${item.value}`"
|
||
:label="item.label"
|
||
:value="item.value"
|
||
/>
|
||
</el-select>
|
||
</div>
|
||
<div class="array-table__op">
|
||
<el-button type="danger" plain size="small" @click="removeExpressBindRow(bindIdx)">删除</el-button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<el-button type="primary" plain size="small" @click="addExpressBindRow">新增一项</el-button>
|
||
<!-- <div class="field-desc">
|
||
多行配置:每行指定快递使用的本机打印机;「使用打印机」留空则该行使用上方默认打印机。
|
||
</div> -->
|
||
</div>
|
||
</el-form-item>
|
||
</section>
|
||
|
||
<!-- <section class="info-section">
|
||
<h3 class="info-section__title">高级</h3>
|
||
<el-form-item label="主题配置" prop="subject_config_text" class="info-form__item-block">
|
||
<el-input
|
||
v-model="form.subject_config_text"
|
||
type="textarea"
|
||
:rows="4"
|
||
class="info-form__control-wide"
|
||
placeholder='可选 JSON 对象;留空则不修改服务端已有 subject_config'
|
||
spellcheck="false"
|
||
/>
|
||
</el-form-item>
|
||
</section> -->
|
||
</el-form>
|
||
|
||
<div class="info-form-actions">
|
||
<el-button type="primary" @click="handleSave" :loading="saveLoading">保存</el-button>
|
||
</div>
|
||
</div>
|
||
|
||
<aside class="print-station-info__side">
|
||
<div class="right-panel">
|
||
<div class="panel-item">
|
||
<div class="panel-label">仓库 ID</div>
|
||
<div class="panel-value">{{ data.warehouse_id ?? '—' }}</div>
|
||
</div>
|
||
<div class="panel-item" v-if="data.creator_name">
|
||
<div class="panel-label">创建人</div>
|
||
<div class="panel-value">{{ data.creator_name }}</div>
|
||
</div>
|
||
</div>
|
||
</aside>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, watch, nextTick } from 'vue'
|
||
import { ElMessage } from 'element-plus'
|
||
import * as stationApi from '@/api/print/print'
|
||
import { getExpressList } from '@/api/index/index'
|
||
|
||
const okCode = (c: unknown) => c === 0 || c === 200 || c === 1
|
||
|
||
// 从父组件接收的外部属性
|
||
const props = defineProps({
|
||
id: {
|
||
type: [String, Number],
|
||
required: true
|
||
},
|
||
name: {
|
||
type: String,
|
||
default: ''
|
||
},
|
||
/** 列表当前行,用于与 print.getItem 字段/嵌套结构不一致时立即对齐表单展示 */
|
||
tableRow: {
|
||
type: Object,
|
||
default: undefined
|
||
},
|
||
computer_id: {
|
||
type: [String, Number],
|
||
required: false
|
||
},
|
||
custom_barcode: {
|
||
type: String,
|
||
required: false
|
||
},
|
||
default_printer_name: {
|
||
type: String,
|
||
required: false
|
||
},
|
||
express: {
|
||
type:Array,
|
||
required: false
|
||
},
|
||
})
|
||
|
||
// 声明组件可以向父组件派发的自定义事件
|
||
const emit = defineEmits(['refresh', 'updated'])
|
||
|
||
// 保存从后端获取到的数据
|
||
const data = ref<any>({})
|
||
|
||
const loading = ref(false)
|
||
|
||
// 用于编辑的表单;只有部分字段可以编辑
|
||
const form = ref({
|
||
name: '',
|
||
custom_barcode: '',
|
||
default_printer_name: '',
|
||
computer_id: '',
|
||
printer_id: '',
|
||
/** print.updateItem / print.add 的 subject_config,编辑为 JSON 文本 */
|
||
subject_config_text: ''
|
||
})
|
||
|
||
/** 快递 → 打印机绑定行(与 basicManagement 中 array-table 编辑方式一致) */
|
||
type ExpressBindRow = { expressId: string; printerName: string }
|
||
const expressBindRows = ref<ExpressBindRow[]>([])
|
||
const saveLoading = ref(false)
|
||
|
||
/** 避免 getData 与「切换行」并发时旧请求覆盖新表单 */
|
||
let loadDetailSeq = 0
|
||
/** 从接口拉取并回填表单期间,跳过 computer_id 的 watch,防止误清空打印机/竞态 */
|
||
const hydratingFromApi = ref(false)
|
||
|
||
const STATION_LEAF_KEYS = [
|
||
'name',
|
||
'station_name',
|
||
'print_station_name',
|
||
'computer_id',
|
||
'computerId',
|
||
'custom_barcode',
|
||
'customBarcode',
|
||
'default_printer_name',
|
||
'defaultPrinterName'
|
||
] as const
|
||
|
||
function unwrapPrintStationRecord(raw: unknown, depth = 0): Record<string, any> {
|
||
if (raw == null || typeof raw !== 'object' || depth > 8) return {}
|
||
const o = raw as Record<string, any>
|
||
if (STATION_LEAF_KEYS.some((k) => k in o)) {
|
||
return o
|
||
}
|
||
const inner =
|
||
o.record ??
|
||
o.item ??
|
||
o.detail ??
|
||
o.printStation ??
|
||
o.print_station ??
|
||
o.station ??
|
||
(typeof o.data === 'object' && o.data !== null ? o.data : null)
|
||
if (inner && typeof inner === 'object') {
|
||
return unwrapPrintStationRecord(inner, depth + 1)
|
||
}
|
||
return o
|
||
}
|
||
|
||
/** 将列表行或接口记录上的别名统一到表单使用的字段语义 */
|
||
function normalizeStationRecord(src: Record<string, any>): Record<string, any> {
|
||
if (!src || typeof src !== 'object') return {}
|
||
const name =
|
||
src.name ?? src.station_name ?? src.print_station_name ?? src.printName ?? src.stationName ?? ''
|
||
const custom_barcode =
|
||
src.custom_barcode ?? src.customBarcode ?? src.barcode ?? ''
|
||
const computer_id = src.computer_id ?? src.computerId ?? src.print_computer_id ?? ''
|
||
const default_printer_name =
|
||
src.default_printer_name ?? src.defaultPrinterName ?? src.printer_name ?? src.default_printer ?? ''
|
||
return {
|
||
...src,
|
||
name,
|
||
custom_barcode,
|
||
computer_id,
|
||
default_printer_name
|
||
}
|
||
}
|
||
|
||
function rowMatchesPropsId(row: Record<string, any> | null | undefined, id: string | number) {
|
||
if (!row || typeof row !== 'object') return false
|
||
const rid = row.id ?? row.print_station_id ?? row.station_id ?? row.print_id ?? row.printId
|
||
if (rid == null || rid === '') return false
|
||
return String(rid) === String(id)
|
||
}
|
||
|
||
function seedFormFromTableRow(row: Record<string, any>) {
|
||
const n = normalizeStationRecord(row)
|
||
form.value.name = n.name != null ? String(n.name) : ''
|
||
form.value.custom_barcode =
|
||
n.custom_barcode != null && String(n.custom_barcode) !== '' ? String(n.custom_barcode) : ''
|
||
const rawCid = n.computer_id
|
||
form.value.computer_id =
|
||
rawCid !== undefined && rawCid !== null && String(rawCid).trim() !== ''
|
||
? String(rawCid).trim()
|
||
: ''
|
||
const listDef = String(n.default_printer_name ?? '').trim()
|
||
if (listDef) {
|
||
form.value.default_printer_name = listDef
|
||
}
|
||
if (form.value.computer_id) {
|
||
ensureComputerOption(form.value.computer_id)
|
||
}
|
||
}
|
||
|
||
function ensureComputerOption(id: string | number | null | undefined) {
|
||
if (id === null || id === undefined || id === '') return
|
||
const v = String(id)
|
||
if (computerOptions.value.some((o) => o.value === v)) return
|
||
computerOptions.value = [...computerOptions.value, { label: `电脑 ${v}`, value: v }]
|
||
}
|
||
|
||
// 选项列表
|
||
const computerOptions = ref<Array<{ label: string; value: string }>>([])
|
||
const onComputerVisibleChange = (visible: boolean) => {
|
||
if (visible) getComputers()
|
||
}
|
||
const getComputers = async () => {
|
||
try {
|
||
const res: any = await stationApi.getComputer()
|
||
if (res && okCode(res.code)) {
|
||
const list = Array.isArray(res.data) ? res.data : []
|
||
computerOptions.value = list.map((it: any) => {
|
||
if (typeof it === 'string') return { label: it, value: it }
|
||
if (it.id !== undefined && it.name !== undefined) return { label: String(it.name), value: String(it.id) }
|
||
if (it.value !== undefined && it.label !== undefined) return { label: String(it.label), value: String(it.value) }
|
||
if (it.name !== undefined) return { label: String(it.name), value: String(it.id ?? it.name) }
|
||
return { label: JSON.stringify(it), value: JSON.stringify(it) }
|
||
})
|
||
}
|
||
} catch (e) {
|
||
console.error('Failed to fetch computers', e)
|
||
}
|
||
}
|
||
|
||
const expressOptions = ref<Array<{ label: string; value: string }>>([])
|
||
|
||
/** index.getExpressList 常见返回:data 为数组,或 { list | records };项上多为 express_id/id、express_code/code、express_name/name */
|
||
function unwrapExpressListPayload(data: unknown): any[] {
|
||
if (Array.isArray(data)) return data
|
||
if (data && typeof data === 'object') {
|
||
const o = data as Record<string, unknown>
|
||
if (Array.isArray(o.list)) return o.list
|
||
if (Array.isArray(o.records)) return o.records
|
||
if (Array.isArray(o.items)) return o.items
|
||
}
|
||
return []
|
||
}
|
||
|
||
function mapExpressRowToSelectOption(it: unknown): { label: string; value: string } | null {
|
||
if (it == null) return null
|
||
if (typeof it === 'string') {
|
||
const s = it.trim()
|
||
return s ? { label: s, value: s } : null
|
||
}
|
||
if (typeof it !== 'object') {
|
||
const s = String(it).trim()
|
||
return s ? { label: s, value: s } : null
|
||
}
|
||
const row = it as Record<string, unknown>
|
||
if (row.value !== undefined && row.label !== undefined) {
|
||
const v = String(row.value ?? '').trim()
|
||
const lab = String(row.label ?? '').trim()
|
||
if (v) return { label: lab || v, value: v }
|
||
}
|
||
const idRaw = row.express_id ?? row.id ?? row.expressId
|
||
const codeRaw = row.code ?? row.express_code ?? row.logistics_code
|
||
const valueRaw =
|
||
idRaw !== undefined && idRaw !== null && String(idRaw).trim() !== ''
|
||
? idRaw
|
||
: codeRaw !== undefined && codeRaw !== null && String(codeRaw).trim() !== ''
|
||
? codeRaw
|
||
: null
|
||
if (valueRaw === null) return null
|
||
const value = String(valueRaw).trim()
|
||
if (!value) return null
|
||
|
||
const nameRaw = row.name ?? row.express_name ?? row.logistics_name ?? row.title
|
||
const name = nameRaw != null && String(nameRaw).trim() !== '' ? String(nameRaw).trim() : ''
|
||
const codeStr =
|
||
codeRaw !== undefined && codeRaw !== null && String(codeRaw).trim() !== '' ? String(codeRaw).trim() : ''
|
||
let label = ''
|
||
if (codeStr && name) label = `${codeStr} - ${name}`
|
||
else if (name) label = name
|
||
else if (codeStr) label = codeStr
|
||
else label = value
|
||
|
||
return { label, value }
|
||
}
|
||
|
||
const loadExpressOptions = async () => {
|
||
try {
|
||
const res: any = await getExpressList({ systemOnly: true })
|
||
if (res && okCode(res.code)) {
|
||
const list = unwrapExpressListPayload(res.data)
|
||
const mapped = list.map(mapExpressRowToSelectOption).filter(Boolean) as Array<{ label: string; value: string }>
|
||
expressOptions.value = mapped
|
||
}
|
||
} catch (e) {
|
||
console.error('Failed to fetch express companies', e)
|
||
}
|
||
}
|
||
|
||
const onExpressVisibleChange = (visible: boolean) => {
|
||
if (visible) void loadExpressOptions()
|
||
}
|
||
|
||
function addExpressBindRow() {
|
||
expressBindRows.value.push({ expressId: '', printerName: '' })
|
||
}
|
||
|
||
function removeExpressBindRow(idx: number) {
|
||
expressBindRows.value.splice(idx, 1)
|
||
}
|
||
|
||
/** 从详情/列表记录中取快递绑定数组:优先 express_bind_printer,其次 subject_config 内,最后兼容 express */
|
||
function pickExpressBindSourceArray(record: Record<string, any> | null | undefined): any[] {
|
||
if (!record || typeof record !== 'object') return []
|
||
const top = record.express_bind_printer
|
||
if (Array.isArray(top) && top.length) return top
|
||
const sc = record.subject_config
|
||
if (sc && typeof sc === 'object' && !Array.isArray(sc)) {
|
||
const nested = (sc as Record<string, unknown>).express_bind_printer
|
||
if (Array.isArray(nested) && nested.length) return nested as any[]
|
||
}
|
||
const legacy = record.express
|
||
if (Array.isArray(legacy) && legacy.length) return legacy
|
||
return []
|
||
}
|
||
|
||
function mapExpressBindItemToRow(it: any): ExpressBindRow {
|
||
const expressId = String(
|
||
it?.express_id ?? it?.expressId ?? it?.express_code ?? it?.code ?? it?.id ?? ''
|
||
).trim()
|
||
const printerName = String(
|
||
it?.printer_name ?? it?.printerName ?? it?.name ?? it?.bind_printer ?? it?.default_printer_name ?? ''
|
||
).trim()
|
||
return { expressId, printerName }
|
||
}
|
||
|
||
function syncExpressRowsFromRecord(record: Record<string, any>) {
|
||
const arr = pickExpressBindSourceArray(record)
|
||
if (!arr.length) {
|
||
expressBindRows.value = []
|
||
return
|
||
}
|
||
expressBindRows.value = arr.map(mapExpressBindItemToRow).filter((r) => r.expressId !== '')
|
||
}
|
||
|
||
function snapshotExpressBindForList(): Array<{ express_code: string; printer_name: string }> {
|
||
const def = String(form.value.printer_id ?? '').trim()
|
||
return expressBindRows.value
|
||
.filter((r) => String(r.expressId ?? '').trim() !== '')
|
||
.map((r) => ({
|
||
express_code: String(r.expressId).trim(),
|
||
printer_name: String(r.printerName ?? '').trim() || def
|
||
}))
|
||
}
|
||
|
||
function hasStoredExpressBind(d: Record<string, any> | null | undefined): boolean {
|
||
if (!d || typeof d !== 'object') return false
|
||
const check = (arr: unknown) =>
|
||
Array.isArray(arr) && arr.some((x: any) => String(x?.express_id ?? x?.express_code ?? x?.id ?? '').trim() !== '')
|
||
if (check(d.express_bind_printer)) return true
|
||
const sc = d.subject_config
|
||
if (sc && typeof sc === 'object' && !Array.isArray(sc)) {
|
||
if (check((sc as Record<string, unknown>).express_bind_printer)) return true
|
||
}
|
||
if (check(d.express)) return true
|
||
return false
|
||
}
|
||
|
||
const printerOptions = ref<Array<{ label: string; value: string }>>([])
|
||
const onPrinterVisibleChange = (visible: boolean) => {
|
||
if (visible) getComputerPrinter(form.value.computer_id)
|
||
}
|
||
const getComputerPrinter = async (id: any) => {
|
||
const targetId = id ?? form.value.computer_id
|
||
if (!targetId) {
|
||
printerOptions.value = []
|
||
return
|
||
}
|
||
try {
|
||
const res: any = await stationApi.getComputerPrinter(targetId)
|
||
if (res && okCode(res.code)) {
|
||
const raw = res.data
|
||
const list = Array.isArray(raw) ? raw : []
|
||
printerOptions.value = list.map((it: any) => {
|
||
if (typeof it === 'string') return { label: it, value: it }
|
||
if (it.id !== undefined && it.name !== undefined) return { label: it.name, value: String(it.id) }
|
||
// If API returns objects with a `name` but no `id`, use `name` for label and value
|
||
if (it.name !== undefined) return { label: it.name, value: String(it.name) }
|
||
if (it.value !== undefined && it.label !== undefined) return { label: it.label, value: it.value }
|
||
try {
|
||
return { label: JSON.stringify(it), value: JSON.stringify(it) }
|
||
} catch (e) {
|
||
return { label: String(it), value: String(it) }
|
||
}
|
||
})
|
||
}
|
||
} catch (e) {
|
||
console.error('Failed to fetch computer printers', e)
|
||
}
|
||
}
|
||
|
||
/** 将 print.getList / getItem 的 default_printer_name 对齐到 el-select 的 value(与 printerOptions 一致) */
|
||
function matchPrinterSelectValue(raw: string, options: Array<{ label: string; value: string }>): string {
|
||
const want = String(raw ?? '').trim()
|
||
if (!want || !options.length) return ''
|
||
if (options.some((o) => o.value === want)) return want
|
||
const byLabel = options.find((o) => String(o.label).trim() === want)
|
||
if (byLabel) return byLabel.value
|
||
const byValueLoose = options.find((o) => String(o.value).trim() === want)
|
||
if (byValueLoose) return byValueLoose.value
|
||
return ''
|
||
}
|
||
|
||
function ensurePrinterOptionForDefault(rawName: string) {
|
||
const want = String(rawName ?? '').trim()
|
||
if (!want) return
|
||
if (printerOptions.value.some((o) => o.value === want || String(o.label).trim() === want)) return
|
||
printerOptions.value = [...printerOptions.value, { label: want, value: want }]
|
||
}
|
||
|
||
/**
|
||
* 默认打印机:优先 print.getItem 的 default_printer_name;为空则回退 print.getList 当前行(tableRow)
|
||
*/
|
||
function applyDefaultPrinterFromSources(record: Record<string, any>) {
|
||
const recordDef = String(record?.default_printer_name ?? '').trim()
|
||
let listDef = ''
|
||
const tr = props.tableRow as Record<string, any> | null | undefined
|
||
if (tr && rowMatchesPropsId(tr, props.id)) {
|
||
listDef = String(normalizeStationRecord(tr).default_printer_name ?? '').trim()
|
||
}
|
||
const source = recordDef || listDef || String(form.value.default_printer_name ?? '').trim()
|
||
if (!source) {
|
||
form.value.printer_id = ''
|
||
return
|
||
}
|
||
const matched = matchPrinterSelectValue(source, printerOptions.value)
|
||
if (matched) {
|
||
form.value.printer_id = matched
|
||
return
|
||
}
|
||
ensurePrinterOptionForDefault(source)
|
||
form.value.printer_id = matchPrinterSelectValue(source, printerOptions.value) || source
|
||
}
|
||
|
||
|
||
|
||
// api接口
|
||
const getData = async () => {
|
||
if (props.id === null || props.id === undefined || props.id === '') return
|
||
const seq = ++loadDetailSeq
|
||
loading.value = true
|
||
hydratingFromApi.value = true
|
||
try {
|
||
const tr = props.tableRow as Record<string, any> | null | undefined
|
||
if (tr && rowMatchesPropsId(tr, props.id)) {
|
||
seedFormFromTableRow(tr)
|
||
}
|
||
|
||
await getComputers()
|
||
if (seq !== loadDetailSeq) return
|
||
|
||
const response: any = await stationApi.getItem(props.id)
|
||
if (seq !== loadDetailSeq) return
|
||
|
||
if (!okCode(response?.code)) return
|
||
|
||
const record = normalizeStationRecord(unwrapPrintStationRecord(response.data))
|
||
data.value = record
|
||
|
||
// 仅在接口返回了非空值时才覆盖,避免把表格行已种子的字段冲掉为空
|
||
if (record.name != null && String(record.name) !== '') {
|
||
form.value.name = String(record.name)
|
||
}
|
||
if (record.custom_barcode != null && String(record.custom_barcode) !== '') {
|
||
form.value.custom_barcode = String(record.custom_barcode)
|
||
}
|
||
if (record.default_printer_name != null && String(record.default_printer_name) !== '') {
|
||
form.value.default_printer_name = String(record.default_printer_name)
|
||
}
|
||
|
||
const sc = record.subject_config
|
||
if (sc && typeof sc === 'object' && !Array.isArray(sc)) {
|
||
try {
|
||
form.value.subject_config_text = JSON.stringify(sc, null, 2)
|
||
} catch {
|
||
form.value.subject_config_text = ''
|
||
}
|
||
} else {
|
||
form.value.subject_config_text = ''
|
||
}
|
||
|
||
const rawCid = record.computer_id ?? record.computerId
|
||
if (rawCid !== undefined && rawCid !== null && String(rawCid).trim() !== '') {
|
||
form.value.computer_id = String(rawCid).trim()
|
||
}
|
||
|
||
ensureComputerOption(form.value.computer_id || rawCid)
|
||
|
||
if (form.value.computer_id) {
|
||
await getComputerPrinter(form.value.computer_id)
|
||
if (seq !== loadDetailSeq) return
|
||
applyDefaultPrinterFromSources(record)
|
||
} else {
|
||
printerOptions.value = []
|
||
form.value.printer_id = ''
|
||
}
|
||
|
||
syncExpressRowsFromRecord(record)
|
||
if (!expressBindRows.value.length) {
|
||
const tr = props.tableRow as Record<string, any> | null | undefined
|
||
if (tr && rowMatchesPropsId(tr, props.id)) {
|
||
syncExpressRowsFromRecord(normalizeStationRecord(tr))
|
||
}
|
||
}
|
||
void loadExpressOptions()
|
||
} catch (error) {
|
||
console.error('Failed to fetch item:', error)
|
||
} finally {
|
||
if (seq === loadDetailSeq) {
|
||
loading.value = false
|
||
await nextTick()
|
||
hydratingFromApi.value = false
|
||
}
|
||
}
|
||
}
|
||
|
||
const hasName = async (name: string, id?: string | number) => {
|
||
if (!name) return false
|
||
try {
|
||
const payload: { name: string; id?: string | number } = { name: name.trim() }
|
||
if (id !== undefined && id !== null && id !== '') payload.id = id
|
||
const res: any = await stationApi.hasName(payload)
|
||
const raw = res?.data
|
||
const exists =
|
||
raw === true ||
|
||
raw === 1 ||
|
||
raw?.exists === true ||
|
||
(typeof raw === 'object' && raw !== null && raw.exists === true) ||
|
||
res?.exists === true
|
||
return okCode(res?.code) && Boolean(exists)
|
||
} catch (e) {
|
||
console.error('Failed to check name uniqueness', e)
|
||
return false
|
||
}
|
||
}
|
||
|
||
/** print.savePrinters.express:每项 express_code + printer_name(模块首选字段;服务端兼容 express_id / name) */
|
||
const buildExpressPayload = (): Array<Record<string, unknown>> => {
|
||
const def = String(form.value.printer_id ?? '').trim()
|
||
const out: Array<Record<string, unknown>> = []
|
||
for (const row of expressBindRows.value) {
|
||
const express_code = String(row.expressId ?? '').trim()
|
||
if (!express_code) continue
|
||
const pn = String(row.printerName ?? '').trim()
|
||
const printer_name = pn || def || ''
|
||
out.push({ express_code, printer_name })
|
||
}
|
||
return out
|
||
}
|
||
|
||
const persistPrinterBindings = async () => {
|
||
const sid = Number(props.id)
|
||
if (!Number.isFinite(sid)) return false
|
||
const defaultPrinter = String(form.value.printer_id ?? '').trim()
|
||
if (!defaultPrinter) {
|
||
ElMessage.warning('保存打印机与快递绑定须先选择默认打印机(print.savePrinters 要求 defaultPrinter)')
|
||
return false
|
||
}
|
||
const expressPayload = buildExpressPayload()
|
||
try {
|
||
const res: any = await stationApi.savePrinters({
|
||
defaultPrinter,
|
||
print_station_id: sid,
|
||
express: expressPayload
|
||
})
|
||
if (!okCode(res?.code)) {
|
||
ElMessage.error((res && (res.msg || res.message)) || '打印机与快递绑定保存失败')
|
||
return false
|
||
}
|
||
if (!data.value) data.value = {}
|
||
data.value.default_printer_name = defaultPrinter
|
||
data.value.express = expressPayload
|
||
const bindSnap = snapshotExpressBindForList()
|
||
data.value.express_bind_printer = bindSnap
|
||
const sc = data.value.subject_config
|
||
if (sc && typeof sc === 'object' && !Array.isArray(sc)) {
|
||
data.value.subject_config = { ...(sc as Record<string, unknown>), express_bind_printer: bindSnap }
|
||
}
|
||
emit('updated', {
|
||
id: props.id,
|
||
default_printer_name: defaultPrinter,
|
||
express: expressPayload,
|
||
express_bind_printer: bindSnap,
|
||
subject_config: data.value.subject_config
|
||
})
|
||
return true
|
||
} catch (e) {
|
||
console.error('savePrinters error', e)
|
||
ElMessage.error('打印机与快递绑定保存失败')
|
||
return false
|
||
}
|
||
}
|
||
|
||
const handleSave = async () => {
|
||
if (!props.id) return
|
||
if (form.value.name?.trim()) {
|
||
const nameExists = await hasName(form.value.name, props.id)
|
||
if (nameExists) {
|
||
ElMessage.error('打印台名称已存在')
|
||
return
|
||
}
|
||
}
|
||
|
||
const expressNow = expressBindRows.value.some((r) => String(r.expressId ?? '').trim() !== '')
|
||
const printerNow = String(form.value.printer_id ?? '').trim()
|
||
if (expressNow && !printerNow) {
|
||
ElMessage.warning('已配置快递绑定时,请先选择默认打印机')
|
||
return
|
||
}
|
||
for (const row of expressBindRows.value) {
|
||
const eid = String(row.expressId ?? '').trim()
|
||
if (!eid) continue
|
||
const pn = String(row.printerName ?? '').trim()
|
||
if (!pn && !printerNow) {
|
||
ElMessage.warning('请为已选快递指定「使用打印机」,或先选择默认打印机')
|
||
return
|
||
}
|
||
}
|
||
|
||
let subject_config: Record<string, unknown> | undefined
|
||
const scRaw = String(form.value.subject_config_text ?? '').trim()
|
||
if (scRaw) {
|
||
try {
|
||
const parsed = JSON.parse(scRaw) as unknown
|
||
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
|
||
ElMessage.warning('主题配置须为 JSON 对象')
|
||
return
|
||
}
|
||
subject_config = parsed as Record<string, unknown>
|
||
} catch {
|
||
ElMessage.warning('主题配置 JSON 格式无效')
|
||
return
|
||
}
|
||
}
|
||
|
||
saveLoading.value = true
|
||
try {
|
||
const payload: Record<string, unknown> = {
|
||
id: props.id,
|
||
name: form.value.name?.trim() ?? '',
|
||
custom_barcode: form.value.custom_barcode ?? '',
|
||
default_printer_name: printerNow
|
||
}
|
||
if (form.value.computer_id) {
|
||
payload.computer_id = Number(form.value.computer_id)
|
||
}
|
||
if (subject_config !== undefined) {
|
||
payload.subject_config = subject_config
|
||
}
|
||
|
||
const res: any = await stationApi.updateItem(payload)
|
||
if (!okCode(res?.code)) {
|
||
ElMessage.error(res?.message || res?.msg || '保存失败')
|
||
return
|
||
}
|
||
|
||
const hadPrinterBefore = String(data.value?.default_printer_name ?? '').trim() !== ''
|
||
const hadExpressBefore = hasStoredExpressBind(data.value)
|
||
|
||
Object.assign(data.value, {
|
||
name: form.value.name,
|
||
custom_barcode: form.value.custom_barcode,
|
||
computer_id: form.value.computer_id ? Number(form.value.computer_id) : data.value.computer_id,
|
||
default_printer_name: printerNow,
|
||
...(subject_config !== undefined ? { subject_config } : {})
|
||
})
|
||
emit('updated', {
|
||
id: props.id,
|
||
name: form.value.name,
|
||
custom_barcode: form.value.custom_barcode,
|
||
computer_id: form.value.computer_id,
|
||
default_printer_name: printerNow
|
||
})
|
||
|
||
const shouldSaveBind =
|
||
printerNow !== '' || expressNow || hadPrinterBefore || hadExpressBefore
|
||
|
||
if (!shouldSaveBind) {
|
||
ElMessage.success('保存成功')
|
||
return
|
||
}
|
||
|
||
if (!printerNow) {
|
||
ElMessage.success('保存成功(未同步打印机绑定:请先选择默认打印机)')
|
||
return
|
||
}
|
||
|
||
const bindOk = await persistPrinterBindings()
|
||
if (bindOk) {
|
||
ElMessage.success('保存成功')
|
||
} else {
|
||
ElMessage.warning('打印台信息已保存,默认打印机或快递绑定未保存成功,请检查后重试')
|
||
}
|
||
} catch (e) {
|
||
console.error(e)
|
||
ElMessage.error('保存失败')
|
||
} finally {
|
||
saveLoading.value = false
|
||
}
|
||
}
|
||
|
||
|
||
// 只监听 id:避免 tableRow 引用因列表刷新/保存合并而变时重复 getData,导致 seed 或竞态把表单清空
|
||
watch(
|
||
() => props.id,
|
||
(id) => {
|
||
if (id === null || id === undefined || id === '') return
|
||
getData()
|
||
},
|
||
{ immediate: true }
|
||
)
|
||
|
||
// 用户手动切换电脑时重载本机打印机列表;回填表单期间由 hydratingFromApi 跳过
|
||
watch(
|
||
() => form.value.computer_id,
|
||
(newId) => {
|
||
if (hydratingFromApi.value) return
|
||
if (newId) {
|
||
getComputerPrinter(newId)
|
||
if (data.value && String(data.value.computer_id ?? data.value.computerId ?? '') !== String(newId)) {
|
||
form.value.printer_id = ''
|
||
expressBindRows.value = []
|
||
}
|
||
} else {
|
||
printerOptions.value = []
|
||
form.value.printer_id = ''
|
||
expressBindRows.value = []
|
||
}
|
||
},
|
||
{ flush: 'post' }
|
||
)
|
||
</script>
|
||
|
||
<style scoped>
|
||
.print-station-info {
|
||
display: flex;
|
||
flex-wrap: wrap;
|
||
align-items: flex-start;
|
||
gap: 24px 32px;
|
||
padding: 16px 20px 20px;
|
||
background: #fff;
|
||
min-height: 0;
|
||
}
|
||
|
||
.print-station-info__main {
|
||
flex: 1 1 400px;
|
||
min-width: 0;
|
||
}
|
||
|
||
.print-station-info__side {
|
||
flex: 0 0 220px;
|
||
width: 220px;
|
||
}
|
||
|
||
.info-form {
|
||
max-width: 720px;
|
||
}
|
||
|
||
.info-form-actions {
|
||
display: flex;
|
||
justify-content: flex-start;
|
||
margin-top: 20px;
|
||
padding-top: 16px;
|
||
border-top: 1px solid #ebeef5;
|
||
}
|
||
|
||
.info-section {
|
||
margin-bottom: 22px;
|
||
}
|
||
|
||
.info-section:last-of-type {
|
||
margin-bottom: 0;
|
||
}
|
||
|
||
.info-section__title {
|
||
margin: 0 0 12px;
|
||
padding-bottom: 8px;
|
||
font-size: 14px;
|
||
font-weight: 600;
|
||
color: #303133;
|
||
border-bottom: 1px solid #ebeef5;
|
||
}
|
||
|
||
.info-section__grid {
|
||
display: grid;
|
||
grid-template-columns: repeat(2, minmax(0, 1fr));
|
||
gap: 4px 20px;
|
||
align-items: start;
|
||
}
|
||
|
||
@media (max-width: 640px) {
|
||
.info-section__grid {
|
||
grid-template-columns: 1fr;
|
||
}
|
||
}
|
||
|
||
.info-form__item-span2 {
|
||
grid-column: 1 / -1;
|
||
}
|
||
|
||
.info-form__item-block :deep(.el-form-item__content) {
|
||
display: block;
|
||
max-width: 100%;
|
||
}
|
||
|
||
.info-form__control-wide {
|
||
width: 100%;
|
||
max-width: 520px;
|
||
}
|
||
|
||
.info-form .el-form-item {
|
||
margin-bottom: 14px;
|
||
}
|
||
|
||
.field-desc {
|
||
margin-top: 6px;
|
||
font-size: 12px;
|
||
color: #909399;
|
||
line-height: 1.5;
|
||
max-width: 640px;
|
||
}
|
||
|
||
.array-editor {
|
||
display: flex;
|
||
flex-direction: column;
|
||
gap: 10px;
|
||
max-width: 640px;
|
||
}
|
||
|
||
.array-table {
|
||
border: 1px solid #e4e7ed;
|
||
border-radius: 6px;
|
||
overflow: hidden;
|
||
background: #fafbfc;
|
||
}
|
||
|
||
.array-table__head,
|
||
.array-table__row {
|
||
display: flex;
|
||
flex-wrap: nowrap;
|
||
border-bottom: 1px solid #ebeef5;
|
||
min-width: max-content;
|
||
background: #fff;
|
||
}
|
||
|
||
.array-table__head {
|
||
background: #f5f7fa;
|
||
font-size: 12px;
|
||
color: #606266;
|
||
font-weight: 600;
|
||
}
|
||
|
||
.array-table__row:last-child {
|
||
border-bottom: none;
|
||
}
|
||
|
||
.array-table__cell,
|
||
.array-table__op {
|
||
padding: 10px 12px;
|
||
border-right: 1px solid #ebeef5;
|
||
box-sizing: border-box;
|
||
}
|
||
|
||
.array-table__cell--express {
|
||
flex: 0 0 200px;
|
||
min-width: 200px;
|
||
}
|
||
|
||
.array-table__cell--printer {
|
||
flex: 0 0 220px;
|
||
min-width: 220px;
|
||
}
|
||
|
||
.array-table__cell:last-child,
|
||
.array-table__op:last-child {
|
||
border-right: none;
|
||
}
|
||
|
||
.array-table__op {
|
||
flex: 0 0 84px;
|
||
min-width: 84px;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
}
|
||
|
||
.array-editor :deep(.el-select) {
|
||
width: 100%;
|
||
}
|
||
|
||
.right-panel {
|
||
position: sticky;
|
||
top: 12px;
|
||
padding: 16px;
|
||
border: 1px solid #ebeef5;
|
||
border-radius: 8px;
|
||
background: #fafafa;
|
||
box-shadow: 0 1px 2px rgba(0, 0, 0, 0.04);
|
||
}
|
||
|
||
.panel-item {
|
||
margin-bottom: 14px;
|
||
}
|
||
|
||
.panel-label {
|
||
color: #909399;
|
||
font-size: 12px;
|
||
margin-bottom: 4px;
|
||
}
|
||
|
||
.panel-value {
|
||
font-size: 14px;
|
||
font-weight: 500;
|
||
color: #303133;
|
||
word-break: break-all;
|
||
}
|
||
</style>
|