search_result.js 15 KB

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