lisenzhou 2 vuotta sitten
vanhempi
commit
e9e26f1a43
2 muutettua tiedostoa jossa 163 lisäystä ja 22 poistoa
  1. 127
    0
      src/components/UploadFileList/index.jsx
  2. 36
    22
      src/pages/guarantee/edit/index.jsx

+ 127
- 0
src/components/UploadFileList/index.jsx Näytä tiedosto

@@ -0,0 +1,127 @@
1
+import React, { useCallback, useState } from "react";
2
+import { UploadOutlined } from "@ant-design/icons";
3
+import { Upload, Button, message } from "antd";
4
+import { uploadFile } from "@/services/rotationChart";
5
+
6
+export default (props) => {
7
+  const { value, onChange, preview, multiple = false, ...inputProps } = props;
8
+  const [loading, setLoading] = useState(false);
9
+
10
+  const onUpload = useCallback(
11
+    (e) => {
12
+      const { files } = e.target;
13
+      if (!files || files.length < 1) return;
14
+
15
+      setLoading(true);
16
+      const data = new FormData();
17
+      data.append("file", files[0]);
18
+
19
+      uploadFile(data)
20
+        .then((res) => {
21
+          setLoading(false);
22
+          if (typeof onChange === "function") {
23
+            if (multiple) {
24
+              onChange([...(value || []), { fileAddr: res.url }]);
25
+            } else {
26
+              onChange(value);
27
+            }
28
+          }
29
+        })
30
+        .catch((err) => {
31
+          setLoading(false);
32
+          // onError(err);
33
+        });
34
+    },
35
+    [onChange]
36
+  );
37
+
38
+  const onClick = () => {
39
+    const input = document.createElement("input");
40
+    input.type = "file";
41
+    Object.assign(input, inputProps);
42
+    input.addEventListener("change", onUpload);
43
+    // input.removeEventListener('change', onUpload);
44
+    input.click();
45
+  };
46
+
47
+  const uploadProps = {
48
+    name: "file",
49
+    fileList:
50
+      value?.map((x, index) => ({
51
+        uid: `uid-${index}`,
52
+        name: x?.url?.substring(x?.url?.lastIndexOf("/") + 1),
53
+        status: "dome",
54
+        ...x,
55
+      })) || [],
56
+    customRequest: ({ file, onError, onSuccess }) => {
57
+      const data = new FormData();
58
+      data.append("file", file);
59
+      setLoading(true);
60
+      uploadFile(data)
61
+        .then((res) => {
62
+          // setLoading(false);
63
+          console.log(res, "===---===");
64
+          onChange([
65
+            ...(value || []),
66
+            {
67
+              url: res.url,
68
+              name: res?.url?.substring(res?.url?.lastIndexOf("/") + 1),
69
+            },
70
+          ]);
71
+          // onSuccess(res.url);
72
+        })
73
+        .catch((err) => {
74
+          onError(err);
75
+        });
76
+
77
+      return {
78
+        abort() {
79
+          console.log("upload progress is aborted.");
80
+        },
81
+      };
82
+    },
83
+    onRemove: (e) => {
84
+      console.log(e, value, "123");
85
+
86
+      onChange(value.filter((x) => x.url !== e.url));
87
+    },
88
+  };
89
+
90
+  return (
91
+    <div>
92
+      <Upload {...uploadProps}>
93
+        <Button icon={<UploadOutlined />}>上传文件</Button>
94
+      </Upload>
95
+
96
+      {/* {value &&
97
+        preview &&
98
+        (typeof preview === "function" ? (
99
+          preview(value)
100
+        ) : multiple ? (
101
+          value?.map((x) => {
102
+            return (
103
+              <div style={{ marginTop: "0.7em" }} key={`${x?.fileAddr}`}>
104
+                <a
105
+                  href={`${x?.fileAddr}`}
106
+                  download
107
+
108
+                >
109
+                  {x?.fileAddr?.substring(x?.fileAddr?.lastIndexOf("/") + 1)}
110
+                </a>
111
+              </div>
112
+            );
113
+          })
114
+        ) : (
115
+          <div style={{ marginTop: "1em" }}>
116
+            <a
117
+              href={`${value}`}
118
+              download
119
+
120
+            >
121
+              {value?.substring(value?.lastIndexOf("/") + 1)}
122
+            </a>
123
+          </div>
124
+        ))} */}
125
+    </div>
126
+  );
127
+};

