gateWay.jsx 18.2 KB
Newer Older
1
/* eslint-disable no-unused-expressions */
皮倩雯's avatar
皮倩雯 committed
2
/* eslint-disable no-else-return */
3
import React, { useEffect, useState } from 'react';
皮倩雯's avatar
皮倩雯 committed
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
import {
  Card,
  Form,
  Switch,
  message,
  Divider,
  Row,
  Col,
  Spin,
  Tooltip,
  Input,
  Space,
  Button,
  Popconfirm,
  Table,
  notification,
皮倩雯's avatar
皮倩雯 committed
20
  Tag,
皮倩雯's avatar
皮倩雯 committed
21 22 23 24 25 26 27 28
} from 'antd';
import {
  EditTwoTone,
  DeleteOutlined,
  PlusOutlined,
  SyncOutlined,
  SearchOutlined,
} from '@ant-design/icons';
皮倩雯's avatar
皮倩雯 committed
29
import { useHistory } from 'react-router-dom';
30
import styles from './gateWay.less';
皮倩雯's avatar
皮倩雯 committed
31 32 33 34
import {
  GetGateWay,
  UpdateGeteWay,
  GetReRoutes,
35
  GetReRoutesFirst,
皮倩雯's avatar
皮倩雯 committed
36 37
  DelRoutes,
} from '@/services/hostmanager/hostmanager';
38
import { get, PUBLISH_SERVICE } from '@/services/index';
39
import configuration from '../../../../assets/images/icons/消息.svg';
40

皮倩雯's avatar
皮倩雯 committed
41
import AddModal from './AddModal';
皮倩雯's avatar
皮倩雯 committed
42
import CheckModal from './CheckModal';
皮倩雯's avatar
皮倩雯 committed
43

