AddModal.jsx 7.45 KB
Newer Older
1
import React, { useState, useEffect } from 'react';
2
import { Form, Modal, Input, Select, notification, Button } from 'antd';
3
import {
4 5 6 7
  AddSchema,
  AddSchemaBaseMap,
  bindSchemeBaseMap,
  GetBaseMapList,
8
  GetVectorDataList,
9
} from '@/services/webConfig/api';
10
import MapScope from '@/components/ThreeMapScope';
11 12 13
const { Item } = Form;
const { Option } = Select;
const AddModal = props => {
14
  const { callBackSubmit = () => {}, type, formObj, visible, serviceList } = props;
15 16 17
  const [loading, setLoading] = useState(false);
  const [baseMap, setBaseMap] = useState([]);
  const [pipeArr, setPipeArr] = useState([]);
18 19
  const [data, setData] = useState([]);
  const [mapSettings, setMapSettings] = useState({ areaName: '选择视角' });
20 21
  const [mapScopeVisible, setMapScopeVisible] = useState(false);
  const [baseMapData, setBaseMapData] = useState([]);
22
  const [form] = Form.useForm();
23
  const baseMapList = {
24 25 26 27 28 29 30 31 32
    'amap-v': '高德街道',
    'amap-i': '高德影像',
    'tianditu-v': '天地图街道',
    'tianditu-i': '天地图影像',
    baiduMapStreet: '百度街道',
    baiduMapImage: '百度影像',
    mapBoxImage: 'mapBox地图',
    arcgisImage: 'arcgis地图',
  };
33 34 35 36 37 38
  const onSubmit = () => {
    form.validateFields().then(validate => {
      if (validate) {
        setLoading(true);
        let obj = form.getFieldsValue();
        if (type === 'add') {
39
          AddSchemaBaseMap({
40
            schemename: formObj.schemename,
41
            type: obj.serverName,
42 43
          }).then(res => {
            setLoading(false);
shaoan123's avatar
shaoan123 committed
44
            if (res.msg === '') {
45 46
              form.resetFields();
              callBackSubmit();
47 48 49
              prompt('success', '瓦片新增成功');
            } else {
              prompt('fail', '瓦片新增失败');
50
            }
51
          });
52 53 54 55 56 57 58 59 60 61 62 63 64
        } else {
          handleEdit();
        }
      }
    });
  };
  const prompt = (type, content) => {
    if (type == 'success') {
      notification.success({
        message: '提示',
        duration: 3,
        description: content,
      });
65
    } else {
66 67 68 69 70 71
      notification.error({
        message: '提示',
        duration: 3,
        description: content,
      });
    }
72
  };
73 74
  const handleEdit = () => {
    let obj = form.getFieldsValue();
75
    AddSchema({
76
      schemename: obj.schemename,
77 78
      data,
      mapSettings: mapSettings.areaName === '选择视角' ? {} : mapSettings,
79
      baseMap: baseMapData,
80
    })
81 82
      .then(res => {
        setLoading(false);
83
        if (res.msg === '') {
84 85
          form.resetFields();
          callBackSubmit();
86
          prompt('success', '方案新增成功');
87
        } else {
88
          prompt('fail', res.msg);
89 90 91 92 93 94 95
        }
      })
      .catch(err => {
        setLoading(false);
      });
  };

96
  const onFinish = value => {};
97 98 99
  useEffect(() => {
    switch (type) {
      case 'add':
100
        console.log('serviceList', serviceList);
101
        addTile();
102 103
        break;
      case 'schemeAdd':
104
        pipeNetwork();
105 106 107 108 109 110
        break;
      default:
        break;
    }
  }, [visible]);

111
  // 添加瓦片
112 113
  const addTile = () => {
    form.setFieldsValue({
114 115 116 117
      serverName: serviceList[0],
    });
  };
  // 获取管网及默认底图
118 119
  const pipeNetwork = () => {
    form.resetFields();
120 121 122 123 124
    setMapSettings({ areaName: '选择视角' });
    let req1 = GetBaseMapList();
    let req2 = GetVectorDataList();
    let pipeArr = [],
      baseMap = [];
125 126
    Promise.all([req1, req2]).then(res => {
      if (res[0].msg === 'Ok') {
127
        setBaseMap(res[0].data);
128 129
      }
      if (res[1].msg === 'Ok') {
130
        (res[1].data || []).map(item => {
131 132
          pipeArr.push(item.id);
        });
133
      }
134
      setPipeArr(pipeArr);
135
      form.setFieldsValue({
136 137 138 139
        baseMap: res[0].data[0].name,
      });
    });
  };
140 141 142 143 144 145
  const layout = {
    layout: 'horizontal',
    labelCol: {
      span: 4,
    },
    wrapperCol: {
146
      span: 18,
147 148 149
    },
  };

150 151 152 153 154 155
  // 选择服务名
  const handleChange = value => {};
  // 选择管网
  const handleService = value => {
    setData(value);
  };
156

157
  // 选择底图
158
  const handleBaseMap = (value, option) => {
159
    let baseMapDataArr = [];
160
    value.map((item, index) => {
161 162 163 164 165 166
      baseMapDataArr.push({ type: item, status: index == 0 ? 'active' : 'notActive' });
    });
    setBaseMapData(baseMapDataArr);
  };
  const submitExtent = mapSettings => {
    setMapScopeVisible(false);
167
    if (JSON.stringify(mapSettings) != '{}') {
168
      setMapSettings(mapSettings);
169
      form.setFieldsValue({
170 171
        camera: mapSettings.areaName,
      });
172
    }
173
  };
174
  return (
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
    <>
      <Modal
        title={`${type === 'add' ? '添加底图' : '添加方案'}`}
        bodyStyle={{ width: '100%', minHeight: '100px' }}
        style={{ top: '150px' }}
        width="700px"
        destroyOnClose
        maskClosable={false}
        cancelText="取消"
        okText="确认"
        {...props}
        onOk={() => onSubmit()}
        confirmLoading={loading}
        forceRender={true}
        getContainer={false}
      >
        {visible && (
          <Form form={form} {...layout} onFinish={onFinish}>
            {type === 'add' ? (
              <Item label="服务名" name="serverName">
                <Select onChange={handleChange}>
                  {serviceList.length
                    ? serviceList.map((item, index) => {
                        return (
                          <Option key={index} value={item}>
                            {baseMapList[item]}
                          </Option>
                        );
                      })
                    : ''}
205 206
                </Select>
              </Item>
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
            ) : (
              <>
                <Item
                  label="方案名"
                  name="schemename"
                  rules={[{ required: true, message: '请输入方案名' }]}
                >
                  <Input placeholder="请输入方案名" allowClear />
                </Item>
                <Item label="数据源">
                  <Select onChange={handleService} mode="multiple">
                    {pipeArr.length
                      ? pipeArr.map((item, index) => {
                          return (
                            <Option key={index} value={item}>
                              {item}
                            </Option>
                          );
                        })
                      : ''}
                  </Select>
                </Item>
                <Item label="瓦片" name="" rules={[{ required: true, message: '请选择瓦片' }]}>
                  <Select onChange={handleBaseMap} mode="multiple">
                    {baseMap.length
                      ? baseMap.map((item, index) => {
                          return (
                            <Option key={index} value={item.type}>
                              {item.name}
                            </Option>
                          );
                        })
                      : ''}
                  </Select>
                </Item>
                <Item label="视角" name="camera">
                  <Button style={{ width: '100%' }} onClick={() => setMapScopeVisible(true)}>
                    {mapSettings.areaName}
                  </Button>
                </Item>
              </>
            )}
          </Form>
        )}
      </Modal>
252 253 254
      <MapScope
        visible={mapScopeVisible}
        onCancel={() => setMapScopeVisible(false)}
255 256 257 258 259 260
        baseMapData={baseMapData}
        baseMap={baseMap}
        handleType="add"
        confirmModal={submitExtent}
      />
    </>
261 262 263
  );
};
export default AddModal;