AddTemplate.jsx 4.99 KB
Newer Older
皮倩雯's avatar
皮倩雯 committed
1 2 3 4 5
/* eslint-disable indent */
/*
 * @Description:
 * @Author: leizhe
 * @Date: 2022-04-07 10:23:26
6
 * @LastEditTime: 2022-05-19 18:41:38
皮倩雯's avatar
皮倩雯 committed
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
 * @LastEditors: leizhe
 */
import React, { useEffect, useState } from 'react';
import {
  Modal,
  Form,
  Input,
  notification,
  message,
  Upload,
  Drawer,
  Select,
  Space,
  Button,
  Row,
  Col,
} from 'antd';
import { SaveUpload } from '@/services/drawBoardManage/api';

const AddTemplate = props => {
  const { visible, treeData, pickItem, callBackSubmit = () => {} } = props;
  const [addTemplateForm] = Form.useForm();
  const [selectValue, setSelectValue] = useState('');
  const [file, setFile] = useState('');
  const { Option } = Select;

  useEffect(() => {
    console.log(pickItem);
    addTemplateForm.resetFields();
    if (pickItem) {
      addTemplateForm.setFieldsValue({ Type: pickItem.name });
      setSelectValue(pickItem.name);
    }
  }, [visible]);

  const submitAdd = () => {
    addTemplateForm.validateFields().then(validate => {
      if (validate) {
45 46
        let width = addTemplateForm.getFieldsValue().imageWidth.toString();
        let height = addTemplateForm.getFieldsValue().imageHeight.toString();
皮倩雯's avatar
皮倩雯 committed
47
        const formData = new FormData();
48 49 50 51
        formData.append('ModelName', addTemplateForm.getFieldsValue().ModelFile);
        formData.append('ModelType', selectValue);
        formData.append('width', width);
        formData.append('height', height);
皮倩雯's avatar
皮倩雯 committed
52 53 54
        formData.append('ModelFile', file);
        console.log(formData);
        SaveUpload(formData).then(res => {
55
          if (res.code === 0) {
皮倩雯's avatar
皮倩雯 committed
56 57 58 59
            callBackSubmit();
            notification.success({
              message: '提示',
              duration: 3,
60
              description: res.msg,
皮倩雯's avatar
皮倩雯 committed
61 62 63 64 65
            });
          } else {
            notification.error({
              message: '提示',
              duration: 3,
66
              description: res.msg,
皮倩雯's avatar
皮倩雯 committed
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
            });
          }
        });
      }
    });
  };

  const changTable = e => {
    console.log(e);
    setSelectValue(e);
  };

  const aa = {
    beforeUpload: file => {
      // const isPNG = file.type === 'image/png';
      // if (!isPNG) {
      //   message.error(`${file.name} 不是png类型的图片`);
      // } else {
      setFile(file);
      let reader = new FileReader();
      reader.readAsDataURL(file);
      reader.onload = () => {
        const image = new Image();
        image.src = reader.result;
        image.onload = () => {
          addTemplateForm.setFieldsValue({
            imageWidth: `${image.width}px`,
            imageHeight: `${image.height}px`,
            ModelFile: file.name,
            Name: file.name.substring(0, file.name.lastIndexOf('.')),
          });
        };
        // };
      };
      return Upload.LIST_IGNORE;
    },
    onChange: info => {
      console.log(info);
    },
  };

  return (
    <Drawer
      title="新增模型"
      visible={visible}
      width="500px"
      destroyOnClose
      {...props}
      footer={
        <Space>
          <Button onClick={submitAdd} type="primary">
            确定
          </Button>
        </Space>
      }
    >
      <Form form={addTemplateForm} labelCol={{ span: 5 }}>
        <Form.Item name="Type" label="模板类型" rules={[{ required: true, message: '不能为空' }]}>
          <Select
            placeholder="选择模板类型"
            onChange={changTable}
            style={{ marginLeft: '-3px' }}
            showSearch
          >
            {treeData
              ? treeData.map((item, index) => (
                  <Option key={item.id} value={item.name}>
                    {item.name}
                  </Option>
                ))
              : ''}
          </Select>
        </Form.Item>
        <Row>
          <Col span={17}>
            <Form.Item
              label="模型文件"
              name="ModelFile"
              labelCol={{ span: 7 }}
              rules={[
                {
                  required: true,
                  message: '请选择模型文件',
                },
              ]}
            >
153
              <Input placeholder="仅支持png和svg格式" disabled />
皮倩雯's avatar
皮倩雯 committed
154 155 156 157 158 159 160 161 162 163 164
            </Form.Item>
          </Col>
          <Col span={7}>
            <Form.Item>
              <Upload {...aa} accept=".png, .svg">
                <Button style={{ width: '130px' }}>上传图片</Button>
              </Upload>
            </Form.Item>
          </Col>
        </Row>
        <Form.Item label="模型名称" name="Name">
165
          <Input placeholder="菜单组名称" disabled />
皮倩雯's avatar
皮倩雯 committed
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
        </Form.Item>
        <Row>
          <Col span={13}>
            <Form.Item label="宽" name="imageWidth" labelCol={{ span: 9 }}>
              <Input style={{ width: '153px' }} disabled />
            </Form.Item>
          </Col>
          <Col span={11}>
            <Form.Item label="高" name="imageHeight" labelCol={{ span: 7 }}>
              <Input style={{ width: '146px' }} disabled />
            </Form.Item>
          </Col>
        </Row>
      </Form>
    </Drawer>
  );
};
export default AddTemplate;