search_result.js 17 KB

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