liu
4 天以前 6373c12e97dc5d52ad87271d1adb6743b0052607
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
<script lang="ts" setup>
import type { ErpPurchaseInvoiceAiApi } from '#/api/erp/purchase/invoice/ai';
 
import { ref } from 'vue';
 
import { IconifyIcon } from '@vben/icons';
 
import {
  Alert,
  Button,
  Descriptions,
  Modal,
  Tag,
  Upload,
  message,
} from 'ant-design-vue';
 
import { ocrPurchaseInvoice } from '#/api/erp/purchase/invoice/ai';
import { uploadFile } from '#/api/system/storage';
 
defineOptions({ name: 'ErpPurchaseInvoiceOcrUpload' });
 
const emit = defineEmits<{
  success: [data: ErpPurchaseInvoiceAiApi.OcrResultVO];
}>();
 
const open = ref(false);
const step = ref(0); // 0=upload, 1=processing, 2=preview
const fileList = ref<any[]>([]);
const ocrResult = ref<ErpPurchaseInvoiceAiApi.OcrResultVO>();
const processingTip = ref('');
 
function handleOpen() {
  open.value = true;
  step.value = 0;
  fileList.value = [];
  ocrResult.value = undefined;
}
 
/** 自定义上传 */
async function handleUpload(options: any) {
  const { file, onSuccess, onError } = options;
  try {
    const result = await uploadFile([file as File]);
    const blobId = result?.[0]?.id;
    if (!blobId) {
      onError(new Error('上传失败'));
      return;
    }
    onSuccess({ blobId }, file);
    // 开始 OCR 识别
    step.value = 1;
    processingTip.value = '正在进行发票识别,请稍候...';
    const ocrData = await ocrPurchaseInvoice(blobId);
    // 检查错误:rawText 为错误提示,且业务字段全空
    const hasData = !!(
      ocrData.invoiceNo ||
      ocrData.invoiceTitle ||
      ocrData.supplierName ||
      ocrData.price != null ||
      ocrData.invoiceTime ||
      ocrData.invoiceType ||
      ocrData.taxRate != null ||
      ocrData.remark
    );
    if (ocrData.rawText && !hasData) {
      processingTip.value = '';
      step.value = 0;
      message.error(ocrData.rawText);
      return;
    }
    if (!hasData) {
      processingTip.value = '';
      step.value = 0;
      message.warning('未识别到发票数据,请手动录入或重新上传');
      return;
    }
    ocrResult.value = ocrData;
    step.value = 2;
  } catch {
    processingTip.value = '';
    step.value = 0;
    onError(new Error('上传失败'));
  }
}
 
function handleConfirm() {
  if (ocrResult.value) {
    emit('success', ocrResult.value);
  }
  handleClose();
}
 
function handleReset() {
  step.value = 0;
  fileList.value = [];
  ocrResult.value = undefined;
  processingTip.value = '';
}
 
function handleClose() {
  open.value = false;
  handleReset();
}
 
defineExpose({ open: handleOpen });
</script>
 
<template>
  <Modal
    v-model:open="open"
    title="识别录入发票"
    width="680px"
    :footer="null"
    @cancel="handleClose"
  >
    <!-- Step 0: 上传 -->
    <template v-if="step === 0">
      <Upload.Dragger
        v-model:file-list="fileList"
        :max-count="1"
        :custom-request="handleUpload"
        accept=".pdf,.png,.jpg,.jpeg"
        @remove="handleReset"
      >
        <p class="text-4xl text-gray-400">
          <IconifyIcon icon="ant-design:inbox-outlined" />
        </p>
        <p class="text-base text-gray-500">点击或拖拽发票文件到此区域上传</p>
        <p class="text-sm text-gray-400">支持 PDF、JPG、PNG 格式,与来票附件上传格式一致</p>
      </Upload.Dragger>
    </template>
 
    <!-- Step 1: 识别中 -->
    <template v-else-if="step === 1">
      <div class="flex flex-col items-center gap-4 py-12">
        <a-spin size="large" />
        <span class="text-base text-gray-500">{{ processingTip }}</span>
      </div>
    </template>
 
    <!-- Step 2: 预览结果 -->
    <template v-else-if="step === 2 && ocrResult">
      <div class="mb-4">
        <Descriptions :column="2" bordered size="small">
          <Descriptions.Item v-if="ocrResult.invoiceNo" label="发票号码">
            {{ ocrResult.invoiceNo }}
          </Descriptions.Item>
          <Descriptions.Item v-if="ocrResult.invoiceType" label="发票类型">
            {{ ocrResult.invoiceType }}
          </Descriptions.Item>
          <Descriptions.Item v-if="ocrResult.invoiceTitle" label="发票抬头">
            {{ ocrResult.invoiceTitle }}
          </Descriptions.Item>
          <Descriptions.Item v-if="ocrResult.supplierName" label="销售方(需核对)">
            <Tag color="orange">{{ ocrResult.supplierName }}</Tag>
          </Descriptions.Item>
          <Descriptions.Item v-if="ocrResult.price != null" label="来票金额(元)">
            {{ ocrResult.price }}
          </Descriptions.Item>
          <Descriptions.Item v-if="ocrResult.invoiceTime" label="开票日期">
            {{ ocrResult.invoiceTime }}
          </Descriptions.Item>
          <Descriptions.Item v-if="ocrResult.taxRate != null" label="税率(%)">
            {{ ocrResult.taxRate }}
          </Descriptions.Item>
          <Descriptions.Item v-if="ocrResult.remark" label="备注" :span="2">
            {{ ocrResult.remark }}
          </Descriptions.Item>
        </Descriptions>
      </div>
 
      <Alert
        message="销售方名称仅为发票文本识别结果,请在表单中选择采购订单后核对订单供应商与销售方是否一致;识别数据可手动修改。"
        type="info"
        show-icon
        class="mb-4"
      />
 
      <div class="flex justify-end gap-2">
        <Button @click="handleReset">重新上传</Button>
        <Button type="primary" @click="handleConfirm">确认并填入表单</Button>
      </div>
    </template>
  </Modal>
</template>