PlatformConfig.jsx 14.2 KB
Newer Older
1
/* eslint-disable global-require */
邓超's avatar
邓超 committed
2
import '@wangeditor/editor/dist/css/style.css'; // 引入 css
3 4 5 6
import React, { useState, useEffect, useRef } from 'react';
import { Form, Input, Select, Space, TreeSelect, Empty, Switch, Button, message } from 'antd';
import ReactQuill from 'react-quill';
import 'react-quill/dist/quill.snow.css';
邓超's avatar
邓超 committed
7 8
import { Editor, Toolbar } from '@wangeditor/editor-for-react';
import { IDomEditor, IEditorConfig, IToolbarConfig } from '@wangeditor/editor';
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 45 46 47
import {
  DeleteOutlined,
  PictureOutlined,
  DesktopOutlined,
  FolderFilled,
  FileOutlined,
  MobileOutlined,
} from '@ant-design/icons';
import { AddPlatformStatus } from '@/services/messagemanage/messagemanage';
import { GetMessageIcon } from '@/services/messagemanage/messagemanage';
import styles from './PlatformConfig.less';
import PreviewModal from '../../schemeDetail/PreviewModal';
const { Option } = Select;
const { TreeNode } = TreeSelect;
const typeList = [
  { name: '通用报警', value: 1 },
  { name: '工单通知', value: 2 },
  { name: '系统通知', value: 3 },
  { name: '自定义', value: 4 },
];
const modules = {
  // toolbar: { container: '#toolbar' },
  toolbar: [
    [{ size: ['small', false, 'large', 'huge'] }], // custom dropdown
    [{ header: [1, 2, 3, 4, 5, 6, false] }],
    [{ color: [] }, { background: [] }],
    [{ font: [] }],
    [{ align: [] }],
    ['bold', 'italic', 'underline', 'strike', 'blockquote'],
    [{ list: 'ordered' }, { list: 'bullet' }, { indent: '-1' }, { indent: '+1' }],
    ['clean'], // 清空
  ],
};
const defaultWebIcon = require('../../../../../assets/images/icons/imgNone.png'); // 默认图标
const PlatformConfig = props => {
  const { menuWebList, menuMoblieList, formValue, id } = props;
  const [iconMaskShow, setIconMaskShow] = useState(false);
  const [previewModal, setPreviewModal] = useState(false); // 图片选择
  const [imgDataList, setImgDataList] = useState([]);
邓超's avatar
邓超 committed
48 49 50 51 52 53 54 55 56 57 58
  const [editor, setEditor] = useState(null);
  // 编辑器内容
  const [html, setHtml] = useState();
  const toolbarConfig = {};
  const editorConfig = {
    placeholder: '请输入内容...',
    MENU_CONF: {
      uploadImage: {},
      lineHeightList: ['1', '1.5', '2', '2.5'],
    },
  };
59 60 61 62 63 64 65 66 67 68 69 70 71
  const [form] = Form.useForm();
  useEffect(() => {
    getMessageIcon();
    getFormData();
  }, []);
  const getFormData = () => {
    console.log(formValue, 'formValue');
    if (formValue) {
      form.setFieldsValue({
        ...formValue,
        PlatformIcon: `${window.location.origin}/${formValue.PlatformIcon}`,
        PlatformStatus: formValue.PlatformStatus === 1,
      });
邓超's avatar
邓超 committed
72
      setHtml(formValue.PlatformContent);
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
      if (!formValue?.PlatformIcon) {
        form.setFieldsValue({ PlatformIcon: defaultWebIcon });
      }
    }
  };
  // 获取图片库
  const getMessageIcon = () => {
    GetMessageIcon().then(res => {
      setImgDataList(res.data);
    });
  };
  const callBackSubmit = value => {
    console.log(value, 'lvaue');
    form.setFieldsValue({ PlatformIcon: `${window.location.origin}/${value}` });
  };
  // 监听表单
  const changeValue = (changedFields, allFields) => {
邓超's avatar
邓超 committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    // if (Object.keys(changedFields)[0] === 'PlatformContent') {
    //   let regex = /\{@(.+?)\}/g;
    //   let originalList = allFields.lines || [];
    //   let list = changedFields.PlatformContent.getHtml()
    //     .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: [] });
    //   }
    // }
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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
  };
  const mapTree = org => {
    const haveChildren = Array.isArray(org.children) && org.children.length > 0;
    let icon;
    let value = org.id;
    if (org.menuType === 'Web4singleStation') {
      value = org.stationID;
      icon = <DesktopOutlined />;
    }
    if (org.menuType === 'Web4MenuGroup') {
      value = org.id;
      icon = <FolderFilled />;
    }
    if (org.menuType === 'Web4Menu') {
      icon = <FileOutlined style={{ color: '#1890ff' }} />;
      value = org.pageUrl;
    }

    return (
      <TreeNode
        value={value}
        title={org.text}
        key={value}
        icon={icon}
        disabled={org.menuType !== 'Web4Menu'}
      >
        {haveChildren ? org.children.map(item => mapTree(item)) : null}
      </TreeNode>
    );
  };
  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>
    );
  };
邓超's avatar
邓超 committed
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
  const editorChange = editor => {
    let regex = /\{@(.+?)\}/g;
    let originalList = form.getFieldValue('lines') || [];
    let list = editor
      .getHtml()
      .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: [] });
    }
    setHtml(editor.getHtml());
  };
