index.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. //index.js
  2. //获取应用实例
  3. let app = getApp();
  4. let wechat = require("../../utils/wechat");
  5. let amap = require("../../utils/amap");
  6. let Util = require("../../utils/util");
  7. let mapregionchange_makertap = false;
  8. Page({
  9. data: {
  10. markers: [],
  11. latitude: 39.9088120620532,
  12. longitude: 116.39747668717192,
  13. user_lat: 39.9088120620532,
  14. user_lon: 116.39747668717192,
  15. textData: null,
  16. city: null,
  17. markerId: null,
  18. keywords: "搜索充电桩",
  19. userInfo: {},
  20. isLogin: false,
  21. myChargeStationsIds: '',
  22. polygon: [{
  23. points: [{
  24. latitude: 31,
  25. longitude: 131
  26. }, {
  27. latitude: 31,
  28. longitude: 131.1
  29. }, {
  30. latitude: 31.1,
  31. longitude: 131.1
  32. }, , {
  33. latitude: 31.1,
  34. longitude: 131
  35. }]
  36. }]
  37. },
  38. onLoad(e) {
  39. amap.getRegeo(function() {
  40. var fail = function(obj) {
  41. wx.showModal({
  42. title: '请求失败',
  43. content: obj.errMsg,
  44. });
  45. }
  46. })
  47. .then(d => {
  48. //console.log(d);
  49. let {
  50. latitude,
  51. longitude,
  52. latitude: user_lat,
  53. longitude: user_lon
  54. } = d[0];
  55. //console.log(d[0].regeocodeData.addressComponent);
  56. var {
  57. city
  58. } = d[0].regeocodeData.addressComponent;
  59. if (d[0].regeocodeData.addressComponent.city.length == 0) {
  60. var {
  61. province: city
  62. } = d[0].regeocodeData.addressComponent;
  63. }
  64. let that = this;
  65. let userInfo = wx.getStorageSync('userInfo');
  66. let isLogin = wx.getStorageSync('isLogin');
  67. this.setData({
  68. city,
  69. latitude,
  70. longitude,
  71. user_lat,
  72. user_lon,
  73. userInfo,
  74. isLogin
  75. });
  76. //开始请求充电站信息
  77. var chargStationType;
  78. if (!userInfo.flag) {
  79. //用户是普通用户
  80. chargStationType = '2';
  81. }
  82. wx.request({
  83. url: getApp().globalData.postHeadAgreement+'://cdgl.xinyhy.cn/restapi/wechat/chargStations',
  84. data: {
  85. lon: longitude,
  86. lat: latitude,
  87. distance: 10000,
  88. chargStationType
  89. },
  90. method: 'POST',
  91. success(res) {
  92. //userInfo.flag = true;
  93. if (isLogin && userInfo.flag) {
  94. wx.request({
  95. url: getApp().globalData.postHeadAgreement+'://cdgl.xinyhy.cn/restapi/wechat/userChargStations',
  96. data: {
  97. userId: userInfo.userId,
  98. lat: latitude,
  99. lon: longitude
  100. },
  101. method: 'POST',
  102. success(res1) {
  103. //console.log(res1);
  104. var myChargeStationsIds = '';
  105. res1.data.forEach((item, index) => {
  106. myChargeStationsIds += item.id + ",";
  107. });
  108. console.log(myChargeStationsIds);
  109. let {
  110. data
  111. } = res;
  112. let markers = [];
  113. data.forEach((item, index) => {
  114. //item.callout = {
  115. // content: item.name, //文本 String 1.2.0
  116. // display: 'BYCLICK', //'BYCLICK': 点击显示; 'ALWAYS': 常显 String 1.2.0
  117. // textAlign: 'center' //文本对齐方式。有效值: left, right, center String 1.6.0
  118. // };
  119. var iconPath = "/images/marker.png";
  120. if (myChargeStationsIds.indexOf(item.id + ',') != -1) {
  121. iconPath = "/images/marker1.png";
  122. }
  123. var marker = {
  124. name: item.chargStationName,
  125. address: item.address,
  126. width: "88rpx",
  127. height: "112rpx",
  128. iconPath: iconPath,
  129. id: item.id,
  130. callout: {},
  131. latitude: item.lat,
  132. longitude: item.lon,
  133. //distance: item.distance / 1000,
  134. distance: Util.distance(that.data.user_lat, that.data.user_lon, item.lat, item.lon),
  135. chargPileNum: item.fastCharg + item.slowCharg,
  136. fastCharg: item.fastCharg,
  137. slowCharg: item.slowCharg,
  138. freenum: item.freenum,
  139. fastfreenum: item.fastfreenum,
  140. slowfreenum: item.slowfreenum,
  141. breaknum: item.breaknum,
  142. /** 电费 */
  143. chargprice: item.chargprice,
  144. /** 服务费 */
  145. serviceprice: item.serviceprice,
  146. /** 停车费 */
  147. stopprice: item.stopprice
  148. };
  149. markers[index] = marker;
  150. });
  151. that.setData({
  152. markers,
  153. myChargeStationsIds
  154. });
  155. }
  156. });
  157. } else {
  158. let {
  159. data
  160. } = res;
  161. let markers = [];
  162. data.forEach((item, index) => {
  163. //item.callout = {
  164. // content: item.name, //文本 String 1.2.0
  165. // display: 'BYCLICK', //'BYCLICK': 点击显示; 'ALWAYS': 常显 String 1.2.0
  166. // textAlign: 'center' //文本对齐方式。有效值: left, right, center String 1.6.0
  167. // };
  168. var iconPath = "/images/marker.png";
  169. var marker = {
  170. name: item.chargStationName,
  171. address: item.address,
  172. width: "88rpx",
  173. height: "112rpx",
  174. iconPath: iconPath,
  175. id: item.id,
  176. callout: {},
  177. latitude: item.lat,
  178. longitude: item.lon,
  179. //distance: item.distance / 1000,
  180. distance: Util.distance(that.data.user_lat, that.data.user_lon, item.lat, item.lon),
  181. chargPileNum: item.fastCharg + item.slowCharg,
  182. fastCharg: item.fastCharg,
  183. slowCharg: item.slowCharg,
  184. freenum: item.freenum,
  185. fastfreenum: item.fastfreenum,
  186. slowfreenum: item.slowfreenum,
  187. breaknum: item.breaknum,
  188. /** 电费 */
  189. chargprice: item.chargprice,
  190. /** 服务费 */
  191. serviceprice: item.serviceprice,
  192. /** 停车费 */
  193. stopprice: item.stopprice
  194. };
  195. markers[index] = marker;
  196. });
  197. that.setData({
  198. markers
  199. });
  200. }
  201. }
  202. });
  203. })
  204. .catch(e => {
  205. console.log(e);
  206. });
  207. },
  208. //跳转个人中心
  209. bindUserAvatarUrl() {
  210. let url = `/pages/ucenter/index/index`;
  211. wx.navigateTo({
  212. url
  213. });
  214. },
  215. //搜索
  216. bindInput() {
  217. var {
  218. user_lat: latitude,
  219. user_lon: longitude,
  220. city
  221. } = this.data;
  222. //console.log(this.data.city);
  223. let url = `/pages/inputtip/inputtip?city=${city}&lonlat=${longitude},${latitude}`;
  224. wx.navigateTo({
  225. url
  226. });
  227. },
  228. //单击地图mark
  229. makertap(e) {
  230. mapregionchange_makertap = true;
  231. //console.log(e);
  232. let {
  233. markerId
  234. } = e;
  235. //console.log({
  236. // markerId
  237. //});
  238. let {
  239. markers
  240. } = this.data;
  241. markers.forEach((item, index) => {
  242. item.iconPath = "/images/marker.png";
  243. if (this.data.myChargeStationsIds.indexOf(item.id + ',') != -1) {
  244. item.iconPath = "/images/marker1.png";
  245. }
  246. if (item.id == markerId) {
  247. item.iconPath = "/images/marker_checked.png";
  248. this.showMarkerInfo(item);
  249. }
  250. });
  251. this.setData({
  252. markers,
  253. markerId
  254. });
  255. },
  256. //将mark呈现在地图中心点,并弹出详细信息框
  257. showMarkerInfo(data) {
  258. let {
  259. latitude,
  260. longitude
  261. } = data;
  262. this.setData({
  263. textData: data,
  264. latitude: latitude,
  265. longitude: longitude
  266. })
  267. },
  268. //改变选中mark的颜色
  269. changeMarkerColor(markerId) {
  270. let {
  271. markers
  272. } = this.data;
  273. markers.forEach((item, index) => {
  274. item.iconPath = "/images/marker.png";
  275. if (this.data.myChargeStationsIds.indexOf(item.id + ',') != -1) {
  276. item.iconPath = "/images/marker1.png";
  277. }
  278. if (item.id == markerId) {
  279. item.iconPath = "/images/marker_checked.png";
  280. }
  281. })
  282. this.setData({
  283. markers,
  284. markerId
  285. });
  286. },
  287. //导航
  288. getRoute(e) {
  289. //console.log(e);
  290. // 起点
  291. let {
  292. user_lat: latitude,
  293. user_lon: longitude,
  294. markers,
  295. markerId,
  296. city,
  297. textData
  298. } = this.data;
  299. let {
  300. name,
  301. address
  302. } = textData;
  303. if (!markers.length) return;
  304. // 终点
  305. markers.forEach((item, index) => {
  306. if (markerId && markerId == item.id) {
  307. let {
  308. latitude: latitude2,
  309. longitude: longitude2
  310. } = item;
  311. let url = `/pages/routes/routes?longitude=${longitude}&latitude=${latitude}&longitude2=${longitude2}&latitude2=${latitude2}&city=${city}&name=${name}&desc=${address}`;
  312. //console.log(url);
  313. wx.navigateTo({
  314. url
  315. });
  316. }
  317. });
  318. },
  319. //回到当前位置
  320. click_location_control(e) {
  321. //console.log("回到用户当前定位点");
  322. let {
  323. controlId
  324. } = e;
  325. let mpCtx = wx.createMapContext("map");
  326. mpCtx.moveToLocation();
  327. },
  328. //打开扫一扫
  329. click_scan_control(e) {
  330. console.log("打开扫一扫");
  331. if (!this.data.isLogin) {
  332. let url = `/pages/login/login`;
  333. wx.navigateTo({
  334. url
  335. });
  336. return;
  337. } else {
  338. wx.request({
  339. url: getApp().globalData.postHeadAgreement+'://cdgl.xinyhy.cn/restapi/pileLog/wechatsettlement',
  340. data: {
  341. userId: this.data.userInfo.userId
  342. },
  343. method: 'POST',
  344. success(res) {
  345. let {
  346. data
  347. } = res;
  348. let {
  349. result: order
  350. } = data;
  351. if (data && order && data.code == 200) {
  352. wx.showModal({
  353. showCancel: false,
  354. content: '你有未支付的订单,请先支付',
  355. success: function(res) {
  356. console.log(res);
  357. if (!res.cancel) {
  358. //点击确定
  359. let url = `/pages/order/order?orderid=${order.orderid}&userId=${order.userId}&chargPileId=${order.chargPileId}`;
  360. wx.navigateTo({
  361. url
  362. });
  363. }
  364. }
  365. });
  366. //console.log(that.data.result);
  367. } else {
  368. console.log('无订单');
  369. //console.log("打开扫一扫");
  370. wx.scanCode({
  371. success: function(d) {
  372. //console.log("完成扫一扫");
  373. //console.log(d);
  374. wx.request({
  375. url: getApp().globalData.postHeadAgreement+'://cdgl.xinyhy.cn/restapi/wechat/chargPile',
  376. data: d.result,
  377. method: 'POST',
  378. success(res) {
  379. console.log(res.data);
  380. if (!res.data) {
  381. //没有该充电桩信息
  382. wx.showModal({
  383. showCancel: false,
  384. content: '无效的终端编号'
  385. });
  386. } else {
  387. //充电状态0:离线、1:故障、2:空闲中、3:充电中、4:欠压故障、5:过压故障、6:过电流故障、8:预约、9:在线升级、10:操作中等
  388. if (!res.data.chargstatus || res.data.chargstatus == 0 || res.data.chargstatus == 1 || res.data.chargstatus == 4 || res.data.chargstatus == 5 || res.data.chargstatus == 6 || res.data.chargstatus == 9) {
  389. //充电桩故障
  390. wx.showModal({
  391. showCancel: false,
  392. content: '终端故障,维修中'
  393. });
  394. } else if (res.data.chargstatus == 3 || res.data.chargstatus == 8 || res.data.chargstatus == 10) {
  395. wx.showModal({
  396. showCancel: false,
  397. content: '正在充电中'
  398. });
  399. } else if (res.data.chargstatus == 2) {
  400. wx.setStorage({
  401. key: "scan_chargpile",
  402. data: res.data
  403. });
  404. let url = `/pages/scan_result/scan_result`;
  405. wx.navigateTo({
  406. url
  407. });
  408. }
  409. }
  410. }
  411. });
  412. }
  413. });
  414. }
  415. }
  416. });
  417. }
  418. },
  419. //地图位移
  420. mapchange(e) {
  421. if (e.type == 'end') {
  422. if (!mapregionchange_makertap) {
  423. //console.log("移动地图");
  424. let that = this;
  425. let wMap = wx.createMapContext('map');
  426. wMap.getCenterLocation({
  427. type: 'gcj02',
  428. success: function(res) {
  429. let {
  430. latitude,
  431. longitude
  432. } = res;
  433. let userInfo = wx.getStorageSync('userInfo');
  434. let isLogin = wx.getStorageSync('isLogin');
  435. var chargStationType;
  436. if (!userInfo.flag) {
  437. //用户是普通用户
  438. chargStationType = '2';
  439. }
  440. wx.request({
  441. url: getApp().globalData.postHeadAgreement+'://cdgl.xinyhy.cn/restapi/wechat/chargStations',
  442. data: {
  443. lon: longitude,
  444. lat: latitude,
  445. distance: 10000,
  446. chargStationType
  447. },
  448. method: 'POST',
  449. success(res) {
  450. let {
  451. data
  452. } = res;
  453. let markers = [];
  454. var flag = false;
  455. data.forEach((item, index) => {
  456. var iconPath;
  457. if (that.data.markerId && that.data.markerId == item.id) {
  458. iconPath = "/images/marker_checked.png";
  459. flag = true;
  460. } else {
  461. iconPath = "/images/marker.png";
  462. if (that.data.myChargeStationsIds.indexOf(item.id + ',') != -1) {
  463. iconPath = "/images/marker1.png";
  464. }
  465. }
  466. //console.log(Util.distance(that.data.user_lat, that.data.user_lon, item.lat, item.lon));
  467. var marker = {
  468. name: item.chargStationName,
  469. address: item.address,
  470. width: "88rpx",
  471. height: "112rpx",
  472. iconPath: iconPath,
  473. id: item.id,
  474. callout: {},
  475. latitude: item.lat,
  476. longitude: item.lon,
  477. //distance: item.distance / 1000,
  478. distance: Util.distance(that.data.user_lat, that.data.user_lon, item.lat, item.lon),
  479. chargPileNum: item.fastCharg + item.slowCharg,
  480. fastCharg: item.fastCharg,
  481. slowCharg: item.slowCharg,
  482. freenum: item.freenum,
  483. fastfreenum: item.fastfreenum,
  484. slowfreenum: item.slowfreenum,
  485. breaknum: item.breaknum,
  486. /** 电费 */
  487. chargprice: item.chargprice,
  488. /** 服务费 */
  489. serviceprice: item.serviceprice,
  490. /** 停车费 */
  491. stopprice: item.stopprice
  492. };
  493. markers[index] = marker;
  494. });
  495. if (!flag) {
  496. that.setData({
  497. textData: null,
  498. markerId: null,
  499. });
  500. }
  501. that.setData({
  502. markers: markers
  503. });
  504. }
  505. });
  506. }
  507. });
  508. } else {
  509. //点击marker的情况下
  510. //console.log("点击marker");
  511. mapregionchange_makertap = false;
  512. }
  513. }
  514. },
  515. /**
  516. * 生命周期函数--监听页面显示
  517. */
  518. onShow: function() {
  519. let userInfo = wx.getStorageSync('userInfo');
  520. let isLogin = wx.getStorageSync('isLogin');
  521. // 页面显示
  522. if (userInfo && isLogin) {
  523. //console.log(userInfo);
  524. //userInfo.flag = true;
  525. this.setData({
  526. userInfo: userInfo,
  527. isLogin: isLogin
  528. });
  529. wx.request({
  530. url: getApp().globalData.postHeadAgreement +'://cdgl.xinyhy.cn/restapi/pileLog/wechatchargstatus',
  531. data: {
  532. userId: userInfo.userId
  533. },
  534. method: 'POST',
  535. success(res) {
  536. let {
  537. data
  538. } = res;
  539. if (data && data.code == 200) {
  540. //获取用户正在充电的订单,并跳转到该页面
  541. let url = `/pages/charging/charging?orderid=${data.result.orderid}&userId=${userInfo.userId}&chargPileId=${data.result.chargPileId}`;
  542. //let url = `/pages/charging/charging?userId=${userInfo.userId}`;
  543. wx.navigateTo({
  544. url
  545. });
  546. } else {
  547. console.log(res);
  548. }
  549. }
  550. });
  551. } else {
  552. //未登录信息
  553. this.setData({
  554. userInfo: {}
  555. });
  556. }
  557. }
  558. })