WebDic.js 8.68 KB
Newer Older
陈前坚's avatar
陈前坚 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
import React, { useState, useEffect } from 'react';
import {
  Table,
  Modal,
  Form,
  Input,
  Space,
  Button,
  Popconfirm,
  notification,
  message,
} from 'antd';
import { get } from '@/services/index';
import styles from './WebDic.less';
陈前坚's avatar
陈前坚 committed
15
import AddForm from '@/pages/webConfig/menuconfig/AddForm';
陈前坚's avatar
陈前坚 committed
16 17 18

const WebDic = () => {
  const [loading, setLoading] = useState(false);
陈前坚's avatar
陈前坚 committed
19 20
  const [level, setLevel] = useState(1);
  const [addVisible, setAddVisible] = useState(false);
陈前坚's avatar
陈前坚 committed
21
  const [editVisible, setEditVisible] = useState(false);
陈前坚's avatar
陈前坚 committed
22 23 24 25
  const [data, setData] = useState([]); // 一级条目
  const [subData, setSubData] = useState([]); // 二级条目
  const [select, setSelect] = useState({}); // 当前选中条目
  const [addForm] = Form.useForm();
陈前坚's avatar
陈前坚 committed
26 27 28 29 30 31 32 33 34 35 36 37 38
  const [editForm] = Form.useForm();

  const columns = [
    {
      title: '名称',
      dataIndex: 'nodeName',
      key: 'nodeName',
      width: 300,
    },
    {
      title: '值',
      dataIndex: 'nodeValue',
      key: 'nodeValue',
陈前坚's avatar
陈前坚 committed
39 40 41 42 43 44
      render: record => {
        if (!record) {
          return '-';
        }
        return record;
      },
陈前坚's avatar
陈前坚 committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    },
    {
      title: '操作',
      key: 'action',
      width: 200,
      render: record => (
        <Space>
          <Button
            type="primary"
            size="small"
            onClick={e => {
              e.stopPropagation();
              setSelect(record);
              setEditVisible(true);
              console.log(record);
              editForm.setFieldsValue({
                nodeName: record.nodeName,
                nodeValue: record.nodeValue,
              });
            }}
          >
            修改
          </Button>
          <Popconfirm
            title="是否删除该数据?"
            okText="确认"
            cancelText="取消"
陈前坚's avatar
陈前坚 committed
72
            onConfirm={submitDelete}
陈前坚's avatar
陈前坚 committed
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
          >
            <Button
              size="small"
              danger
              onClick={e => {
                e.stopPropagation();
                setSelect(record);
              }}
            >
              删除
            </Button>
          </Popconfirm>
        </Space>
      ),
    },
  ];

  useEffect(() => {
    getData('-1');
  }, []);
  const getData = value => {
    setLoading(true);
    get(`/Cityinterface/rest/services/OMS.svc/D_GetDataDictionaryList`, {
      _version: 9999,
      _dc: new Date().getTime(),
      nodeID: value,
      key: '',
    })
      .then(res => {
陈前坚's avatar
陈前坚 committed
102
        if (res && res.length > 0) {
陈前坚's avatar
陈前坚 committed
103 104 105 106 107 108
          res.map(item => {
            item.key = item.nodeID;
            return item;
          });
          if (value === '-1') {
            setData(res); // 设置一级条目
陈前坚's avatar
陈前坚 committed
109
            setSelect(res[0]); // 默认当前选中一级条目第一条
陈前坚's avatar
陈前坚 committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
          } else if (value) {
            setSubData(res); // 设置二级条目
          }
          // 获取二级条目
          if (res && res[0] && res[0].parentID === '-1') {
            getData(res[0].nodeID);
          }
        } else {
          notification.error({
            message: '获取失败',
            description: res.message,
          });
        }
        setLoading(false);
      })
      .catch(err => {
        setLoading(false);
        message.error(err);
      });
  };
  const setRowClassName = record =>
    record.nodeID === select.nodeID ? styles.clickRowStyle : '';

陈前坚's avatar
陈前坚 committed
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
  // 提交-添加
  const submitAdd = value => {
    const nodeName = addForm.getFieldValue('nodeName');
    const nodeValue = addForm.getFieldValue('nodeValue');
    get(`/Cityinterface/rest/services/OMS.svc/D_AddDataDictionary`, {
      _version: 9999,
      _dc: new Date().getTime(),
      nodeID: value,
      nodeName,
      nodeValue,
    })
      .then(res => {
        if (res.success) {
          setAddVisible(false);
          getData('-1');
          notification.success({
            message: '提交成功',
          });
        } else {
          notification.error({
            message: '提交失败',
            description: res.message,
          });
        }
      })
      .catch(err => {
        message.error(err);
      });
  };
  // 提交-编辑
陈前坚's avatar
陈前坚 committed
163 164 165
  const submitEdit = () => {
    const nodeName = editForm.getFieldValue('nodeName');
    const nodeValue = editForm.getFieldValue('nodeValue');
陈前坚's avatar
陈前坚 committed
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
    get(`/Cityinterface/rest/services/OMS.svc/D_EditDataDictionary`, {
      _version: 9999,
      _dc: new Date().getTime(),
      nodeID: select.nodeID,
      nodeName,
      nodeValue,
    })
      .then(res => {
        if (res.success) {
          setEditVisible(false);
          getData(select.parentID);
          notification.success({
            message: '提交成功',
          });
        } else {
          notification.error({
            message: '提交失败',
            description: res.message,
          });
        }
      })
      .catch(err => {
        message.error(err);
      });
  };

  const submitDelete = () => {
    get(`/Cityinterface/rest/services/OMS.svc/D_DeleteDataDictionary`, {
      _version: 9999,
      _dc: new Date().getTime(),
      nodeID: select.nodeID,
    })
      .then(res => {
        if (res.success) {
          getData(select.parentID);
          notification.success({
            message: '删除成功',
          });
        } else {
          notification.error({
            message: '删除失败',
            description: res.message,
          });
        }
      })
      .catch(err => {
        message.error(err);
      });
陈前坚's avatar
陈前坚 committed
214 215 216 217
  };

  return (
    <div className={styles.WebDic}>
陈前坚's avatar
陈前坚 committed
218 219 220 221 222 223 224 225 226 227 228 229 230
      <div style={{ marginBottom: '10px', fontSize: '16px' }}>
        <span style={{ padding: '0 10px' }}>一级条目</span>
        <Button
          type="primary"
          size="small"
          onClick={() => {
            setLevel(1);
            setAddVisible(true);
          }}
        >
          添加
        </Button>
      </div>
陈前坚's avatar
陈前坚 committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
      <Table
        size="small"
        bordered
        columns={columns}
        dataSource={data}
        scroll={{ x: 'max-content' }}
        rowClassName={setRowClassName}
        onRow={record => ({
          onClick: () => {
            getData(record.nodeID);
            setSelect(record);
          },
        })}
        pagination={{
          showTotal: (total, range) =>
            `第${range[0]}-${range[1]} 条/共 ${total} 条`,
          pageSizeOptions: [10, 20, 50, 100],
          showQuickJumper: true,
          showSizeChanger: true,
        }}
      />
陈前坚's avatar
陈前坚 committed
252 253 254 255 256 257 258 259 260 261 262 263 264 265
      <hr color="#cfe7fd" />
      <div style={{ marginBottom: '10px', fontSize: '16px' }}>
        <span style={{ padding: '0 10px' }}>二级条目</span>
        <Button
          type="primary"
          size="small"
          onClick={() => {
            setLevel(2);
            setAddVisible(true);
          }}
        >
          添加
        </Button>
      </div>
陈前坚's avatar
陈前坚 committed
266 267 268 269 270 271 272 273 274 275 276 277 278 279
      <Table
        size="small"
        bordered
        columns={columns}
        dataSource={subData}
        scroll={{ x: 'max-content' }}
        pagination={{
          showTotal: (total, range) =>
            `第${range[0]}-${range[1]} 条/共 ${total} 条`,
          pageSizeOptions: [10, 20, 50, 100],
          showQuickJumper: true,
          showSizeChanger: true,
        }}
      />
陈前坚's avatar
陈前坚 committed
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
      {/* 添加 */}
      <Modal
        title={level === 1 ? '添加一级条目' : '添加二级条目'}
        visible={addVisible}
        onOk={() => {
          if (level === 1) {
            submitAdd('-1');
          } else {
            // submitAdd(select.nodeID);
          }
        }}
        onCancel={() => {
          setAddVisible(false);
        }}
        okText="确认"
        cancelText="取消"
      >
        <Form form={addForm} labelCol={{ span: 3 }}>
          <Form.Item
            name="nodeName"
            label="名称"
            rules={[{ required: true, message: '不能为空' }]}
          >
            <Input placeholder="请输入名称" />
          </Form.Item>
          <Form.Item name="nodeValue" label="值">
            <Input placeholder="请输入值" />
          </Form.Item>
        </Form>
      </Modal>
陈前坚's avatar
陈前坚 committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
      {/* 修改 */}
      <Modal
        title={select.parentID === '-1' ? '修改一级条目' : '修改二级条目'}
        visible={editVisible}
        onOk={submitEdit}
        onCancel={() => {
          setEditVisible(false);
        }}
        okText="确认"
        cancelText="取消"
      >
        <Form form={editForm} labelCol={{ span: 3 }}>
          <Form.Item
            name="nodeName"
            label="名称"
            rules={[{ required: true, message: '不能为空' }]}
          >
            <Input placeholder="请输入名称" />
          </Form.Item>
陈前坚's avatar
陈前坚 committed
329 330 331
          <Form.Item name="nodeValue" label="值">
            <Input placeholder="请输入值" />
          </Form.Item>
陈前坚's avatar
陈前坚 committed
332 333 334 335 336 337 338
        </Form>
      </Modal>
    </div>
  );
};

export default WebDic;