search_result.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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){
  51. item["elecPrice"] = '暂无费用'
  52. }
  53. if(!item.servicePrice){
  54. item["servicePrice"] = '暂无费用'
  55. }
  56. })
  57. },
  58. resetResultListSumPrice(item){
  59. if(!item.elecPrice){
  60. return '暂无费用'
  61. }
  62. if(!item.servicePrice){
  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.chargStationId
  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.chargStationId
  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.chargStationId
  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.chargStationId
  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 markerId = e.currentTarget.id;
  473. let pages = getCurrentPages();
  474. let prevPage = pages[0]; //首页
  475. let markers = this.data.markers; //首页
  476. this.data.markers.forEach((item, index) => {
  477. if (markerId && markerId == item.id) {
  478. let {
  479. latitude,
  480. longitude
  481. } = item;
  482. prevPage.setData({
  483. markers,
  484. markerId,
  485. latitude,
  486. longitude,
  487. keywords: this.data.keywords,
  488. textData: item
  489. });
  490. console.info({
  491. markers,
  492. markerId,
  493. latitude,
  494. longitude,
  495. keywords: this.data.keywords,
  496. textData: item
  497. })
  498. wx.navigateBack({
  499. delta: pages.length
  500. });
  501. }
  502. });
  503. },
  504. myChargeStationsOrder(e) {
  505. let {
  506. markers
  507. } = this.data;
  508. if (markers && 0 != markers.length) {
  509. markers.sort(function(ma, mb) {
  510. return mb.userFlag - ma.userFlag == 0 ? ma.distance - mb.distance : mb.userFlag - ma.userFlag;
  511. });
  512. }
  513. this.setData({
  514. markers,
  515. myChargeStationsActive: true,
  516. distanceActive: false,
  517. moneyActive: false,
  518. moneyPng: '',
  519. distancePng: ''
  520. });
  521. },
  522. distanceOrder(e) {
  523. let {
  524. markers
  525. } = this.data;
  526. let {
  527. distancePng
  528. } = this.data;
  529. if (distancePng == '' || distancePng == 'desc') {
  530. if (markers && 0 != markers.length) {
  531. markers.sort(function(ma, mb) {
  532. return ma.distance - mb.distance;
  533. });
  534. }
  535. this.setData({
  536. distancePng: 'asc'
  537. });
  538. } else {
  539. if (markers && 0 != markers.length) {
  540. markers.sort(function(ma, mb) {
  541. return mb.distance - ma.distance;
  542. });
  543. }
  544. this.setData({
  545. distancePng: 'desc'
  546. });
  547. }
  548. this.setData({
  549. markers,
  550. myChargeStationsActive: false,
  551. distanceActive: true,
  552. moneyActive: false,
  553. moneyPng: ''
  554. });
  555. },
  556. moneyOrder(e) {
  557. let {
  558. markers
  559. } = this.data;
  560. let {
  561. moneyPng
  562. } = this.data;
  563. if (moneyPng == '' || moneyPng == 'desc') {
  564. if (markers && 0 != markers.length) {
  565. markers.sort(function(ma, mb) {
  566. return (ma.chargprice + ma.serviceprice + ma.stopprice) - (mb.chargprice + mb.serviceprice + mb.stopprice);
  567. });
  568. }
  569. this.setData({
  570. moneyPng: 'asc'
  571. });
  572. } else {
  573. if (markers && 0 != markers.length) {
  574. markers.sort(function(ma, mb) {
  575. return (mb.chargprice + mb.serviceprice + mb.stopprice) - (ma.chargprice + ma.serviceprice + ma.stopprice);
  576. });
  577. }
  578. this.setData({
  579. moneyPng: 'desc'
  580. });
  581. }
  582. this.setData({
  583. markers,
  584. myChargeStationsActive: false,
  585. distanceActive: false,
  586. moneyActive: true,
  587. distancePng: ''
  588. });
  589. }
  590. })