search_result.js 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. Page({
  8. data: {
  9. markers: [],
  10. latitude: null,
  11. longitude: null,
  12. city: null,
  13. userInfo: {},
  14. isLogin: false,
  15. myChargeStationsIds: '',
  16. moneyActive: false,
  17. distanceActive: true,
  18. myChargeStationsActive: false,
  19. distancePng: 'asc',
  20. keywords: null,
  21. moneyPng: ''
  22. },
  23. resetchargStations(data){
  24. let that = this
  25. for(let i=0;i<data.length;i++){
  26. let seeInfoChargPile = data[i];
  27. let resultTable = seeInfoChargPile.resultList
  28. that.resetResultList(resultTable);
  29. that.resetResultData(resultTable,seeInfoChargPile)
  30. }
  31. },
  32. resetResultData(resultList,data){
  33. if(!resultList){
  34. return;
  35. }
  36. for(let i=0;i<resultList.length;i++){
  37. if(resultList[i].currentTime){
  38. data.chargprice = resultList[i].elecPrice
  39. data.serviceprice = resultList[i].servicePrice
  40. }
  41. }
  42. },
  43. resetResultList(resultList){
  44. let that = this
  45. if(!resultList){
  46. return;
  47. }
  48. resultList.forEach(item=>{
  49. item["sumPrice"] = that.resetResultListSumPrice(item);
  50. if(!item.elecPrice && item.elecPrice!=0){
  51. item["elecPrice"] = '暂无费用'
  52. }
  53. if(!item.servicePrice && item.servicePrice!=0){
  54. item["servicePrice"] = '暂无费用'
  55. }
  56. })
  57. },
  58. resetResultListSumPrice(item){
  59. if(!item.elecPrice && item.elecPrice!=0){
  60. return '暂无费用'
  61. }
  62. if(!item.servicePrice && item.servicePrice!=0){
  63. return '暂无费用'
  64. }
  65. return parseFloat((item.elecPrice + item.servicePrice).toFixed(6))
  66. },
  67. onLoad(e) {
  68. let {
  69. city,
  70. latitude,
  71. longitude,
  72. searchString
  73. } = e;
  74. console.log(city, latitude, longitude, searchString);
  75. this.setData({
  76. city,
  77. latitude,
  78. longitude,
  79. keywords: searchString
  80. });
  81. let userInfo = wx.getStorageSync('userInfo');
  82. let isLogin = wx.getStorageSync('isLogin');
  83. // 页面显示
  84. if (userInfo && isLogin) {
  85. //console.log(userInfo);
  86. //userInfo.flag = true;
  87. this.setData({
  88. userInfo: userInfo,
  89. isLogin: isLogin
  90. });
  91. } else {
  92. //未登录信息
  93. this.setData({
  94. userInfo: {}
  95. });
  96. }
  97. //开始请求充电站信息
  98. let that = this;
  99. var chargStationType;
  100. if (!userInfo.flag) {
  101. //用户是普通用户
  102. chargStationType = '2';
  103. }
  104. wx.request({
  105. url: getApp().globalData.postHeadAgreement +'/restapi/wechat/chargStationsSearch',
  106. data: {
  107. searchString,
  108. chargStationType
  109. },
  110. method: 'POST',
  111. success(res) {
  112. that.resetchargStations(res.data)
  113. //userInfo.flag = true;
  114. if (isLogin && userInfo.flag) {
  115. wx.request({
  116. url: getApp().globalData.postHeadAgreement +'/restapi/wechat/userChargStations',
  117. data: {
  118. userId: userInfo.userId,
  119. lat: latitude,
  120. lon: longitude
  121. },
  122. method: 'POST',
  123. success(res1) {
  124. var myChargeStationsIds = '';
  125. res1.data.forEach((item, index) => {
  126. myChargeStationsIds += item.id + ",";
  127. });
  128. console.log(myChargeStationsIds);
  129. let {
  130. data
  131. } = res;
  132. let markers = [];
  133. data.forEach((item, index) => {
  134. var marker = {
  135. name: item.chargStationName,
  136. address: item.address,
  137. width: "46rpx",
  138. height: "67rpx",
  139. iconPath: "/images/marker.png",
  140. chargfeatures:item.chargfeatures,
  141. id: item.id,
  142. callout: {},
  143. latitude: item.lat,
  144. longitude: item.lon,
  145. //distance: item.distance / 1000,
  146. distance: Util.distance(latitude, longitude, item.lat, item.lon),
  147. chargPileNum: item.fastCharg + item.slowCharg,
  148. fastCharg: item.fastCharg,
  149. slowCharg: item.slowCharg,
  150. freenum: item.freenum,
  151. fastfreenum: item.fastfreenum,
  152. slowfreenum: item.slowfreenum,
  153. breaknum: item.breaknum,
  154. /** 电费 */
  155. chargprice: item.chargprice,
  156. /** 服务费 */
  157. serviceprice: item.serviceprice,
  158. /** 停车费 */
  159. stopprice: item.stopprice,
  160. operationState: item.operationState,
  161. sharpChargPrice : item.sharpChargPrice,
  162. sharpServicePrice : item.sharpServicePrice,
  163. peakChargPrice : item.peakChargPrice,
  164. peakServicePrice : item.peakServicePrice,
  165. flatChargPrice : item.flatChargPrice,
  166. flatServicePrice : item.flatServicePrice,
  167. valleyChargPrice : item.valleyChargPrice,
  168. valleyServicePrice : item.valleyServicePrice,
  169. resultList: item.resultList,
  170. chargStationId: item.id
  171. };
  172. if (myChargeStationsIds.indexOf(item.id + ',') != -1) {
  173. marker.userFlag = true;
  174. } else {
  175. marker.userFlag = false;
  176. }
  177. markers[index] = marker;
  178. });
  179. markers.sort(function (ma, mb) {
  180. return ma.distance - mb.distance;
  181. });
  182. that.setData({
  183. markers,
  184. myChargeStationsIds
  185. });
  186. console.log(markers);
  187. }
  188. });
  189. } else {
  190. let {
  191. data
  192. } = res;
  193. let markers = [];
  194. data.forEach((item, index) => {
  195. var marker = {
  196. name: item.chargStationName,
  197. address: item.address,
  198. width: "46rpx",
  199. height: "67rpx",
  200. iconPath: "/images/marker.png",
  201. chargfeatures:item.chargfeatures,
  202. id: item.id,
  203. callout: {},
  204. latitude: item.lat,
  205. longitude: item.lon,
  206. //distance: item.distance / 1000,
  207. distance: Util.distance(latitude, longitude, item.lat, item.lon),
  208. chargPileNum: item.fastCharg + item.slowCharg,
  209. fastCharg: item.fastCharg,
  210. slowCharg: item.slowCharg,
  211. freenum: item.freenum,
  212. fastfreenum: item.fastfreenum,
  213. slowfreenum: item.slowfreenum,
  214. breaknum: item.breaknum,
  215. /** 电费 */
  216. chargprice: item.chargprice,
  217. /** 服务费 */
  218. serviceprice: item.serviceprice,
  219. /** 停车费 */
  220. stopprice: item.stopprice,
  221. operationState: item.operationState,
  222. sharpChargPrice : item.sharpChargPrice,
  223. sharpServicePrice : item.sharpServicePrice,
  224. peakChargPrice : item.peakChargPrice,
  225. peakServicePrice : item.peakServicePrice,
  226. flatChargPrice : item.flatChargPrice,
  227. flatServicePrice : item.flatServicePrice,
  228. valleyChargPrice : item.valleyChargPrice,
  229. valleyServicePrice : item.valleyServicePrice,
  230. resultList: item.resultList,
  231. chargStationId: item.id
  232. };
  233. markers[index] = marker;
  234. });
  235. markers.sort(function (ma, mb) {
  236. return ma.distance - mb.distance;
  237. });
  238. that.setData({
  239. markers
  240. });
  241. console.log(markers);
  242. }
  243. }
  244. });
  245. },
  246. onLoad2(e) {
  247. let {
  248. city,
  249. name,
  250. latitude,
  251. longitude,
  252. location
  253. } = e;
  254. console.log(name, latitude, longitude, location);
  255. this.setData({
  256. city,
  257. latitude,
  258. longitude
  259. });
  260. let userInfo = wx.getStorageSync('userInfo');
  261. let isLogin = wx.getStorageSync('isLogin');
  262. // 页面显示
  263. if (userInfo && isLogin) {
  264. //console.log(userInfo);
  265. //userInfo.flag = true;
  266. this.setData({
  267. userInfo: userInfo,
  268. isLogin: isLogin
  269. });
  270. } else {
  271. //未登录信息
  272. this.setData({
  273. userInfo: {}
  274. });
  275. }
  276. //开始请求充电站信息
  277. let that = this;
  278. var chargStationType;
  279. if (!userInfo.flag) {
  280. //用户是普通用户
  281. chargStationType = '2';
  282. }
  283. wx.request({
  284. url: getApp().globalData.postHeadAgreement +'/restapi/wechat/chargStations',
  285. data: {
  286. lon: location.split(",")[0],
  287. lat: location.split(",")[1],
  288. distance: 10000,
  289. chargStationType
  290. },
  291. method: 'POST',
  292. success(res) {
  293. that.resetchargStations(res.data)
  294. //userInfo.flag = true;
  295. if (isLogin && userInfo.flag) {
  296. wx.request({
  297. url: getApp().globalData.postHeadAgreement +'/restapi/wechat/userChargStations',
  298. data: {
  299. userId: userInfo.userId,
  300. lat: latitude,
  301. lon: longitude
  302. },
  303. method: 'POST',
  304. success(res1) {
  305. var myChargeStationsIds = '';
  306. res1.data.forEach((item, index) => {
  307. myChargeStationsIds += item.id + ",";
  308. });
  309. console.log(myChargeStationsIds);
  310. let {
  311. data
  312. } = res;
  313. let markers = [];
  314. data.forEach((item, index) => {
  315. console.log("itemitemitemitemitem");
  316. console.log(item);
  317. //item.callout = {
  318. // content: item.name, //文本 String 1.2.0
  319. // display: 'BYCLICK', //'BYCLICK': 点击显示; 'ALWAYS': 常显 String 1.2.0
  320. // textAlign: 'center' //文本对齐方式。有效值: left, right, center String 1.6.0
  321. // };
  322. //console.log(Util.distance(latitude, longitude, item.lat, item.lon));
  323. var marker = {
  324. name: item.chargStationName,
  325. address: item.address,
  326. width: "46rpx",
  327. height: "67rpx",
  328. iconPath: "/images/marker.png",
  329. chargfeatures:item.chargfeatures,
  330. id: item.id,
  331. callout: {},
  332. latitude: item.lat,
  333. longitude: item.lon,
  334. //distance: item.distance / 1000,
  335. distance: Util.distance(latitude, longitude, item.lat, item.lon),
  336. chargPileNum: item.fastCharg + item.slowCharg,
  337. fastCharg: item.fastCharg,
  338. slowCharg: item.slowCharg,
  339. freenum: item.freenum,
  340. fastfreenum: item.fastfreenum,
  341. slowfreenum: item.slowfreenum,
  342. breaknum: item.breaknum,
  343. /** 电费 */
  344. chargprice: item.chargprice,
  345. /** 服务费 */
  346. serviceprice: item.serviceprice,
  347. /** 停车费 */
  348. stopprice: item.stopprice,
  349. operationState: item.operationState,
  350. sharpChargPrice : item.sharpChargPrice,
  351. sharpServicePrice : item.sharpServicePrice,
  352. peakChargPrice : item.peakChargPrice,
  353. peakServicePrice : item.peakServicePrice,
  354. flatChargPrice : item.flatChargPrice,
  355. flatServicePrice : item.flatServicePrice,
  356. valleyChargPrice : item.valleyChargPrice,
  357. valleyServicePrice : item.valleyServicePrice,
  358. resultList: item.resultList,
  359. chargStationId: item.id
  360. };
  361. if (myChargeStationsIds.indexOf(item.id + ',') != -1) {
  362. marker.userFlag = true;
  363. } else {
  364. marker.userFlag = false;
  365. }
  366. markers[index] = marker;
  367. });
  368. markers.sort(function(ma, mb) {
  369. return ma.distance - mb.distance;
  370. });
  371. that.setData({
  372. markers,
  373. myChargeStationsIds
  374. });
  375. console.log(markers);
  376. }
  377. });
  378. } else {
  379. let {
  380. data
  381. } = res;
  382. let markers = [];
  383. data.forEach((item, index) => {
  384. console.log("itemitemitemitemitem");
  385. //item.callout = {
  386. // content: item.name, //文本 String 1.2.0
  387. // display: 'BYCLICK', //'BYCLICK': 点击显示; 'ALWAYS': 常显 String 1.2.0
  388. // textAlign: 'center' //文本对齐方式。有效值: left, right, center String 1.6.0
  389. // };
  390. //console.log(Util.distance(latitude, longitude, item.lat, item.lon));
  391. var marker = {
  392. name: item.chargStationName,
  393. address: item.address,
  394. width: "46rpx",
  395. height: "67rpx",
  396. iconPath: "/images/marker.png",
  397. chargfeatures:item.chargfeatures,
  398. id: item.id,
  399. callout: {},
  400. latitude: item.lat,
  401. longitude: item.lon,
  402. //distance: item.distance / 1000,
  403. distance: Util.distance(latitude, longitude, item.lat, item.lon),
  404. chargPileNum: item.fastCharg + item.slowCharg,
  405. fastCharg: item.fastCharg,
  406. slowCharg: item.slowCharg,
  407. freenum: item.freenum,
  408. fastfreenum: item.fastfreenum,
  409. slowfreenum: item.slowfreenum,
  410. breaknum: item.breaknum,
  411. /** 电费 */
  412. chargprice: item.chargprice,
  413. /** 服务费 */
  414. serviceprice: item.serviceprice,
  415. /** 停车费 */
  416. stopprice: item.stopprice,
  417. operationState: item.operationState,
  418. sharpChargPrice : item.sharpChargPrice,
  419. sharpServicePrice : item.sharpServicePrice,
  420. peakChargPrice : item.peakChargPrice,
  421. peakServicePrice : item.peakServicePrice,
  422. flatChargPrice : item.flatChargPrice,
  423. flatServicePrice : item.flatServicePrice,
  424. valleyChargPrice : item.valleyChargPrice,
  425. valleyServicePrice : item.valleyServicePrice,
  426. resultList: item.resultList,
  427. chargStationId: item.id
  428. };
  429. markers[index] = marker;
  430. });
  431. markers.sort(function(ma, mb) {
  432. return ma.distance - mb.distance;
  433. });
  434. that.setData({
  435. markers
  436. });
  437. console.log(markers);
  438. }
  439. }
  440. });
  441. },
  442. getRoute(e) {
  443. console.log(e);
  444. // 起点
  445. let {
  446. latitude,
  447. longitude,
  448. markers,
  449. city
  450. } = this.data;
  451. if (!markers.length) return;
  452. let markerId = e.currentTarget.id;
  453. // 终点
  454. markers.forEach((item, index) => {
  455. if (markerId && markerId == item.id) {
  456. let {
  457. name,
  458. address,
  459. latitude: latitude2,
  460. longitude: longitude2
  461. } = item;
  462. let url = `/pages/routes/routes?longitude=${longitude}&latitude=${latitude}&longitude2=${longitude2}&latitude2=${latitude2}&city=${city}&name=${name}&desc=${address}`;
  463. //console.log(url);
  464. wx.navigateTo({
  465. url
  466. });
  467. }
  468. });
  469. },
  470. goIndex(e) {
  471. //console.log(e);
  472. let that = this;
  473. let markerId = e.currentTarget.id;
  474. let pages = getCurrentPages();
  475. let prevPage = pages[0]; //首页
  476. let markers = this.data.markers; //首页
  477. this.data.markers.forEach((item, index) => {
  478. if (markerId && markerId == item.id) {
  479. wx.navigateBack({
  480. delta: pages.length
  481. });
  482. wx.request({
  483. url: getApp().globalData.postHeadAgreement + '/restapi/wechat/carParkingRate?stationId=' + item.id,
  484. method: 'POST',
  485. success: function (res) {
  486. // 是否减免停车费
  487. let carParkingRate = false
  488. let parkTime = 0;
  489. console.info(res)
  490. let reduceTime = res.data?.reduceTime;
  491. if(!reduceTime){
  492. carParkingRate = false
  493. }else{
  494. carParkingRate = true
  495. }
  496. try{
  497. parkTime = parseFloat((reduceTime/ 3600).toFixed(2))
  498. }catch(e){
  499. carParkingRate = false
  500. }
  501. let {
  502. latitude,
  503. longitude
  504. } = item;
  505. prevPage.setData({
  506. markers,
  507. markerId,
  508. latitude,
  509. longitude,
  510. carParkingRate,
  511. parkTime,
  512. keywords: that.data.keywords,
  513. textData: item
  514. });
  515. console.info({
  516. markers,
  517. markerId,
  518. latitude,
  519. longitude,
  520. keywords: that.data.keywords,
  521. textData: item
  522. })
  523. },
  524. fail(e) {
  525. console.log("用户数据同步失败");
  526. log.info('[扫码]', '[同步用户数据]', '[fail]', e);
  527. }
  528. });
  529. }
  530. });
  531. },
  532. myChargeStationsOrder(e) {
  533. let {
  534. markers
  535. } = this.data;
  536. if (markers && 0 != markers.length) {
  537. markers.sort(function(ma, mb) {
  538. return mb.userFlag - ma.userFlag == 0 ? ma.distance - mb.distance : mb.userFlag - ma.userFlag;
  539. });
  540. }
  541. this.setData({
  542. markers,
  543. myChargeStationsActive: true,
  544. distanceActive: false,
  545. moneyActive: false,
  546. moneyPng: '',
  547. distancePng: ''
  548. });
  549. },
  550. distanceOrder(e) {
  551. let {
  552. markers
  553. } = this.data;
  554. let {
  555. distancePng
  556. } = this.data;
  557. if (distancePng == '' || distancePng == 'desc') {
  558. if (markers && 0 != markers.length) {
  559. markers.sort(function(ma, mb) {
  560. return ma.distance - mb.distance;
  561. });
  562. }
  563. this.setData({
  564. distancePng: 'asc'
  565. });
  566. } else {
  567. if (markers && 0 != markers.length) {
  568. markers.sort(function(ma, mb) {
  569. return mb.distance - ma.distance;
  570. });
  571. }
  572. this.setData({
  573. distancePng: 'desc'
  574. });
  575. }
  576. this.setData({
  577. markers,
  578. myChargeStationsActive: false,
  579. distanceActive: true,
  580. moneyActive: false,
  581. moneyPng: ''
  582. });
  583. },
  584. moneyOrder(e) {
  585. let {
  586. markers
  587. } = this.data;
  588. let {
  589. moneyPng
  590. } = this.data;
  591. if (moneyPng == '' || moneyPng == 'desc') {
  592. if (markers && 0 != markers.length) {
  593. markers.sort(function(ma, mb) {
  594. return (ma.chargprice + ma.serviceprice + ma.stopprice) - (mb.chargprice + mb.serviceprice + mb.stopprice);
  595. });
  596. }
  597. this.setData({
  598. moneyPng: 'asc'
  599. });
  600. } else {
  601. if (markers && 0 != markers.length) {
  602. markers.sort(function(ma, mb) {
  603. return (mb.chargprice + mb.serviceprice + mb.stopprice) - (ma.chargprice + ma.serviceprice + ma.stopprice);
  604. });
  605. }
  606. this.setData({
  607. moneyPng: 'desc'
  608. });
  609. }
  610. this.setData({
  611. markers,
  612. myChargeStationsActive: false,
  613. distanceActive: false,
  614. moneyActive: true,
  615. distancePng: ''
  616. });
  617. }
  618. })