search_result.js 16 KB

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