44
const GateConfig = () => {
45 46
  const [loading, setLoading] = useState(false); // 加载
  const [form] = Form.useForm();
47

48 49
  const [flag, setFlag] = useState(1);
  const [currentConfig, setCurrentConfig] = useState();
皮倩雯's avatar
皮倩雯 committed
50 51 52 53 54
  const [tableData, setTableData] = useState([]);
  const [showSearchStyle, setShowSearchStyle] = useState(false); // 是否显示模糊查询样式
  const [searchWord, setSearchWord] = useState(''); // 关键字
  const [searchWord1, setSearchWord1] = useState(''); // 关键字
  const [addVisible, setAddVisible] = useState(false);
皮倩雯's avatar
皮倩雯 committed
55
  const [checkVisible, setCheckVisible] = useState(false);
皮倩雯's avatar
皮倩雯 committed
56 57
  const [pickItem, setPickItem] = useState('');
  const [type, setType] = useState('');
皮倩雯's avatar
皮倩雯 committed
58 59
  const history = useHistory();
  const [keepData, setKeepData] = useState([]); // 保存内置关键字
皮倩雯's avatar
皮倩雯 committed
60
  const { Search } = Input;
61

62
  const OperateNginx = checked => {
63
    localStorage.setItem('panda-publish', '');
64
    console.log(checked);
65 66
    if (checked) {
      UpdateGeteWay({ isUsed: checked }).then(res => {
皮倩雯's avatar
皮倩雯 committed
67
        if (res.code === 0) {
68
          console.log(localStorage.getItem('token'));
皮倩雯's avatar
皮倩雯 committed
69
          // localStorage.removeItem('token');
70
          window.globalConfig = {
71
            ...window.globalConfig,
72 73 74 75
            access_token: localStorage.getItem('token'),
            hasGateWay: true,
            apiGatewayDomain: `${window.location.origin}${'/PandaCore/GateWay'}`,
          };
76 77
          setFlag(flag + 1);
          message.success('设置成功');
皮倩雯's avatar
皮倩雯 committed
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
          // const key = 'authrizeFail';
          // notification.warning({
          //   key,
          //   title: '提示',
          //   message: '授权失败,即将跳转到登录页',
          //   duration: 2,
          // });
          // setTimeout(() => {
          //   history.push(`/user/login`);
          // }, 2000);
          const key = 'authrizeFail';
          const btn = (
            <Button
              type="primary"
              size="small"
              onClick={() => {
                notification.close(key);
                window.location.href = `/${process.env.PUBLIC_PATH || 'civmanage'}/user/login`;
              }}
            >
              确定
            </Button>
          );
          if (!/\/user\/login$/.test(window.location.pathname)) {
            notification.warning({
              key,
              title: '提示',
              message: '授权失败,即将跳转到登录页',
              duration: 2,
              btn,
            });
            setTimeout(() => {
              window.location.href = `/${process.env.PUBLIC_PATH || 'civmanage'}/user/login`;
            }, 2000);
          }
113 114
        } else {
          message.error('设置失败');
皮倩雯's avatar
皮倩雯 committed
115 116
        }
      });
117 118 119 120 121 122 123 124 125 126 127 128 129
      setTimeout(() => {
        localStorage.setItem('panda-publish', 'getway');
        setLoading(true);
        GetReRoutes({
          UpstreamPathTemplate: '',
          key: '',
        }).then(res => {
          setLoading(false);
          if (res.code === 0) {
            setTableData(res.data);
          }
        });
      }, 0);
130
    } else {
131 132
      UpdateGeteWay({ isUsed: checked }).then(res => {
        if (res.code === 0) {
133 134
          // 关闭网关清空token
          localStorage.setItem('token', '');
135
          window.globalConfig = {
136
            ...window.globalConfig,
137
            access_token: '',
138 139 140
            hasGateWay: false,
            apiGatewayDomain: `${window.location.origin}`,
          };
141 142 143 144 145 146
          setFlag(flag + 1);
          message.success('设置成功');
        } else {
          message.error('设置失败');
        }
      });
147
    }
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
  };
  // useEffect(() => {
  //   if (currentConfig) {
  //     localStorage.setItem('panda-publish', 'getway');
  //     GetReRoutes({
  //       UpstreamPathTemplate: '',
  //       key: '',
  //     }).then(res => {
  //       setLoading(false);
  //       if (res.code === 0) {
  //         setTableData(res.data);
  //       }
  //     });
  //   } else {
  //     console.log(123333);
  //     localStorage.setItem('panda-publish', '');
  //   }
  // }, [currentConfig]);
166

167 168 169 170 171 172 173
  useEffect(() => {
    GetGateWay().then(res => {
      if (res.code === 0) {
        setCurrentConfig(res.data);
        console.log(currentConfig);
      }
    });
皮倩雯's avatar
皮倩雯 committed
174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
    // .catch(err => {
    //   setLoading(false);
    //   GetReRoutes({
    //     UpstreamPathTemplate: '',
    //     key: '',
    //   }).then(resdata => {
    //     setLoading(false);
    //     if (resdata.code === 0) {
    //       console.log(resdata.data);
    //       setTableData(resdata.data);
    //     } else {
    //       console.log(123);
    //     }
    //   });
    // });
皮倩雯's avatar
皮倩雯 committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
    // if (res.data) {
    GetReRoutes({
      UpstreamPathTemplate: '',
      key: '',
    }).then(resdata => {
      setLoading(false);
      if (resdata.code === 0) {
        console.log(resdata.data);
        let data = [];
        resdata.data.map(i => {
          data.push(i.key);
        });
        setKeepData(data);
        setTableData(resdata.data);
      }
    });
    // }
206
  }, [flag]);
207

皮倩雯's avatar
皮倩雯 committed
208
  const columns = [
皮倩雯's avatar
皮倩雯 committed
209 210 211 212 213 214 215 216 217 218 219
    {
      title: '关键字',
      dataIndex: 'key',
      key: 'key',
      align: 'center',
      width: 150,
      render: (text, record) => {
        if (record.key === 'CityServer') {
          return (
            <Tooltip placement="top" title={text}>
              <span>
皮倩雯's avatar
皮倩雯 committed
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 249 250 251 252 253 254 255 256
                {searchStyle1(text)}{' '}
                <Tag color="cyan" style={{ marginRight: '0px' }}>
                  万能模板
                </Tag>
              </span>
            </Tooltip>
          );
        } else if (record.key === 'IOT') {
          return (
            <Tooltip placement="top" title={text}>
              <span>
                {searchStyle1(text)}{' '}
                <Tag color="purple" style={{ marginRight: '0px' }}>
                  物联
                </Tag>
              </span>
            </Tooltip>
          );
        } else if (record.key === 'CivData') {
          return (
            <Tooltip placement="top" title={text}>
              <span>
                {searchStyle1(text)}{' '}
                <Tag color="green" style={{ marginRight: '0px' }}>
                  中台
                </Tag>
              </span>
            </Tooltip>
          );
        } else if (record.key === 'GIS') {
          return (
            <Tooltip placement="top" title={text}>
              <span>
                {searchStyle1(text)}{' '}
                <Tag color="volcano" style={{ marginRight: '0px' }}>
                  GIS
                </Tag>
皮倩雯's avatar
皮倩雯 committed
257 258 259 260 261 262 263 264 265 266 267 268 269 270
              </span>
            </Tooltip>
          );
        } else {
          return (
            <span>
              <Tooltip placement="top" title={text}>
                {searchStyle1(text)}
              </Tooltip>
            </span>
          );
        }
      },
    },
皮倩雯's avatar
皮倩雯 committed
271
    {
皮倩雯's avatar
皮倩雯 committed
272
      title: '上游服务地址',
皮倩雯's avatar
皮倩雯 committed
273 274 275 276 277
      dataIndex: 'upstreamPathTemplate',
      key: 'upstreamPathTemplate',
      align: 'center',
      render: (text, record) => (
        <span>
皮倩雯's avatar
皮倩雯 committed
278 279
          <Tooltip placement="top" title={`上游路由模板:${text}`}>
            {window.location.origin + `/PandaCore/GateWay${text}`}
皮倩雯's avatar
皮倩雯 committed
280 281 282 283
          </Tooltip>
        </span>
      ),
    },
皮倩雯's avatar
皮倩雯 committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297
    // {
    //   title: '上游路由模板',
    //   dataIndex: 'upstreamPathTemplate',
    //   key: 'upstreamPathTemplate',
    //   align: 'center',
    //   ellipsis: true,
    //   render: (text, record) => (
    //     <span>
    //       <Tooltip placement="top" title={text}>
    //         {searchStyle(text)}
    //       </Tooltip>
    //     </span>
    //   ),
    // },
皮倩雯's avatar
皮倩雯 committed
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
    {
      title: '上游请求方式',
      dataIndex: 'methods',
      key: 'methods',
      align: 'center',
      ellipsis: true,
      width: 200,
      render: (text, record) => (
        <span>
          <Tooltip placement="top" title={text}>
            {text}
          </Tooltip>
        </span>
      ),
    },
    {
      title: '下游服务地址',
      dataIndex: 'url',
      key: 'url',
      align: 'center',
      ellipsis: true,
      render: (text, record) => (
        <span>
皮倩雯's avatar
皮倩雯 committed
321 322
          <Tooltip placement="top" title={`下游路由模板:${record.downstreamPathTemplate}`}>
            {`${text}${record.downstreamPathTemplate}`}
皮倩雯's avatar
皮倩雯 committed
323 324 325 326
          </Tooltip>
        </span>
      ),
    },
皮倩雯's avatar
皮倩雯 committed
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
    // {
    //   title: '下游路由模板',
    //   dataIndex: 'downstreamPathTemplate',
    //   key: 'downstreamPathTemplate',
    //   ellipsis: true,
    //   width: 150,
    //   align: 'center',
    //   render: (text, record) => (
    //     <span>
    //       <Tooltip placement="top" title={text}>
    //         {text}
    //       </Tooltip>
    //     </span>
    //   ),
    // },
皮倩雯's avatar
皮倩雯 committed
342 343 344 345 346 347 348 349
    {
      title: '开启身份认证',
      dataIndex: 'isAuthentication',
      key: 'isAuthentication',
      align: 'center',
      width: 120,
      render: (text, record) => {
        if (text == true) {
皮倩雯's avatar
皮倩雯 committed
350
          return <Tag color="#87d068"></Tag>;
皮倩雯's avatar
皮倩雯 committed
351
        } else {
皮倩雯's avatar
皮倩雯 committed
352
          return <Tag color="#2db7f5"></Tag>;
皮倩雯's avatar
皮倩雯 committed
353 354 355
        }
      },
    },
356 357 358 359 360
    {
      title: '优先级',
      dataIndex: 'priority',
      key: 'priority',
      align: 'center',
皮倩雯's avatar
皮倩雯 committed
361
      width: 100,
362 363 364 365 366 367 368 369 370 371 372
      render: (text, record) => {
        console.log(text);
        return (
          <span>
            <Tooltip placement="top" title={text}>
              {text}
            </Tooltip>
          </span>
        );
      },
    },
皮倩雯's avatar
皮倩雯 committed
373 374 375 376 377
    {
      title: '操作',
      key: 'action',
      width: 100,
      align: 'center',
皮倩雯's avatar
皮倩雯 committed
378
      fixed: 'right',
379 380 381 382 383 384 385
      render: record => {
        if (record.key != 'CityServer') {
          return (
            <Space size="middle">
              <Tooltip title="编辑">
                <EditTwoTone onClick={() => edit(record)} style={{ fontSize: '16px' }} />
              </Tooltip>
386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
              {record.key != 'IOT' && record.key != 'CivData' && record.key != 'GIS' && (
                <Tooltip title="删除">
                  <Popconfirm
                    placement="bottomRight"
                    title={
                      <p>
                        即将删除 <span>{record.loginName}</span>
                        ,是否确认删除?
                      </p>
                    }
                    okText="确认"
                    cancelText="取消"
                    onConfirm={() => dele(record)}
                  >
                    <DeleteOutlined style={{ fontSize: '16px', color: '#e86060' }} />
                  </Popconfirm>
                </Tooltip>
              )}
404 405 406 407 408 409 410 411 412 413 414 415
            </Space>
          );
        } else {
          return (
            <Space size="middle">
              <Tooltip title="查看">
                <SearchOutlined onClick={() => look(record)} style={{ fontSize: '16px' }} />
              </Tooltip>
            </Space>
          );
        }
      },
皮倩雯's avatar
皮倩雯 committed
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 442 443 444 445 446 447
    },
  ];

  // 模糊查询匹配的样式
  const searchStyle = val => {
    let n;
    if (showSearchStyle) {
      n = val.replace(new RegExp(searchWord, 'g'), `<span style='color:red'>${searchWord}</span>`);
    } else {
      n = val;
    }
    return <div dangerouslySetInnerHTML={{ __html: n }} />;
  };

  // 模糊查询匹配的样式
  const searchStyle1 = val => {
    let n;
    if (showSearchStyle) {
      n = val.replace(
        new RegExp(searchWord1, 'g'),
        `<span style='color:red'>${searchWord1}</span>`,
      );
    } else {
      n = val;
    }
    return <div dangerouslySetInnerHTML={{ __html: n }} />;
  };

  const handleReset = () => {
    setSearchWord('');
    setSearchWord1('');
    setShowSearchStyle(false);
448 449 450 451 452 453 454 455 456 457
    setLoading(true);
    GetReRoutes({
      UpstreamPathTemplate: '',
      key: '',
    }).then(res => {
      setLoading(false);
      if (res.code === 0) {
        setTableData(res.data);
      }
    });
皮倩雯's avatar
皮倩雯 committed
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494
  };

  const handleSearch = e => {
    setSearchWord(e.target.value);
  };

  const handleSearch1 = e => {
    setSearchWord1(e.target.value);
  };

  const submitSearch = () => {
    setLoading(true);
    GetReRoutes({ UpstreamPathTemplate: searchWord, Key: searchWord1 }).then(resnew => {
      setLoading(false);
      if (resnew.code === 0) {
        setTableData(resnew.data);
        setShowSearchStyle(true);
      } else {
        notification.error({
          message: '查询失败',
          description: resnew.msg,
        });
      }
    });
  };

  const add = () => {
    setType('add');
    setAddVisible(true);
  };

  const edit = e => {
    setType('edit');
    setPickItem(e);
    setAddVisible(true);
  };

皮倩雯's avatar
皮倩雯 committed
495 496 497 498 499
  const look = e => {
    setPickItem(e);
    setCheckVisible(true);
  };

皮倩雯's avatar
皮倩雯 committed
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
  const dele = e => {
    setPickItem(e);
    let data = [];
    data.push(e.id);
    console.log(data);
    if (e.id == 1) {
      notification.error({
        message: '删除失败',
        description: '默认数据无法删除',
      });
    } else {
      DelRoutes({
        Ids: e.id,
      }).then(res => {
        if (res.code === 0) {
515 516 517 518 519 520 521 522 523
          GetReRoutes({
            UpstreamPathTemplate: '',
            key: '',
          }).then(res => {
            setLoading(false);
            if (res.code === 0) {
              setTableData(res.data);
            }
          });
皮倩雯's avatar
皮倩雯 committed
524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
          notification.success({
            message: '删除成功',
            description: res.msg,
          });
        } else {
          notification.error({
            message: '删除失败',
            description: res.msg,
          });
        }
      });
    }
  };

  const onSubmit = () => {
539 540 541 542 543 544 545 546 547
    GetReRoutes({
      UpstreamPathTemplate: '',
      key: '',
    }).then(res => {
      setLoading(false);
      if (res.code === 0) {
        setTableData(res.data);
      }
    });
皮倩雯's avatar
皮倩雯 committed
548 549
  };

550 551 552
  return (
    <div className={styles.gateWay_container}>
      <Card style={{ width: '100%', height: 'calc(100vh - 130px)' }}>
皮倩雯's avatar
皮倩雯 committed
553 554 555 556 557 558 559
        <div style={{ display: 'flex', alignItems: 'center', marginTop: '10px' }}>
          <img src={configuration} style={{ height: '16px' }} alt="" />
          <span style={{ marginLeft: '10px', fontWeight: 'bold' }}>网关配置</span>
        </div>
        <Divider />
        <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '20px' }}>
          <div style={{ display: 'flex', alignItems: 'center' }}>
