AppDic.js 9.67 KB
Newer Older
陈前坚's avatar
陈前坚 committed
1 2 3
import React, { useState, useEffect } from 'react';
import {
  Table,
陈前坚's avatar
陈前坚 committed
4
  Tooltip,
陈前坚's avatar
陈前坚 committed
5 6 7 8 9 10 11 12 13 14
  Spin,
  Modal,
  Form,
  Input,
  Space,
  Button,
  Popconfirm,
  notification,
  message,
} from 'antd';
陈前坚's avatar
陈前坚 committed
15
import { get, CITY_SERVICE } from '@/services';
陈前坚's avatar
陈前坚 committed
16
import { EditTwoTone, DeleteOutlined } from '@ant-design/icons';
陈前坚's avatar
陈前坚 committed
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 48 49 50 51 52 53 54 55 56 57
import styles from './AppDic.less';

const AppDic = () => {
  const [loading, setLoading] = useState(false);
  const [addVisible, setAddVisible] = useState(false);
  const [editVisible, setEditVisible] = useState(false);
  const [data, setData] = useState([]); // 表数据
  const [select, setSelect] = useState({}); // 当前选中条目,修改/删除时设置
  const [addForm] = Form.useForm();
  const [editForm] = Form.useForm();

  const columns = [
    {
      title: '名称',
      dataIndex: 'Label',
      key: 'Label',
    },
    {
      title: '键名',
      dataIndex: 'Key',
      key: 'Key',
    },
    {
      title: '值',
      dataIndex: 'Value',
      key: 'Value',
    },
    {
      title: '描述',
      dataIndex: 'Description',
      key: 'Description',
      render: record => {
        if (!record) {
          return '-';
        }
        return record;
      },
    },
    {
      title: '操作',
      key: 'action',
陈前坚's avatar
陈前坚 committed
58
      width: 100,
陈前坚's avatar
陈前坚 committed
59 60
      render: record => (
        <Space>
陈前坚's avatar
陈前坚 committed
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
          <Tooltip title="编辑">
            <EditTwoTone
              onClick={() => {
                setSelect(record);
                setEditVisible(true);
                editForm.setFieldsValue({
                  label: record.Label,
                  key: record.Key,
                  value: record.Value,
                  description: record.Description,
                });
              }}
              style={{ fontSize: '16px' }}
            />
          </Tooltip>
陈前坚's avatar
陈前坚 committed
76 77 78 79 80 81
          <Popconfirm
            title="是否删除该数据?"
            okText="确认"
            cancelText="取消"
            onConfirm={submitDelete}
          >
陈前坚's avatar
陈前坚 committed
82
            <DeleteOutlined
陈前坚's avatar
陈前坚 committed
83 84 85
              onClick={() => {
                setSelect(record);
              }}
陈前坚's avatar
陈前坚 committed
86 87 88 89 90 91
              style={{
                fontSize: '16px',
                margin: '0px 10px',
                color: '#e86060',
              }}
            />
陈前坚's avatar
陈前坚 committed
92 93 94 95 96 97 98 99 100 101 102
          </Popconfirm>
        </Space>
      ),
    },
  ];

  useEffect(() => {
    getData('-1');
  }, []);
  const getData = () => {
    setLoading(true);
陈前坚's avatar
陈前坚 committed
103
    get(`${CITY_SERVICE}/OMS.svc/M_GetKeyValue`, {
陈前坚's avatar
陈前坚 committed
104 105 106 107 108 109 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
      _version: 9999,
      _dc: new Date().getTime(),
    })
      .then(res => {
        if (res) {
          if (res.length > 0) {
            res.map(item => {
              item.key = item.id;
              return item;
            });
          }
          setData(res);
        } else {
          notification.error({
            message: '获取失败',
            description: res.message,
          });
        }
        setLoading(false);
      })
      .catch(err => {
        setLoading(false);
        message.error(err);
      });
  };
  // 提交-添加
  const submitAdd = () => {
    const label = addForm.getFieldValue('label');
    const key = addForm.getFieldValue('key');
    const value = addForm.getFieldValue('value');
    const description = addForm.getFieldValue('description');
    if (label && key && value) {
陈前坚's avatar
陈前坚 committed
136
      get(`${CITY_SERVICE}/OMS.svc/M_AddKeyValue`, {
陈前坚's avatar
陈前坚 committed
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
        _version: 9999,
        _dc: new Date().getTime(),
        label,
        key,
        value,
        desc: description,
      })
        .then(res => {
          if (res.success) {
            setAddVisible(false);
            getData();
            notification.success({
              message: '提交成功',
            });
          } else {
            notification.error({
              message: '提交失败',
              description: res.message,
            });
          }
        })
        .catch(err => {
          message.error(err);
        });
    } else {
      notification.error({
        message: '提交失败',
        description: '名称/键名/值不能为空',
      });
    }
  };
  // 提交-重置
  const submitReset = () => {
陈前坚's avatar
陈前坚 committed
170
    get(`${CITY_SERVICE}/OMS.svc/M_ResetKeyValue`, {
陈前坚's avatar
陈前坚 committed
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
      _version: 9999,
      _dc: new Date().getTime(),
    })
      .then(res => {
        if (res.success) {
          getData();
          notification.success({
            message: '重置成功',
          });
        } else {
          notification.error({
            message: '重置失败',
            description: res.message,
          });
        }
      })
      .catch(err => {
        message.error(err);
      });
  };
  // 提交-编辑
  const submitEdit = () => {
    const label = editForm.getFieldValue('label');
    const key = editForm.getFieldValue('key');
    const value = editForm.getFieldValue('value');
    const description = editForm.getFieldValue('description');
    if (label && key && value) {
陈前坚's avatar
陈前坚 committed
198
      get(`${CITY_SERVICE}/OMS.svc/M_EditKeyValue`, {
陈前坚's avatar
陈前坚 committed
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
        _version: 9999,
        _dc: new Date().getTime(),
        id: select.id,
        label,
        key,
        value,
        desc: description,
      })
        .then(res => {
          if (res.success) {
            setEditVisible(false);
            getData();
            notification.success({
              message: '提交成功',
            });
          } else {
            notification.error({
              message: '提交失败',
              description: res.message,
            });
          }
        })
        .catch(err => {
          message.error(err);
        });
    } else {
      notification.error({
        message: '提交失败',
        description: '名称/键名/值不能为空',
      });
    }
  };

  const submitDelete = () => {
陈前坚's avatar
陈前坚 committed
233
    get(`${CITY_SERVICE}/OMS.svc/M_DeleteKeyValue`, {
陈前坚's avatar
陈前坚 committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
      _version: 9999,
      _dc: new Date().getTime(),
      key: select.Key,
    })
      .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
255 256

  return (
陈前坚's avatar
陈前坚 committed
257 258 259 260 261 262 263
    <div className={styles.AppDic}>
      <Spin spinning={loading} tip="loading...">
        <div
          style={{
            marginBottom: '10px',
            fontSize: '16px',
            height: 'calc(100vh-200px)',
陈前坚's avatar
陈前坚 committed
264
          }}
陈前坚's avatar
陈前坚 committed
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
        >
          <span style={{ padding: '0 10px' }}>数据字典</span>
          <Button
            style={{ marginLeft: '10px' }}
            type="primary"
            size="small"
            onClick={() => {
              setAddVisible(true);
              addForm.resetFields();
            }}
          >
            添加
          </Button>
          <Popconfirm
            title="是否重置默认配置?"
            okText="确认"
            cancelText="取消"
            onConfirm={submitReset}
          >
            <Button size="small" danger style={{ marginLeft: '10px' }}>
              重置
            </Button>
          </Popconfirm>
        </div>
陈前坚's avatar
陈前坚 committed
289 290 291
        <Table
          size="small"
          bordered
陈前坚's avatar
陈前坚 committed
292 293
          columns={columns}
          dataSource={data}
陈前坚's avatar
陈前坚 committed
294 295 296 297 298
          scroll={{ x: 'max-content' }}
          pagination={{
            showTotal: (total, range) =>
              `第${range[0]}-${range[1]} 条/共 ${total} 条`,
            pageSizeOptions: [10, 20, 50, 100],
陈前坚's avatar
陈前坚 committed
299
            defaultPageSize: 20,
陈前坚's avatar
陈前坚 committed
300 301 302 303
            showQuickJumper: true,
            showSizeChanger: true,
          }}
        />
陈前坚's avatar
陈前坚 committed
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
      </Spin>

      {/* 添加 */}
      <Modal
        title="添加数据字典"
        visible={addVisible}
        onOk={submitAdd}
        onCancel={() => {
          setAddVisible(false);
        }}
        okText="确认"
        cancelText="取消"
      >
        <Form form={addForm} labelCol={{ span: 3 }}>
          <Form.Item
            name="label"
            label="名称"
            rules={[{ required: true, message: '不能为空' }]}
          >
            <Input placeholder="请输入名称" />
          </Form.Item>
          <Form.Item
            name="key"
            label="键名"
            rules={[{ required: true, message: '不能为空' }]}
          >
            <Input placeholder="请输入键名" />
          </Form.Item>
          <Form.Item
            name="value"
            label="值"
            rules={[{ required: true, message: '不能为空' }]}
          >
            <Input placeholder="请输入值" />
          </Form.Item>
          <Form.Item name="description" label="描述">
            <Input placeholder="请输入相关描述" />
          </Form.Item>
        </Form>
      </Modal>
      {/* 修改 */}
      <Modal
        title="修改数据字典"
        visible={editVisible}
        onOk={submitEdit}
        onCancel={() => {
          setEditVisible(false);
        }}
        okText="确认"
        cancelText="取消"
      >
        <Form form={editForm} labelCol={{ span: 3 }}>
          <Form.Item
            name="label"
            label="名称"
            rules={[{ required: true, message: '不能为空' }]}
          >
            <Input placeholder="请输入名称" />
          </Form.Item>
          <Form.Item name="key" label="键名">
            <Input placeholder="请输入键名" />
          </Form.Item>
          <Form.Item
            name="value"
            label="值"
            rules={[{ required: true, message: '不能为空' }]}
          >
            <Input placeholder="请输入值" />
          </Form.Item>
          <Form.Item name="description" label="描述">
            <Input placeholder="请输入相关描述" />
          </Form.Item>
        </Form>
      </Modal>
    </div>
陈前坚's avatar
陈前坚 committed
379 380 381
  );
};

陈前坚's avatar
陈前坚 committed
382
export default AppDic;