192 193 194 195 196
  const onFinish = () => {
    if (!id) {
      message.error('请保存基础信息');
      return;
    }
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    form.validateFields().then(values => {
      console.log(values, 'values');
      let value = JSON.parse(JSON.stringify(values));
      console.log(value);
      let PlatformIcon =
        value.PlatformIcon === defaultWebIcon ? '' : new URL(value.PlatformIcon).pathname;
      PlatformIcon = PlatformIcon.substring(1);
      console.log(PlatformIcon, 'PlatformIcon');
      if (value)
        AddPlatformStatus({
          ...value,
          PlatformIcon,
          PlatformStatus: value.PlatformStatus ? 1 : 0,
          PlatformContent: html,
          id,
        }).then(res => {
          if (res.code === 0) {
            message.success('保存成功');
          } else {
            message.error(res.msg);
          }
        });
    });
220 221 222 223 224 225 226 227 228 229 230
  };
  return (
    <div>
      <Form
        form={form}
        labelCol={{ span: 2 }}
        wrapperCol={{ span: 22 }}
        onValuesChange={changeValue}
        labelAlign="left"
      >
        <div style={{ display: 'flex' }}>
邓超's avatar
邓超 committed
231
          {/* <Form.Item
232 233 234 235 236 237 238 239 240 241 242 243 244
            label="消息类型"
            name="PlatformType"
            labelCol={{ span: 4 }}
            wrapperCol={{ span: 20 }}
            style={{ width: '400px', marginRight: '15px', display: 'block' }}
          >
            <Select placeholder="请选择消息类型" allowClear>
              {typeList.map(item => (
                <Option value={item.value} key={item.value}>
                  {item.name}
                </Option>
              ))}
            </Select>
邓超's avatar
邓超 committed
245
          </Form.Item> */}
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
          <Form.Item
            label="消息标题"
            name="PlatformTitle"
            style={{ width: '400px', display: 'block' }}
            labelCol={{ span: 4 }}
            wrapperCol={{ span: 20 }}
          >
            <Input placeholder="请填写消息标题" />
          </Form.Item>
          <Form.Item
            label="是否开启"
            name="PlatformStatus"
            valuePropName="checked"
            style={{ width: '400px', display: 'block' }}
            labelCol={{ span: 4 }}
            wrapperCol={{ span: 20 }}
          >
            <Switch checkedChildren="是" unCheckedChildren="否" />
          </Form.Item>
        </div>
        <Form.Item
          label="网页跳转路径"
          name="PlatformWebUrl"
          style={{ display: 'block', width: '1200px' }}
        >
          <TreeSelect
            showSearch
            treeNodeFilterProp="title"
            treeNodeLabelProp="value"
            dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
            placeholder="请选择功能路径"
            allowClear
            treeDefaultExpandAll
            treeIcon
          >
            {menuWebList ? (
              menuWebList.map(item => mapTree(item))
            ) : (
              <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
            )}
          </TreeSelect>
        </Form.Item>
        <Form.Item
          label="APP跳转路径"
          name="PlatformAppUrl"
          style={{ display: 'block', width: '1200px' }}
        >
          <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>
邓超's avatar
邓超 committed
310 311 312 313 314 315 316
        <Form.Item
          label="视频地址"
          name="PlatformVideoUrl"
          style={{ display: 'block', width: '1200px' }}
        >
          <Input placeholder="请填写视频地址" />
        </Form.Item>
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
        <Form.Item label="图标" style={{ display: 'block' }}>
          <div
            className={styles.formImg}
            onMouseEnter={() => {
              if (form.getFieldValue('PlatformIcon') !== defaultWebIcon) {
                setIconMaskShow(true);
              }
            }}
            onMouseLeave={() => setIconMaskShow(false)}
          >
            <div className={styles.mask} style={{ display: iconMaskShow ? 'flex' : 'none' }}>
              <DeleteOutlined
                className={styles.icon}
                onClick={() => form.setFieldsValue({ PlatformIcon: defaultWebIcon })}
              />
              <PictureOutlined className={styles.icon} onClick={() => setPreviewModal(true)} />
            </div>
            <Form.Item name="PlatformIcon" valuePropName="src" initialValue={defaultWebIcon}>
              <img alt="" onClick={() => setPreviewModal(true)} />
            </Form.Item>
          </div>
        </Form.Item>
        <Form.Item
          label="内容"
邓超's avatar
邓超 committed
341
          style={{ display: 'block', width: '1200px', height: '350px', marginBottom: '45px' }}
342
        >
邓超's avatar
邓超 committed
343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359
          <div style={{ border: '1px solid #ccc', zIndex: 100 }}>
            <Toolbar
              editor={editor}
              defaultConfig={toolbarConfig}
              mode="default"
              style={{ borderBottom: '1px solid #ccc' }}
            />
            <Editor
              defaultConfig={editorConfig}
              value={html}
              onCreated={setEditor}
              onChange={editor => editorChange(editor)}
              mode="default"
              style={{ height: '250px', overflowY: 'hidden' }}
            />
          </div>
          {/* <ReactQuill
360 361 362 363
            style={{ height: '150px' }}
            placeholder="请输入内容"
            modules={modules}
            theme="snow" // 主题
邓超's avatar
邓超 committed
364
          /> */}
365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
        </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 }}
393 394 395 396 397 398
                    rules={[
                      {
                        required: true,
                        message: '请填写含义',
                      },
                    ]}
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441
                  >
                    <Input placeholder="请填写含义" />
                  </Form.Item>
                  <Form.Item
                    {...restField}
                    name={[name, 'DefaultValue']}
                    label="默认值"
                    labelCol={{ span: 6 }}
                    wrapperCol={{ span: 18 }}
                  >
                    <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>
      <PreviewModal
        visible={previewModal}
        onCancel={() => {
          setPreviewModal(false);
        }}
        keepImgeUrl={form.getFieldValue('PlatformIcon')?.replace(`${window.location.origin}/`, '')}
        imgDataList={imgDataList}
        callBackSubmit={callBackSubmit}
      />
    </div>
  );
};

export default PlatformConfig;