1246 lines
41 KiB
Vue
1246 lines
41 KiB
Vue
<template>
|
||
<div class="storage-receiving-container">
|
||
<scan-box
|
||
ref="scanBox"
|
||
width="320px"
|
||
:tree-props="{ children: 'children', label: 'label' }"
|
||
:auto-submit="false"
|
||
:preset-tree="presetTree"
|
||
:available-fields="availableFieldsRef"
|
||
@condition-change="handleConditionChange"
|
||
@reset="handleConditionReset"
|
||
@condition-clear="handleConditionClear"
|
||
@submit="handleSubmit"
|
||
/>
|
||
|
||
<div class="main-content">
|
||
<div class="toolbar">
|
||
<div class="toolbar-left">
|
||
<el-button size="small" type="primary" @click="loadData">刷新</el-button>
|
||
<el-button size="small" @click="handleCreateReceiving">新建收货单</el-button>
|
||
<el-button type="success" size="small" icon="el-icon-delete" @click="confirmcomplete" title="入库单入库"
|
||
:disabled="!hasSelection" class="toolbar-btn">
|
||
入库单入库
|
||
</el-button>
|
||
<el-button type="primary" size="small" icon="el-icon-delete" @click="confirmcomplete" title="一键入库"
|
||
:disabled="!hasSelection" class="toolbar-btn">
|
||
一键入库
|
||
</el-button>
|
||
<el-button size="small" :disabled="!hasSelection" @click="confirmBatchCancel">批量作废</el-button>
|
||
<el-button type="info" size="small" icon="el-icon-printer" @click="handleOpenPrintDialog" title="打印收货单"
|
||
:disabled="!hasSelection" class="toolbar-btn">
|
||
打印收货单
|
||
</el-button>
|
||
<el-button size="small" :loading="exportLoading" @click="handleExport">导出</el-button>
|
||
</div>
|
||
|
||
<!-- <div class="toolbar-right">
|
||
<el-input size="small" placeholder="搜索收货单号" v-model="quickSearch" style="width:220px" @keyup.enter.native="onQuickSearch" />
|
||
</div> -->
|
||
</div>
|
||
|
||
<div class="upper-wrapper">
|
||
<upper-table
|
||
ref="upperTable"
|
||
title="收货单列表"
|
||
:data="list"
|
||
:columns="columns"
|
||
:table-size="tableSize"
|
||
:pagination="pagination"
|
||
:show-index="true"
|
||
:show-actions="true"
|
||
:show-selection="true"
|
||
:loading="loading"
|
||
:queryTime="queryTime"
|
||
@current-change="handleRowClick"
|
||
@page-change="handlePageChange"
|
||
@page-size-change="handlePageSizeChange"
|
||
@sort-change="handleSortChange"
|
||
@refresh="loadData"
|
||
@selection-change="onSelectionChange"
|
||
>
|
||
<template #column-status="{ row }">
|
||
<el-tag v-if="row.status" :type="statusConfig[row.status as keyof typeof statusConfig]?.tagType || 'info'" size="small">
|
||
{{ statusConfig[row.status as keyof typeof statusConfig]?.text || row.status }}
|
||
</el-tag>
|
||
</template>
|
||
|
||
<template #column-detail_status="{ row }">
|
||
<el-tag v-if="row.detail_status" :type="detailStatusConfig[row.detail_status as keyof typeof detailStatusConfig]?.tagType || 'info'" size="small">
|
||
{{ detailStatusConfig[row.detail_status as keyof typeof detailStatusConfig]?.text || row.detail_status }}
|
||
</el-tag>
|
||
</template>
|
||
|
||
<template #column-storage_status="{ row }">
|
||
<el-tag v-if="row.storage_status" :type="storageStatusConfig[row.storage_status as keyof typeof storageStatusConfig]?.tagType || 'info'" size="small">
|
||
{{ storageStatusConfig[row.storage_status as keyof typeof storageStatusConfig]?.text || row.storage_status }}
|
||
</el-tag>
|
||
</template>
|
||
|
||
<template #column-delivery_method="{ row }">
|
||
<el-tag v-if="row.delivery_method" :type="deliveryMethodConfig[row.delivery_method as keyof typeof deliveryMethodConfig]?.tagType || 'info'" size="small">
|
||
{{ deliveryMethodConfig[row.delivery_method as keyof typeof deliveryMethodConfig]?.text || row.delivery_method }}
|
||
</el-tag>
|
||
</template>
|
||
|
||
<template #column-loading_method="{ row }">
|
||
<el-tag v-if="row.loading_method" :type="loadingMethodConfig[row.loading_method as keyof typeof loadingMethodConfig]?.tagType || 'info'" size="small">
|
||
{{ loadingMethodConfig[row.loading_method as keyof typeof loadingMethodConfig]?.text || row.loading_method }}
|
||
</el-tag>
|
||
</template>
|
||
|
||
<template #empty>
|
||
<div class="empty-tip">暂无收货单</div>
|
||
</template>
|
||
</upper-table>
|
||
</div>
|
||
|
||
<div class="lower-level" v-if="selectedReceivingId" :class="tableSize">
|
||
<div class="detail-header">
|
||
<el-tabs v-model="activeTab" class="detail-tab">
|
||
<el-tab-pane label="收货明细" name="details" />
|
||
<el-tab-pane label="商品明细" name="product" />
|
||
<el-tab-pane label="入库记录" name="storage" />
|
||
<el-tab-pane label="操作日志" name="logs" />
|
||
</el-tabs>
|
||
</div>
|
||
|
||
<div class="detail-container">
|
||
<KeepAlive>
|
||
<component
|
||
:is="activeComponent"
|
||
:id="selectedReceivingId"
|
||
:row="selectedRow"
|
||
:product-data="productData"
|
||
:product-total="productTotal"
|
||
:loading="productLoading"
|
||
@refresh-product="loadProductData"
|
||
@page-change="handleProductPageChange"
|
||
@page-size-change="handleProductSizeChange"
|
||
/>
|
||
</KeepAlive>
|
||
</div>
|
||
</div>
|
||
|
||
<div class="no-selection" v-else>
|
||
<div class="empty-tip">
|
||
<p>请从上方列表中选择一项查看详情</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 打印对话框 -->
|
||
<el-dialog
|
||
v-model="printDialogVisible"
|
||
title="批量打印收货单"
|
||
width="1135px"
|
||
custom-class="print-dialog"
|
||
>
|
||
<div class="print-dialog-layout">
|
||
<!-- 左侧待打印列表 -->
|
||
<div class="print-batch-list">
|
||
<div class="list-title">打印队列 ({{ selectionRows.length }})</div>
|
||
<div
|
||
v-for="(row, index) in selectionRows"
|
||
:key="row.id"
|
||
class="batch-item"
|
||
:class="{ active: currentPrintIndex === index }"
|
||
@click="switchPrintItem(index)"
|
||
>
|
||
<span class="item-index">{{ index + 1 }}</span>
|
||
<span class="item-number">{{ row.receiving_number }}</span>
|
||
<el-tag size="small" :type="printStatusMap[row.id] === 'done' ? 'success' : printStatusMap[row.id] === 'error' ? 'danger' : 'info'">
|
||
{{ printStatusMap[row.id] === 'done' ? '已打印' : printStatusMap[row.id] === 'error' ? '失败' : '待打印' }}
|
||
</el-tag>
|
||
</div>
|
||
</div>
|
||
|
||
<!-- 右侧:打印设置 + 模板预览 -->
|
||
<div class="print-preview-main">
|
||
<!-- 打印设置工具栏 -->
|
||
<div class="print-settings-bar">
|
||
<el-select v-model="printSettings.station" placeholder="请选择打印台" size="small" @change="handleStationChange">
|
||
<el-option v-for="item in stationOptions" :key="item.id" :label="item.name" :value="item.id" />
|
||
</el-select>
|
||
<el-select v-model="printSettings.printer" placeholder="请选择打印机" size="small" :loading="printerLoading">
|
||
<el-option v-for="item in printerOptions" :key="item.value" :label="item.label" :value="item.value" />
|
||
</el-select>
|
||
<el-select v-model="printSettings.template" placeholder="请选择打印模板" size="small">
|
||
<el-option v-for="item in templateOptions" :key="item.id" :label="item.name" :value="item.id" />
|
||
</el-select>
|
||
</div>
|
||
|
||
<!-- 模板画布预览 -->
|
||
<div class="print-canvas-area">
|
||
<div v-if="previewInfo.width && previewInfo.height"
|
||
class="preview-page"
|
||
:style="{ width: previewInfo.width + 'mm', height: previewInfo.height + 'mm', position: 'relative' }"
|
||
>
|
||
<div
|
||
v-for="(item, idx) in previewObjects"
|
||
:key="item.id"
|
||
:style="computePreviewItemStyle(item, idx)"
|
||
>
|
||
<template v-if="item.type === 'text'">
|
||
<span>{{ getPreviewValue(item) }}</span>
|
||
</template>
|
||
<template v-else-if="item.type === 'barcode'">
|
||
<barcode-img :text="getPreviewValue(item) || '0123456789'" />
|
||
</template>
|
||
<template v-else-if="item.type === 'qrcode'">
|
||
<img
|
||
:src="'https://ye4s.wms.gztbyc.com/qrcode/build?text=' + encodeURIComponent(getPreviewValue(item) || '')"
|
||
alt=""
|
||
style="width: 100%; height: 100%; object-fit: contain;"
|
||
/>
|
||
</template>
|
||
<template v-else-if="item.type === 'image'">
|
||
<img :src="item.content" alt="" style="width: 100%; height: 100%; object-fit: fill;" />
|
||
</template>
|
||
<template v-else-if="item.type === 'table'">
|
||
<table style="border: 1px solid #000; border-collapse: collapse;" border="1" cellspacing="0" cellpadding="0" width="100%" height="auto">
|
||
<thead>
|
||
<tr>
|
||
<td v-for="c in item.rows" :key="c.id" :style="{ width: c.width + 'mm', fontSize: '3mm', textAlign: 'center', border: '1px solid #000', height: (item.headerHeight || 4) + 'mm' }">{{ c.title }}</td>
|
||
</tr>
|
||
</thead>
|
||
<tbody>
|
||
<!-- 如果有 details 数据则遍历显示,否则显示一行占位 -->
|
||
<tr v-for="(detailItem, dIdx) in (printForm.details && printForm.details.length ? printForm.details : [printForm])" :key="dIdx">
|
||
<td v-for="c in item.rows" :key="c.id + '_v'" :style="{ fontSize: '3mm', textAlign: 'center', border: '1px solid #000', height: (item.rowHeight || 4) + 'mm' }">
|
||
{{ detailItem[c.field_code] || '' }}
|
||
</td>
|
||
</tr>
|
||
</tbody>
|
||
</table>
|
||
</template>
|
||
<template v-else-if="item.type === 'shape'">
|
||
<div style="width: 100%; height: 100%;"></div>
|
||
</template>
|
||
</div>
|
||
</div>
|
||
<div v-else class="preview-empty">
|
||
<el-empty description="请在上方选择打印模板" :image-size="60" />
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<template #footer>
|
||
<div class="dialog-footer">
|
||
<span class="progress-tip">进度: {{ completedPrintCount }} / {{ selectionRows.length }}</span>
|
||
<div>
|
||
<el-button @click="printDialogVisible = false">关闭</el-button>
|
||
<el-button type="primary" :loading="printing" @click="doPrint">
|
||
{{ currentPrintIndex < selectionRows.length - 1 ? '打印并切换下一个' : '打印当前单据' }}
|
||
</el-button>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
</el-dialog>
|
||
|
||
<!-- 新建收货单对话框 -->
|
||
<el-dialog
|
||
v-model="createDialogVisible"
|
||
title="新建收货单"
|
||
width="600px"
|
||
align-center
|
||
@closed="handleCreateDialogClosed"
|
||
>
|
||
<el-form :model="createForm" label-width="100px" size="small">
|
||
<el-row :gutter="20">
|
||
<el-col v-for="field in addReceivingFieldsRef" :key="field.prop" :span="field.type === 'textarea' ? 24 : 12">
|
||
<el-form-item :label="field.label">
|
||
<el-select v-if="field.type === 'select'" v-model="createForm[field.prop]" :placeholder="'请选择' + field.label" style="width: 100%" :filterable="field.filterable">
|
||
<el-option v-for="opt in field.options" :key="opt.value" :label="opt.label" :value="opt.value" />
|
||
</el-select>
|
||
<el-input v-else-if="field.type === 'textarea'" v-model="createForm[field.prop]" type="textarea" :rows="3" :placeholder="'请输入' + field.label" />
|
||
<el-input v-else v-model="createForm[field.prop]" :placeholder="'请输入' + field.label" />
|
||
</el-form-item>
|
||
</el-col>
|
||
</el-row>
|
||
</el-form>
|
||
<template #footer>
|
||
<el-button size="small" @click="createDialogVisible = false">取消</el-button>
|
||
<el-button size="small" type="primary" :loading="createLoading" @click="submitCreateReceiving">确定</el-button>
|
||
</template>
|
||
</el-dialog>
|
||
</div>
|
||
</template>
|
||
|
||
<script setup lang="ts">
|
||
import { ref, reactive, onMounted, computed, watch, h } from 'vue'
|
||
import { ElMessage, ElMessageBox } from 'element-plus'
|
||
import ScanBox from '@/components/ScanBox/index.vue'
|
||
import UpperTable from '@/components/UpperTable/index.vue'
|
||
import Details from './component/DetailsPanel.vue'
|
||
import StorageComp from './component/Storage.vue'
|
||
import Logs from './component/Logs.vue'
|
||
import Product from './component/Product.vue'
|
||
import * as indexApi from '@/api/index'
|
||
import * as receivingApi from '@/api/storage/receiving'
|
||
import {
|
||
receivingColumns,
|
||
availableFields,
|
||
formatReceivingData,
|
||
statusConfig,
|
||
detailStatusConfig,
|
||
storageStatusConfig,
|
||
deliveryMethodConfig,
|
||
loadingMethodConfig,
|
||
printFormConfig,
|
||
addReceivingFields
|
||
} from './receiving-config'
|
||
import * as printerApi from '@/api/printer'
|
||
import * as templateApi from '@/api/printTemplate'
|
||
import * as stationApi from '@/api/print/station'
|
||
import JsBarcode from 'jsbarcode'
|
||
import * as XLSX from 'xlsx'
|
||
|
||
const scanBox = ref<any>(null)
|
||
const upperTable = ref<any>(null)
|
||
|
||
const presetTree = ref<any[]>([])
|
||
const availableFieldsRef = reactive(JSON.parse(JSON.stringify(availableFields)))
|
||
const addReceivingFieldsRef = reactive(JSON.parse(JSON.stringify(addReceivingFields)))
|
||
|
||
const activeTab = ref('details')
|
||
|
||
const list = ref<any[]>([])
|
||
const loading = ref(false)
|
||
const exportLoading = ref(false)
|
||
const queryTime = ref(0)
|
||
|
||
const pagination = reactive({ currentPage: 1, pageSize: 20, total: 0 })
|
||
const tableSize = ref('small')
|
||
const columns = ref(receivingColumns)
|
||
|
||
const selectionRows = ref<any[]>([])
|
||
const hasSelection = ref(false)
|
||
const selectedReceivingId = ref<number | string | null>(null)
|
||
|
||
const quickSearch = ref('')
|
||
const currentSort = reactive({ prop: '', order: '' })
|
||
const currentCondition = ref<any>(null)
|
||
|
||
const componentMap: Record<string, any> = {
|
||
details: Details,
|
||
storage: StorageComp,
|
||
logs: Logs,
|
||
product: Product
|
||
}
|
||
|
||
const activeComponent = computed(() => componentMap[activeTab.value] || componentMap.details)
|
||
|
||
const selectedRow = computed(() => {
|
||
return list.value.find((i: any) => i.id === selectedReceivingId.value) || null
|
||
})
|
||
|
||
onMounted(() => {
|
||
loadData()
|
||
loadFilterData()
|
||
})
|
||
|
||
async function loadData() {
|
||
if (loading.value) return
|
||
loading.value = true
|
||
const start = Date.now()
|
||
try {
|
||
// 直接使用 handleConditionChange 保存的完整条件(含 operator),避免丢失操作符信息
|
||
const where = currentCondition.value || {}
|
||
console.log('当前筛选条件:', where)
|
||
|
||
const params: any = {
|
||
pagination: { page: pagination.currentPage, pageSize: pagination.pageSize },
|
||
where,
|
||
order: currentSort.prop ? { [currentSort.prop]: currentSort.order === 'ascending' ? 'ASC' : 'DESC' } : undefined
|
||
}
|
||
|
||
if (quickSearch.value) {
|
||
params.where = { ...(params.where || {}), receiving_number: quickSearch.value }
|
||
}
|
||
|
||
const res: any = await receivingApi.getReceivingList(params)
|
||
if (res && res.code === 200) {
|
||
console.log('API 返回数据:', res.data.list)
|
||
list.value = formatReceivingData(res.data.list || [])
|
||
pagination.currentPage = res.data.pagination?.currentPage || pagination.currentPage
|
||
pagination.pageSize = res.data.pagination?.pageSize || pagination.pageSize
|
||
pagination.total = res.data.pagination?.total || (res.data.pagination?.total || 0)
|
||
} else {
|
||
ElMessage.error(res?.message || '加载失败')
|
||
}
|
||
} catch (e) {
|
||
console.error(e)
|
||
ElMessage.error('加载收货单失败')
|
||
} finally {
|
||
loading.value = false
|
||
queryTime.value = Date.now() - start
|
||
}
|
||
}
|
||
|
||
async function loadFilterData() {
|
||
try {
|
||
const [storesRes, presetRes]: any = await Promise.all([indexApi.loadStores({ type: 'receiving' }), indexApi.fastWhereTree('receiving')])
|
||
if (storesRes && storesRes.code === 200) {
|
||
const stores = storesRes.stores || []
|
||
const f = availableFieldsRef.find((i: any) => i.value === 'store_id')
|
||
if (f) f.options = stores
|
||
|
||
const af = addReceivingFieldsRef.find((i: any) => i.prop === 'store_id')
|
||
if (af) af.options = stores
|
||
}
|
||
if (presetRes && presetRes.code === 200) {
|
||
console.log('筛选配置', presetRes)
|
||
presetTree.value = presetRes.presetTree || []
|
||
}
|
||
} catch (e) {
|
||
console.error('加载筛选配置失败', e)
|
||
}
|
||
}
|
||
|
||
function handleConditionChange(condition: any) {
|
||
currentCondition.value = condition // 保存完整条件(含 operator),供 loadData 使用
|
||
pagination.currentPage = 1
|
||
loadData()
|
||
}
|
||
|
||
function handleSortChange({ prop, order }: any) {
|
||
currentSort.prop = prop
|
||
currentSort.order = order
|
||
pagination.currentPage = 1
|
||
loadData()
|
||
}
|
||
|
||
function handleSubmit() {
|
||
// condition-change 在 submit 之前已触发并更新了 currentCondition,直接加载即可
|
||
pagination.currentPage = 1
|
||
loadData()
|
||
}
|
||
|
||
const handleConditionClear = () => {
|
||
currentCondition.value = null // 清空已保存的条件
|
||
pagination.currentPage = 1
|
||
loadData()
|
||
}
|
||
|
||
function handleConditionReset() {
|
||
currentCondition.value = null // 清空已保存的条件
|
||
pagination.currentPage = 1
|
||
// 如果 ScanBox 提供 reset/clear 方法,调用以清空筛选条件
|
||
if (scanBox.value?.reset) {
|
||
scanBox.value.reset()
|
||
} else if (scanBox.value?.clear) {
|
||
scanBox.value.clear()
|
||
}
|
||
loadData()
|
||
}
|
||
|
||
function onSelectionChange(rows: any[]) {
|
||
selectionRows.value = rows || []
|
||
hasSelection.value = selectionRows.value.length > 0
|
||
}
|
||
|
||
const productData = ref<any[]>([])
|
||
const productLoading = ref(false)
|
||
const productTotal = ref(0)
|
||
const productPagination = reactive({ currentPage: 1, pageSize: 20 })
|
||
|
||
async function handleRowClick(row: any) {
|
||
selectedReceivingId.value = row?.id || null
|
||
if (!selectedReceivingId.value) {
|
||
productData.value = []
|
||
productTotal.value = 0
|
||
return
|
||
}
|
||
// 重置分页
|
||
productPagination.currentPage = 1
|
||
loadProductData()
|
||
}
|
||
|
||
async function loadProductData() {
|
||
productLoading.value = true
|
||
const start = Date.now()
|
||
try {
|
||
const params = {
|
||
id: selectedReceivingId.value,
|
||
pagination: {
|
||
page: productPagination.currentPage,
|
||
pageSize: productPagination.pageSize
|
||
}
|
||
}
|
||
const res: any = await receivingApi.getReceivingDetail(params)
|
||
if (res && res.code === 200 && res.data) {
|
||
// 这里的路径需要匹配实际返回的数据结构 res.data.receiving.details
|
||
const receivingData = res.data.receiving || {}
|
||
if (Array.isArray(receivingData.details)) {
|
||
productData.value = receivingData.details
|
||
productTotal.value = receivingData.pagination?.total || receivingData.details.length || 0
|
||
} else {
|
||
// 兼容处理
|
||
productData.value = []
|
||
productTotal.value = 0
|
||
}
|
||
} else {
|
||
productData.value = []
|
||
productTotal.value = 0
|
||
}
|
||
} catch (e) {
|
||
console.error('获取收货单详情失败', e)
|
||
productData.value = []
|
||
productTotal.value = 0
|
||
} finally {
|
||
productLoading.value = false
|
||
// queryTime.value = Date.now() - start // 这里queryTime是给主表的,子表可能需要单独传
|
||
}
|
||
}
|
||
|
||
function handleProductPageChange(page: number) {
|
||
productPagination.currentPage = page
|
||
loadProductData()
|
||
}
|
||
|
||
function handleProductSizeChange(size: number) {
|
||
productPagination.pageSize = size
|
||
productPagination.currentPage = 1
|
||
loadProductData()
|
||
}
|
||
|
||
function handlePageChange(page: number) {
|
||
pagination.currentPage = page
|
||
loadData()
|
||
}
|
||
|
||
function handlePageSizeChange(size: number) {
|
||
pagination.pageSize = size
|
||
pagination.currentPage = 1
|
||
loadData()
|
||
}
|
||
|
||
function onQuickSearch() {
|
||
pagination.currentPage = 1
|
||
loadData()
|
||
}
|
||
|
||
function handleCreateReceiving() {
|
||
// 重置表单并打开对话框
|
||
Object.keys(createForm).forEach(key => createForm[key] = '')
|
||
createDialogVisible.value = true
|
||
}
|
||
|
||
// 新建收货单相关
|
||
const createDialogVisible = ref(false)
|
||
const createLoading = ref(false)
|
||
const createForm = reactive<any>({
|
||
store_id: '',
|
||
delivery_method: '',
|
||
loading_method: '',
|
||
license_plate: '',
|
||
tracking_number: '',
|
||
remark: ''
|
||
})
|
||
|
||
function handleCreateDialogClosed() {
|
||
Object.keys(createForm).forEach(key => createForm[key] = '')
|
||
}
|
||
|
||
async function submitCreateReceiving() {
|
||
// 表单校验:除备注外必须填写
|
||
const requiredFields = addReceivingFieldsRef.filter((f: { prop: string }) => f.prop !== 'remark')
|
||
for (const field of requiredFields) {
|
||
if (!createForm[field.prop]) {
|
||
ElMessage.warning(`请填写${field.label}`)
|
||
return
|
||
}
|
||
}
|
||
|
||
createLoading.value = true
|
||
try {
|
||
const res: any = await receivingApi.createReceiving(createForm)
|
||
console.log('新建收货单返回结果:', res)
|
||
if (res && res.code === 200) {
|
||
ElMessage.success('新建收货单成功')
|
||
createDialogVisible.value = false
|
||
loadData()
|
||
} else {
|
||
ElMessage.error(res?.message || '新建失败')
|
||
}
|
||
} catch (e) {
|
||
console.error('新建收货单出错:', e)
|
||
ElMessage.error('新建收货单失败')
|
||
} finally {
|
||
createLoading.value = false
|
||
}
|
||
}
|
||
|
||
// 打印相关
|
||
const printDialogVisible = ref(false)
|
||
const printing = ref(false)
|
||
const currentPrintIndex = ref(0)
|
||
const printStatusMap = ref<Record<string, string>>({})
|
||
const printForm = reactive<any>({})
|
||
const printSettings = reactive({ station: '' as string | number, printer: '', template: '' })
|
||
const stationOptions = ref<any[]>([])
|
||
const printerOptions = ref<any[]>([])
|
||
const templateOptions = ref<any[]>([])
|
||
const printerLoading = ref(false)
|
||
|
||
// --- 模板预览辅助组件(条码) ---
|
||
const barcodeImg = {
|
||
props: ['text'],
|
||
setup(props: any) {
|
||
const src = ref('')
|
||
const renderBarcode = (val: string) => {
|
||
if (!val) { src.value = ''; return }
|
||
const canvas = document.createElement('canvas')
|
||
try {
|
||
JsBarcode(canvas, val, { format: 'CODE128', width: 2, height: 40, displayValue: false, margin: 0 })
|
||
src.value = canvas.toDataURL()
|
||
} catch (e) { src.value = '' }
|
||
}
|
||
watch(() => props.text, (val: string) => renderBarcode(val), { immediate: true })
|
||
return () => h('img', { src: src.value, style: { display: 'block', width: '100%', height: '100%', objectFit: 'fill' } })
|
||
}
|
||
}
|
||
|
||
// 当前选中模板的画布尺寸
|
||
const previewInfo = computed(() => {
|
||
const t = templateOptions.value.find((t: any) => t.id === printSettings.template)
|
||
if (!t) return { width: 0, height: 0 }
|
||
return { width: t.page_width || 0, height: t.page_height || 0 }
|
||
})
|
||
|
||
// 当前选中模板的元素列表
|
||
const previewObjects = computed(() => {
|
||
const t = templateOptions.value.find((t: any) => t.id === printSettings.template)
|
||
if (!t || !Array.isArray(t.template_content)) return []
|
||
return t.template_content
|
||
})
|
||
|
||
// 获取元素的实际展示值(用 printForm 中的值替换 field_code 占位符)
|
||
function getPreviewValue(item: any): string {
|
||
if (item.field_code && printForm[item.field_code] !== undefined && printForm[item.field_code] !== '') {
|
||
return String(printForm[item.field_code])
|
||
}
|
||
return item.content || ''
|
||
}
|
||
|
||
// 计算预览元素的绝对定位样式(与 TemplateEditor 保持一致)
|
||
function computePreviewItemStyle(value: any, idx: string | number): any {
|
||
const idxNum = Number(idx)
|
||
const base: any = {
|
||
position: 'absolute',
|
||
left: value.left + 'mm',
|
||
top: value.top + 'mm',
|
||
width: value.width + 'mm',
|
||
height: value.height + 'mm',
|
||
zIndex: idxNum + 1,
|
||
textAlign: value.textAlign || 'left',
|
||
lineHeight: (value.lineHeight || 6) + 'mm',
|
||
transform: `rotate(${(value.rotate || 0) * 90}deg)`,
|
||
transformOrigin: 'center center',
|
||
overflow: 'hidden',
|
||
}
|
||
if (value.type === 'text') {
|
||
Object.assign(base, {
|
||
fontSize: (value.fontSize || 8) + 'pt',
|
||
lineHeight: (value.lineHeight || 6) + 'mm',
|
||
fontWeight: value.bold ? 'bold' : 'normal',
|
||
fontStyle: value.italic ? 'italic' : 'normal',
|
||
textDecoration: value.underline ? 'underline' : 'none',
|
||
whiteSpace: value.autoWrap ? 'pre-wrap' : 'nowrap',
|
||
writingMode: value.direction === 'vertical' ? 'vertical-rl' : 'horizontal-tb'
|
||
})
|
||
}
|
||
if (value.type === 'table') {
|
||
base.width = 'auto'
|
||
base.height = 'auto'
|
||
}
|
||
if (value.type === 'shape') {
|
||
if (value.shapeType === 'line') {
|
||
base.border = 'none'
|
||
base.borderTop = `${value.lineWeight || 1}pt ${value.lineStyle || 'solid'} #000`
|
||
base.backgroundColor = 'transparent'
|
||
base.height = 'auto'
|
||
} else if (value.shapeType === 'vertical-line') {
|
||
base.border = 'none'
|
||
base.borderLeft = `${value.lineWeight || 1}pt ${value.lineStyle || 'solid'} #000`
|
||
base.backgroundColor = 'transparent'
|
||
base.width = 'auto'
|
||
} else if (value.shapeType === 'box') {
|
||
base.border = `${value.lineWeight || 1}pt ${value.lineStyle || 'solid'} #000`
|
||
base.backgroundColor = 'transparent'
|
||
}
|
||
}
|
||
return base
|
||
}
|
||
|
||
const completedPrintCount = computed(() => {
|
||
return Object.values(printStatusMap.value).filter(s => s === 'done').length
|
||
})
|
||
|
||
async function handleOpenPrintDialog() {
|
||
if (!selectionRows.value.length) {
|
||
ElMessage.warning('请先选择要打印的收货单')
|
||
return
|
||
}
|
||
|
||
// 校验收货状态,只有“已完成”的才能打印
|
||
const invalidRows = selectionRows.value.filter(row => row.status !== 'completed')
|
||
if (invalidRows.length > 0) {
|
||
ElMessage.warning(`只有“已完成”状态的单据才能打印。当前选中包含 ${invalidRows.length} 个非已完成单据。`)
|
||
return
|
||
}
|
||
|
||
// 重置打印状态
|
||
currentPrintIndex.value = 0
|
||
printStatusMap.value = {}
|
||
selectionRows.value.forEach(row => {
|
||
printStatusMap.value[row.id] = 'pending'
|
||
})
|
||
|
||
// 加载第一项数据
|
||
await loadPrintFormData(0)
|
||
|
||
// 加载打印台和模板
|
||
loadPrintOptions()
|
||
printDialogVisible.value = true
|
||
}
|
||
|
||
async function switchPrintItem(index: number) {
|
||
if (printing.value) return
|
||
currentPrintIndex.value = index
|
||
await loadPrintFormData(index)
|
||
}
|
||
|
||
async function loadPrintFormData(index: number) {
|
||
const mainRow = selectionRows.value[index]
|
||
if (!mainRow) return
|
||
|
||
// 1. 从主表填充
|
||
const formData: any = {}
|
||
printFormConfig.forEach(group => {
|
||
group.fields.forEach((field: any) => {
|
||
const val = mainRow[field.prop] || ''
|
||
// 如果是选择器类型,尝试从 options 中提取 label(翻译后的文本)
|
||
if (field.type === 'select' && field.options) {
|
||
const opt = field.options.find((o: any) => o.value === val)
|
||
formData[field.prop] = opt ? opt.label : val
|
||
} else {
|
||
formData[field.prop] = val
|
||
}
|
||
})
|
||
})
|
||
|
||
// 2. 清空旧明细字段
|
||
const detailFields = ['product_name', 'barcode', 'box_quantity', 'boxes_per_pallet', 'actual_quantity', 'remark']
|
||
detailFields.forEach(f => formData[f] = '')
|
||
|
||
// 3. 获取详情填充第一个商品的明细(如果有的话)
|
||
try {
|
||
const res: any = await receivingApi.getReceivingDetail({
|
||
id: mainRow.id,
|
||
pagination: { page: 1, pageSize: 1000 }
|
||
})
|
||
if (res && res.code === 200) {
|
||
const details = res.data.receiving?.details || []
|
||
|
||
// 翻译并整理明细列表
|
||
const processedDetails = details.map((item: any) => {
|
||
const newItem = { ...item }
|
||
// 尝试对明细字段进行翻译(如果有配置)
|
||
const detailConfig = printFormConfig.find(g => g.group === '商品明细')
|
||
if (detailConfig) {
|
||
detailConfig.fields.forEach((field: any) => {
|
||
const val = newItem[field.prop]
|
||
if (field.type === 'select' && field.options) {
|
||
const opt = field.options.find((o: any) => o.value === val)
|
||
newItem[field.prop] = opt ? opt.label : val
|
||
} else if (val === undefined || val === null) {
|
||
newItem[field.prop] = ''
|
||
}
|
||
})
|
||
}
|
||
return newItem
|
||
})
|
||
|
||
// 将明细列表挂载到 formData,通常命名为 details 以便模板遍历
|
||
formData.details = processedDetails
|
||
|
||
if (processedDetails.length > 0) {
|
||
const detail = processedDetails[0]
|
||
const detailConfig = printFormConfig.find(g => g.group === '商品明细')
|
||
if (detailConfig) {
|
||
detailConfig.fields.forEach((field: any) => {
|
||
// 使用处理过的数据
|
||
formData[field.prop] = detail[field.prop]
|
||
})
|
||
}
|
||
}
|
||
}
|
||
} catch (e) {
|
||
console.error('加载打印明细失败', e)
|
||
}
|
||
|
||
Object.assign(printForm, formData)
|
||
}
|
||
|
||
async function loadPrintOptions() {
|
||
try {
|
||
const [stations, templates]: any = await Promise.all([
|
||
stationApi.getList({ page: 1, pageSize: 100, where: {} }),
|
||
templateApi.listTemplates({})
|
||
])
|
||
// 打印台:兼容 code 0 和 code 200,数据在 data.list 中
|
||
if (stations && (stations.code === 200 || stations.code === 0)) {
|
||
const rawList: any[] = stations.data?.list || stations.data || []
|
||
stationOptions.value = rawList.map((item: any) => ({
|
||
id: item.id,
|
||
name: item.name || item.label || String(item.id)
|
||
}))
|
||
} else {
|
||
console.warn('获取打印台列表失败:', stations)
|
||
}
|
||
// 打印模板:与模板编辑页保持一致,从 res.presetTree 中递归提取所有非分类节点
|
||
if (templates && templates.code === 200) {
|
||
const flattenTree = (nodes: any[]): any[] => {
|
||
const result: any[] = []
|
||
for (const node of nodes) {
|
||
if (node.type !== 'category') {
|
||
// 保存原始节点数据,特别是 template_content 等后端需要的字段
|
||
result.push({
|
||
...node,
|
||
id: node.id,
|
||
name: node.title || node.name || node.template_name || String(node.id)
|
||
})
|
||
}
|
||
if (node.children && node.children.length > 0) {
|
||
result.push(...flattenTree(node.children))
|
||
}
|
||
}
|
||
return result
|
||
}
|
||
templateOptions.value = flattenTree(templates.presetTree || [])
|
||
} else {
|
||
console.warn('获取模板列表失败:', templates)
|
||
}
|
||
} catch (e) {
|
||
console.error('加载打印配置失败', e)
|
||
}
|
||
}
|
||
|
||
async function handleStationChange(val: string | number) {
|
||
printSettings.printer = ''
|
||
printerOptions.value = []
|
||
if (!val) return
|
||
|
||
printerLoading.value = true
|
||
try {
|
||
const res: any = await stationApi.getComputerPrinter(String(val))
|
||
if (res && res.code === 0 && res.data) {
|
||
// res.data 可能是一个包含打印机列表的对象,具体取决于后端实现
|
||
// 这里的逻辑参考 system/setting/printStation 页面的预期数据格式
|
||
const printerList = Array.isArray(res.data) ? res.data : (res.data.list || [])
|
||
printerOptions.value = printerList.map((p: any) => ({
|
||
label: p.name || p.printerName || p,
|
||
value: p.name || p.printerName || p
|
||
}))
|
||
} else {
|
||
console.warn('获取打印机列表失败:', res)
|
||
}
|
||
} catch (e) {
|
||
console.error('获取打印机列表出错:', e)
|
||
} finally {
|
||
printerLoading.value = false
|
||
}
|
||
}
|
||
|
||
async function doPrint() {
|
||
if (!printSettings.station || !printSettings.template) {
|
||
ElMessage.warning('请选择打印台和模板')
|
||
return
|
||
}
|
||
if (!printSettings.printer) {
|
||
ElMessage.warning('请选择打印机')
|
||
return
|
||
}
|
||
|
||
printing.value = true
|
||
const currentRow = selectionRows.value[currentPrintIndex.value]
|
||
|
||
try {
|
||
// 后端字段:computerId (打印台id)、printer (打印机名)、template (模板对象)、data、type、size
|
||
const selectedTemplateObj = templateOptions.value.find((t: any) => t.id === printSettings.template) || null
|
||
const printPayload = {
|
||
type: 'customize',
|
||
computerId: printSettings.station,
|
||
printer: printSettings.printer,
|
||
template: selectedTemplateObj,
|
||
data: { ...printForm },
|
||
timestamp: Date.now(),
|
||
size: 1
|
||
}
|
||
|
||
const res: any = await stationApi.sendPrint(printPayload)
|
||
|
||
// 兼容 code 0(PDA 风格)和 code 200 两种后端响应
|
||
if (res && (res.code === 0 || res.code === 200 || res.status === 'success' || res.flag === 'success')) {
|
||
printStatusMap.value[currentRow.id] = 'done'
|
||
ElMessage.success(`单据 ${currentRow.receiving_number} 打印成功`)
|
||
|
||
// 自动切换到下一个待打印单据
|
||
if (currentPrintIndex.value < selectionRows.value.length - 1) {
|
||
currentPrintIndex.value++
|
||
await loadPrintFormData(currentPrintIndex.value)
|
||
} else {
|
||
ElMessage.info('已完成所有选中单据的打印操作')
|
||
}
|
||
} else {
|
||
printStatusMap.value[currentRow.id] = 'error'
|
||
ElMessage.error(res?.msg || res?.message || `单据 ${currentRow.receiving_number} 打印失败`)
|
||
}
|
||
} catch (e: any) {
|
||
console.error('打印出错:', e)
|
||
printStatusMap.value[currentRow.id] = 'error'
|
||
ElMessage.error('打印请求失败,请检查网络或打印台状态')
|
||
} finally {
|
||
printing.value = false
|
||
}
|
||
}
|
||
|
||
async function confirmcomplete() {
|
||
if (!selectionRows.value.length) {
|
||
ElMessage.warning('请先选择要操作的收货单')
|
||
return
|
||
}
|
||
|
||
try {
|
||
await ElMessageBox.confirm(`确定要批量完成 ${selectionRows.value.length} 个收货单的收货吗?`, '确认完成', { type: 'success' })
|
||
|
||
for (const row of selectionRows.value) {
|
||
console.log('正在执行完成收货,请求数据:', { id: row.id, type: 'complete' })
|
||
// 传递 id 标识单号,以及 type: 'complete' 来表示完成收货操作(补全请求数据)
|
||
const res: any = await receivingApi.completeReceiving({ id: row.id, type: 'complete' })
|
||
console.log('完成收货接口返回数据:', res)
|
||
|
||
if (res && res.code === 200) {
|
||
ElMessage.success(`收货单 ${row.receiving_number} 已完成`)
|
||
} else {
|
||
ElMessage.error(`收货单 ${row.receiving_number} 操作失败: ${res.message || '系统错误'}`)
|
||
}
|
||
}
|
||
|
||
selectionRows.value = []
|
||
loadData()
|
||
} catch (e) {
|
||
if (e !== 'cancel') {
|
||
console.error('完成收货操作出错:', e)
|
||
// ElMessage.error('操作执行失败')
|
||
}
|
||
}
|
||
}
|
||
|
||
async function confirmBatchCancel() {
|
||
if (!selectionRows.value.length) {
|
||
ElMessage.warning('请先选择要操作的收货单')
|
||
return
|
||
}
|
||
|
||
try {
|
||
await ElMessageBox.confirm(`确定要批量作废 ${selectionRows.value.length} 个收货单吗?`, '确认作废', { type: 'warning' })
|
||
|
||
const ids = selectionRows.value.map(row => row.id)
|
||
const res: any = await receivingApi.batchCancelReceiving({ ids })
|
||
|
||
if (res && res.code === 200) {
|
||
ElMessage.success('批量作废成功')
|
||
} else {
|
||
ElMessage.error(res.message || '批量作废失败')
|
||
}
|
||
|
||
selectionRows.value = []
|
||
loadData()
|
||
} catch (e) {
|
||
if (e !== 'cancel') {
|
||
console.error('批量作废操作出错:', e)
|
||
// ElMessage.error('操作执行失败')
|
||
}
|
||
}
|
||
}
|
||
|
||
function printReceiving() {
|
||
if (!selectionRows.value.length) {
|
||
ElMessage.warning('请选择要打印的收货单')
|
||
return
|
||
}
|
||
const payload = selectionRows.value.map(r => ({ ...r, businessId: r.receiving_number }))
|
||
// 简单打印:在新窗口显示 JSON,后续可替换为正式打印组件
|
||
const w = window.open('', '_blank')
|
||
if (w) {
|
||
w.document.write(`<pre>${JSON.stringify(payload, null, 2)}</pre>`)
|
||
w.document.close()
|
||
}
|
||
}
|
||
|
||
async function handleExport() {
|
||
if (list.value.length === 0) {
|
||
ElMessage.warning('当前无数据可导出')
|
||
return
|
||
}
|
||
exportLoading.value = true
|
||
try {
|
||
// 1. 准备表头
|
||
const headers = columns.value.map(col => col.label)
|
||
|
||
// 2. 准备数据行,应用配置中的 formatter(如果有)
|
||
const data = list.value.map(row => {
|
||
return columns.value.map(col => {
|
||
if (col.formatter && typeof col.formatter === 'function') {
|
||
return col.formatter(row)
|
||
}
|
||
|
||
const val = row[col.prop]
|
||
|
||
// 针对特定字段进行硬编码翻译(如果 formatter 没处理)
|
||
if (col.prop === 'status') {
|
||
return statusConfig[val as keyof typeof statusConfig]?.text || val
|
||
}
|
||
if (col.prop === 'detail_status') {
|
||
return detailStatusConfig[val as keyof typeof detailStatusConfig]?.text || val
|
||
}
|
||
if (col.prop === 'storage_status') {
|
||
return storageStatusConfig[val as keyof typeof storageStatusConfig]?.text || val
|
||
}
|
||
if (col.prop === 'delivery_method') {
|
||
return deliveryMethodConfig[val]?.text || val
|
||
}
|
||
if (col.prop === 'loading_method') {
|
||
return loadingMethodConfig[val]?.text || val
|
||
}
|
||
|
||
return val === null || val === undefined ? '' : val
|
||
})
|
||
})
|
||
|
||
// 3. 创建工作簿和工作表
|
||
const ws = XLSX.utils.aoa_to_sheet([headers, ...data])
|
||
const wb = XLSX.utils.book_new()
|
||
XLSX.utils.book_append_sheet(wb, ws, '收货单列表')
|
||
|
||
// 4. 导出文件
|
||
const fileName = `收货单列表_${new Date().getTime()}.xlsx`
|
||
XLSX.writeFile(wb, fileName)
|
||
|
||
ElMessage.success('导出成功')
|
||
} catch (e) {
|
||
console.error('导出 Excel 失败:', e)
|
||
ElMessage.error('导出失败')
|
||
} finally {
|
||
exportLoading.value = false
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<style scoped lang="scss">
|
||
.storage-receiving-container {
|
||
display: flex;
|
||
height: calc(100vh - 100px);
|
||
width: 100%;
|
||
background-color: #f0f2f5;
|
||
|
||
.main-content {
|
||
flex: 1;
|
||
display: flex;
|
||
flex-direction: column;
|
||
padding: 10px;
|
||
overflow: hidden;
|
||
|
||
.toolbar {
|
||
padding: 5px;
|
||
border-bottom: 1px solid #ebeef5;
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
background-color: #fff;
|
||
}
|
||
.toolbar-right {
|
||
display: flex;
|
||
flex-direction: row;
|
||
gap:15px;
|
||
height: 30px;
|
||
align-items: center;
|
||
}
|
||
|
||
.upper-wrapper {
|
||
flex: 1 1 auto;
|
||
min-height: 0;
|
||
/* upper-wrapper 自动填充剩余空间 */
|
||
overflow: hidden;
|
||
}
|
||
|
||
/* upper-table-container 已在组件内支持弹性高度,此处确保撑满 upper-wrapper */
|
||
.upper-wrapper :deep(.upper-table-container) {
|
||
height: 100%;
|
||
}
|
||
|
||
.lower-level {
|
||
flex: 0 0 40%;
|
||
height: 40%;
|
||
display: flex;
|
||
flex-direction: column;
|
||
border-top: 1px solid #ebeef5;
|
||
overflow: hidden;
|
||
background-color: #fff;
|
||
|
||
.detail-header {
|
||
padding: 0 10px;
|
||
border-bottom: 1px solid #ebeef5;
|
||
background-color: #fafafa;
|
||
|
||
.detail-tab {
|
||
margin-bottom: -1px;
|
||
|
||
:deep(.el-tabs__header) {
|
||
margin: 0;
|
||
}
|
||
:deep(.el-tabs__item) {
|
||
height: 32px;
|
||
line-height: 32px;
|
||
font-size: 13px;
|
||
}
|
||
:deep(.el-tabs__nav-wrap::after) {
|
||
height: 1px;
|
||
}
|
||
}
|
||
}
|
||
|
||
.detail-container {
|
||
flex: 1;
|
||
overflow: auto;
|
||
padding: 10px;
|
||
}
|
||
}
|
||
|
||
.no-selection {
|
||
flex: 0 0 40%;
|
||
height: 40%;
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
color: #909399;
|
||
background-color: #fff;
|
||
|
||
.empty-tip { text-align: center;
|
||
font-size: 14px;
|
||
.el-icon { font-size: 48px; margin-bottom: 10px;} }
|
||
}
|
||
}
|
||
}
|
||
|
||
|
||
.print-dialog-layout {
|
||
display: flex;
|
||
height: 1150px;
|
||
border: 1px solid #ebeef5;
|
||
|
||
.print-batch-list {
|
||
width: 250px;
|
||
border-right: 1px solid #ebeef5;
|
||
background-color: #fafafa;
|
||
display: flex;
|
||
flex-direction: column;
|
||
overflow-y: auto;
|
||
/* 限制左侧列表高度,使其在对话框中可滚动 */
|
||
max-height: 100%;
|
||
|
||
.list-title {
|
||
padding: 10px;
|
||
font-weight: bold;
|
||
border-bottom: 1px solid #ebeef5;
|
||
background-color: #fff;
|
||
}
|
||
|
||
.batch-item {
|
||
padding: 12px 15px;
|
||
cursor: pointer;
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
border-bottom: 1px dotted #eee;
|
||
transition: background 0.2s;
|
||
|
||
&:hover { background-color: #f0f7ff; }
|
||
&.active {
|
||
background-color: #e6f7ff;
|
||
border-left: 3px solid #1890ff;
|
||
}
|
||
|
||
.item-index { color: #999; font-size: 12px; }
|
||
.item-number { flex: 1; font-weight: 500; font-family: monospace; }
|
||
}
|
||
}
|
||
|
||
.print-preview-main {
|
||
flex: 1;
|
||
display: flex;
|
||
flex-direction: column;
|
||
overflow: hidden;
|
||
background-color: #fff;
|
||
|
||
.print-settings-bar {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
padding: 10px 15px;
|
||
border-bottom: 1px solid #ebeef5;
|
||
background-color: #fafafa;
|
||
flex-shrink: 0;
|
||
|
||
.el-select { flex: 1; min-width: 130px; }
|
||
}
|
||
|
||
.print-canvas-area {
|
||
flex: 1;
|
||
overflow: auto;
|
||
display: flex;
|
||
justify-content: center;
|
||
align-items: flex-start;
|
||
padding: 20px;
|
||
background-color: #f0f2f5;
|
||
|
||
.preview-page {
|
||
background: #fff;
|
||
box-shadow: 0 2px 12px rgba(0, 0, 0, 0.15);
|
||
border: 1px solid #ddd;
|
||
flex-shrink: 0;
|
||
}
|
||
|
||
.preview-empty {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
width: 100%;
|
||
height: 100%;
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
.dialog-footer {
|
||
display: flex;
|
||
justify-content: space-between;
|
||
align-items: center;
|
||
width: 100%;
|
||
|
||
.progress-tip {
|
||
color: #606266;
|
||
font-size: 14px;
|
||
font-weight: 500;
|
||
}
|
||
}
|
||
|
||
/* 居中打印对话框(垂直居中) */
|
||
.print-dialog .el-dialog {
|
||
top: 50% !important;
|
||
transform: translateY(-50%) !important;
|
||
}
|
||
</style> |