EditTemplate.jsx 5.61 KB
Newer Older
皮倩雯's avatar
皮倩雯 committed
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
import React, { useEffect, useState } from 'react';
import {
  Modal,
  Form,
  Input,
  notification,
  message,
  Upload,
  Drawer,
  Select,
  Space,
  Button,
  Row,
  Col,
} from 'antd';
import { SaveUpload, UpdateModelInfo } from '@/services/drawBoardManage/api';

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

  useEffect(() => {
    addTemplateForm.resetFields();
    console.log(pickItem);
    console.log(obj);
29 30 31 32
    if (obj.Path) {
      let index = obj.Path.lastIndexOf('\\');
      let ab = obj.Path.substring(index + 1, obj.Path.length);
      let a = JSON.parse(obj.Json);
皮倩雯's avatar
皮倩雯 committed
33 34 35 36 37 38 39 40 41 42 43 44 45
      console.log(a);
      let aa;
      let bb;
      if (a) {
        if (a.width.charAt(a.width.length - 1) == 'x') {
          aa = `${a.width}`;
          bb = `${a.height}`;
        } else {
          aa = `${a.width}px`;
          bb = `${a.height}px`;
        }
      }
      addTemplateForm.setFieldsValue({
46 47
        Name: obj.ModelName,
        Type: obj.ModelTypeName,
皮倩雯's avatar
皮倩雯 committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
        ModelFile: ab,
        imageWidth: aa,
        imageHeight: bb,
      });
    }
  }, [visible]);

  const submitAdd = () => {
    addTemplateForm.validateFields().then(validate => {
      if (validate) {
        let aa = {};
        aa.width = addTemplateForm.getFieldsValue().imageWidth.toString();
        aa.height = addTemplateForm.getFieldsValue().imageHeight.toString();
        console.log(aa);
        console.log(file);
        const formData = new FormData();
64 65
        formData.append('ModelName', addTemplateForm.getFieldsValue().ModelFile);
        formData.append('ModelType', obj.ModelTypeName);
皮倩雯's avatar
皮倩雯 committed
66 67
        formData.append('width', aa.width);
        formData.append('height', aa.height);
68
        formData.append('ID', obj.ID);
皮倩雯's avatar
皮倩雯 committed
69 70 71 72 73 74 75 76 77 78 79
        formData.append('ModelFile', file);
        // if (obj.realModal != 0) {
        //   formData.append('RelModel', obj.realModel);
        // }
        console.log(formData);
        UpdateModelInfo(formData).then(res => {
          if (res.code === 0) {
            callBackSubmit();
            notification.success({
              message: '提示',
              duration: 3,
80
              description: res.msg,
皮倩雯's avatar
皮倩雯 committed
81 82 83 84 85
            });
          } else {
            notification.error({
              message: '提示',
              duration: 3,
86
              description: res.msg,
皮倩雯's avatar
皮倩雯 committed
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
            });
          }
        });
      }
    });
  };
  const changTable = 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,
114
            Name: file.name.substring(0, file.name.lastIndexOf('.')),
皮倩雯's avatar
皮倩雯 committed
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
          });
        };
        // };
      };
      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
            disabled
          >
            {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: '请选择模型文件',
                },
              ]}
            >
172
              <Input placeholder="仅支持png和svg格式" disabled />
皮倩雯's avatar
皮倩雯 committed
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
            </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">
          <Input placeholder="请输入菜单组名称" disabled />
        </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 EditTemplate;