index.jsx 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import getWeather from '@/components/AMap/weather';
  2. // import GeoMap from '@/components/GeoMap';
  3. import ScreenHeader from '@/components/ScreenBox/ScreenHeader';
  4. import classNames from 'classnames';
  5. import { useEffect, useRef, useState } from 'react';
  6. import { getDeviceMachineryList } from '@/services/monitoringScreen';
  7. import { regeo } from '@/components/AMap/service';
  8. import { useParticlesJs } from './hook';
  9. import Map from './components/map';
  10. import Banner from './components/Banner';
  11. import DeviceList from './components/DeviceList';
  12. import DeviceType from './components/DeviceType';
  13. import makeMachinery from './components/machinery';
  14. import MachineryInfo from './components/MachineryInfo';
  15. import Styles from './style.less';
  16. export default (props) => {
  17. const screenRef = useRef();
  18. const mapRef = useRef();
  19. const aMapRef = useRef();
  20. const [mapReady, setMapReady] = useState(false);
  21. const [weather, setWeather] = useState('暂无天气信息');
  22. const [machines, setMachines] = useState([]);
  23. const machineMarkers = useRef([]);
  24. const [deviceType, setDeviceType] = useState();
  25. const [curMachine, setCurMachine] = useState();
  26. const curPos = useRef();
  27. const infoWinRef = useRef();
  28. const onMapReady = (map, AMap) => {
  29. mapRef.current = map;
  30. aMapRef.current = AMap;
  31. setMapReady(true);
  32. };
  33. useEffect(() => {
  34. if (mapReady) {
  35. makeMachinery(
  36. machines,
  37. machineMarkers.current,
  38. mapRef.current,
  39. aMapRef.current,
  40. (machine, position) => {
  41. // 创建信息窗体
  42. setCurMachine({ ...machine });
  43. curPos.current = position;
  44. },
  45. );
  46. }
  47. }, [mapReady, machines]);
  48. useEffect(() => {
  49. if (mapReady) {
  50. const map = mapRef.current;
  51. const AMap = aMapRef.current;
  52. if (curMachine) {
  53. const infoWindow = new AMap.InfoWindow({
  54. isCustom: true, //使用自定义窗体
  55. content: infoWinRef.current,
  56. offset: new AMap.Pixel(0, -45),
  57. });
  58. infoWindow.on('close', () => {
  59. map.clearInfoWindow();
  60. });
  61. infoWindow.open(map, curPos.current);
  62. } else {
  63. map.clearInfoWindow();
  64. }
  65. }
  66. }, [curMachine, mapReady]);
  67. useEffect(() => {
  68. const params = deviceType ? { deviceType } : undefined;
  69. getDeviceMachineryList(params).then((res) => {
  70. const locIds = [];
  71. const locList = [];
  72. const list = res || [];
  73. for (let item of list) {
  74. if (item.location && item.location.indexOf('0.0') === -1) {
  75. locIds.push(item.machineryId);
  76. locList.push(item.location);
  77. }
  78. }
  79. if (locList.length > 0) {
  80. // 高德接口, 请求地址的描述
  81. regeo({ location: locList.join('|') })
  82. .then((regs) => {
  83. if (regs.status === '1') {
  84. regs.regeocodes.forEach((addrs, inx) => {
  85. const addr = addrs['formatted_address'];
  86. const addressComponent = addrs.addressComponent;
  87. const machine = list.filter((x) => x.machineryId === locIds[inx])[0];
  88. machine.loc = addr.replace(addressComponent.province, ''); // 去掉省
  89. });
  90. }
  91. setMachines(list);
  92. })
  93. .catch((er) => {
  94. console.error(er);
  95. setMachines(list);
  96. });
  97. }
  98. });
  99. }, [deviceType]);
  100. useEffect(() => {
  101. getWeather()
  102. .then((res) => {
  103. if (res && res.length) {
  104. const { casts, city } = res[0];
  105. const { dayweather, nighttemp, daytemp } = casts[0];
  106. const [min, max] =
  107. parseInt(nighttemp) > parseInt(daytemp) ? [daytemp, nighttemp] : [nighttemp, daytemp];
  108. setWeather(`${city} ${dayweather} ${min}-${max} °C`);
  109. } else {
  110. setWeather('暂无天气信息');
  111. }
  112. })
  113. .catch((err) => {
  114. console.log(err.message);
  115. });
  116. }, []);
  117. useParticlesJs(Styles['particles-js']);
  118. return (
  119. <div className={classNames(Styles['screen-page'], Styles['pd-lr-40'])} ref={screenRef}>
  120. <div id={Styles['particles-js']} />
  121. <div className={Styles['grail-layout']}>
  122. <div className={Styles['grail-header']}>
  123. <ScreenHeader weather={weather} />
  124. </div>
  125. <div className={classNames(Styles['grail-container'], Styles['mg-tp-30'])}>
  126. <div className={classNames(Styles['grail-content'], Styles['pd-lr-40'])}>
  127. <div className={Styles['statis-container']}>
  128. <Banner />
  129. </div>
  130. <Map onReady={onMapReady}>
  131. <div
  132. className={Styles['map-addon']}
  133. style={{
  134. top: '60px',
  135. left: '60px',
  136. width: '120px',
  137. }}
  138. >
  139. <DeviceType onChange={setDeviceType} />
  140. </div>
  141. </Map>
  142. </div>
  143. </div>
  144. </div>
  145. <div style={{ position: 'absolute', zIndex: -1 }}>
  146. <MachineryInfo
  147. data={curMachine}
  148. ref={infoWinRef}
  149. onClose={() => {
  150. mapRef.current.clearInfoWindow();
  151. }}
  152. />
  153. </div>
  154. </div>
  155. );
  156. };