search_result.js 17 KB

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