From 4d785a21e36f21c75ee1a7361a071b330cf6b5dd Mon Sep 17 00:00:00 2001 From: gwbvipvip Date: Sat, 24 Feb 2024 17:12:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../deviceVesion/components/CreateForm.jsx | 131 ++++++++++++++++-- src/pages/device/deviceVesion/index.jsx | 52 ++++++- 2 files changed, 174 insertions(+), 9 deletions(-) diff --git a/src/pages/device/deviceVesion/components/CreateForm.jsx b/src/pages/device/deviceVesion/components/CreateForm.jsx index 130c31d..27a7d05 100644 --- a/src/pages/device/deviceVesion/components/CreateForm.jsx +++ b/src/pages/device/deviceVesion/components/CreateForm.jsx @@ -1,5 +1,5 @@ -import React from 'react'; -import { Modal, Form, Input, Button, Select,Upload} from 'antd'; +import React, { useRef, useState } from 'react'; +import { Modal, Form, Input, Button, Select, Upload, InputNumber,message } from 'antd'; import { PlusOutlined, UploadOutlined } from '@ant-design/icons'; import { GetCosRequestURL } from '../services'; import axios from 'axios'; @@ -8,7 +8,7 @@ import axios from 'axios'; const CreateForm = (props) => { props.form.setFieldsValue(props.values); const { Option, OptGroup } = Select; - const uploadProp = { + const uploadProp1 = { name: 'file', showUploadList: false, multiple: false, @@ -52,7 +52,7 @@ const CreateForm = (props) => { }).then(response => { console.log('onSuccess', response, file); if (response.status == 200 || response.statusText == 'OK') { - props.form.setFieldsValue({ templatePath: r.seeUrl }); + props.form.setFieldsValue({ productUrl: r.seeUrl }); } // onSuccess(response, file); }) @@ -69,6 +69,91 @@ const CreateForm = (props) => { }; + const uploadProp = { + name: 'file', + showUploadList: true, + multiple: false, + accept: '.png, .jpg, .jpeg, .gif', + // 这里需要指定文件上传的content-type + headers: { + 'Content-Type': 'application/octet-stream', + }, + customRequest({ + action, + file, + headers, + onError, + onProgress, + withCredentials, + }) { + //覆盖action 上传之前获取上传地址 + var index = file.name.lastIndexOf("."); + var ext = file.name.substr(index + 1); + let fileData = null; + GetCosRequestURL({ directory: "applet", fileExtension: ext, method: "PUT" }).then((r) => { + action = r.allUrl; + const reader = new FileReader(); + reader.readAsArrayBuffer(file); + reader.onload = (e) => { + fileData = e.target.result; + axios.put(action, fileData, { + withCredentials, + headers, + onUploadProgress: ({ total, loaded }) => { + onProgress({ percent: Math.round(loaded / total * 100).toFixed(2) }, file); + }, + }).then(response => { + + if (response.status == 200 || response.statusText == 'OK') { + debugger + const tempUploadImage = JSON.parse(JSON.stringify(props.currentImage)); + tempUploadImage.materialPath=[]; + tempUploadImage.materialPath.push(r.seeUrl); + props.setCurrentImage(tempUploadImage); + // props.form.setFieldsValue({ productUrl: r.seeUrl }); + message.success('上传成功!'); + } + }).catch(onError); + }; + }); + return { + abort() { + console.log('upload progress is aborted.'); + }, + }; + }, + }; + + const uploadButton = ( +
+ +
+ Upload +
+
+ ); + + const handlePreview = async (file) => { + if (!file.url && !file.preview) { + file.preview = await getBase64(file.originFileObj); + } + setPreviewImage(file.url || file.preview); + setPreviewVisible(true); + setPreviewTitle(file.name || file.url.substring(file.url.lastIndexOf('/') + 1)); + }; + + const handleChange = ({ fileList: newFileList }) => { + debugger + newFileList.forEach(item => { + item.status = 'done' + }); + props.setFileList(newFileList) + }; + return ( { - - {props.dicData.map((item, index) => { return ( @@ -102,10 +187,40 @@ const CreateForm = (props) => { })} - + - + + + + + { + const findIndex = props.fileList.findIndex(item => item.uid === remove.uid); + if (findIndex > -1) { + const tempUpload = JSON.parse(JSON.stringify(props.currentImage)); + tempUpload.materialPath.splice(findIndex, 1); + props.setCurrentImage(tempUpload); + } + }} + {...uploadProp} + > + { + (() => { + if (props.fileList.length >= 1) { + return null; + } else { + return uploadButton + } + })() + } + + +