index.jsx 12.3 KB
Newer Older
邓超's avatar
邓超 committed
1 2 3
/* eslint-disable no-new */
import React, { useEffect, useState, useRef } from 'react';
import { ArcGISMap, Drawtool, geomUtils } from '@wisdom-map/arcgismap';
4 5 6 7 8 9 10 11 12 13 14 15
import {
  Form,
  Input,
  Slider,
  InputNumber,
  Select,
  Button,
  Cascader,
  notification,
  Tooltip,
} from 'antd';
import { EnvironmentOutlined } from '@ant-design/icons';
邓超's avatar
邓超 committed
16 17 18 19 20
import classnames from 'classnames';
import { GetWebSiteConfig } from '@/services/gis/gis';
import { SetServiceConfig } from '@/services/webConfig/api';
import styles from './index.less';
const { Option } = Select;
21 22 23 24 25 26 27 28 29 30 31 32
const defaultMap = {
  basemaps: [
    {
      id: '高德地形',
      title: '高德地形',
      thumbnailUrl: 'assets/images/thumbnail_2.jpg',
      baseLayers: [
        {
          title: '高德地形',
          icon: '',
          layerType: 'amap-v',
          url: '',
邓超's avatar
邓超 committed
33
          opacity: 1,
34 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
          visible: false,
          useProxy: false,
          proxyUrl: '',
          style: '',
          extent: '',
          baseLayer: '',
          levelStart: '',
          levelEnd: '',
          levelEndEnlarge: false,
          resolution: '',
          origin: '',
          tileMatrix: '',
        },
      ],
    },
  ],
  id: '',
  title: '',
  icon: '',
  layerType: 'PipenetLayer',
  url: '',
  opacity: 1,
  showLegend: true,
  visible: true,
  useProxy: true,
  proxyUrl: '',
  extent: '',
  baseLayer: '',
  levelStart: '',
  levelEnd: null,
  resolution: '',
  origin: '',
  tileMatrix: '',
  wmtsUrl: '',
  schemename: '',
  roles: '',
  areaName: '上海市',
  boundColor: '#86c8f8',
  backgroundColor: '#000000',
  boundWidth: '10px',
  backgroundOpacity: '0.6',
};
邓超's avatar
邓超 committed
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 102 103 104 105 106 107 108 109 110 111 112 113
const widgets = [
  {
    label: '缩放',
    right: 20,
    bottom: 130,
    config: {},
  },
  {
    label: '坐标',
    left: 20,
    bottom: 20,
    config: {},
  },
  {
    label: '比例尺',
    left: 60,
    bottom: 70,
    config: {},
  },
  {
    label: '复位',
    right: 20,
    bottom: 90,
    config: {},
  },
  {
    label: '图层',
    right: 20,
    bottom: 50,
    config: {},
  },
  {
    label: '底图切换',
    right: 60,
    bottom: 45,
    config: {},
  },
];
邓超's avatar
邓超 committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
const Map = props => {
  const { visible, onCancel, schemename } = props;
  const [form] = Form.useForm();
  const [canLoadMap, setCanLoadMap] = useState(false); // 是否加载地图
  const [colorArr, setColorArr] = useState([
    { color: '#ffffff', isCheck: false },
    { color: '#000000', isCheck: false },
    { color: '#9E9E9E', isCheck: false },
    { color: '#009688', isCheck: false },
    { color: '#2196f3', isCheck: false },
    { color: '#19233c', isCheck: false },
  ]);
  const [schemeList, setSchemeList] = useState([]);
  const [options, setOptions] = useState([]);
  const [mapsettings, setMapsettings] = useState({});
129
  const [setttingsSave, setSetttingsSave] = useState({});
邓超's avatar
邓超 committed
130 131 132
  const [flag, setFlag] = useState(0);
  const mapRef = useRef();
  const mapInfo = useRef(null);
邓超's avatar
邓超 committed
133

邓超's avatar
邓超 committed
134 135 136
  useEffect(() => {
    GetWebSiteConfig({ client: 'sandbox' }).then(res => {
      console.log(res.data[0].mapsettings, 'res.data[0].mapsettings');
137 138 139 140 141 142
      let setttings = {};

      if (res.data[0].mapsettings.layers.length === 0) {
        res.data[0].mapsettings.layers = [defaultMap];
        setttings = defaultMap;
      }
邓超's avatar
邓超 committed
143 144
      setSchemeList(res.data[0].mapsettings.layers);
      setMapsettings(res.data[0].mapsettings);
145
      setSetttingsSave(res.data[0].mapsettings);
146

邓超's avatar
邓超 committed
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
      res.data[0].mapsettings.layers.forEach(item => {
        if (item.layerType === 'PipenetLayer') {
          setttings = item;
        }
      });
      setTimeout(() => {
        form.setFieldsValue({
          areaName: [setttings.areaName],
          backgroundColor: setttings.backgroundColor,
          backgroundOpacity: setttings.backgroundOpacity * 100,
          boundColor: setttings.boundColor,
          boundWidth: parseInt(setttings.boundWidth),
          extent: setttings.extent,
          schemename: setttings.schemename,
        });

        setCanLoadMap(true);
      }, 0);
    });
    // 获取城市选择器列表
    window.AMap.plugin('AMap.DistrictSearch', () => {
      let districtSearch = new AMap.DistrictSearch({
        // 关键字对应的行政区级别,country表示国家
        level: 'country',
        //  显示下级行政区级数,1表示返回下一级行政区
        subdistrict: 3,
      });

      // 搜索所有省/直辖市信息
      districtSearch.search('中国', (status, result) => {
        // 查询成功时,result即为对应的行政区信息
        console.log(result, '行政区划信息');
邓超's avatar
邓超 committed
179
        result.districtList[0].districtList.push({ adcode: '100000', name: '中华人民共和国' });
邓超's avatar
邓超 committed
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        setOptions(result.districtList[0].districtList);
      });
    });
  }, []);
  // 获取地图实例
  const getMapInfo = viewObject => {
    mapInfo.current = viewObject;
  };
  // 选择颜色
  const checkColor = color => {
    form.setFieldsValue({ backgroundColor: color });
    mapRef.current.updateAreaColor(color);
    setFlag(flag + 1);
  };
  // 表单修改后对地图进行配置
  const changeValue = changedFields => {
邓超's avatar
邓超 committed
196 197 198 199
    if (changedFields.length === 0) {
      return;
    }
    console.log(changedFields, 'changedFields[0].name[0]');
200
    let obj = JSON.parse(JSON.stringify(setttingsSave));
邓超's avatar
邓超 committed
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 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
    const index = obj.layers.findIndex(
      item => item.schemename === form.getFieldValue('schemename'),
    );
    switch (changedFields[0].name[0]) {
      case 'backgroundOpacity':
        obj.layers[index].backgroundOpacity = changedFields[0].value / 100;
        mapRef.current.changeBackgroundOpacity(obj.layers[index].backgroundOpacity);
        break;
      case 'boundWidth':
        obj.layers[index].boundWidth = changedFields[0].value;
        mapRef.current.changeBoundWidthValue(obj.layers[index].boundWidth);
        break;
      case 'areaName':
        Drawtool.deactivate(false);
        obj.layers[index].areaName = changedFields[0].value[changedFields[0].value.length - 1];
        mapRef.current.changeAreaName(obj.layers[index].areaName);
        break;
      case 'schemename':
        Drawtool.deactivate(false);
        let setttings;
        obj.layers.forEach(item => {
          if (item.schemename === changedFields[0].value) {
            item.layerType = 'PipenetLayer';
            setttings = item;
          } else {
            item.layerType = 'dynamic';
          }
        });
        form.setFieldsValue({
          areaName: [setttings.areaName],
          backgroundColor: setttings.backgroundColor,
          backgroundOpacity: setttings.backgroundOpacity * 100,
          boundColor: setttings.boundColor,
          boundWidth: parseInt(setttings.boundWidth),
          extent: setttings.extent,
        });
        setMapsettings(obj);
        break;

      default:
        break;
    }
  };
  // 选择范围
  const onTangleClick = e => {
    Drawtool.activate({
      view: mapInfo.current,
      action: 'extent',
      drawEnd: data => {
        const geom = geomUtils.toGeometry({
          type: 'extent',
          xmin: data.rings[0][0][0],
          xmax: data.rings[0][2][0],
          ymin: data.rings[0][0][1],
          ymax: data.rings[0][1][1],
        });
        form.setFieldsValue({
          extent: `${data.rings[0][0][0]},${data.rings[0][0][1]},${data.rings[0][2][0]},${
            data.rings[0][1][1]
          }`,
        });
        mapRef.current.gotoGeometry(geom);
      },
    });
  };
  // 提交
  const onFinish = () => {
    if (!Drawtool.graphic) {
      notification.error({
        message: '提示',
        duration: 3,
        description: '请选择复位范围再保存',
      });
      return;
    }
    const obj = form.getFieldsValue();
    const jsConfig = {
      extent: form.getFieldValue('extent'),
      areaName: obj.areaName[obj.areaName.length - 1],
      boundColor: '#86c8f8',
      boundWidth: `${obj.boundWidth}px`,
      backgroundColor: obj.backgroundColor,
      backgroundOpacity: obj.backgroundOpacity / 100,
    };
    SetServiceConfig({
      schemename: obj.schemename,
      terminalType: 'web',
      isBaseMap: false,
      jsonCfg: JSON.stringify(jsConfig),
    }).then(res => {
      if (res.code === 0) {
        notification.success({
          message: '提示',
          duration: 3,
          description: '保存成功',
        });
邓超's avatar
邓超 committed
297 298 299 300 301 302 303 304 305 306
        let mapObj = JSON.parse(JSON.stringify(mapsettings));
        const index = mapObj.layers.findIndex(
          item => item.schemename === form.getFieldValue('schemename'),
        );
        mapObj.layers[index].backgroundOpacity = obj.backgroundOpacity / 100;
        mapObj.layers[index].boundWidth = `${obj.boundWidth}px`;
        mapObj.layers[index].areaName = obj.areaName[obj.areaName.length - 1];
        mapObj.layers[index].backgroundColor = obj.backgroundColor;
        mapObj.layers[index].extent = form.getFieldValue('extent');
        console.log(mapObj);
307
        setSetttingsSave(mapObj);
邓超's avatar
邓超 committed
308
        Drawtool.deactivate(false);
邓超's avatar
邓超 committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
      } else {
        notification.error({
          message: '提示',
          duration: 3,
          description: res.msg,
        });
      }
    });
    SetServiceConfig({
      schemename: obj.schemename,
      terminalType: 'phone',
      isBaseMap: false,
      jsonCfg: JSON.stringify({ extent: form.getFieldValue('extent') }),
    }).then();
  };
324
  console.log(mapsettings);
邓超's avatar
邓超 committed
325 326 327 328 329 330
  return (
    <div>
      <div className={styles.mapContent}>
        <div className={styles.mapTool}>
          <Form
            form={form}
331 332
            labelCol={{ span: 6 }}
            wrapperCol={{ span: 18 }}
邓超's avatar
邓超 committed
333 334 335 336 337 338 339 340 341 342 343 344
            onFieldsChange={changeValue}
            onFinish={onFinish}
          >
            <Form.Item label="切换方案" name="schemename">
              <Select placeholder="请选择方案">
                {schemeList.map((item, index) => (
                  <Option value={item.schemename} key={index}>
                    {item.schemename}
                  </Option>
                ))}
              </Select>
            </Form.Item>
345
            <Form.Item label="地区选择" name="areaName">
邓超's avatar
邓超 committed
346 347 348 349 350 351 352 353 354 355 356 357 358 359
              <Cascader
                fieldNames={{
                  label: 'name',
                  value: 'name',
                  children: 'districtList',
                }}
                showSearch
                options={options}
                placeholder="请选择行政区"
                changeOnSelect
                allowClear={false}
              />
            </Form.Item>

360
            <Form.Item label="遮罩颜色" name="backgroundColor">
邓超's avatar
邓超 committed
361 362 363 364 365 366 367 368 369 370 371 372 373
              <div className={styles.colorContent}>
                {colorArr.map(item => (
                  <div
                    className={classnames(styles.colorBox, {
                      [styles.currentColor]: item.color === form.getFieldValue('backgroundColor'),
                    })}
                    key={item.color}
                    style={{ background: item.color }}
                    onClick={() => checkColor(item.color)}
                  />
                ))}
              </div>
            </Form.Item>
374
            <Form.Item label="透明度(%)" name="backgroundOpacity">
邓超's avatar
邓超 committed
375 376
              <Slider min={0} max={100} />
            </Form.Item>
377
            <Form.Item label="边界宽度" name="boundWidth">
邓超's avatar
邓超 committed
378 379 380
              <InputNumber min={1} max={10} />
            </Form.Item>

381 382 383 384 385 386 387 388 389 390 391 392 393
            <Form.Item label="复位范围">
              <div style={{ display: 'flex' }}>
                <Form.Item name="extent" style={{ marginBottom: '0', width: '100%' }}>
                  <Input.TextArea style={{ resize: 'none', height: '76px' }} disabled />
                </Form.Item>
                <Tooltip title="复位范围框选">
                  <Button
                    style={{ height: '76px', borderLeft: 'none' }}
                    icon={<EnvironmentOutlined style={{ marginTop: '5px' }} />}
                    onClick={e => onTangleClick(e)}
                  />
                </Tooltip>
              </div>
邓超's avatar
邓超 committed
394
            </Form.Item>
395
            <Form.Item wrapperCol={{ offset: 6, span: 18 }}>
邓超's avatar
邓超 committed
396 397 398 399 400 401 402 403
              <Button type="primary" htmlType="submit">
                提交
              </Button>
            </Form.Item>
          </Form>
        </div>
        <div className={styles.mapBox}>
          {canLoadMap && (
邓超's avatar
邓超 committed
404 405 406 407 408 409
            <ArcGISMap
              ref={mapRef}
              getMapInfo={e => getMapInfo(e)}
              config={mapsettings}
              widgets={widgets}
            />
邓超's avatar
邓超 committed
410 411 412 413 414 415 416 417
          )}
        </div>
      </div>
    </div>
  );
};

export default Map;