3 天以前 d2cd585f4d3b015789a5f6c8d75e352605932f85
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
<script lang="ts" setup>
  import type { MesProBagCodeApi } from "#/api/mes/pro/bagCode";
  import type { MesProBatchApi } from "#/api/mes/pro/batch";
  import type { MesProPalletApi } from "#/api/mes/pro/pallet";
 
  import { ref } from "vue";
 
  import { Barcode, useVbenModal } from "@vben/common-ui";
  import { DICT_TYPE } from "@vben/constants";
  import { getDictLabel } from "@vben/hooks";
 
  import { Button, Empty, Input, message, Spin, Tag } from "ant-design-vue";
 
  import { getBagCodeByCode } from "#/api/mes/pro/bagCode";
  import { getBatchByCode } from "#/api/mes/pro/batch";
  import { getPalletByCode } from "#/api/mes/pro/pallet";
  import { useDescription } from "#/components/description";
 
  defineOptions({ name: "MesProQrTraceModal" });
 
  type TraceType = "batch" | "pallet" | "bag";
 
  const MAX_CODE_LENGTH = 64;
 
  const codeInput = ref<string>();
  const currentCode = ref<string>();
  const traceType = ref<TraceType>();
  const loading = ref(false);
  const searched = ref(false);
  const searchError = ref(false);
  const bagTrace = ref<MesProBagCodeApi.BagCodeTrace>();
  const palletTrace = ref<MesProPalletApi.PalletTrace>();
  const batchTrace = ref<MesProBatchApi.BatchTrace>();
  const barcodeRef = ref<InstanceType<typeof Barcode>>();
 
  const traceTypeLabel = ref("");
 
  /** 识别二维码内容类型:批次号 / 托盘码 / 袋码 */
  function resolveCodeType(raw: string): TraceType | undefined {
    const value = raw.trim().toUpperCase();
    if (/^.*-P\d{2}$/.test(value)) {
      return "pallet";
    }
    if (/^.*-\d{4}$/.test(value)) {
      return "bag";
    }
    return "batch";
  }
 
  function resetState() {
    currentCode.value = undefined;
    traceType.value = undefined;
    traceTypeLabel.value = "";
    bagTrace.value = undefined;
    palletTrace.value = undefined;
    batchTrace.value = undefined;
    searched.value = false;
    searchError.value = false;
  }
 
  /** 扫码/输入二维码内容查询追溯信息 */
  async function handleSearch(raw?: string) {
    const value = (raw ?? codeInput.value)?.trim().toUpperCase();
    if (!value) {
      message.warning("请扫描或输入二维码内容");
      return;
    }
    if (value.length > MAX_CODE_LENGTH) {
      message.warning("二维码内容过长,请检查是否扫描正确");
      return;
    }
    const type = resolveCodeType(value);
    if (!type) {
      message.warning("无法识别二维码类型");
      return;
    }
    loading.value = true;
    searched.value = true;
    searchError.value = false;
    currentCode.value = value;
    traceType.value = undefined;
    traceTypeLabel.value = "";
    bagTrace.value = undefined;
    palletTrace.value = undefined;
    batchTrace.value = undefined;
    try {
      if (type === "batch") {
        batchTrace.value = await getBatchByCode(value);
        traceTypeLabel.value = "批次号";
      } else if (type === "pallet") {
        palletTrace.value = await getPalletByCode(value);
        traceTypeLabel.value = "托盘码";
      } else {
        bagTrace.value = await getBagCodeByCode(value);
        traceTypeLabel.value = "袋码";
      }
      traceType.value = type;
    } catch {
      searchError.value = true;
      currentCode.value = undefined;
      traceType.value = undefined;
    } finally {
      loading.value = false;
    }
  }
 
  /** 当前追溯数据的关键信息(下载/打印图片叠加文字用) */
  function buildLabelText(): { label: string; value: string }[] {
    if (traceType.value === "bag" && bagTrace.value) {
      const d = bagTrace.value;
      return [
        { label: "袋码", value: d.code || "" },
        { label: "袋序号", value: d.bagNo ? String(d.bagNo) : "" },
        { label: "批次号", value: d.batchCode || "" },
        { label: "品种", value: d.itemName || "" },
        { label: "规格", value: d.itemSpecification || "" },
        {
          label: "产线",
          value: [d.lineCode, d.lineName].filter(Boolean).join(" / "),
        },
        { label: "生产日期", value: d.produceDate || "" },
        { label: "责任人", value: d.ownerUserName || "" },
      ];
    }
    if (traceType.value === "pallet" && palletTrace.value) {
      const d = palletTrace.value;
      return [
        { label: "托盘码", value: d.code || "" },
        { label: "托盘序号", value: d.palletNo ? String(d.palletNo) : "" },
        { label: "批次号", value: d.batchCode || "" },
        { label: "品种", value: d.itemName || "" },
        { label: "规格", value: d.itemSpecification || "" },
        {
          label: "产线",
          value: [d.lineCode, d.lineName].filter(Boolean).join(" / "),
        },
        { label: "生产日期", value: d.produceDate || "" },
        { label: "责任人", value: d.ownerUserName || "" },
      ];
    }
    if (batchTrace.value) {
      const d = batchTrace.value;
      return [
        { label: "批次号", value: d.code || "" },
        { label: "品种", value: d.itemName || "" },
        { label: "规格", value: d.itemSpecification || "" },
        {
          label: "产线",
          value: [d.lineCode, d.lineName].filter(Boolean).join(" / "),
        },
        { label: "生产日期", value: d.produceDate || "" },
        { label: "计划数量", value: d.planQuantity ? String(d.planQuantity) : "" },
        { label: "责任人", value: d.ownerUserName || "" },
      ];
    }
    return [];
  }
 
  function escapeHtml(value: string) {
    return value
      .replaceAll("&", "&amp;")
      .replaceAll("<", "&lt;")
      .replaceAll(">", "&gt;");
  }
 
  /** 下载二维码 PNG(二维码 + 关键信息文字合成) */
  function handleDownload() {
    const base64 = barcodeRef.value?.getImageBase64();
    if (!base64) {
      message.warning("二维码生成失败,无法下载");
      return;
    }
    const items = buildLabelText();
    const canvas = document.createElement("canvas");
    const ctx = canvas.getContext("2d");
    if (!ctx) {
      message.error("Canvas 初始化失败");
      return;
    }
    const img = new Image();
    img.onload = () => {
      const padding = 20;
      const infoX = img.width + padding * 2;
      const infoWidth = 320;
      canvas.width = infoX + infoWidth + padding;
      canvas.height = Math.max(img.height + padding * 2, 300);
      ctx.fillStyle = "#ffffff";
      ctx.fillRect(0, 0, canvas.width, canvas.height);
      ctx.drawImage(img, padding, padding);
      ctx.fillStyle = "#000000";
      let y = padding + 10;
      const lineHeight = 26;
      items.forEach((item) => {
        ctx.font = "bold 14px 'Microsoft YaHei', Arial, sans-serif";
        ctx.fillText(`${item.label}:`, infoX, y);
        y += 18;
        ctx.font = "14px 'Microsoft YaHei', Arial, sans-serif";
        const text = item.value || "-";
        const maxWidth = infoWidth - 10;
        if (ctx.measureText(text).width > maxWidth) {
          let currentText = text;
          while (currentText.length > 0) {
            let line = "";
            for (let i = 0; i < currentText.length; i++) {
              const testLine = line + currentText[i];
              if (ctx.measureText(testLine).width > maxWidth) {
                break;
              }
              line = testLine;
            }
            ctx.fillText(line, infoX + 5, y);
            y += 16;
            currentText = currentText.slice(line.length);
          }
          y += 6;
        } else {
          ctx.fillText(text, infoX + 5, y);
          y += lineHeight;
        }
      });
      const link = document.createElement("a");
      link.href = canvas.toDataURL("image/png");
      link.download = `${currentCode.value || "qr"}_${Date.now()}.png`;
      link.click();
      message.success("下载成功");
    };
    img.src = base64;
  }
 
  /** 打印二维码 */
  function handlePrint() {
    const base64 = barcodeRef.value?.getImageBase64();
    if (!base64) {
      message.warning("二维码生成失败,无法打印");
      return;
    }
    const items = buildLabelText();
    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:'Microsoft YaHei',Arial,sans-serif;padding:20px}.print-container{text-align:center}.qr-img{max-width:100%;margin:20px 0}.info{margin-top:20px;text-align:left;font-size:13px}.info p{margin:4px 0}@media print{body{padding:0}.print-container{padding:20px}}</style>
