index.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. //index.js
  2. //获取应用实例
  3. const { request, serverName,imgServer } = require('../../utils/services');
  4. const { Toast, qqmapsdk } = require('../../utils/util.js');
  5. const { defaultImg, noExhibitionImg } = require('../../utils/images');
  6. const WxParse = require('../../common/component/wxParse/wxParse.js');
  7. const {
  8. newRequestFns,
  9. newServerName
  10. } = require('../../utils/newServices.js');
  11. // const QQMapWX = require('../../common/component/mapSDK/qqmap-wx-jssdk.min.js');
  12. const app = getApp();
  13. const distItems = [
  14. {
  15. name: '按距离排序',
  16. id: '距离排序',
  17. idx: 1
  18. },
  19. {
  20. name: '按热度排序',
  21. id: '热度排序',
  22. idx: 2
  23. }
  24. ]
  25. const typeItems = [
  26. {
  27. name: '展览中',
  28. id: '展览中',
  29. idx: 1
  30. },
  31. {
  32. name: '即将开始',
  33. id: '即将开始',
  34. idx: 2
  35. },
  36. {
  37. name: '已结束',
  38. id: '已结束',
  39. idx: 3
  40. },
  41. {
  42. name: '全部状态',
  43. id: '全部状态',
  44. idx: 0
  45. }
  46. ]
  47. Page({
  48. data: {
  49. navItem: distItems,
  50. indicatorDots: true,
  51. autoplay: true,
  52. interval: 5000,
  53. duration: 1000,
  54. activeIndex: 0,
  55. locationName: "北京",
  56. locationNameDesp:"",
  57. isLike: false,
  58. showConfirm: false,
  59. tag: "",
  60. exhibitionList: [],
  61. commodityImgs: [
  62. '../../imgs/testImg/thumbSmallImg.jpg',
  63. '../../imgs/testImg/thumbSmallImg.jpg',
  64. ],
  65. type: 5,
  66. testImg: '../../imgs/testImg/fdkz.png',
  67. getLocationBtn: false,
  68. isShow: false,
  69. exhiNum:0,
  70. activeDist:'距离排序',
  71. activeType:'展览中'
  72. },
  73. to_search_exhibition: function () {
  74. wx.navigateTo({
  75. url: '../yuezhan/search/index',
  76. success: function (res) { },
  77. fail: function (res) { },
  78. complete: function (res) { },
  79. })
  80. },
  81. onLoad: function () {
  82. this.getLocationName();
  83. // this.getList(1)
  84. this.setData({
  85. serverName,
  86. noExhibitionImg,
  87. defaultImg,
  88. imgServer
  89. });
  90. },
  91. onShareAppMessage: function () {
  92. },
  93. onShow: function () {
  94. let { city, clickToSelect, collectedArr, collectedChange, latitude, longitude, locationName } = app.globalData;
  95. let { activeIndex, exhibitionList } = this.data
  96. if (clickToSelect) {
  97. // 使用 globalData 中的位置信息
  98. let finalLocationName = locationName || city || "北京";
  99. this.setData({
  100. locationName: finalLocationName,
  101. exhibitionList: [],
  102. activeIndex: 0,
  103. type: 5
  104. });
  105. // 如果有完整的位置信息,也设置坐标
  106. if (latitude && longitude) {
  107. this.setData({
  108. latitude: latitude,
  109. longitude: longitude
  110. });
  111. }
  112. // 确保 city 与 locationName 一致
  113. if (finalLocationName && finalLocationName !== "北京") {
  114. app.globalData.city = finalLocationName;
  115. } else {
  116. app.globalData.city = "北京";
  117. }
  118. if (activeIndex == 1) {
  119. this.getNearByList(1);
  120. }
  121. else {
  122. this.getList(1);
  123. }
  124. }
  125. if (collectedChange) {
  126. for (let i = 0; i < exhibitionList.length; i++) {
  127. for (let j = 0; j < collectedArr.length; j++) {
  128. if (collectedArr[j].collectedId == exhibitionList[i].id) {
  129. exhibitionList[i].hasCollect = collectedArr[j].status
  130. if (exhibitionList[i].hasCollect) {
  131. exhibitionList[i].collectionsCount += 1;
  132. }
  133. else {
  134. exhibitionList[i].collectionsCount -= 1;
  135. }
  136. if (exhibitionList[i].collectionsCount < 0) {
  137. exhibitionList[i].collectionsCount = 0
  138. }
  139. }
  140. }
  141. }
  142. this.setData({
  143. exhibitionList
  144. })
  145. }
  146. app.globalData.clickToSelect = false;
  147. // app.globalData.collectedChange = false;
  148. },
  149. addLike: function (e) {
  150. let { type, id, idx } = e.currentTarget.dataset;
  151. let exhibitionList = this.data.exhibitionList;
  152. let { collectedArr, collectedChange } = app.globalData, hasItem = true;
  153. Toast.showToast2('loading');
  154. let loginSessionKey = wx.getStorageSync('token') || "";
  155. // if (loginSessionKey){
  156. newRequestFns['isCollect']({
  157. loginSessionKey,
  158. exhibitionId: id,
  159. type: Number(type),
  160. }, "post", res => {
  161. if (res.data.code > -1) {
  162. for (let i = 0; i < collectedArr.length; i++) {
  163. if (collectedArr[i].collectedId && id == collectedArr[i].collectedId) {
  164. collectedArr[i] = {
  165. collectedId: id,
  166. status: res.data.data.hasCollect,
  167. }
  168. hasItem = false;
  169. }
  170. }
  171. if (hasItem) {
  172. collectedArr.push({
  173. collectedId: id,
  174. status: res.data.data.hasCollect,
  175. })
  176. }
  177. app.globalData.collectedArr = collectedArr;
  178. app.globalData.collectedChange = true;
  179. exhibitionList[idx].hasCollect = res.data.data.hasCollect
  180. this.setData({
  181. exhibitionList: exhibitionList
  182. })
  183. }
  184. }, err => {
  185. }, complete => {
  186. Toast.hideLoading();
  187. })
  188. // }
  189. },
  190. showDrop: function (e) {
  191. let tag = e.target.dataset.id
  192. let name = e.target.dataset.test
  193. if (tag==='type'){
  194. this.setData({
  195. navItem: {
  196. type: tag,
  197. name: name,
  198. arr: typeItems
  199. },
  200. isShow: !this.data.isShow
  201. })
  202. }
  203. else{
  204. this.setData({
  205. navItem: {
  206. type: tag,
  207. name: name,
  208. arr: distItems
  209. },
  210. isShow: !this.data.isShow
  211. })
  212. }
  213. },
  214. getLocationName: function () {
  215. // 检查 globalData 是否已有位置信息
  216. // if (app.globalData.latitude && app.globalData.longitude && app.globalData.locationName) {
  217. // 使用 globalData 中的值
  218. this.setData({
  219. latitude: app.globalData.latitude,
  220. longitude: app.globalData.longitude,
  221. locationName: app.globalData.locationName,
  222. locationNameDesp: app.globalData.locationName
  223. });
  224. app.globalData.city = app.globalData.locationName || '北京';
  225. this.getList(1);
  226. return;
  227. // }
  228. // 没有完整位置信息,获取位置
  229. // wx.getLocation({
  230. // type: 'wgs84',
  231. // success: (res) => {
  232. // // 保存到 globalData
  233. // app.globalData.latitude = res.latitude;
  234. // app.globalData.longitude = res.longitude;
  235. // this.setData({
  236. // latitude: res.latitude,
  237. // longitude: res.longitude
  238. // })
  239. // //2、根据坐标获取当前位置名称,显示在顶部:腾讯地图逆地址解析
  240. // qqmapsdk.reverseGeocoder({
  241. // location: {
  242. // latitude: res.latitude,
  243. // longitude: res.longitude
  244. // },
  245. // success: (res) => {
  246. // let { city: locationName } = res.result.address_component;
  247. // locationName = locationName.substring(0, 2);
  248. // // 保存到 globalData
  249. // app.globalData.city = locationName;
  250. // app.globalData.locationName = locationName;
  251. // this.setData({
  252. // locationName,
  253. // locationNameDesp: locationName
  254. // })
  255. // this.getList(1)
  256. // },
  257. // fail: function (res) {
  258. // // 失败时设置默认值
  259. // app.globalData.city = "北京";
  260. // app.globalData.locationName = "北京";
  261. // this.setData({
  262. // locationName: "北京"
  263. // })
  264. // },
  265. // complete: function (res) {
  266. // }
  267. // });
  268. // }
  269. // })
  270. },
  271. offlineRecommend: function () {
  272. // 检查 globalData 是否已有位置信息
  273. if (app.globalData.latitude && app.globalData.longitude) {
  274. // 使用 globalData 中的经纬度
  275. this.setData({
  276. latitude: app.globalData.latitude,
  277. longitude: app.globalData.longitude
  278. });
  279. // 处理 city 设置
  280. if (app.globalData.locationName) {
  281. app.globalData.city = app.globalData.locationName;
  282. } else {
  283. app.globalData.city = "北京";
  284. }
  285. }
  286. // else {
  287. // // 没有位置信息,获取位置
  288. // wx.getLocation({
  289. // type: 'wgs84',
  290. // success: (res) => {
  291. // // 保存到 globalData
  292. // app.globalData.latitude = res.latitude;
  293. // app.globalData.longitude = res.longitude;
  294. // this.setData({
  295. // latitude: res.latitude,
  296. // longitude: res.longitude
  297. // })
  298. // }
  299. // })
  300. // }
  301. let { latitude: lat, longitude: lng } = this.data;
  302. newRequestFns["offlineRecommend"]({
  303. lat,
  304. lng,
  305. }, "", res => {
  306. this.setData({
  307. ExhibitionPopular: res.data.data
  308. })
  309. }, err => {
  310. }, complete => {
  311. })
  312. },
  313. getList: function (page) {
  314. let { locationNameDesp, locationName, latitude, longitude, activeDist, activeType } = this.data;
  315. let loginSessionKey = wx.getStorageSync("token");
  316. let sort = distItems.find(item => item.id === activeDist).idx
  317. let type = typeItems.find(item => item.id === activeType).idx
  318. this.setData({
  319. loading: true
  320. })
  321. newRequestFns["getExhibitionListOffline"]({
  322. page: page,
  323. type: type,
  324. loginSessionKey,
  325. city: locationName,
  326. sort,
  327. type,
  328. lng: longitude,
  329. lat: latitude
  330. }, '', res => {
  331. let tempContent = this.data.exhibitionList
  332. ? this.data.exhibitionList
  333. : [];
  334. let { pageData: exhibitionList, total } = res.data.data;
  335. // 判断是否为最后一页(返回空数组表示没有更多数据)
  336. let isLastPage = !exhibitionList || exhibitionList.length === 0;
  337. if (isLastPage) {
  338. this.setData({
  339. loading: false,
  340. lastPage: true, // 标记为最后一页
  341. exhiNum: total || 0
  342. });
  343. wx.stopPullDownRefresh();
  344. if (total <= 3) {
  345. this.offlineRecommend();
  346. }
  347. return;
  348. }
  349. exhibitionList.forEach((currentValue) => {
  350. currentValue.product ? currentValue.product.link = escape(currentValue.product.link) : '';
  351. })
  352. // 拼接新数据
  353. let newExhibitionList = tempContent.concat(exhibitionList);
  354. this.setData({
  355. currentPage: page,
  356. lastPage: false, // 有数据时设为false
  357. loading: false,
  358. exhibitionList: newExhibitionList,
  359. exhiNum: total
  360. });
  361. wx.stopPullDownRefresh();
  362. // WxParse.wxParseTemArray("replyTemArray", 'reply', replyArr.length, that)
  363. if (total <= 3){
  364. this.offlineRecommend();
  365. }
  366. }, err => {
  367. }, complete => {
  368. })
  369. },
  370. getNearByList: function (page) {
  371. let { type, locationName, latitude, longitude } = this.data;
  372. let loginSessionKey = wx.getStorageSync("token");
  373. console.log(locationName, latitude, longitude, 999)
  374. wx.getSetting({
  375. success: res => {
  376. if (!res.authSetting['scope.userLocation']) {
  377. Toast.showToast('tip', "无法获取用户位置", () => {
  378. return;
  379. })
  380. this.setData({
  381. getLocationBtn: true
  382. })
  383. }
  384. else {
  385. this.setData({
  386. loading: true,
  387. getLocationBtn: false
  388. })
  389. if (!longitude && !latitude) {
  390. // 检查 globalData 是否已有位置信息
  391. if (app.globalData.latitude && app.globalData.longitude) {
  392. // 使用 globalData 中的经纬度
  393. let longitude = app.globalData.longitude;
  394. let latitude = app.globalData.latitude;
  395. // 处理 city 设置
  396. if (app.globalData.locationName) {
  397. app.globalData.city = app.globalData.locationName;
  398. } else {
  399. app.globalData.city = "北京";
  400. }
  401. this.setData({
  402. longitude,
  403. latitude
  404. });
  405. newRequestFns["getNearByList"]({
  406. page: page,
  407. loginSessionKey,
  408. city: locationName,
  409. lng: longitude,
  410. lat: latitude
  411. }, '', res => {
  412. let tempContent = this.data.exhibitionList
  413. ? this.data.exhibitionList
  414. : [];
  415. let { pageData: exhibitionList, total } = res.data.data;
  416. // 判断是否为最后一页(返回空数组表示没有更多数据)
  417. let isLastPage = !exhibitionList || exhibitionList.length === 0;
  418. if (isLastPage) {
  419. this.setData({
  420. loading: false,
  421. lastPage: true
  422. });
  423. wx.stopPullDownRefresh();
  424. return;
  425. }
  426. // 拼接新数据
  427. let newExhibitionList = tempContent.concat(exhibitionList);
  428. this.setData({
  429. currentPage: page,
  430. lastPage: false, // 有数据时设为false
  431. loading: false,
  432. exhibitionList: newExhibitionList,
  433. });
  434. wx.stopPullDownRefresh();
  435. // WxParse.wxParseTemArray("replyTemArray", 'reply', replyArr.length, that)
  436. }, err => {
  437. }, complete => {
  438. })
  439. } else {
  440. // 没有位置信息,获取位置
  441. // wx.getLocation({
  442. // success: res => {
  443. // // 保存到 globalData
  444. // app.globalData.latitude = res.latitude;
  445. // app.globalData.longitude = res.longitude;
  446. // let longitude = (this.longitude = res.longitude);
  447. // let latitude = (this.latitude = res.latitude);
  448. // newRequestFns["getNearByList"]({
  449. // page: page,
  450. // loginSessionKey,
  451. // city: locationName,
  452. // lng: longitude,
  453. // lat: latitude
  454. // }, '', res => {
  455. // let tempContent = this.data.exhibitionList
  456. // ? this.data.exhibitionList
  457. // : [];
  458. // let { pageData: exhibitionList, total } = res.data.data;
  459. // // 判断是否为最后一页(返回空数组表示没有更多数据)
  460. // let isLastPage = !exhibitionList || exhibitionList.length === 0;
  461. // if (isLastPage) {
  462. // this.setData({
  463. // loading: false,
  464. // lastPage: true
  465. // });
  466. // wx.stopPullDownRefresh();
  467. // return;
  468. // }
  469. // // 拼接新数据
  470. // let newExhibitionList = tempContent.concat(exhibitionList);
  471. // this.setData({
  472. // currentPage: page,
  473. // lastPage: false, // 有数据时设为false
  474. // loading: false,
  475. // longitude,
  476. // latitude,
  477. // exhibitionList: newExhibitionList,
  478. // });
  479. // wx.stopPullDownRefresh();
  480. // // WxParse.wxParseTemArray("replyTemArray", 'reply', replyArr.length, that)
  481. // }, err => {
  482. // }, complete => {
  483. // })
  484. // },
  485. // })
  486. }
  487. } else {
  488. newRequestFns["getNearByList"]({
  489. page: page,
  490. loginSessionKey,
  491. city: locationName,
  492. lng: longitude,
  493. lat: latitude
  494. }, '', res => {
  495. let tempContent = this.data.exhibitionList
  496. ? this.data.exhibitionList
  497. : [];
  498. let { pageData: exhibitionList, total } = res.data.data;
  499. // 判断是否为最后一页(返回空数组表示没有更多数据)
  500. let isLastPage = !exhibitionList || exhibitionList.length === 0;
  501. if (isLastPage) {
  502. this.setData({
  503. loading: false,
  504. lastPage: true
  505. });
  506. wx.stopPullDownRefresh();
  507. return;
  508. }
  509. // 拼接新数据
  510. let newExhibitionList = tempContent.concat(exhibitionList);
  511. this.setData({
  512. currentPage: page,
  513. lastPage: false, // 有数据时设为false
  514. loading: false,
  515. exhibitionList: newExhibitionList,
  516. });
  517. wx.stopPullDownRefresh();
  518. }, err => {
  519. }, complete => {
  520. })
  521. }
  522. }
  523. }
  524. })
  525. },
  526. closeDialog: function () {
  527. this.setData({
  528. showConfirm: false
  529. })
  530. },
  531. loadMore: function () {
  532. let { activeIndex } = this.data
  533. if (!this.data.lastPage) {
  534. if (activeIndex == 1) {
  535. this.getNearByList(this.data.currentPage + 1);
  536. }
  537. else {
  538. this.getList(this.data.currentPage + 1);
  539. }
  540. } else {
  541. return;
  542. }
  543. },
  544. onReachBottom: function () {
  545. if (!this.data.loading) {
  546. this.loadMore();
  547. }
  548. },
  549. tabClick: function (e) {
  550. console.log(111111)
  551. this.setData({
  552. exhibitionList: [],
  553. activeIndex: e.currentTarget.id,
  554. type: e.currentTarget.dataset.tag,
  555. currentPage: 1
  556. });
  557. if (e.currentTarget.id == 1) {
  558. this.getNearByList(1);
  559. }
  560. else {
  561. this.getList(1);
  562. }
  563. },
  564. onIsclose: function (e) {
  565. this.setData({
  566. isShow: e.detail
  567. })
  568. },
  569. getActiveItem:function(e){
  570. let { activeDist, activeType} = e.detail
  571. this.setData({
  572. activeDist,
  573. activeType,
  574. exhibitionList:[]
  575. })
  576. this.getList(1)
  577. },
  578. to_search: function () {
  579. // 检查 globalData 是否已有位置信息
  580. if (app.globalData.latitude && app.globalData.longitude && app.globalData.locationName) {
  581. // 已有位置信息,直接跳转
  582. wx.navigateTo({
  583. url: './search/index',
  584. success: function (res) { },
  585. fail: function (res) { },
  586. complete: function (res) { },
  587. })
  588. return;
  589. }
  590. // // 没有位置信息,显示弹窗
  591. // wx.showModal({
  592. // title: '获取你的位置信息',
  593. // content: '将获取你的具体位置信息,用于推荐你所在定位附近的展览',
  594. // confirmText: '同意',
  595. // cancelText: '拒绝',
  596. // success: (res) => {
  597. // if (res.confirm) {
  598. // // 用户点击同意,获取位置信息(使用 getLocationName 的逻辑)
  599. // wx.getLocation({
  600. // type: 'wgs84',
  601. // success: (locationRes) => {
  602. // // 设置坐标到 globalData 和页面数据
  603. // app.globalData.latitude = locationRes.latitude;
  604. // app.globalData.longitude = locationRes.longitude;
  605. // this.setData({
  606. // latitude: locationRes.latitude,
  607. // longitude: locationRes.longitude
  608. // });
  609. // // 使用腾讯地图逆地址解析获取城市名称
  610. // qqmapsdk.reverseGeocoder({
  611. // location: {
  612. // latitude: locationRes.latitude,
  613. // longitude: locationRes.longitude
  614. // },
  615. // success: (geoRes) => {
  616. // let { city: locationName } = geoRes.result.address_component;
  617. // locationName = locationName.substring(0, 2);
  618. // // 保存到 globalData
  619. // app.globalData.city = locationName;
  620. // app.globalData.locationName = locationName;
  621. // this.setData({
  622. // locationName,
  623. // locationNameDesp: locationName
  624. // });
  625. // // 获取位置成功,跳转到搜索页面
  626. // wx.navigateTo({
  627. // url: './search/index',
  628. // success: function (res) { },
  629. // fail: function (res) { },
  630. // complete: function (res) { },
  631. // })
  632. // },
  633. // fail: (geoErr) => {
  634. // // 逆地址解析失败,使用默认城市(北京),直接跳转
  635. // app.globalData.city = "北京";
  636. // app.globalData.locationName = "北京";
  637. // this.setData({
  638. // locationName: "北京"
  639. // });
  640. // wx.navigateTo({
  641. // url: './search/index',
  642. // success: function (res) { },
  643. // fail: function (res) { },
  644. // complete: function (res) { },
  645. // })
  646. // }
  647. // });
  648. // },
  649. // fail: (locationErr) => {
  650. // // 获取位置失败,保持默认城市(北京),直接跳转
  651. // console.log('获取位置失败:', locationErr);
  652. // app.globalData.city = "北京";
  653. // app.globalData.locationName = "北京";
  654. // this.setData({
  655. // getLocationBtn: true,
  656. // locationName: "北京"
  657. // });
  658. // wx.navigateTo({
  659. // url: './search/index',
  660. // success: function (res) { },
  661. // fail: function (res) { },
  662. // complete: function (res) { },
  663. // })
  664. // }
  665. // })
  666. // } else if (res.cancel) {
  667. // // 用户点击拒绝,保持默认城市(北京),直接跳转
  668. // app.globalData.city = "北京";
  669. // app.globalData.locationName = "北京";
  670. // wx.navigateTo({
  671. // url: './search/index',
  672. // success: function (res) { },
  673. // fail: function (res) { },
  674. // complete: function (res) { },
  675. // })
  676. // }
  677. // },
  678. // fail: function (res) {
  679. // // 弹窗失败,直接跳转
  680. // wx.navigateTo({
  681. // url: './search/index',
  682. // success: function (res) { },
  683. // fail: function (res) { },
  684. // complete: function (res) { },
  685. // })
  686. // }
  687. // })
  688. }
  689. })