search_result.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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: "88rpx",
  94. height: "112rpx",
  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: "88rpx",
  144. height: "112rpx",
  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: "88rpx",
  259. height: "112rpx",
  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. };
  282. if (myChargeStationsIds.indexOf(item.id + ',') != -1) {
  283. marker.userFlag = true;
  284. } else {
  285. marker.userFlag = false;
  286. }
  287. markers[index] = marker;
  288. });
  289. markers.sort(function(ma, mb) {
  290. return ma.distance - mb.distance;
  291. });
  292. that.setData({
  293. markers,
  294. myChargeStationsIds
  295. });
  296. console.log(markers);
  297. }
  298. });
  299. } else {
  300. let {
  301. data
  302. } = res;
  303. let markers = [];
  304. data.forEach((item, index) => {
  305. //item.callout = {
  306. // content: item.name, //文本 String 1.2.0
  307. // display: 'BYCLICK', //'BYCLICK': 点击显示; 'ALWAYS': 常显 String 1.2.0
  308. // textAlign: 'center' //文本对齐方式。有效值: left, right, center String 1.6.0
  309. // };
  310. //console.log(Util.distance(latitude, longitude, item.lat, item.lon));
  311. var marker = {
  312. name: item.chargStationName,
  313. address: item.address,
  314. width: "88rpx",
  315. height: "112rpx",
  316. iconPath: "/images/marker.png",
  317. id: item.id,
  318. callout: {},
  319. latitude: item.lat,
  320. longitude: item.lon,
  321. //distance: item.distance / 1000,
  322. distance: Util.distance(latitude, longitude, item.lat, item.lon),
  323. chargPileNum: item.fastCharg + item.slowCharg,
  324. fastCharg: item.fastCharg,
  325. slowCharg: item.slowCharg,
  326. freenum: item.freenum,
  327. fastfreenum: item.fastfreenum,
  328. slowfreenum: item.slowfreenum,
  329. breaknum: item.breaknum,
  330. /** 电费 */
  331. chargprice: item.chargprice,
  332. /** 服务费 */
  333. serviceprice: item.serviceprice,
  334. /** 停车费 */
  335. stopprice: item.stopprice,
  336. operationState: item.operationState
  337. };
  338. markers[index] = marker;
  339. });
  340. markers.sort(function(ma, mb) {
  341. return ma.distance - mb.distance;
  342. });
  343. that.setData({
  344. markers
  345. });
  346. console.log(markers);
  347. }
  348. }
  349. });
  350. },
  351. getRoute(e) {
  352. console.log(e);
  353. // 起点
  354. let {
  355. latitude,
  356. longitude,
  357. markers,
  358. city
  359. } = this.data;
  360. if (!markers.length) return;
  361. let markerId = e.currentTarget.id;
  362. // 终点
  363. markers.forEach((item, index) => {
  364. if (markerId && markerId == item.id) {
  365. let {
  366. name,
  367. address,
  368. latitude: latitude2,
  369. longitude: longitude2
  370. } = item;
  371. let url = `/pages/routes/routes?longitude=${longitude}&latitude=${latitude}&longitude2=${longitude2}&latitude2=${latitude2}&city=${city}&name=${name}&desc=${address}`;
  372. //console.log(url);
  373. wx.navigateTo({
  374. url
  375. });
  376. }
  377. });
  378. },
  379. goIndex(e) {
  380. //console.log(e);
  381. let markerId = e.currentTarget.id;
  382. let pages = getCurrentPages();
  383. let prevPage = pages[0]; //首页
  384. this.data.markers.forEach((item, index) => {
  385. if (markerId && markerId == item.id) {
  386. let {
  387. latitude,
  388. longitude
  389. } = item;
  390. prevPage.setData({
  391. markerId,
  392. latitude,
  393. longitude,
  394. keywords:this.data.keywords,
  395. textData: item
  396. });
  397. wx.navigateBack({
  398. delta: pages.length
  399. });
  400. }
  401. });
  402. },
  403. myChargeStationsOrder(e) {
  404. let {
  405. markers
  406. } = this.data;
  407. if (markers && 0 != markers.length) {
  408. markers.sort(function(ma, mb) {
  409. return mb.userFlag - ma.userFlag == 0 ? ma.distance - mb.distance : mb.userFlag - ma.userFlag;
  410. });
  411. }
  412. this.setData({
  413. markers,
  414. myChargeStationsActive: true,
  415. distanceActive: false,
  416. moneyActive: false,
  417. moneyPng: '',
  418. distancePng: ''
  419. });
  420. },
  421. distanceOrder(e) {
  422. let {
  423. markers
  424. } = this.data;
  425. let {
  426. distancePng
  427. } = this.data;
  428. if (distancePng == '' || distancePng == 'desc') {
  429. if (markers && 0 != markers.length) {
  430. markers.sort(function(ma, mb) {
  431. return ma.distance - mb.distance;
  432. });
  433. }
  434. this.setData({
  435. distancePng: 'asc'
  436. });
  437. } else {
  438. if (markers && 0 != markers.length) {
  439. markers.sort(function(ma, mb) {
  440. return mb.distance - ma.distance;
  441. });
  442. }
  443. this.setData({
  444. distancePng: 'desc'
  445. });
  446. }
  447. this.setData({
  448. markers,
  449. myChargeStationsActive: false,
  450. distanceActive: true,
  451. moneyActive: false,
  452. moneyPng: ''
  453. });
  454. },
  455. moneyOrder(e) {
  456. let {
  457. markers
  458. } = this.data;
  459. let {
  460. moneyPng
  461. } = this.data;
  462. if (moneyPng == '' || moneyPng == 'desc') {
  463. if (markers && 0 != markers.length) {
  464. markers.sort(function(ma, mb) {
  465. return (ma.chargprice + ma.serviceprice + ma.stopprice) - (mb.chargprice + mb.serviceprice + mb.stopprice);
  466. });
  467. }
  468. this.setData({
  469. moneyPng: 'asc'
  470. });
  471. } else {
  472. if (markers && 0 != markers.length) {
  473. markers.sort(function(ma, mb) {
  474. return (mb.chargprice + mb.serviceprice + mb.stopprice) - (ma.chargprice + ma.serviceprice + ma.stopprice);
  475. });
  476. }
  477. this.setData({
  478. moneyPng: 'desc'
  479. });
  480. }
  481. this.setData({
  482. markers,
  483. myChargeStationsActive: false,
  484. distanceActive: false,
  485. moneyActive: true,
  486. distancePng: ''
  487. });
  488. }
  489. })