map.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693
  1. <template>
  2. <view class="container">
  3. <map
  4. id="map"
  5. style="width: 100%; height: 100%; z-index: 1"
  6. :latitude="mapProps.latitude"
  7. :longitude="mapProps.longitude"
  8. :markers="markers"
  9. min-scale="1"
  10. :scale="mapProps.scale"
  11. @regionchange="mapChange"
  12. @updated="mapUpdated"
  13. @labeltap="tapMarker"
  14. @markertap="tapMarker"
  15. :show-scale="true"
  16. ></map>
  17. <view class="card" v-if="ready">
  18. <swiper
  19. class="mt-68"
  20. :autoplay="false"
  21. @change="changeMarker"
  22. :current="markersIndex"
  23. >
  24. <block v-if="!empty">
  25. <swiper-item v-for="(item, index) in station" :key="index">
  26. <view class="station">
  27. <charge-station
  28. :title="item.stationName"
  29. :address="item.address"
  30. :price="item.totalFee"
  31. :fast="item.fastEquipmentInfos"
  32. :slow="item.slowEquipmentInfos"
  33. :sId="item.StationID"
  34. :distance="item.stationLatDistance"
  35. :latitude="item.location.stationLat"
  36. :longitude="item.location.stationLng"
  37. :fromMap="true"
  38. ></charge-station>
  39. </view>
  40. </swiper-item>
  41. </block>
  42. <block v-else>
  43. <swiper-item>
  44. <view class="station">
  45. <view class="station-empty flex-column flex-align-center pt-20">
  46. <image src="/static/images/map-empty.png" mode="widthFix" />
  47. <view class="fs-22 mt-14" style="color: rgba(0, 0, 0, 0.5)"
  48. >暂无充电站信息</view
  49. >
  50. </view>
  51. </view>
  52. </swiper-item>
  53. </block>
  54. </swiper>
  55. </view>
  56. <view class="icon-menu" v-if="menuStyle.menu1" :style="menuStyle.menu1">
  57. <view class="flex-center mt-40" @click="search" hover-class="hover">
  58. <image src="/static/images/map-search.png" mode="widthFix" />
  59. </view>
  60. <view
  61. class="flex-center mt-40"
  62. @click="toggleDialogVisible"
  63. hover-class="hover"
  64. >
  65. <image src="/static/images/map-filter.png" mode="widthFix" />
  66. </view>
  67. </view>
  68. <view class="icon-menu" v-if="menuStyle.menu2" :style="menuStyle.menu2">
  69. <view class="flex-center" hover-class="hover" @click="resetLocation">
  70. <image src="/static/images/map-location.png" mode="widthFix" />
  71. </view>
  72. </view>
  73. <view
  74. class="dialog"
  75. v-if="filterDialog.visible"
  76. @click="toggleDialogVisible"
  77. >
  78. <view class="filter-dialog" @click.stop="emptyTap">
  79. <view :style="filterDialog.style"></view>
  80. <view class="pl-40 pr-40">
  81. <view class="pt-20 pb-20">
  82. <text class="fs-30 fw-500">距离</text>
  83. </view>
  84. <view class="flex-wrap pb-14">
  85. <view
  86. :class="[
  87. 'type',
  88. 'flex-shrink',
  89. 'flex-center',
  90. 'mt-20',
  91. (index + 1) % 5 === 0 ? 'mr-0' : 'mr-20',
  92. `type-${
  93. item.value === filterDialog.options.distance ? 'active' : ''
  94. }`,
  95. ]"
  96. v-for="(item, index) in filterDialog.range"
  97. :key="index"
  98. @click.stop="changeFilterDistance(index)"
  99. style="width: 117rpx"
  100. >{{ item.value }}km</view
  101. >
  102. </view>
  103. <view class="fs-30 fw-500 pt-38 pb-20">充电状态</view>
  104. <view class="flex pb-30">
  105. <view
  106. :class="[
  107. 'type',
  108. 'flex-center',
  109. 'mr-20',
  110. `type-${index === filterDialog.options.status ? 'active' : ''}`,
  111. ]"
  112. v-for="(item, index) in filterDialog.status"
  113. :key="index"
  114. @click.stop="changeFilterStatus(index)"
  115. >{{ item.title }}</view
  116. >
  117. </view>
  118. <view class="foot flex-align-center">
  119. <style-button size="small" @click.stop="resetFilter"
  120. >重置</style-button
  121. >
  122. <view style="width: 30rpx"></view>
  123. <style-button size="small" type="primary" @click.stop="submitFilter"
  124. >确定</style-button
  125. >
  126. </view>
  127. </view>
  128. </view>
  129. </view>
  130. </view>
  131. <view class="login-mask" v-if="!token">
  132. <button open-type="getPhoneNumber" @getphonenumber="loginMask" class="full">
  133. 登录按钮
  134. </button>
  135. </view>
  136. </template>
  137. <script setup lang="ts">
  138. const defaulDistance = 3;
  139. const defaultScale = 12;
  140. const pointSize = {
  141. width: 34,
  142. height: 58,
  143. fontSize: 10,
  144. iconPath: "/static/images/map-point.png",
  145. currentWidth: 52,
  146. currentHeight: 86,
  147. currentFontSize: 11,
  148. currentIconPath: "/static/images/map-point-current.png",
  149. androidX: -14,
  150. androidCurrentX: -20,
  151. };
  152. import { deCode } from "../../utils/code";
  153. import { fetchToken, login, onLogin } from "@/api/auth";
  154. import { fetchStations } from "@/api/charge";
  155. import { fetchCollectList } from "@/api/user";
  156. import { fetchLocation } from "@/utils/location";
  157. import { onLoad } from "@dcloudio/uni-app";
  158. import { ref } from "vue";
  159. const isIOS = ref(false);
  160. const token = ref<string>();
  161. const ready = ref(false);
  162. const empty = ref(true);
  163. const mapProps = ref({
  164. latitude: 23.098994,
  165. longitude: 113.32252,
  166. selflatitude: 23.098994,
  167. selflongitude: 113.32252,
  168. scale: defaultScale,
  169. });
  170. const filterDialog = ref({
  171. visible: false,
  172. style: {},
  173. range: [
  174. {
  175. value: 1,
  176. scale: defaultScale + 3,
  177. },
  178. {
  179. value: 2,
  180. scale: defaultScale + 1,
  181. },
  182. {
  183. value: 3,
  184. scale: defaultScale,
  185. },
  186. {
  187. value: 5,
  188. scale: defaultScale - 0.2,
  189. },
  190. {
  191. value: 10,
  192. scale: defaultScale - 0.5,
  193. },
  194. {
  195. value: 20,
  196. scale: defaultScale - 1.5,
  197. },
  198. {
  199. value: 30,
  200. scale: defaultScale - 2,
  201. },
  202. {
  203. value: 50,
  204. scale: defaultScale - 2.5,
  205. },
  206. {
  207. value: 100,
  208. scale: defaultScale - 3.5,
  209. },
  210. {
  211. value: 200,
  212. scale: defaultScale - 4.5,
  213. },
  214. ],
  215. options: {
  216. distance: defaulDistance,
  217. status: 0,
  218. },
  219. status: [
  220. {
  221. title: "全部",
  222. },
  223. {
  224. title: "空闲",
  225. },
  226. {
  227. title: "忙碌",
  228. },
  229. ],
  230. });
  231. const menuStyle = ref({
  232. menu1: {},
  233. menu2: {},
  234. });
  235. const stationPage = ref({
  236. page: 1,
  237. pageSize: 6,
  238. hasNext: false,
  239. });
  240. const station = ref<any[]>([]);
  241. const markersIndex = ref(0);
  242. const markers = ref<any[]>([]);
  243. let isIgnoreChangeLocation = false;
  244. const refreshStation = (location: any) => {
  245. let length = 0;
  246. let available = 0;
  247. const { latitude, longitude } = location;
  248. if (!token.value) {
  249. return;
  250. }
  251. return fetchStations(
  252. stationPage.value.page,
  253. stationPage.value.pageSize,
  254. latitude,
  255. longitude,
  256. mapProps.value.selflatitude,
  257. mapProps.value.selflongitude,
  258. {
  259. distance: filterDialog.value.options.distance,
  260. status: filterDialog.value.options.status,
  261. }
  262. ).then((res) => {
  263. const _markersIndex = stationPage.value.page === 1 ? 0 : markersIndex.value;
  264. const _markers: any[] = res.map((item, index) => {
  265. length = 0;
  266. available = 0;
  267. item.equipmentInfos &&
  268. item.equipmentInfos.forEach((eq: any) => {
  269. eq.connectorInfos &&
  270. eq.connectorInfos.forEach((co: any) => {
  271. length += 1;
  272. if (
  273. co.connectorStatusInfo &&
  274. co.connectorStatusInfo.status === 1
  275. ) {
  276. available += 1;
  277. }
  278. });
  279. });
  280. return {
  281. id: Number(item.StationID),
  282. latitude: item.location.stationLat,
  283. longitude: item.location.stationLng,
  284. iconPath:
  285. index === _markersIndex
  286. ? pointSize.currentIconPath
  287. : pointSize.iconPath,
  288. width:
  289. index === _markersIndex ? pointSize.currentWidth : pointSize.width,
  290. height:
  291. index === _markersIndex ? pointSize.currentHeight : pointSize.height,
  292. label: {
  293. content: `${available}/${length}`,
  294. color: "#ffffff",
  295. fontSize:
  296. index === _markersIndex
  297. ? pointSize.currentFontSize
  298. : pointSize.fontSize,
  299. textAlign: isIOS.value ? "center" : "left",
  300. anchorX: isIOS.value
  301. ? 0
  302. : index === _markersIndex
  303. ? pointSize.androidCurrentX
  304. : pointSize.androidX,
  305. anchorY: -(
  306. (index === _markersIndex
  307. ? pointSize.currentHeight
  308. : pointSize.height) - 3
  309. ),
  310. },
  311. };
  312. });
  313. if (stationPage.value.page === 1) {
  314. _markers.push({
  315. id: -1,
  316. latitude: mapProps.value.selflatitude,
  317. longitude: mapProps.value.selflongitude,
  318. iconPath: "/static/images/map-current.png",
  319. width: 34,
  320. height: 34,
  321. });
  322. }
  323. isIgnoreChangeLocation = true;
  324. stationPage.value.hasNext = res.length >= stationPage.value.pageSize;
  325. empty.value = stationPage.value.page === 1 ? res.length <= 0 : false;
  326. station.value =
  327. stationPage.value.page === 1 ? res : [...station.value, ...res];
  328. markersIndex.value = _markersIndex;
  329. markers.value = _markers;
  330. return res;
  331. });
  332. };
  333. const refresh = () => {
  334. console.log("刷新电站");
  335. uni.showLoading({
  336. title: "加载中",
  337. });
  338. stationPage.value.page = 1;
  339. stationPage.value.hasNext = false;
  340. station.value = [];
  341. markers.value = [];
  342. markersIndex.value = 0;
  343. filterDialog.value.visible = false;
  344. fetchLocation()
  345. .then((res: any) => {
  346. mapProps.value.latitude = res.latitude;
  347. mapProps.value.longitude = res.longitude;
  348. mapProps.value.selflatitude = res.latitude;
  349. mapProps.value.selflongitude = res.longitude;
  350. isIgnoreChangeLocation = true;
  351. return refreshStation(res);
  352. })
  353. .then(() => {
  354. uni.hideLoading();
  355. ready.value = true;
  356. })
  357. .catch((err) => {
  358. console.log(err);
  359. uni.hideLoading();
  360. uni.showModal({
  361. content: `${err.errMsg},请重试`,
  362. });
  363. });
  364. };
  365. onLoad((query: any) => {
  366. // 只为了打包进tab-bar使用
  367. console.log(fetchToken, login, onLogin);
  368. // 扫普通码
  369. if (query.q) {
  370. console.log("扫普通码", decodeURIComponent(query.q));
  371. getApp<any>().globalData.normalCode = decodeURIComponent(query.q); // 获取到二维码原始链接内容
  372. }
  373. const menu = uni.getMenuButtonBoundingClientRect();
  374. const window = uni.getWindowInfo();
  375. const device = uni.getSystemInfoSync();
  376. isIOS.value = device.osName === "ios";
  377. menuStyle.value.menu1 = `top:${menu.top + menu.height}px;right:${
  378. window.windowWidth - menu.right
  379. }px;`;
  380. menuStyle.value.menu2 = `bottom:420rpx;right:${
  381. window.windowWidth - menu.right
  382. }px;margin-bottom: constant(safe-area-inset-bottom);margin-bottom: env(safe-area-inset-bottom);`;
  383. filterDialog.value.style = `height:${menu.bottom + 6}px;`;
  384. setTimeout(() => {
  385. token.value = getApp<any>().globalData.token || "";
  386. if (!token.value) {
  387. isIgnoreChangeLocation = true;
  388. fetchLocation().then((res: any) => {
  389. mapProps.value.latitude = res.latitude;
  390. mapProps.value.longitude = res.longitude;
  391. mapProps.value.selflatitude = res.latitude;
  392. mapProps.value.selflongitude = res.longitude;
  393. markers.value = [
  394. {
  395. id: -1,
  396. latitude: res.latitude,
  397. longitude: res.longitude,
  398. iconPath: "/static/images/map-current.png",
  399. width: 34,
  400. height: 34,
  401. },
  402. ];
  403. });
  404. onLogin((_token) => {
  405. if (getApp<any>().globalData.normalCode) {
  406. const code: string = getApp<any>().globalData.normalCode;
  407. getApp<any>().globalData.normalCode = "";
  408. deCode(code);
  409. }
  410. token.value = _token;
  411. fetchCollectList().then(() => {
  412. refresh();
  413. });
  414. });
  415. return;
  416. }
  417. fetchCollectList().then(() => {
  418. if (getApp<any>().globalData.normalCode) {
  419. const code: string = getApp<any>().globalData.normalCode;
  420. getApp<any>().globalData.normalCode = "";
  421. deCode(code);
  422. }
  423. refresh();
  424. });
  425. }, 300);
  426. });
  427. const toggleDialogVisible = () => {
  428. filterDialog.value.visible = !filterDialog.value.visible;
  429. };
  430. const changeFilterDistance = (index: number) => {
  431. filterDialog.value.options.distance = filterDialog.value.range[index].value;
  432. };
  433. const changeFilterStatus = (index: number) => {
  434. filterDialog.value.options.status = index;
  435. };
  436. const resetFilter = () => {
  437. filterDialog.value.options.distance = defaulDistance;
  438. filterDialog.value.options.status = 0;
  439. refresh();
  440. };
  441. const submitFilter = () => {
  442. const findIndex = filterDialog.value.range.findIndex(
  443. (item) => item.value === filterDialog.value.options.distance
  444. );
  445. if (mapProps.value.scale === filterDialog.value.range[findIndex].scale) {
  446. filterDialog.value.visible = false;
  447. refresh();
  448. return;
  449. }
  450. filterDialog.value.visible = false;
  451. mapProps.value.scale = filterDialog.value.range[findIndex].scale;
  452. };
  453. const resetLocation = () => {
  454. // eslint-disable-next-line promise/catch-or-return
  455. fetchLocation().then((res: any) => {
  456. const mapCtx = uni.createMapContext("map");
  457. const { latitude, longitude } = res;
  458. mapCtx.moveToLocation({
  459. latitude,
  460. longitude,
  461. });
  462. mapProps.value.scale = defaultScale;
  463. });
  464. };
  465. const mapUpdated = (e: any) => {
  466. setTimeout(() => {
  467. isIgnoreChangeLocation = false;
  468. }, 500);
  469. };
  470. const mapChange = (e: any) => {
  471. if (isIgnoreChangeLocation) {
  472. return;
  473. }
  474. if (e.type === "end" && markers.value.length) {
  475. // console.log("refreshStation");
  476. const { latitude, longitude } = e.detail.centerLocation;
  477. stationPage.value.page = 1;
  478. refreshStation({
  479. latitude,
  480. longitude,
  481. });
  482. }
  483. };
  484. const _changeMarker = (current: number) => {
  485. const _markers = JSON.parse(JSON.stringify(markers.value));
  486. const markersNewIndex = current;
  487. _markers[markersIndex.value].iconPath = pointSize.iconPath;
  488. _markers[markersIndex.value].width = pointSize.width;
  489. _markers[markersIndex.value].height = pointSize.height;
  490. _markers[markersIndex.value].label.fontSize = pointSize.fontSize;
  491. _markers[markersIndex.value].label.anchorY = -(pointSize.height - 3);
  492. if (!isIOS) {
  493. _markers[markersIndex.value].label.anchorX = pointSize.androidX;
  494. }
  495. _markers[markersNewIndex].iconPath = pointSize.currentIconPath;
  496. _markers[markersNewIndex].width = pointSize.currentWidth;
  497. _markers[markersNewIndex].height = pointSize.currentHeight;
  498. _markers[markersNewIndex].label.fontSize = pointSize.currentFontSize;
  499. _markers[markersNewIndex].label.anchorY = -(pointSize.currentHeight - 3);
  500. if (!isIOS) {
  501. _markers[markersNewIndex].label.anchorX = pointSize.androidCurrentX;
  502. }
  503. isIgnoreChangeLocation = true;
  504. markers.value = _markers;
  505. markersIndex.value = markersNewIndex;
  506. if (stationPage.value.hasNext && markersNewIndex >= _markers.length - 2) {
  507. stationPage.value.page += 1;
  508. refreshStation({
  509. latitude: mapProps.value.selflatitude,
  510. longitude: mapProps.value.selflongitude,
  511. });
  512. }
  513. };
  514. const changeMarker = (e: any) => {
  515. _changeMarker(e.detail.current);
  516. };
  517. const tapMarker = (e: any) => {
  518. if (e.detail.markerId === -1) {
  519. return;
  520. }
  521. const findIndex = station.value.findIndex(
  522. (item) => Number(item.StationID) === Number(e.detail.markerId)
  523. );
  524. if (findIndex >= 0) {
  525. _changeMarker(findIndex);
  526. }
  527. };
  528. const search = () => {
  529. uni.navigateTo({
  530. url: "/pages-charge/search/search",
  531. });
  532. };
  533. const loginMask = (e: any) => {
  534. login(e);
  535. };
  536. const emptyTap = () => {};
  537. </script>
  538. <style lang="scss">
  539. @import "../../styles/dialog.scss";
  540. page {
  541. background-color: #ffffff;
  542. }
  543. .container {
  544. position: relative;
  545. height: 100vh;
  546. width: 100vw;
  547. background-color: #ffffff;
  548. }
  549. .login-mask {
  550. position: fixed;
  551. left: 0;
  552. top: 0;
  553. width: 100%;
  554. height: 100%;
  555. z-index: 999999;
  556. opacity: 0;
  557. .full {
  558. width: 100%;
  559. height: 100%;
  560. }
  561. }
  562. .icon-menu {
  563. position: absolute;
  564. z-index: 9;
  565. image {
  566. width: 60rpx;
  567. }
  568. & > view {
  569. height: 96rpx;
  570. width: 96rpx;
  571. border-radius: 50%;
  572. background-color: #fff;
  573. box-shadow: 0px 8rpx 20rpx rgba(0, 0, 0, 0.2);
  574. }
  575. .hover {
  576. box-shadow: none;
  577. }
  578. }
  579. .filter-dialog {
  580. background-color: #fff;
  581. .slider {
  582. position: relative;
  583. height: 12rpx;
  584. width: 100%;
  585. border-radius: 8rpx;
  586. background-color: var(--color-sec);
  587. margin-top: 10rpx;
  588. &_active {
  589. position: absolute;
  590. left: 0;
  591. top: 0;
  592. background-color: var(--color-primary);
  593. border-radius: 8rpx;
  594. height: 12rpx;
  595. width: 0%;
  596. }
  597. &_block {
  598. position: absolute;
  599. width: 40rpx;
  600. height: 40rpx;
  601. border-radius: 50%;
  602. top: -15rpx;
  603. left: 0;
  604. background: rgba(255, 255, 255, 1);
  605. box-shadow: 0px 4rpx 6rpx rgba(52, 125, 255, 0.4);
  606. }
  607. &_wx {
  608. position: absolute;
  609. width: 100%;
  610. left: 0px;
  611. top: -5px;
  612. margin: 0;
  613. opacity: 0;
  614. }
  615. }
  616. .type {
  617. width: 160rpx;
  618. height: 60rpx;
  619. background: var(--color-sec);
  620. border-radius: 4rpx;
  621. color: var(--color-gray);
  622. font-size: 26rpx;
  623. border: 1px solid var(--color-sec);
  624. }
  625. .type-active {
  626. border: 1px solid var(--color-primary);
  627. color: var(--color-primary);
  628. }
  629. .foot {
  630. height: 120rpx;
  631. border-top: 1rpx solid var(--color-sec);
  632. }
  633. }
  634. .card {
  635. position: absolute;
  636. width: 100%;
  637. height: 450rpx;
  638. left: 0px;
  639. bottom: 0rpx;
  640. z-index: 9;
  641. background: linear-gradient(180deg, rgba(87, 104, 133, 0) 0%, #576885 100%);
  642. padding-left: 10rpx;
  643. margin-bottom: constant(safe-area-inset-bottom);
  644. margin-bottom: env(safe-area-inset-bottom);
  645. swiper,
  646. swiper-item {
  647. width: 100%;
  648. height: 300rpx;
  649. }
  650. .station {
  651. width: 100%;
  652. height: 100%;
  653. padding: 0rpx 20rpx;
  654. }
  655. .station-empty {
  656. width: 100%;
  657. height: 100%;
  658. background-color: #fff;
  659. border-radius: 20rpx 20rpx 0 0;
  660. image {
  661. width: 160rpx;
  662. }
  663. }
  664. }
  665. </style>