+ 36
- 22
src/pages/guarantee/edit/index.jsx Näytä tiedosto

@@ -13,6 +13,7 @@ import {
13 13
   savetCooperationUnits,
14 14
   updatetCooperationUnits,
15 15
 } from "@/services/cooperationUnits";
16
+import UploadFileList from "@/components/UploadFileList";
16 17
 
17 18
 export default (props) => {
18 19
   const [searchParams] = useSearchParams();
@@ -27,35 +28,41 @@ export default (props) => {
27 28
           ...res,
28 29
           timeRange: [res.startDate, res.endDate],
29 30
           orgType: res?.orgType?.split(","),
31
+          fileList:
32
+            res?.fileList?.map((x) => ({
33
+              url: x.fileAddr,
34
+              name: x.fileName,
35
+              uid: x.id,
36
+              ...x,
37
+            })) || null,
30 38
         });
31 39
       });
32 40
     }
33 41
   }, [id]);
34 42
 
35 43
   const onFinish = async (values) => {
36
-    console.log(values);
37 44
 
38
-    if (id) {
39
-      updatetCooperationUnits(id, {
40
-        ...values,
45
+    const { timeRange = [], ...otherValues } = values;
41 46
 
42
-        orgType: values.orgType ? values.orgType?.join(",") : null,
43
-        startDate: values.timeRange ? values.timeRange[0] : null,
44
-        endDate: values.timeRange ? values.timeRange[1] : null,
45
-      }).then((res) => {
46
-        navigate(-1);
47
-      });
48
-    } else {
49
-      savetCooperationUnits({
50
-        ...values,
47
+    savetCooperationUnits({
48
+      ...otherValues,
51 49
 
52
-        orgType: values.orgType ? values.orgType?.join(",") : null,
53
-        startDate: values.timeRange ? values.timeRange[0] : null,
54
-        endDate: values.timeRange ? values.timeRange[1] : null,
55
-      }).then((res) => {
56
-        navigate(-1);
57
-      });
58
-    }
50
+      orgType: values.orgType ? values.orgType?.join(",") : null,
51
+      startDate: timeRange[0] ? timeRange[0] : null,
52
+      endDate: timeRange[1] ? timeRange[1] : null,
53
+      fileList:
54
+        values?.fileList?.length > 0
55
+          ? values?.fileList?.map((x) => ({
56
+              fileName: x?.name,
57
+              fileAddr: x?.url,
58
+              id:x.id||null,
59
+              orgId:id ? Number(id) : null,
60
+            }))
61
+          : null,
62
+      id: id ? Number(id) : null,
63
+    }).then((res) => {
64
+      navigate(-1);
65
+    });
59 66
   };
60 67
 
61 68
   return (
@@ -84,12 +91,16 @@ export default (props) => {
84 91
             },
85 92
           }}
86 93
         >
87
-          <ProFormText name="name" label="机构名称" width={460}   rules={[{ required: true, message: '请输入机构名称' }]}/>
94
+          <ProFormText
95
+            name="name"
96
+            label="机构名称"
97
+            width={460}
98
+            rules={[{ required: true, message: "请输入机构名称" }]}
99
+          />
88 100
           <ProFormText
89 101
             name="businessLicense"
90 102
             label="工商注册证号"
91 103
             width={460}
92
-           
93 104
           />
94 105
           <ProFormText name="hygieneLicense" label="卫生许可证号" width={460} />
95 106
 
@@ -145,6 +156,9 @@ export default (props) => {
145 156
             label="机构内住宿供应能力(人)"
146 157
             width={460}
147 158
           />
159
+          <ProForm.Item name="fileList" label="文件">
160
+            <UploadFileList preview={true} multiple={true}></UploadFileList>
161
+          </ProForm.Item>
148 162
         </ProForm>
149 163
       </Card>
150 164
     </PageContainer>