560 561 562 563
            <div
              style={{
                marginLeft: '35px',
              }}
564
            >
皮倩雯's avatar
皮倩雯 committed
565
              网关状态
566 567 568 569 570 571 572 573 574
              <Switch
                checkedChildren="开启"
                unCheckedChildren="关闭"
                checked={currentConfig}
                onChange={OperateNginx}
                style={{ marginLeft: '35px' }}
              />
            </div>
            {console.log(currentConfig)}
575
          </div>
皮倩雯's avatar
皮倩雯 committed
576 577
          {/* {currentConfig ? (
          
皮倩雯's avatar
皮倩雯 committed
578 579
          ) : (
            <></>
皮倩雯's avatar
皮倩雯 committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
          )} */}
          <div className={styles.head1}>
            <span>快速搜索上游路由模板:</span>
            <Input
              value={searchWord}
              placeholder="请输入上游路由模板"
              style={{ width: 200 }}
              onChange={handleSearch}
            />
            <span style={{ marginLeft: '20px' }}>快速搜索关键字:</span>
            <Input
              value={searchWord1}
              placeholder="请输入键值"
              style={{ width: 200 }}
              onChange={handleSearch1}
            />
            <Button
              type="primary"
              icon={<SearchOutlined />}
              onClick={submitSearch}
              style={{ marginLeft: '20px' }}
            >
              搜索
            </Button>
            <Button icon={<SyncOutlined />} onClick={handleReset} style={{ marginLeft: '20px' }}>
              重置
            </Button>
            <Button
              icon={<PlusOutlined className={styles.icon} />}
              onClick={add}
              style={{
                verticalAlign: 'middle',
                marginTop: '-3px',
                marginLeft: '20px',
              }}
            >
              新增
            </Button>
          </div>
