2026-04-19 18:27:27 +08:00

536 lines
15 KiB
Vue
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.

<template>
<div class="printer-management-container">
<ScanBox
ref="scanBox"
width="300px"
:preset-tree="presetTree"
:available-fields="availableFields"
@condition-change="onScanConditionChange"
@condition-clear="onScanConditionClear"
/>
<div class="main-content">
<div class="toolbar">
<div class="toolbar-left">
<el-button type="primary" size="small" :icon="Refresh" title="刷新" :loading="loading" @click="handleRefresh">
刷新
</el-button>
<el-button type="primary" size="small" :icon="Plus" title="添加打印台" @click="handleAdd">
添加
</el-button>
<el-button
type="danger"
size="small"
:icon="Delete"
title="批量删除"
:disabled="!hasSelection"
@click="confirmBatchDelete"
>
批量删除
</el-button>
</div>
</div>
<div class="upper-wrapper">
<upper-table
ref="accountTableRef"
title="打印台列表"
:data="list"
:columns="tableColumns"
:table-size="tableSize"
:pagination="pagination"
:show-index="true"
:show-actions="true"
:show-selection="showSelection"
:loading="loading"
:query-time="queryTime"
@current-change="handleTableRowSelect"
@row-click="handleTableRowSelect"
@page-change="handlePageChange"
@page-size-change="handlePageSizeChange"
@refresh="handleRefresh"
@selection-change="selectionChange"
>
<template #column-computer_id="{ row }">
<span>{{ computerLabel(row.computer_id) }}</span>
</template>
<template #column-components="{ row }">
<div class="component-tags" v-if="row.components?.length">
<span
v-for="(c, idx) in row.components"
:key="idx"
:class="['comp-pill', componentOnline(c) ? 'comp-pill--online' : 'comp-pill--offline']"
>
<span class="comp-pill__name">{{ c.name }}</span>
<span class="comp-pill__state">{{ componentOnline(c) ? '在线' : '离线' }}</span>
</span>
</div>
<span v-else class="text-muted">—</span>
</template>
<template #column-create_at="{ row }">
{{ formatTime(row.create_at) }}
</template>
<template #column-update_at="{ row }">
{{ formatTime(row.update_at) }}
</template>
<template #empty>
<div class="custom-empty">
<el-icon><Printer /></el-icon>
<p>暂无打印台数据</p>
</div>
</template>
</upper-table>
</div>
<div v-if="currentStationId" class="lower-level" :class="tableSize">
<div class="detail-header">
<el-tabs v-model="activeTab" class="detail-tab">
<el-tab-pane label="属性" name="info" />
</el-tabs>
</div>
<div class="detail-container">
<KeepAlive>
<component
:is="activeComponent"
:key="String(currentStationId ?? '')"
:id="currentStationId"
:name="currentStationName"
:table-row="currentTableRow"
@refresh="handleRefresh"
@updated="handleItemUpdated"
/>
</KeepAlive>
</div>
</div>
<div v-else class="no-selection">
<div class="empty-tip">
<p>请从上方列表中选择一项查看详情</p>
</div>
</div>
</div>
<el-drawer v-model="drawer" :with-header="false" direction="rtl" size="800px">
<Add v-if="drawer" @saved="handleSaved" />
</el-drawer>
</div>
</template>
<script setup lang="ts">
import { ref, computed, onMounted, markRaw } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import ScanBox from '@/components/ScanBox/index.vue'
import UpperTable from '@/components/UpperTable/index.vue'
import Info from './components/info.vue'
import Add from './components/add.vue'
import * as printApi from '@/api/print/print'
import { Delete, Plus, Refresh, Printer } from '@element-plus/icons-vue'
const okCode = (c: unknown) => c === 0 || c === 200 || c === 1
const scanBox = ref<any>(null)
const presetTree = ref<
Array<{
label: string
id: string | number
custom_barcode?: string
is_online?: number
children?: Array<{ label: string; is_online: number }>
}>
>([])
/** 与 print.getList 的 where 一致(print 模块 module.json) */
const availableFields = ref([
{ label: '打印台 ID', value: 'id', type: 'number' },
{ label: '打印台名称', value: 'name', type: 'text' },
{ label: '打印电脑 ID', value: 'computer_id', type: 'text' },
{ label: '自定义条码', value: 'custom_barcode', type: 'text' }
])
const activeTab = ref('info')
const currentStationId = ref<string | number | null>(null)
const currentStationName = ref('')
const currentTableRow = ref<Record<string, any> | undefined>(undefined)
const activeComponent = computed(() => {
if (activeTab.value === 'info') return markRaw(Info)
return null
})
const handleTableRowSelect = (row: any) => {
if (!row) return
const rid = row.id
if (rid == null || rid === '') return
currentStationId.value = rid
currentStationName.value = row.name ?? ''
currentTableRow.value = row
}
const handlePageChange = (page: number) => {
pagination.value.currentPage = page
fetchPrintStationList()
}
const handlePageSizeChange = (size: number) => {
pagination.value.pageSize = size
fetchPrintStationList()
}
const selectedRows = ref<any[]>([])
const selectionChange = (selection: any[]) => {
hasSelection.value = selection.length > 0
selectedRows.value = selection
}
const tableColumns = ref([
{ prop: 'name', label: '打印台名称', fixed: 'left' as const, minWidth: '140', showOverflowTooltip: true, sortable: true },
{ prop: 'id', label: 'ID', minWidth: '72', showOverflowTooltip: true, sortable: true },
{ prop: 'computer_id', label: '打印电脑', minWidth: '160', showOverflowTooltip: true, sortable: true },
{ prop: 'custom_barcode', label: '自定义条码', minWidth: '120', showOverflowTooltip: true },
{ prop: 'default_printer_name', label: '默认打印机', minWidth: '130', showOverflowTooltip: true },
{ prop: 'components', label: '打印组件', minWidth: '280', showOverflowTooltip: true },
{ prop: 'creator_name', label: '创建人', minWidth: '100', showOverflowTooltip: true },
{ prop: 'create_at', label: '创建时间', minWidth: '168', showOverflowTooltip: true },
{ prop: 'update_at', label: '更新时间', minWidth: '168', showOverflowTooltip: true }
])
const drawer = ref(false)
const handleAdd = () => {
drawer.value = true
}
const handleSaved = () => {
drawer.value = false
fetchPrintStationList()
}
const list = ref<any[]>([])
const handleItemUpdated = (updated: any) => {
const idx = list.value.findIndex((i: any) => String(i.id) === String(updated.id))
if (idx !== -1) {
list.value[idx] = { ...list.value[idx], ...updated }
if (currentStationId.value != null && String(currentStationId.value) === String(updated.id)) {
currentTableRow.value = list.value[idx]
}
}
const treeItem: any = presetTree.value.find((i: any) => String(i.id) === String(updated.id))
if (treeItem) {
Object.assign(treeItem, { label: updated.name || treeItem.label, ...updated })
}
}
const tableSize = ref('default')
const pagination = ref({ currentPage: 1, pageSize: 10, total: 0 })
const loading = ref(false)
const queryTime = ref(0)
const hasSelection = ref(false)
const showSelection = ref(true)
const computerMap = ref<Map<string, string>>(new Map())
const PRINT_WHERE_KEYS = ['id', 'name', 'computer_id', 'custom_barcode'] as const
function buildWhereFromScan(raw: Record<string, any>): Record<string, unknown> {
const out: Record<string, unknown> = {}
for (const key of PRINT_WHERE_KEYS) {
const c = raw[key]
if (c === undefined || c === null || c === '') continue
if (c && typeof c === 'object' && 'operator' in c) {
out[key] = c
continue
}
if (key === 'id') {
const n = Number(c)
if (Number.isFinite(n)) out[key] = { operator: 'eq', value: n, type: 'number' }
continue
}
if (key === 'computer_id') {
out[key] = { operator: 'eq', value: String(c).trim(), type: 'text' }
continue
}
if (key === 'name' && typeof c === 'string') {
out[key] = { operator: 'like', value: c.trim(), type: 'text' }
continue
}
out[key] = { operator: 'eq', value: c, type: 'text' }
}
return out
}
function onScanConditionChange() {
pagination.value.currentPage = 1
fetchPrintStationList()
}
function onScanConditionClear() {
pagination.value.currentPage = 1
fetchPrintStationList()
}
function syncCurrentTableRowFromList() {
const sid = currentStationId.value
if (sid == null || sid === '') return
const row = list.value.find((r: any) => String(r?.id) === String(sid))
if (row) currentTableRow.value = row
}
async function loadComputersForLabels() {
try {
const res: any = await printApi.getComputer()
if (!res || !okCode(res.code)) return
const arr = res.data || []
const m = new Map<string, string>()
for (const it of arr) {
if (it && typeof it === 'object' && it.id != null) {
m.set(String(it.id), String(it.name ?? it.id))
}
}
computerMap.value = m
} catch (e) {
console.error(e)
}
}
function computerLabel(computerId: unknown): string {
if (computerId === undefined || computerId === null || computerId === '') return '—'
const key = String(computerId)
const name = computerMap.value.get(key)
return name ? `${name}(${key})` : key
}
function formatTime(v: unknown): string {
if (v == null || v === '') return '—'
const s = String(v)
return s.length >= 19 ? s.slice(0, 19).replace('T', ' ') : s
}
/** 接口 is_online:1 为在线,其余为离线(与 data.json 一致) */
function componentOnline(c: { is_online?: number }): boolean {
return Number(c?.is_online) === 1
}
/** print.getList:分页 + where(id / name / computer_id / custom_barcode) */
const fetchPrintStationList = async () => {
loading.value = true
const start = Date.now()
try {
const scanRaw = scanBox.value?.getConditions?.() || {}
const where = buildWhereFromScan(scanRaw)
const payload = {
page: pagination.value.currentPage || 1,
pageSize: pagination.value.pageSize || 10,
...(Object.keys(where).length ? { where } : {})
}
const response: any = await printApi.getList(payload)
if (response && okCode(response.code)) {
const data = response.data || {}
const rawList: any[] = data.list || []
list.value = rawList
syncCurrentTableRowFromList()
presetTree.value = rawList.map((item: any) => ({
label: item.name,
id: item.id,
custom_barcode: item.custom_barcode,
is_online: item.is_online,
children: Array.isArray(item.components)
? item.components.map((comp: { name: string; is_online: number }) => ({
label: comp.name,
is_online: comp.is_online
}))
: []
}))
pagination.value.total =
data.pagination?.total ?? data.total ?? data.count ?? (Array.isArray(rawList) ? rawList.length : 0)
} else {
ElMessage.error((response && (response.msg || response.message)) || '加载打印台列表失败')
}
} catch (error) {
console.error(error)
ElMessage.error('加载打印台列表失败')
} finally {
queryTime.value = Date.now() - start
loading.value = false
}
}
const handleRefresh = () => {
fetchPrintStationList()
}
const confirmBatchDelete = async () => {
if (!selectedRows.value?.length) {
ElMessage.warning('请先选择要删除的打印台')
return
}
try {
await ElMessageBox.confirm('确认删除选中打印台?', '删除确认', { type: 'warning' })
const ids = selectedRows.value.map((r: any) => r.id).filter((x: any) => x != null && x !== '')
const res: any = await printApi.delItems(ids)
if (res && okCode(res.code)) {
ElMessage.success('删除成功')
await fetchPrintStationList()
const deleted = new Set(ids.map((x: any) => String(x)))
if (currentStationId.value != null && deleted.has(String(currentStationId.value))) {
currentStationId.value = null
currentTableRow.value = undefined
}
selectedRows.value = []
hasSelection.value = false
} else {
ElMessage.error((res && (res.msg || res.message)) || '删除失败')
}
} catch (e: any) {
if (e && e !== 'cancel') {
console.error(e)
ElMessage.error('删除失败')
}
}
}
onMounted(async () => {
await loadComputersForLabels()
await fetchPrintStationList()
})
</script>
<style lang="scss" scoped>
.printer-management-container {
display: flex;
height: calc(100vh - 130px);
width: 100%;
background-color: #f0f2f5;
.main-content {
flex: 1;
display: flex;
flex-direction: column;
padding: 10px;
overflow: hidden;
.toolbar {
padding: 10px;
border-bottom: 1px solid #ebeef5;
display: flex;
justify-content: space-between;
align-items: center;
background-color: #fff;
}
.upper-wrapper {
flex: 1;
min-height: 0;
overflow: hidden;
}
.upper-wrapper :deep(.upper-table-container) {
height: 100%;
display: flex;
flex-direction: column;
}
.upper-wrapper :deep(.upper-table-container) :deep(.table-container) {
height: auto !important;
flex: 1 1 auto;
min-height: 0;
overflow: hidden;
}
.upper-wrapper :deep(.el-table__body-wrapper) {
overflow: auto;
min-height: 0;
}
.component-tags {
display: flex;
flex-wrap: wrap;
gap: 6px;
align-items: center;
}
.comp-pill {
display: inline-flex;
align-items: center;
gap: 4px;
padding: 2px 8px;
border-radius: 4px;
font-size: 12px;
line-height: 1.4;
white-space: nowrap;
}
.comp-pill__name {
font-weight: 500;
}
.comp-pill__state {
font-size: 11px;
opacity: 0.95;
}
.comp-pill--online {
color: #0d5c1f;
background-color: #e8f5e9;
border: 1px solid #81c784;
}
.comp-pill--offline {
color: #b71c1c;
background-color: #ffebee;
border: 1px solid #e57373;
}
.text-muted {
color: #909399;
font-size: 13px;
}
.lower-level {
background-color: #fff;
margin-top: 10px;
flex: 0 0 300px;
display: flex;
flex-direction: column;
overflow: hidden;
&.small {
flex: 0 0 250px;
}
.detail-header {
padding: 0 20px;
border-bottom: 1px solid #ebeef5;
}
.detail-container {
flex: 1;
overflow-y: auto;
min-height: 0;
}
}
.no-selection {
margin-top: 10px;
background-color: #fff;
flex: 0 0 300px;
display: flex;
justify-content: center;
align-items: center;
color: #909399;
}
.custom-empty {
padding: 24px;
text-align: center;
color: #909399;
.el-icon {
font-size: 40px;
margin-bottom: 8px;
}
}
}
}
</style>