AddModal.jsx 14.1 KB
Newer Older
皮倩雯's avatar
皮倩雯 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* eslint-disable react/jsx-boolean-value */
import React, { useState, useEffect } from 'react';
import {
  Form,
  Modal,
  Input,
  notification,
  Radio,
  InputNumber,
  Checkbox,
  Switch,
  Tooltip,
  Row,
  Col,
} from 'antd';
import { SaveRoutes } from '@/services/hostmanager/hostmanager';
import { InfoCircleOutlined } from '@ant-design/icons';

const { Item } = Form;
const AddModal = props => {
皮倩雯's avatar
皮倩雯 committed
21
  const { callBackSubmit = () => {}, type, pickItem, visible, onCancel, keepData } = props;
皮倩雯's avatar
皮倩雯 committed
22 23 24
  const [loading, setLoading] = useState(false);
  const [current, setCurrent] = useState(false);
  const [advanced, setAdvanced] = useState(0);
皮倩雯's avatar
皮倩雯 committed
25
  const [hidden, setHidden] = useState(false);
皮倩雯's avatar
皮倩雯 committed
26 27 28 29 30
  const [form] = Form.useForm();

  useEffect(() => {
    if (visible) {
      if (type === 'edit') {
皮倩雯's avatar
皮倩雯 committed
31 32 33 34
        let data = ['CityServer', 'IOT', 'CivData', 'GIS'];
        if (data.indexOf(pickItem.key) != -1) {
          setHidden(true);
        }
皮倩雯's avatar
皮倩雯 committed
35 36 37 38 39 40 41 42 43 44 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 72 73 74 75 76 77 78 79 80 81 82 83 84 85
        let aa = pickItem.methods.replace(/\s/g, '');
        form.setFieldsValue({
          UpstreamPathTemplate: pickItem.upstreamPathTemplate,
          DownstreamPathTemplate: pickItem.downstreamPathTemplate,
          Methods: aa.split(','),
          Url: pickItem.url,
          IsAuthentication: pickItem.isAuthentication,
          Key: pickItem.key,
          Priority: pickItem.priority,
          UpstreamHost: pickItem.upstreamHost,
          AddHeasersToRequest: pickItem.addHeasersToRequest,
          UpstreamHeaderTransform: pickItem.upstreamHeaderTransform,
          DownstreamHeaderTransform: pickItem.downstreamHeaderTransform,
          Timeout: pickItem.timeout,
          QoSOptions: pickItem.qoSOptions,
          RateLimitOptions: pickItem.rateLimitOptions,
          CacheOptions: pickItem.cacheOptions,
          LoadBalancerOptions: pickItem.loadBalancerOptions,
          SecurityOptions: pickItem.securityOptions,
          RequestIdKey: pickItem.requestIdKey,
          ServiceName: pickItem.serviceName,
          ServiceNamespace: pickItem.serviceNamespace,
          DelegatingHandlers: pickItem.delegatingHandlers,
          ReRouteIsCaseSensitive: pickItem.reRouteIsCaseSensitive,
          DownstreamHttpMethod: pickItem.downstreamHttpMethod,
        });
        if (
          pickItem.upstreamHost ||
          pickItem.addHeasersToRequest ||
          pickItem.upstreamHeaderTransform ||
          pickItem.downstreamHeaderTransform ||
          pickItem.timeout ||
          pickItem.qoSOptions ||
          pickItem.rateLimitOptions ||
          pickItem.cacheOptions ||
          pickItem.loadBalancerOptions ||
          pickItem.securityOptions ||
          pickItem.requestIdKey ||
          pickItem.serviceName ||
          pickItem.serviceNamespace ||
          pickItem.delegatingHandlers ||
          pickItem.reRouteIsCaseSensitive ||
          pickItem.downstreamHttpMethod
        ) {
          setAdvanced(1);
          setCurrent(true);
        }
      } else {
        form.setFieldsValue({ IsAuthentication: true });
      }
    } else {
皮倩雯's avatar
皮倩雯 committed
86
      setHidden(false);
皮倩雯's avatar
皮倩雯 committed
87 88 89 90 91 92 93 94 95 96 97 98
      form.resetFields();
      setAdvanced(0);
      setCurrent(false);
    }
  }, [visible]);
  // 提交
  const onSubmit = () => {
    form.validateFields().then(validate => {
      if (validate) {
        setLoading(true);
        let obj = form.getFieldsValue();
        console.log(obj.IsAuthentication);
99
        console.log(obj.Priority);
皮倩雯's avatar
皮倩雯 committed
100 101 102 103 104 105 106 107 108 109
        let aa = obj.Methods.toString();
        obj.Methods = aa.replace(/\s/g, '');
        console.log(obj.Methods);
        let data = {
          UpstreamPathTemplate: obj.UpstreamPathTemplate || null,
          DownstreamPathTemplate: obj.DownstreamPathTemplate || null,
          Methods: obj.Methods || null,
          Url: obj.Url || null,
          IsAuthentication: obj.IsAuthentication,
          Key: obj.Key || null,
110
          Priority: obj.Priority || 0,
皮倩雯's avatar
皮倩雯 committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
          UpstreamHost: obj.UpstreamHost || null,
          AddHeasersToRequest: obj.AddHeasersToRequest || null,
          UpstreamHeaderTransform: obj.UpstreamHeaderTransform || null,
          DownstreamHeaderTransform: obj.DownstreamHeaderTransform || null,
          Timeout: obj.Timeout || null,
          QoSOptions: obj.QoSOptions || null,
          RateLimitOptions: obj.RateLimitOptions || null,
          CacheOptions: obj.CacheOptions || null,
          LoadBalancerOptions: obj.LoadBalancerOptions || null,
          SecurityOptions: obj.SecurityOptions || null,
          RequestIdKey: obj.RequestIdKey || null,
          ServiceName: obj.ServiceName || null,
          ServiceNamespace: obj.ServiceNamespace || null,
          DelegatingHandlers: obj.DelegatingHandlers || null,
          ReRouteIsCaseSensitive: obj.ReRouteIsCaseSensitive || null,
          DownstreamHttpMethod: obj.DownstreamHttpMethod || null,
        };
128
        console.log(data);
皮倩雯's avatar
皮倩雯 committed
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 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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
        if (type === 'add') {
          SaveRoutes([
            {
              ...data,
              IsEnable: 1,
            },
          ])
            .then(res => {
              setLoading(false);
              if (res.code === 0) {
                onCancel();
                callBackSubmit();
                notification.success({
                  message: '提示',
                  duration: 3,
                  description: res.msg || '新增成功',
                });
              } else {
                notification.error({
                  message: '提示',
                  duration: 3,
                  description: res.msg || '新增失败',
                });
              }
            })
            .catch(err => {
              setLoading(false);
            });
        } else {
          SaveRoutes([
            {
              id: pickItem.id,
              ...data,
              IsEnable: 1,
            },
          ])
            .then(res => {
              setLoading(false);
              if (res.code === 0) {
                onCancel();
                callBackSubmit();
                notification.success({
                  message: '提示',
                  duration: 3,
                  description: res.msg || '编辑成功',
                });
              } else {
                notification.error({
                  message: '提示',
                  duration: 3,
                  description: res.msg || '编辑失败',
                });
              }
            })
            .catch(err => {
              setLoading(false);
            });
        }
      }
    });
  };

  const layout = {
    layout: 'horizontal',
    labelCol: { span: 4 },
    wrapperCol: { span: 18 },
  };

  const plainOptions = ['GET', 'POST', 'PUT', 'DELETE'];

  const change = (e, event) => {
    if (e) {
      setAdvanced(1);
      setCurrent(true);
    } else {
      setAdvanced(0);
      setCurrent(false);
    }
  };

  return (
    <Modal
      title={type === 'add' ? '新增网关配置' : '编辑网关配置'}
      bodyStyle={{ width: '100%', maxHeight: '600px', overflow: 'scroll', minHeight: '360px' }}
      width="700px"
      destroyOnClose
      maskClosable={false}
      cancelText="取消"
      okText="确认"
      {...props}
      onOk={() => onSubmit()}
      confirmLoading={loading}
      forceRender={true}
      getContainer={false}
    >
      <Form form={form} {...layout}>
        <Item
          label="上游路由模板"
          name="UpstreamPathTemplate"
          rules={[
            {
              validator: (rule, value) => {
                let aa = form.getFieldValue().UpstreamPathTemplate;
232 233
                console.log(aa);
                console.log(aa.indexOf('/PandaCore/GateWay'));
皮倩雯's avatar
皮倩雯 committed
234 235 236
                if (!aa.startsWith('/')) {
                  return Promise.reject('必须以/开头');
                }
237 238 239 240
                if (aa.indexOf('/PandaCore/GateWay') != -1) {
                  return Promise.reject('无需添加/PandaCore/GateWay');
                }

皮倩雯's avatar
皮倩雯 committed
241 242 243 244 245 246 247 248 249
                return Promise.resolve();
              },
            },
            {
              required: true,
              message: '请输入上游路由模板',
            },
          ]}
        >
皮倩雯's avatar
皮倩雯 committed
250 251 252
          <Input
            allowClear
            style={{ width: '100%' }}
253
            placeholder="示例:/CivData/{url}"
皮倩雯's avatar
皮倩雯 committed
254 255
            disabled={hidden}
          />
皮倩雯's avatar
皮倩雯 committed
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
        </Item>
        <Item
          label="下游路由模板"
          name="DownstreamPathTemplate"
          rules={[
            {
              validator: (rule, value) => {
                let aa = form.getFieldValue().DownstreamPathTemplate;
                console.log(aa.startsWith('/'));
                if (!aa.startsWith('/')) {
                  return Promise.reject('必须以/开头');
                }
                return Promise.resolve();
              },
            },
            {
              required: true,
              message: '请输入下游路由模板',
            },
          ]}
        >
皮倩雯's avatar
皮倩雯 committed
277
          <Input allowClear placeholder="示例:/{url}" disabled={hidden} />
皮倩雯's avatar
皮倩雯 committed
278 279 280 281 282 283 284 285 286 287 288
        </Item>
        <Item
          label="上游请求方式"
          name="Methods"
          rules={[
            {
              required: true,
              message: '请选择上游请求方式',
            },
          ]}
        >
皮倩雯's avatar
皮倩雯 committed
289
          <Checkbox.Group options={plainOptions} style={{ display: 'flex' }} disabled={hidden} />
皮倩雯's avatar
皮倩雯 committed
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
        </Item>
        <Item
          label="下游服务地址"
          name="Url"
          rules={[
            {
              required: true,
              message: '请输入下游服务地址',
            },
          ]}
        >
          <Input allowClear placeholder="示例:http://localhost:8050" />
        </Item>
        <Item label="身份认证" name="IsAuthentication">
          <Radio.Group>
皮倩雯's avatar
皮倩雯 committed
305 306 307 308 309 310
            <Radio value={true} disabled={hidden}>
              开启
            </Radio>
            <Radio value={false} disabled={hidden}>
              关闭
            </Radio>
皮倩雯's avatar
皮倩雯 committed
311 312 313 314 315 316 317 318 319 320
          </Radio.Group>
        </Item>
        <Item
          label="关键字"
          name="Key"
          rules={[
            {
              required: true,
              message: '请输入关键字',
            },
皮倩雯's avatar
皮倩雯 committed
321 322
            {
              validator: (rule, value) => {
323 324
                let val = form.getFieldValue().Key;
                if (/[\u4E00-\u9FA5]/.test(val)) {
皮倩雯's avatar
皮倩雯 committed
325 326
                  return Promise.reject('不允许输入中文');
                }
皮倩雯's avatar
皮倩雯 committed
327
                if (type === 'edit') {
328
                  if (keepData.indexOf(val) !== -1 && pickItem.key !== val) {
皮倩雯's avatar
皮倩雯 committed
329 330
                    return Promise.reject('关键字已存在');
                  }
331
                } else if (keepData.indexOf(val) !== -1) {
皮倩雯's avatar
皮倩雯 committed
332 333 334 335 336 337
                  return Promise.reject('关键字已存在');
                }

                return Promise.resolve();
              },
            },
皮倩雯's avatar
皮倩雯 committed
338 339
          ]}
        >
皮倩雯's avatar
皮倩雯 committed
340
          <Input allowClear disabled={hidden} />
皮倩雯's avatar
皮倩雯 committed
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
        </Item>
        <Row>
          <Col span={8}>
            <Item
              // label={
              //   <div>
              //     <Tooltip title="0默认级别最低,10最高,优先级越高越先匹配">
              //       <InfoCircleOutlined
              //         style={{
              //           color: 'rgb(24, 144, 255)',
              //           marginLeft: '0px',
              //           marginRight: '5px',
              //         }}
              //       />
              //     </Tooltip>
              //     <span>优先级</span>
              //   </div>
              // }
              label="优先级"
              name="Priority"
              labelCol={{ span: 12 }}
            >
皮倩雯's avatar
皮倩雯 committed
363
              <InputNumber min={0} max={10} defaultValue={0} disabled={hidden} />
皮倩雯'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 393 394 395 396 397 398 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
            </Item>
          </Col>
          <Col span={16}>
            <Item>
              <span style={{ color: 'red' }}>0默认级别最低,10最高,优先级越高越先匹配</span>
            </Item>
          </Col>
        </Row>
        {/* <Switch
          checkedChildren="高级设置"
          unCheckedChildren="高级设置"
          onChange={change}
          checked={current}
          style={{ marginLeft: '40px', marginBottom: '15px' }}
        />
        {advanced === 1 ? (
          <>
            <Item label="上游host" name="UpstreamHost">
              <Input allowClear />
            </Item>
            <Item label="头部信息" name="AddHeasersToRequest">
              <Input allowClear />
            </Item>
            <Item label="上游头信息转发" name="UpstreamHeaderTransform">
              <Input allowClear />
            </Item>
            <Item label="下游头信息转发" name="DownstreamHeaderTransform">
              <Input allowClear />
            </Item>

            <Item label="超时设置" name="Timeout">
              <Input allowClear />
            </Item>
            <Item label="服务质量与熔断" name="QoSOptions">
              <Input allowClear />
            </Item>
            <Item label="限流配置" name="RateLimitOptions">
              <Input allowClear />
            </Item>
            <Item label="缓存" name="CacheOptions">
              <Input allowClear />
            </Item>
            <Item label="负载均衡" name="LoadBalancerOptions">
              <Input allowClear />
            </Item>
            <Item label="安全配置" name="SecurityOptions">
              <Input allowClear />
            </Item>
            <Item label="请求Id Key" name="RequestIdKey">
              <Input allowClear />
            </Item>
            <Item label="服务名" name="ServiceName">
              <Input allowClear />
            </Item>
            <Item label="服务空间" name="ServiceNamespace">
              <Input allowClear />
            </Item>
            <Item label="委托配置" name="DelegatingHandlers">
              <Input allowClear />
            </Item>
            <Item label="路由大小写敏感" name="ReRouteIsCaseSensitive">
              <Radio.Group>
                <Radio value={0}>否</Radio>
                <Radio value={1}>是</Radio>
              </Radio.Group>
            </Item>
            <Item label="下游请求方式" name="DownstreamHttpMethod">
              <Checkbox.Group options={plainOptions} />
            </Item>
          </>
        ) : (
          ''
        )} */}
      </Form>
    </Modal>
  );
};
export default AddModal;