皮倩雯's avatar
皮倩雯 committed
619
        </div>
皮倩雯's avatar
皮倩雯 committed
620 621
        {/* {currentConfig ? (
        
皮倩雯's avatar
皮倩雯 committed
622 623
        ) : (
          <></>
皮倩雯's avatar
皮倩雯 committed
624 625 626 627 628 629 630 631 632 633 634 635
        )} */}
        <Spin spinning={loading} tip="loading">
          <div className={styles.table}>
            <Table
              size="small"
              bordered
              rowKey={record => record.Id}
              columns={columns}
              dataSource={tableData}
              scroll={{ y: 'calc(100vh - 380px)', x: 'max-content' }}
              onRow={record => ({
                onDoubleClick: event => {
636
                  console.log(record);
皮倩雯's avatar
皮倩雯 committed
637
                  event.stopPropagation();
638 639 640 641 642
                  if (record.key == 'CityServer') {
                    look(record);
                  } else {
                    edit(record);
                  }
皮倩雯's avatar
皮倩雯 committed
643 644 645 646 647 648 649 650 651 652 653 654
                }, // 双击
              })}
              pagination={{
                showTotal: (total, range) => `第${range[0]}-${range[1]} 条/共 ${total} 条`,
                pageSizeOptions: [10, 20, 50, 100],
                defaultPageSize: 20,
                showQuickJumper: true,
                showSizeChanger: true,
              }}
            />
          </div>
        </Spin>
皮倩雯's avatar
皮倩雯 committed
655 656 657 658

        <AddModal
          visible={addVisible}
          pickItem={pickItem}
皮倩雯's avatar
皮倩雯 committed
659
          keepData={keepData}
皮倩雯's avatar
皮倩雯 committed
660 661 662 663
          onCancel={() => setAddVisible(false)}
          type={type}
          callBackSubmit={onSubmit}
        />
皮倩雯's avatar
皮倩雯 committed
664 665 666 667 668
        <CheckModal
          visible={checkVisible}
          pickItem={pickItem}
          onCancel={() => setCheckVisible(false)}
        />
669 670 671 672 673
      </Card>
    </div>
  );
};
export default GateConfig;