WeChatConfig.jsx 8.06 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
import React, { useState, useEffect, useRef } from 'react';
import { Form, Input, Select, Space, TreeSelect, Empty, Switch, Button, message } from 'antd';
import { FolderFilled, FileOutlined, MobileOutlined } from '@ant-design/icons';
import { AddWechatStatus } from '@/services/messagemanage/messagemanage';
const { Option } = Select;
const { TextArea } = Input;
const { TreeNode } = TreeSelect;
const typeList = [{ name: '报警通知', value: 1 }, { name: '运行简报', value: 2 }];

const WeChatConfig = props => {
邓超's avatar
邓超 committed
11
  const { menuMoblieList, formValue, id, template } = props;
12 13 14 15 16 17 18 19
  const [form] = Form.useForm();
  useEffect(() => {
    getFormData();
  }, []);

  const getFormData = () => {
    console.log(formValue, 'formValue');
    if (formValue) {
邓超's avatar
邓超 committed
20
      form.setFieldsValue({ ...formValue, WechatStatus: formValue.WechatStatus === 1 });
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
    }
  };
  // 监听表单
  const changeValue = (changedFields, allFields) => {
    // console.log(Object.keys(changedFields), allFields, 'changedFields');
    if (Object.keys(changedFields)[0] === 'WechatContent') {
      let regex = /\{@(.+?)\}/g;
      let originalList = allFields.lines || [];
      let list = changedFields.WechatContent.match(regex)?.map(item => {
        let Name = item.substring(2, item.length - 1);
        let orgItem = originalList.find(ele => ele.Name === Name);
        return {
          Name,
          Description: orgItem ? orgItem.Description : '',
          DefaultValue: orgItem ? orgItem.DefaultValue : '',
        };
      });
      if (list) {
        form.setFieldsValue({ lines: list });
      } else {
        form.setFieldsValue({ lines: [] });
      }
    }
邓超's avatar
邓超 committed
44 45 46 47 48 49 50 51 52 53 54 55 56
    if (Object.keys(changedFields)[0] === 'WechatType') {
      let content = template.find(item => item.id === changedFields.WechatType);
      let regex = /\{@(.+?)\}/g;
      let list = content.Param.match(regex)?.map(item => {
        let Name = item.substring(2, item.length - 1);
        return {
          Name,
          Description: '',
          DefaultValue: '',
        };
      });
      form.setFieldsValue({ WechatContent: content.Param, lines: list, WechatCode: content.Code });
    }
57 58 59 60 61 62 63 64 65 66 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
  };
  const mapAppTree = org => {
    const haveChildren = Array.isArray(org.children) && org.children.length > 0;
    let icon;
    let value = org.id;

    if (org.menuType === 'MiniAppsingleStation') {
      value = org.stationID;
      icon = <MobileOutlined />;
    }
    if (org.menuType === 'MiniAppMenuGroup' || org.menuType === '"MiniAppMenuGroupTwo"') {
      value = org.id;
      icon = <FolderFilled />;
    }
    if (org.menuType === 'MiniAppMenuThree' || org.menuType === 'MiniAppMenu') {
      icon = <FileOutlined style={{ color: '#1890ff' }} />;
      value = org.pageUrl || org.id;
    }

    return (
      <TreeNode
        value={value}
        title={org.text}
        icon={icon}
        key={value}
        disabled={org.menuType !== 'MiniAppMenuThree' && org.menuType !== 'MiniAppMenu'}
      >
        {haveChildren ? org.children.map(item => mapAppTree(item)) : null}
      </TreeNode>
    );
  };
  const onFinish = () => {
    if (!id) {
      message.error('请保存基础信息');
      return;
    }
93 94 95 96 97 98 99 100 101 102 103 104 105
    form.validateFields().then(values => {
      let value = JSON.parse(JSON.stringify(values));
      AddWechatStatus({
        ...value,
        WechatStatus: value.WechatStatus ? 1 : 0,
        id,
      }).then(res => {
        if (res.code === 0) {
          message.success('保存成功');
        } else {
          message.error(res.msg);
        }
      });
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
    });
  };
  return (
    <div>
      <Form
        form={form}
        labelCol={{ span: 2 }}
        wrapperCol={{ span: 22 }}
        onValuesChange={changeValue}
        labelAlign="left"
      >
        <div style={{ display: 'flex' }}>
          <Form.Item
            label="消息类型"
            name="WechatType"
            labelCol={{ span: 4 }}
            wrapperCol={{ span: 20 }}
            style={{ width: '400px', marginRight: '15px', display: 'block' }}
          >
邓超's avatar
邓超 committed
125 126 127 128
            <Select placeholder="请选择消息类型">
              {template.map(item => (
                <Option value={item.id} key={item.id}>
                  {item.Name}
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
                </Option>
              ))}
            </Select>
          </Form.Item>
          <Form.Item
            label="模板编号"
            name="WechatCode"
            style={{ width: '400px', display: 'block' }}
            labelCol={{ span: 4 }}
            wrapperCol={{ span: 20 }}
          >
            <Input placeholder="请填写消息标题" />
          </Form.Item>
          <Form.Item
            label="是否开启"
邓超's avatar
邓超 committed
144
            name="WechatStatus"
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 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
            valuePropName="checked"
            style={{ width: '400px', display: 'block' }}
            labelCol={{ span: 4 }}
            wrapperCol={{ span: 20 }}
          >
            <Switch checkedChildren="是" unCheckedChildren="否" />
          </Form.Item>
        </div>
        <Form.Item label="跳转路径" name="WechatUrl" style={{ display: 'block', width: '1200px' }}>
          <Input placeholder="请填写跳转路径" />
          {/* <TreeSelect
            showSearch
            treeNodeFilterProp="title"
            treeNodeLabelProp="value"
            dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
            placeholder="请选择功能路径"
            allowClear
            treeDefaultExpandAll
            treeIcon
          >
            {menuMoblieList ? (
              menuMoblieList.map(item => mapAppTree(item))
            ) : (
              <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
            )}
          </TreeSelect> */}
        </Form.Item>
        <Form.Item label="内容" name="WechatContent" style={{ display: 'block', width: '1200px' }}>
          <TextArea rows={6} placeholder="请输入内容" />
        </Form.Item>
        <Form.List name="lines">
          {fields => (
            <>
              {fields.map(({ key, name, ...restField }) => (
                <Space
                  key={key}
                  style={{
                    display: 'flex',
                    marginBottom: 8,
                  }}
                  align="baseline"
                >
                  <Form.Item
                    {...restField}
                    label="参数名"
                    name={[name, 'Name']}
                    labelCol={{ span: 6 }}
                    wrapperCol={{ span: 18 }}
                  >
                    <Input readOnly />
                  </Form.Item>
                  <Form.Item
                    {...restField}
                    name={[name, 'Description']}
                    label="含义"
                    labelCol={{ span: 6 }}
                    wrapperCol={{ span: 18 }}
202 203 204 205 206 207
                    rules={[
                      {
                        required: true,
                        message: '请填写含义',
                      },
                    ]}
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
                  >
                    <Input placeholder="请填写含义" />
                  </Form.Item>
                  <Form.Item
                    {...restField}
                    name={[name, 'DefaultValue']}
                    label="默认值"
                    labelCol={{ span: 6 }}
                    wrapperCol={{ span: 18 }}
                    // rules={[
                    //   {
                    //     required: true,
                    //     message: '请填写默认值',
                    //   },
                    // ]}
                  >
                    <Input placeholder="请填写默认值" />
                  </Form.Item>
                  {/* <MinusCircleOutlined onClick={() => remove(name)} /> */}
                </Space>
              ))}
              {/* <Form.Item>
            <Button type="dashed" onClick={() => add()} block icon={<PlusOutlined />}>
              Add field
            </Button>
          </Form.Item> */}
            </>
          )}
        </Form.List>
        <Form.Item>
          <Button type="primary" onClick={onFinish}>
            保存
          </Button>
        </Form.Item>
      </Form>
    </div>
  );
};

export default WeChatConfig;