广州矛调粤政易端
xusd
7 days ago d27794814b69d18aeb8ee96a46cae91d5613570c
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
/*
 * @Company: hugeInfo
 * @Author: ldh
 * @Date: 2022-09-16 18:58:01
 * @LastEditTime: 2024-12-04 11:12:44
 * @LastEditors: lwh
 * @Version: 1.0.0
 * @Description: 文件上传
 */
import React, { useRef } from 'react';
import { ax, showToast } from '../../utils/utility';
 
function uploadFileApi(submitData, ownerId, ownerType) {
  return ax.request({ url: `attachment/uploadFile?ownerId=${ownerId}&ownerType=${ownerType}`, type: 'post', data: submitData, service: 'ninex' });
}
 
// ocr身份证识别
function OCRFileApi(submitData) {
  return ax.request({ url: `ocrIdcard`, type: 'post', data: submitData, service: 'ocr' });
}
 
const UploadFile = ({ children, ownerId, ownerType, OCRshow, accept = 'image/*', capture, maxCount = 9, onSuccessCallback, disabled = false }) => {
  const fileRef = useRef();
 
  let _maxCount = OCRshow ? 1 : parseInt(maxCount);
 
  // 文件上传
  function handleUploadFile(e) {
    let files = e.target.files;
    if (files.length > _maxCount) {
      showToast({ content: `只可同时上传${_maxCount}个文件` });
      return;
    }
    if (_maxCount === 1) {
      let file = e.target.files[0] || {};
      let formdata = new FormData();
      formdata.append('files', file);
      uploadFile(formdata);
    } else {
      let submit = [];
      for (let i = 0; i < files.length; i++) {
        let formdata = new FormData();
        formdata.append('files', files[i]);
        submit.push(formdata);
      }
      uploadFiles(submit);
    }
  }
 
  function onPreview(params) {
    console.log(params);
  }
 
  // 点击上传
  function handleClickUpload() {
    fileRef.current.click();
  }
 
  // 上传文件
  async function uploadFile(submitData) {
    global.setSpinning(true);
    const res = OCRshow ? await OCRFileApi(submitData) : await uploadFileApi(submitData, ownerId, ownerType);
    global.setSpinning(false);
    if (res.type) {
      showToast({ type: 'success', content: OCRshow ? '识别成功' : '上传成功' });
      onSuccessCallback && onSuccessCallback(res.data);
 
    }
    // fileRef.current.value = '';
  }
 
  // 多个文件上传
  async function uploadFiles(submitData) {
    global.setSpinning(true);
    let apiArr = submitData.map((x) => uploadFileApi(x, ownerId, ownerType));
    ax.ax.all(apiArr).then(
      ax.ax.spread(function (...res) {
        global.setSpinning(false);
        let successNum = 0,
          failNum = 0,
          resData = [];
        res.forEach((x) => {
          if (x.type) {
            successNum = successNum + 1;
            resData.push(x.data[0]);
          } else {
            failNum = failNum + 1;
          }
        });
        showToast({ content: `上传成功${successNum},失败${failNum}` });
        onSuccessCallback && onSuccessCallback(resData);
      })
    );
    fileRef.current.value = '';
  }
 
  return (
    <>
      <input
        type="file"
        name="file"
        disabled={disabled}
        ref={fileRef}
        accept="image/gif,image/jpeg"
        capture={capture}
        onChange={handleUploadFile}
        onPreview={onPreview}
        style={{ display: 'none' }}
        multiple={_maxCount > 1 ? true : false}
 
      />
      <div onClick={handleClickUpload}>{children}</div>
    </>
  );
};
 
export default UploadFile;