Commit 783497cd12f2116cb52981da7b7f7da8043d87cf

Authored by zhongnanhuang
1 parent 100628a3

feat: update 发票对账

src/pages/Invoice/components/BankImportModal.tsx
1 1 import { RESPONSE_CODE } from '@/constants/enum';
  2 +import { blobToJson } from '@/utils';
2 3  
3 4 import { UploadOutlined } from '@ant-design/icons';
4 5 import { ModalForm } from '@ant-design/pro-components';
... ... @@ -70,31 +71,36 @@ export default ({ setVisible, onClose }) => {
70 71 data: formData,
71 72 })
72 73 .then((response) => {
73   - if (response.data?.result === RESPONSE_CODE.SUCCESS) {
74   - message.success(response.data?.message);
  74 + let data = response.data;
  75 + if (data.type === 'application/json') {
  76 + blobToJson(data).then((dataJson) => {
  77 + if (dataJson?.result === RESPONSE_CODE.SUCCESS) {
  78 + message.success(dataJson?.message);
  79 + } else {
  80 + message.error(dataJson?.message);
  81 + }
  82 + });
75 83 } else {
76   - if (response.data?.message) {
77   - message.error(response.data?.message);
78   - } else {
79   - // 创建一个新的 Blob 对象,它包含了服务器响应的数据(即你的 Excel 文件)
80   - const blob = new Blob([response.data]); // Excel 的 MIME 类型
81   - const downloadUrl = window.URL.createObjectURL(blob);
82   - const a = document.createElement('a');
83   - a.href = downloadUrl;
84   - a.download = '银行流水导入模板.xlsx'; // 你可以为文件命名
85   - document.body.appendChild(a);
86   - a.click(); // 模拟点击操作来下载文件
87   - URL.revokeObjectURL(downloadUrl); // 释放掉 blob 对象所占用的内存
88   - document.body.removeChild(a);
89   - }
  84 + message.error('上传失败,已下载错误信息表格');
  85 + // 创建一个新的 Blob 对象,它包含了服务器响应的数据(即你的 Excel 文件)
  86 + const blob = new Blob([response.data]); // Excel 的 MIME 类型
  87 + const downloadUrl = window.URL.createObjectURL(blob);
  88 + const a = document.createElement('a');
  89 + a.href = downloadUrl;
  90 + a.download = '银行流水导入模板.xlsx'; // 你可以为文件命名
  91 + document.body.appendChild(a);
  92 + a.click(); // 模拟点击操作来下载文件
  93 + URL.revokeObjectURL(downloadUrl); // 释放掉 blob 对象所占用的内存
  94 + document.body.removeChild(a);
90 95 }
91 96 })
92 97 .catch((error) => {
93 98 // 处理错误
94 99 message.error('系统出现异常了,请联系管理员', error);
  100 + })
  101 + .finally(() => {
  102 + setUploading(false);
95 103 });
96   -
97   - setUploading(false);
98 104 };
99 105 const props: UploadProps = {
100 106 onRemove: (file) => {
... ...
src/utils/index.ts
... ... @@ -260,11 +260,37 @@ function FloatSub(arg1: any, arg2: any) {
260 260 return ((arg1 * m - arg2 * m) / m).toFixed(n);
261 261 }
262 262  
  263 +async function blobToJson(blob: any) {
  264 + return new Promise((resolve, reject) => {
  265 + const reader = new FileReader();
  266 +
  267 + // 设置读取完成的事件处理程序
  268 + reader.onload = function (event) {
  269 + try {
  270 + const jsonString = event?.target?.result;
  271 + const jsonObject = JSON.parse(jsonString);
  272 + resolve(jsonObject);
  273 + } catch (error) {
  274 + reject(error);
  275 + }
  276 + };
  277 +
  278 + // 设置读取出错的事件处理程序
  279 + reader.onerror = function (error) {
  280 + reject(error);
  281 + };
  282 +
  283 + // 开始读取Blob对象
  284 + reader.readAsText(blob, 'UTF-8');
  285 + });
  286 +}
  287 +
263 288 export {
264 289 FloatAdd,
265 290 FloatSub,
266 291 appendFormData,
267 292 blobToFile,
  293 + blobToJson,
268 294 copyToClipboard,
269 295 customMessage,
270 296 dataURItoBlob,
... ...