gaoluyang
2026-06-24 712aa51536236d43e87273e4ce45ac5691dffad8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<script lang="ts" setup>
import type Barcode from './barcode.vue';
 
import type { MesWmBarcodeApi } from '#/api/mes/wm/barcode';
 
import { ref } from 'vue';
 
import { Button, Empty, message, Modal, Tooltip } from 'ant-design-vue';
 
import { createBarcode, getBarcodeByBusiness } from '#/api/mes/wm/barcode';
import { useDescription } from '#/components/description';
 
import { useBarcodeDetailSchema } from '../data';
import MesWmBarcode from './barcode.vue';
 
defineOptions({ name: 'MesWmBarcodeDetail' });
 
const open = ref(false);
const barcodeRef = ref<InstanceType<typeof Barcode>>();
const barcodeData = ref<Partial<MesWmBarcodeApi.Barcode>>({});
 
const [Descriptions] = useDescription({
  bordered: true,
  column: 1,
  schema: useBarcodeDetailSchema(),
  useCard: false,
});
 
function openModal(row: Partial<MesWmBarcodeApi.Barcode>) {
  open.value = true;
  barcodeData.value = { ...row };
}
 
async function openByBusiness(
  bizId: number,
  bizType: number,
  bizCode?: string,
  bizName?: string,
) {
  open.value = true;
  try {
    const data = await getBarcodeByBusiness(bizType, bizId);
    barcodeData.value = data || {
      bizCode,
      bizId,
      bizName,
      bizType,
      content: '',
    };
    if (!data) {
      message.warning('未找到对应条码数据');
    }
  } catch {
    barcodeData.value = { bizCode, bizId, bizName, bizType, content: '' };
    message.error('加载条码数据失败');
  }
}
 
defineExpose({ open: openModal, openByBusiness });
 
function escapeHtml(value: string) {
  return value
    .replaceAll('&', '&amp;')
    .replaceAll('<', '&lt;')
    .replaceAll('>', '&gt;');
}
 
function handlePrint() {
  const base64 = barcodeRef.value?.getImageBase64();
  if (!base64) {
    message.warning('条码生成失败,无法打印');
    return;
  }
  const printWindow = window.open('', '_blank');
  if (!printWindow) {
    message.error('无法打开打印窗口,请检查浏览器设置');
    return;
  }
  printWindow.document.write(`<!DOCTYPE html>
<html><head><meta charset="UTF-8"><title>打印条码</title>
<style>*{margin:0;padding:0}body{font-family:Arial,sans-serif;padding:20px}.print-container{text-align:center}.barcode-img{max-width:100%;margin:20px 0}.info{margin-top:20px;text-align:left;font-size:12px}.info p{margin:5px 0}@media print{body{padding:0}.print-container{padding:20px}}</style>
</head><body><div class="print-container">
<img src="${base64}" class="barcode-img" alt="条码" />
<div class="info">
<p><strong>业务编码:</strong> ${escapeHtml(barcodeData.value.bizCode || '')}</p>
<p><strong>业务名称:</strong> ${escapeHtml(barcodeData.value.bizName || '')}</p>
<p><strong>条码内容:</strong> ${escapeHtml(barcodeData.value.content || '')}</p>
</div></div></body></html>`);
  printWindow.document.close();
  printWindow.addEventListener('load', () => {
    setTimeout(() => printWindow.print(), 500);
  });
}
 
function handleDownload() {
  const base64 = barcodeRef.value?.getImageBase64();
  if (!base64) {
    message.warning('条码生成失败,无法下载');
    return;
  }
  const link = document.createElement('a');
  link.href = base64;
  link.download = `barcode_${barcodeData.value.bizCode || 'unknown'}_${Date.now()}.png`;
  link.click();
  message.success('下载成功');
}
 
async function handleGenerate() {
  const { bizCode, bizId, bizName, bizType } = barcodeData.value;
  if (!bizType || !bizId) {
    message.warning('缺少业务类型或业务编号,无法生成条码');
    return;
  }
  await createBarcode({
    bizCode: bizCode || '',
    bizId,
    bizName: bizName || '',
    bizType,
  });
  message.success('条码生成成功');
  const data = await getBarcodeByBusiness(bizType, bizId);
  if (data) {
    barcodeData.value = { ...data };
  }
}
</script>
 
<template>
  <Modal v-model:open="open" title="查看条码" width="500px">
    <div>
      <div
        class="mb-5 flex min-h-50 items-center justify-center rounded bg-gray-100 p-5"
      >
        <div
          v-if="barcodeData.content"
          class="flex items-center justify-center"
        >
          <MesWmBarcode
            ref="barcodeRef"
            :content="barcodeData.content"
            :format="barcodeData.format"
            :height="150"
            :width="400"
          />
        </div>
        <Empty v-else description="暂无条码数据" />
      </div>
      <Descriptions :data="barcodeData">
        <template #content>
          <Tooltip :title="barcodeData.content">
            <span
              class="inline-block max-w-75 overflow-hidden text-ellipsis whitespace-nowrap"
            >
              {{ barcodeData.content }}
            </span>
          </Tooltip>
        </template>
      </Descriptions>
    </div>
    <template #footer>
      <Button v-if="!barcodeData.content" @click="handleGenerate">
        生成
      </Button>
      <Button type="primary" @click="handlePrint">打印</Button>
      <Button @click="handleDownload">下载</Button>
      <Button @click="open = false">关闭</Button>
    </template>
  </Modal>
</template>