search_result.js 18 KB

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