EditModal.jsx 9.92 KB
Newer Older
1
import React, { useState, useEffect } from 'react';
2
import SiteModal from '@/components/Modal/SiteModa';
3
import { Form, Input, notification, Select, Row, Col } from 'antd';
4

mayongxin's avatar
mayongxin committed
5 6
const { Item } = Form;
const { TextArea } = Input;
7
const EditModal = props => {
8
  const { option, visible, messageVersion } = props;
9

10 11 12 13 14 15 16 17 18
  const [form] = Form.useForm();
  const [templateName, setTemplateName] = useState([]);
  const [loading, setLoading] = useState(false);
  const [flag, setFlag] = useState(0);
  const onSubmit = () => {
    form.submit();
  };
  const onSubmitSuccess = () => {
    const result = form.getFieldValue();
19 20 21 22 23
    if (result.type == 'APP' || result.type == 'WEB') {
      delete result.third_id;
      delete result.third_name;
      delete result.weixin;
    }
24
    props.onSubmit & props.onSubmit({ Id: props.template.Id, ...result });
25
    form.resetFields();
26
  };
皮倩雯's avatar
皮倩雯 committed
27

28
  useEffect(() => {
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    if (visible) {
      form.setFieldsValue({
        name: props.template.name,
        type: props.template.type,
        third_name: props.template.third_name == '-' ? '' : props.template.third_name,
        third_id: props.template.third_id == '-' ? '' : props.template.third_id,
        weixin: props.template.weixin == '-' ? '' : props.template.weixin,
        params: props.template.template_params1 == '-' ? '' : props.template.template_params1,
        param1: props.template.template_params2 == '-' ? '' : props.template.template_params2,
        desc: props.template.desc == '-' ? '' : props.template.desc,
        analysis_params:
          props.template.analysis_params == '-' ? '' : props.template.analysis_params,
      });
      setTimeout(() => {
        console.log(form.getFieldsValue().type);
        setFlag(flag + 1);
      }, 0);
皮倩雯's avatar
皮倩雯 committed
46

47 48 49
      if (option) {
        setTemplateName(option.filter(item => item.Type === props.template.type));
      }
shaoan123's avatar
shaoan123 committed
50
    }
51
  }, [props.template, visible]);
皮倩雯's avatar
皮倩雯 committed
52

53 54 55 56 57 58 59 60 61
  const layout = {
    layout: 'horizontal',
    labelCol: {
      span: 10,
    },
    wrapperCol: {
      span: 19,
    },
  };
皮倩雯's avatar
皮倩雯 committed
62

63
  const onChangeType = value => {
64
    form.setFieldsValue({ third_name: '', third_id: '', weixin: '' });
65 66 67 68 69 70 71 72 73
    console.log(option.filter(item => item.Type === value), '123');
    setTemplateName(option.filter(item => item.Type === value));
  };
  const onChange = (value, options) => {
    form.setFieldsValue({
      third_id: options.code,
    });
    // setTemplateName(option.filter(item => item.Type === value))
  };
74

75 76 77 78 79
  const onChangeType1 = value => {
    form.setFieldsValue({
      third_id: value,
    });
  };
80

81 82 83
  return (
    <SiteModal
      {...props}
84 85 86 87 88 89 90 91 92
      title={
        <span>
          <span style={{ marginRight: '20px' }}>
            <span>模板编辑</span>
          </span>
          <span style={{ fontSize: '14px' }}>当前解析版本规则</span>
          <span style={{ color: 'rgb(24, 144, 255)' }}>{messageVersion}</span>
        </span>
      }
93 94 95 96
      bodyStyle={{ width: '100%', minHeight: '100px' }}
      style={{ top: 200, borderRadius: '20px' }}
      width="820px"
      destroyOnClose
97 98 99
      afterClose={() => {
        form.resetFields();
      }}
100 101 102 103 104
      cancelText="取消"
      okText="确认"
      onOk={() => onSubmit()}
      confirmLoading={loading}
    >
105
      <div style={{ height: '500px', overflowY: 'scroll', overflowX: 'hidden' }}>
106
        <Form form={form} {...layout} onFinish={onSubmitSuccess}>
107 108 109
          <Row>
            <Col span={3} />
            <Col span={12}>
110 111 112
              <Item
                label="模板名称"
                name="name"
113 114
                labelCol={{ span: 6 }}
                wrapperCol={{ span: 18 }}
115 116 117 118 119 120 121
                rules={[
                  {
                    required: true,
                    message: '请输入模板名称',
                  },
                ]}
              >
122
                <Input style={{ width: '80%' }} placeholder="请输入模板名称" />
123 124
              </Item>
            </Col>
125
            <Col span={9}>
126 127 128
              <Item
                label="模板类型"
                name="type"
129 130
                labelCol={{ span: 8 }}
                wrapperCol={{ span: 16 }}
131 132 133 134 135 136 137
                rules={[
                  {
                    required: true,
                    message: '请选择模板类型',
                  },
                ]}
              >
138
                <Select style={{ width: '85%' }} onChange={value => onChangeType(value)}>
139 140
                  <Select.Option value="公众号">公众号</Select.Option>
                  <Select.Option value="短信">短信</Select.Option>
141 142
                  <Select.Option value="APP">APP</Select.Option>
                  <Select.Option value="WEB">WEB</Select.Option>
143 144 145 146 147
                  <Select.Option value="企业微信">企业微信</Select.Option>
                </Select>
              </Item>
            </Col>
          </Row>
148 149
          {form.getFieldsValue().type === 'APP' || form.getFieldsValue().type === 'WEB' ? (
            <></>
150 151
          ) : (
            <>
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
              {form.getFieldsValue().type !== '企业微信' ? (
                <>
                  <Row>
                    <Col span={12}>
                      <Item
                        label="第三方模板名称"
                        name="third_name"
                        wrapperCol={{ span: 12 }}
                        labelCol={{ span: 12 }}
                        rules={[
                          {
                            required: true,
                            message: '请选择第三方模板名称',
                          },
                        ]}
                      >
                        {/* <Input placeholder="请输入模板名称" /> */}
                        <Select
                          placeholder="请选择模板名称"
                          style={{ width: '120%' }}
                          onChange={(value, option) => onChange(value, option)}
                        >
                          {templateName &&
                            templateName.length > 0 &&
                            templateName.map((item, index) => (
                              <Select.Option value={item.Name} key={index} code={item.Code}>
                                {item.Name}
                              </Select.Option>
                            ))}
                        </Select>
                      </Item>
                    </Col>
                    <Col span={12}>
                      <Item
                        label="第三方模板编号"
                        name="third_id"
                        wrapperCol={{ span: 12 }}
                        labelCol={{ span: 12 }}
                        rules={[
                          {
                            required: true,
                            message: '请输入第三方模板编号',
                          },
                        ]}
                      >
                        <Input placeholder="请输入第三方模板编号" style={{ width: '85%' }} />
                      </Item>
                    </Col>
                  </Row>
                </>
              ) : (
                <>
                  <Row>
                    <Col span={12}>
                      <Item
                        label="第三方模板名称"
                        wrapperCol={{ span: 12 }}
                        labelCol={{ span: 12 }}
                        name="third_name"
                        rules={[
                          {
                            required: true,
                            message: '请选择第三方模板名称',
                          },
                        ]}
                      >
                        <Select
                          style={{ width: '120%' }}
                          onChange={value => onChangeType1(value)}
                          placeholder="请选择模板名称"
                        >
                          <Select.Option value="普通文本">普通文本</Select.Option>
                          <Select.Option value="文字卡片">文字卡片</Select.Option>
                          <Select.Option value="图片消息">图片消息</Select.Option>
                          <Select.Option value="图片">图片</Select.Option>
                        </Select>
                      </Item>
                    </Col>
                    <Col span={12}>
                      <Item
                        label="企业微信应用id"
                        wrapperCol={{ span: 12 }}
                        labelCol={{ span: 12 }}
                        name="weixin"
                        rules={[
                          {
                            required: true,
                            message: '请输入企业微信号',
                          },
                        ]}
                      >
                        <Input placeholder="请输入企业微信号" style={{ width: '160px' }} />
                      </Item>
                    </Col>
                  </Row>
                </>
              )}
249 250
            </>
          )}
251 252 253
          <Item label="模板参数(仅针对2.0规则)" labelCol={{ span: 6 }} name="params">
            <TextArea rows={2} style={{ width: '95%' }} placeholder="first|Second|Third|Four" />
          </Item>
254
          <Item label="解析参数(仅针对2.0规则)" labelCol={{ span: 6 }} name="param1">
255 256 257 258 259 260 261 262 263
            <TextArea rows={2} style={{ width: '95%' }} placeholder="first|Second|Third|Four" />
          </Item>
          <Item label="参数说明" labelCol={{ span: 6 }} name="desc">
            <TextArea
              style={{ width: '95%' }}
              rows={4}
              placeholder="first: 标题信息|Second: 展示内容|Third: 时间|Four: 备注信息"
            />
          </Item>
264
          <Item label="解析规则版本(仅针对1.0规则)" labelCol={{ span: 6 }} name="analysis_params">
265 266
            <TextArea style={{ width: '95%' }} rows={2} placeholder="param1|param2|param3|param4" />
          </Item>
267 268 269 270 271 272
        </Form>
      </div>
    </SiteModal>
  );
};
export default EditModal;