</head><body><div class="print-container">
<img src="${base64}" class="qr-img" alt="二维码" />
<div class="info">
${items.map((item) => `<p><strong>${escapeHtml(item.label)}:</strong> ${escapeHtml(item.value || "-")}</p>`).join("")}
</div></div></body></html>`);
    printWindow.document.close();
    printWindow.addEventListener("load", () => {
      setTimeout(() => printWindow.print(), 500);
    });
  }
 
  const [Modal, modalApi] = useVbenModal({
    onOpenChange(isOpen: boolean) {
      if (!isOpen) {
        return;
      }
      const data = modalApi.getData<{ code?: string }>() || {};
      codeInput.value = data.code ?? undefined;
      resetState();
      if (data.code) {
        handleSearch(data.code);
      }
    },
  });
 
  const [BagDescriptions] = useDescription({
    bordered: true,
    column: 2,
    schema: [
      {
        field: "code",
        label: "袋码(二维码内容)",
        span: 2,
      },
      {
        field: "bagNo",
        label: "袋序号",
      },
      {
        field: "status",
        label: "袋码状态",
        render: (val) =>
          val ? getDictLabel(DICT_TYPE.MES_PRO_BAG_CODE_STATUS, val) : "",
      },
      {
        field: "batchCode",
        label: "生产批次号",
      },
      {
        field: "batchStatus",
        label: "批次状态",
        render: (val) =>
          val ? getDictLabel(DICT_TYPE.MES_PRO_BATCH_STATUS, val) : "",
      },
      {
        field: "itemName",
        label: "品种",
      },
      {
        field: "itemSpecification",
        label: "规格",
      },
      {
        field: "produceDate",
        label: "生产日期",
      },
      {
        field: "ownerUserName",
        label: "责任人",
      },
      {
        field: "lineCode",
        label: "产线",
        render: (val, data) =>
          [val, data?.lineName].filter(Boolean).join(" / "),
      },
      {
        field: "palletCode",
        label: "绑定托盘码",
        render: (val) => val || "未绑定",
      },
      {
        field: "printTime",
        label: "导出印刷时间",
      },
      {
        field: "cancelTime",
        label: "作废时间",
      },
      {
        field: "cancelReason",
        label: "作废原因",
      },
      {
        field: "replacementUuid",
        label: "替换组 UUID",
      },
      {
        field: "replacedByCode",
        label: "替换后袋码",
      },
      {
        field: "replacementOfCode",
        label: "替换来源袋码",
      },
    ],
  });
 
  const [PalletDescriptions] = useDescription({
    bordered: true,
    column: 2,
    schema: [
      {
        field: "code",
        label: "托盘码(二维码内容)",
        span: 2,
      },
      {
        field: "palletNo",
        label: "托盘序号",
      },
      {
        field: "capacity",
        label: "可装袋数(袋/托盘)",
      },
      {
        field: "bagCount",
        label: "已装袋数",
      },
      {
        field: "status",
        label: "托盘状态",
        render: (val) =>
          val ? getDictLabel(DICT_TYPE.MES_PRO_PALLET_STATUS, val) : "",
      },
      {
        field: "batchCode",
        label: "生产批次号",
      },
      {
        field: "batchStatus",
        label: "批次状态",
        render: (val) =>
          val ? getDictLabel(DICT_TYPE.MES_PRO_BATCH_STATUS, val) : "",
      },
      {
        field: "itemName",
        label: "品种",
      },
      {
        field: "itemSpecification",
        label: "规格",
      },
      {
        field: "produceDate",
        label: "生产日期",
      },
      {
        field: "ownerUserName",
        label: "责任人",
      },
      {
        field: "lineCode",
        label: "产线",
        render: (val, data) =>
          [val, data?.lineName].filter(Boolean).join(" / "),
      },
    ],
  });
 
  const [BatchDescriptions] = useDescription({
    bordered: true,
    column: 2,
    schema: [
      {
        field: "code",
        label: "批次号(二维码内容)",
        span: 2,
      },
      {
        field: "status",
        label: "批次状态",
        render: (val) =>
          val ? getDictLabel(DICT_TYPE.MES_PRO_BATCH_STATUS, val) : "",
      },
      {
        field: "itemName",
        label: "品种",
      },
      {
        field: "itemSpecification",
        label: "规格",
      },
      {
        field: "lineCode",
        label: "产线",
        render: (val, data) =>
          [val, data?.lineName].filter(Boolean).join(" / "),
      },
      {
        field: "produceDate",
        label: "生产日期",
      },
      {
        field: "planQuantity",
        label: "计划数量",
      },
      {
        field: "bagQuantity",
        label: "已生成袋码数量",
      },
      {
        field: "palletQuantity",
        label: "已生成托盘码数量",
      },
      {
        field: "ownerUserName",
        label: "责任人",
      },
      {
        field: "remark",
        label: "备注",
      },
    ],
  });
</script>
 
<template>
  <Modal
    :show-confirm-button="false"
    cancel-text="关闭"
    class="w-3/5"
    title="扫码追溯 / 查看二维码"
  >
    <div class="mx-4">
      <div class="mb-4 flex gap-2">
        <Input
          v-model:value="codeInput"
          allow-clear
          class="!w-96"
          placeholder="请扫描或输入批次号 / 托盘码 / 袋码,回车查询"
          @press-enter="() => handleSearch()"
        />
        <Button :loading="loading" type="primary" @click="() => handleSearch()">
          查询
        </Button>
      </div>
      <Spin :spinning="loading">
        <template v-if="currentCode && traceType">
          <div
            class="mb-4 flex items-center gap-6 rounded bg-gray-100 p-5"
          >
            <div class="flex-shrink-0">
              <Barcode ref="barcodeRef" :content="currentCode" :width="200" />
            </div>
            <div class="min-w-0 flex-1">
              <p class="text-lg font-bold break-all">{{ currentCode }}</p>
              <p class="mt-1 text-xs text-gray-500">
                码类型:{{ traceTypeLabel }}
              </p>
            </div>
          </div>
          <template v-if="traceType === 'batch' && batchTrace">
            <BatchDescriptions :data="batchTrace" class="mt-2" />
            <div
              v-if="batchTrace.workers?.length"
              class="mt-3 rounded border border-solid border-gray-200 p-3"
            >
              <p class="mb-2 font-bold">
                生产人员({{ batchTrace.workers.length }})
              </p>
              <div class="flex flex-wrap gap-2">
                <Tag v-for="w in batchTrace.workers" :key="w.id">
                  {{ w.userName
                  }}<template v-if="w.workerType"
                    >({{
                      getDictLabel(DICT_TYPE.MES_PRO_BATCH_WORKER_TYPE, w.workerType)
                    }})</template
                  >
                </Tag>
              </div>
            </div>
            <div
              v-if="batchTrace.pallets?.length"
              class="mt-3 rounded border border-solid border-gray-200 p-3"
            >
              <p class="mb-2 font-bold">
                托盘码({{ batchTrace.pallets.length }})
              </p>
              <div class="flex flex-wrap gap-2">
                <Tag v-for="p in batchTrace.pallets" :key="p.id">
                  {{ p.code }}
                </Tag>
              </div>
            </div>
            <div
              v-if="batchTrace.bags?.length"
              class="mt-3 rounded border border-solid border-gray-200 p-3"
            >
              <p class="mb-2 font-bold">
                袋码
                <span
                  v-if="
                    batchTrace.bagTotal != null &&
                    batchTrace.bagTotal > batchTrace.bags.length
                  "
                  class="text-xs text-gray-400"
                >
                  (仅展示前 {{ batchTrace.bags.length }} 条,共
                  {{ batchTrace.bagTotal }} 条)
                </span>
              </p>
              <div class="flex max-h-48 flex-wrap gap-2 overflow-y-auto">
                <Tag v-for="b in batchTrace.bags" :key="b.id">
                  {{ b.code }}
                </Tag>
              </div>
            </div>
          </template>
          <BagDescriptions
            v-else-if="traceType === 'bag' && bagTrace"
            :data="bagTrace"
            class="mt-2"
          />
          <PalletDescriptions
            v-else-if="traceType === 'pallet' && palletTrace"
            :data="palletTrace"
            class="mt-2"
          />
        </template>
        <Empty
          v-else-if="searched"
          :description="searchError ? '该码不存在或已删除' : '未查询到追溯信息'"
        />
      </Spin>
    </div>
    <template v-if="currentCode && traceType" #append-footer>
      <div class="flex gap-2">
        <Button type="primary" @click="handlePrint">打印</Button>
        <Button @click="handleDownload">下载二维码</Button>
      </div>
    </template>
  </Modal>
</template>