search_result.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  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. };
  171. if (myChargeStationsIds.indexOf(item.id + ',') != -1) {
  172. marker.userFlag = true;
  173. } else {
  174. marker.userFlag = false;
  175. }
  176. markers[index] = marker;
  177. });
  178. markers.sort(function (ma, mb) {
  179. return ma.distance - mb.distance;
  180. });
  181. that.setData({
  182. markers,
  183. myChargeStationsIds
  184. });
  185. console.log(markers);
  186. }
  187. });
  188. } else {
  189. let {
  190. data
  191. } = res;
  192. let markers = [];
  193. data.forEach((item, index) => {
  194. var marker = {
  195. name: item.chargStationName,
  196. address: item.address,
  197. width: "46rpx",
  198. height: "67rpx",
  199. iconPath: "/images/marker.png",
  200. chargfeatures:item.chargfeatures,
  201. id: item.id,
  202. callout: {},
  203. latitude: item.lat,
  204. longitude: item.lon,
  205. //distance: item.distance / 1000,
  206. distance: Util.distance(latitude, longitude, item.lat, item.lon),
  207. chargPileNum: item.fastCharg + item.slowCharg,
  208. fastCharg: item.fastCharg,
  209. slowCharg: item.slowCharg,
  210. freenum: item.freenum,
  211. fastfreenum: item.fastfreenum,
  212. slowfreenum: item.slowfreenum,
  213. breaknum: item.breaknum,
  214. /** 电费 */
  215. chargprice: item.chargprice,
  216. /** 服务费 */
  217. serviceprice: item.serviceprice,
  218. /** 停车费 */
  219. stopprice: item.stopprice,
  220. operationState: item.operationState,
  221. sharpChargPrice : item.sharpChargPrice,
  222. sharpServicePrice : item.sharpServicePrice,
  223. peakChargPrice : item.peakChargPrice,
  224. peakServicePrice : item.peakServicePrice,
  225. flatChargPrice : item.flatChargPrice,
  226. flatServicePrice : item.flatServicePrice,
  227. valleyChargPrice : item.valleyChargPrice,
  228. valleyServicePrice : item.valleyServicePrice,
  229. resultList: item.resultList
  230. };
  231. markers[index] = marker;
  232. });
  233. markers.sort(function (ma, mb) {
  234. return ma.distance - mb.distance;
  235. });
  236. that.setData({
  237. markers
  238. });
  239. console.log(markers);
  240. }
  241. }
  242. });
  243. },
  244. onLoad2(e) {
  245. let {
  246. city,
  247. name,
  248. latitude,
  249. longitude,
  250. location
  251. } = e;
  252. console.log(name, latitude, longitude, location);
  253. this.setData({
  254. city,
  255. latitude,
  256. longitude
  257. });
  258. let userInfo = wx.getStorageSync('userInfo');
  259. let isLogin = wx.getStorageSync('isLogin');
  260. // 页面显示
  261. if (userInfo && isLogin) {
  262. //console.log(userInfo);
  263. //userInfo.flag = true;
  264. this.setData({
  265. userInfo: userInfo,
  266. isLogin: isLogin
  267. });
  268. } else {
  269. //未登录信息
  270. this.setData({
  271. userInfo: {}
  272. });
  273. }
  274. //开始请求充电站信息
  275. let that = this;
  276. var chargStationType;
  277. if (!userInfo.flag) {
  278. //用户是普通用户
  279. chargStationType = '2';
  280. }
  281. wx.request({
  282. url: getApp().globalData.postHeadAgreement +'/restapi/wechat/chargStations',
  283. data: {
  284. lon: location.split(",")[0],
  285. lat: location.split(",")[1],
  286. distance: 10000,
  287. chargStationType
  288. },
  289. method: 'POST',
  290. success(res) {
  291. that.resetchargStations(res.data)
  292. //userInfo.flag = true;
  293. if (isLogin && userInfo.flag) {
  294. wx.request({
  295. url: getApp().globalData.postHeadAgreement +'/restapi/wechat/userChargStations',
  296. data: {
  297. userId: userInfo.userId,
  298. lat: latitude,
  299. lon: longitude
  300. },
  301. method: 'POST',
  302. success(res1) {
  303. var myChargeStationsIds = '';
  304. res1.data.forEach((item, index) => {
  305. myChargeStationsIds += item.id + ",";
  306. });
  307. console.log(myChargeStationsIds);
  308. let {
  309. data
  310. } = res;
  311. let markers = [];
  312. data.forEach((item, index) => {
  313. console.log("itemitemitemitemitem");
  314. console.log(item);
  315. //item.callout = {
  316. // content: item.name, //文本 String 1.2.0
  317. // display: 'BYCLICK', //'BYCLICK': 点击显示; 'ALWAYS': 常显 String 1.2.0
  318. // textAlign: 'center' //文本对齐方式。有效值: left, right, center String 1.6.0
  319. // };
  320. //console.log(Util.distance(latitude, longitude, item.lat, item.lon));
  321. var marker = {
  322. name: item.chargStationName,
  323. address: item.address,
  324. width: "46rpx",
  325. height: "67rpx",
  326. iconPath: "/images/marker.png",
  327. chargfeatures:item.chargfeatures,
  328. id: item.id,
  329. callout: {},
  330. latitude: item.lat,
  331. longitude: item.lon,
  332. //distance: item.distance / 1000,
  333. distance: Util.distance(latitude, longitude, item.lat, item.lon),
  334. chargPileNum: item.fastCharg + item.slowCharg,
  335. fastCharg: item.fastCharg,
  336. slowCharg: item.slowCharg,
  337. freenum: item.freenum,
  338. fastfreenum: item.fastfreenum,
  339. slowfreenum: item.slowfreenum,
  340. breaknum: item.breaknum,
  341. /** 电费 */
  342. chargprice: item.chargprice,
  343. /** 服务费 */
  344. serviceprice: item.serviceprice,
  345. /** 停车费 */
  346. stopprice: item.stopprice,
  347. operationState: item.operationState,
  348. sharpChargPrice : item.sharpChargPrice,
  349. sharpServicePrice : item.sharpServicePrice,
  350. peakChargPrice : item.peakChargPrice,
  351. peakServicePrice : item.peakServicePrice,
  352. flatChargPrice : item.flatChargPrice,
  353. flatServicePrice : item.flatServicePrice,
  354. valleyChargPrice : item.valleyChargPrice,
  355. valleyServicePrice : item.valleyServicePrice,
  356. resultList: item.resultList
  357. };
  358. if (myChargeStationsIds.indexOf(item.id + ',') != -1) {
  359. marker.userFlag = true;
  360. } else {
  361. marker.userFlag = false;
  362. }
  363. markers[index] = marker;
  364. });
  365. markers.sort(function(ma, mb) {
  366. return ma.distance - mb.distance;
  367. });
  368. that.setData({
  369. markers,
  370. myChargeStationsIds
  371. });
  372. console.log(markers);
  373. }
  374. });
  375. } else {
  376. let {
  377. data
  378. } = res;
  379. let markers = [];
  380. data.forEach((item, index) => {
  381. console.log("itemitemitemitemitem");
  382. //item.callout = {
  383. // content: item.name, //文本 String 1.2.0
  384. // display: 'BYCLICK', //'BYCLICK': 点击显示; 'ALWAYS': 常显 String 1.2.0
  385. // textAlign: 'center' //文本对齐方式。有效值: left, right, center String 1.6.0
  386. // };
  387. //console.log(Util.distance(latitude, longitude, item.lat, item.lon));
  388. var marker = {
  389. name: item.chargStationName,
  390. address: item.address,
  391. width: "46rpx",
  392. height: "67rpx",
  393. iconPath: "/images/marker.png",
  394. chargfeatures:item.chargfeatures,
  395. id: item.id,
  396. callout: {},
  397. latitude: item.lat,
  398. longitude: item.lon,
  399. //distance: item.distance / 1000,
  400. distance: Util.distance(latitude, longitude, item.lat, item.lon),
  401. chargPileNum: item.fastCharg + item.slowCharg,
  402. fastCharg: item.fastCharg,
  403. slowCharg: item.slowCharg,
  404. freenum: item.freenum,
  405. fastfreenum: item.fastfreenum,
  406. slowfreenum: item.slowfreenum,
  407. breaknum: item.breaknum,
  408. /** 电费 */
  409. chargprice: item.chargprice,
  410. /** 服务费 */
  411. serviceprice: item.serviceprice,
  412. /** 停车费 */
  413. stopprice: item.stopprice,
  414. operationState: item.operationState,
  415. sharpChargPrice : item.sharpChargPrice,
  416. sharpServicePrice : item.sharpServicePrice,
  417. peakChargPrice : item.peakChargPrice,
  418. peakServicePrice : item.peakServicePrice,
  419. flatChargPrice : item.flatChargPrice,
  420. flatServicePrice : item.flatServicePrice,
  421. valleyChargPrice : item.valleyChargPrice,
  422. valleyServicePrice : item.valleyServicePrice,
  423. resultList: item.resultList
  424. };
  425. markers[index] = marker;
  426. });
  427. markers.sort(function(ma, mb) {
  428. return ma.distance - mb.distance;
  429. });
  430. that.setData({
  431. markers
  432. });
  433. console.log(markers);
  434. }
  435. }
  436. });
  437. },
  438. getRoute(e) {
  439. console.log(e);
  440. // 起点
  441. let {
  442. latitude,
  443. longitude,
  444. markers,
  445. city
  446. } = this.data;
  447. if (!markers.length) return;
  448. let markerId = e.currentTarget.id;
  449. // 终点
  450. markers.forEach((item, index) => {
  451. if (markerId && markerId == item.id) {
  452. let {
  453. name,
  454. address,
  455. latitude: latitude2,
  456. longitude: longitude2
  457. } = item;
  458. let url = `/pages/routes/routes?longitude=${longitude}&latitude=${latitude}&longitude2=${longitude2}&latitude2=${latitude2}&city=${city}&name=${name}&desc=${address}`;
  459. //console.log(url);
  460. wx.navigateTo({
  461. url
  462. });
  463. }
  464. });
  465. },
  466. goIndex(e) {
  467. //console.log(e);
  468. let markerId = e.currentTarget.id;
  469. let pages = getCurrentPages();
  470. let prevPage = pages[0]; //首页
  471. let markers = this.data.markers; //首页
  472. this.data.markers.forEach((item, index) => {
  473. if (markerId && markerId == item.id) {
  474. let {
  475. latitude,
  476. longitude
  477. } = item;
  478. prevPage.setData({
  479. markers,
  480. markerId,
  481. latitude,
  482. longitude,
  483. keywords: this.data.keywords,
  484. textData: item
  485. });
  486. console.info({
  487. markers,
  488. markerId,
  489. latitude,
  490. longitude,
  491. keywords: this.data.keywords,
  492. textData: item
  493. })
  494. wx.navigateBack({
  495. delta: pages.length
  496. });
  497. }
  498. });
  499. },
  500. myChargeStationsOrder(e) {
  501. let {
  502. markers
  503. } = this.data;
  504. if (markers && 0 != markers.length) {
  505. markers.sort(function(ma, mb) {
  506. return mb.userFlag - ma.userFlag == 0 ? ma.distance - mb.distance : mb.userFlag - ma.userFlag;
  507. });
  508. }
  509. this.setData({
  510. markers,
  511. myChargeStationsActive: true,
  512. distanceActive: false,
  513. moneyActive: false,
  514. moneyPng: '',
  515. distancePng: ''
  516. });
  517. },
  518. distanceOrder(e) {
  519. let {
  520. markers
  521. } = this.data;
  522. let {
  523. distancePng
  524. } = this.data;
  525. if (distancePng == '' || distancePng == 'desc') {
  526. if (markers && 0 != markers.length) {
  527. markers.sort(function(ma, mb) {
  528. return ma.distance - mb.distance;
  529. });
  530. }
  531. this.setData({
  532. distancePng: 'asc'
  533. });
  534. } else {
  535. if (markers && 0 != markers.length) {
  536. markers.sort(function(ma, mb) {
  537. return mb.distance - ma.distance;
  538. });
  539. }
  540. this.setData({
  541. distancePng: 'desc'
  542. });
  543. }
  544. this.setData({
  545. markers,
  546. myChargeStationsActive: false,
  547. distanceActive: true,
  548. moneyActive: false,
  549. moneyPng: ''
  550. });
  551. },
  552. moneyOrder(e) {
  553. let {
  554. markers
  555. } = this.data;
  556. let {
  557. moneyPng
  558. } = this.data;
  559. if (moneyPng == '' || moneyPng == 'desc') {
  560. if (markers && 0 != markers.length) {
  561. markers.sort(function(ma, mb) {
  562. return (ma.chargprice + ma.serviceprice + ma.stopprice) - (mb.chargprice + mb.serviceprice + mb.stopprice);
  563. });
  564. }
  565. this.setData({
  566. moneyPng: 'asc'
  567. });
  568. } else {
  569. if (markers && 0 != markers.length) {
  570. markers.sort(function(ma, mb) {
  571. return (mb.chargprice + mb.serviceprice + mb.stopprice) - (ma.chargprice + ma.serviceprice + ma.stopprice);
  572. });
  573. }
  574. this.setData({
  575. moneyPng: 'desc'
  576. });
  577. }
  578. this.setData({
  579. markers,
  580. myChargeStationsActive: false,
  581. distanceActive: false,
  582. moneyActive: true,
  583. distancePng: ''
  584. });
  585. }
  586. })