index.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. // pages/zhanxun/index.js
  2. const { request, serverName, imgServer } = require('../../utils/services');
  3. const { Toast, qqmapsdk } = require('../../utils/util.js');
  4. const { defaultImg, noExhibitionImg } = require('../../utils/images');
  5. const WxParse = require('../../common/component/wxParse/wxParse.js');
  6. const app = getApp();
  7. const distItems = [
  8. {
  9. name: '按距离排序',
  10. id: '距离排序',
  11. idx: 1
  12. },
  13. {
  14. name: '按热度排序',
  15. id: '热度排序',
  16. idx: 2
  17. }
  18. ]
  19. const typeItems = [
  20. {
  21. name: '展览中',
  22. id: '展览中',
  23. idx: 1
  24. },
  25. {
  26. name: '即将开始',
  27. id: '即将开始',
  28. idx: 2
  29. },
  30. {
  31. name: '已结束',
  32. id: '已结束',
  33. idx: 3
  34. },
  35. {
  36. name: '全部状态',
  37. id: '全部状态',
  38. idx: 0
  39. }
  40. ]
  41. Page({
  42. data: {
  43. tabs: ['线上看展', '线下展讯'],
  44. activeIndex: 0,
  45. loading: false,
  46. imgServer,
  47. defaultImg,
  48. noExhibitionImg,
  49. // 线上看展数据
  50. onlineExhibitionList: [],
  51. onlineCurrentPage: 1,
  52. onlineLastPage: false,
  53. onlineLikes: {},
  54. // 线下展讯数据
  55. offlineExhibitionList: [],
  56. offlineCurrentPage: 1,
  57. offlineLastPage: false,
  58. navItem: distItems,
  59. indicatorDots: true,
  60. autoplay: true,
  61. interval: 5000,
  62. duration: 1000,
  63. locationName: "珠海",
  64. locationNameDesp: "",
  65. isLike: false,
  66. showConfirm: false,
  67. tag: "",
  68. commodityImgs: [
  69. '../../imgs/testImg/thumbSmallImg.jpg',
  70. '../../imgs/testImg/thumbSmallImg.jpg',
  71. ],
  72. type: 5,
  73. testImg: '../../imgs/testImg/fdkz.png',
  74. getLocationBtn: false,
  75. isShow: false,
  76. exhiNum: 0,
  77. activeDist: '距离排序',
  78. activeType: '展览中'
  79. },
  80. onLoad: function () {
  81. this.setData({
  82. serverName,
  83. defaultImg,
  84. noExhibitionImg,
  85. imgServer
  86. });
  87. this.getLocationName();
  88. // 初始加载线上看展数据
  89. this.getOnlineExhibitionList(1);
  90. },
  91. tabClick: function (e) {
  92. const index = e.currentTarget.id;
  93. this.setData({
  94. activeIndex: index
  95. });
  96. // 切换到线上看展
  97. if (index == 0) {
  98. if (this.data.onlineExhibitionList.length === 0) {
  99. this.getOnlineExhibitionList(1);
  100. }
  101. }
  102. // 切换到线下展讯
  103. else {
  104. if (this.data.offlineExhibitionList.length === 0) {
  105. this.getOfflineExhibitionList(1);
  106. }
  107. }
  108. },
  109. // 获取线上看展数据
  110. getOnlineExhibitionList: function (page) {
  111. this.setData({
  112. loading: true
  113. });
  114. request["getExhibitionList"]({
  115. page: page,
  116. type: 1 // 线上看展类型
  117. }, '', res => {
  118. let tempContent = this.data.onlineExhibitionList || [];
  119. let { last: lastPage, content: exhibitionList } = res.data.data;
  120. exhibitionList.forEach((currentValue) => {
  121. currentValue.product ? currentValue.product.link = escape(currentValue.product.link) : '';
  122. currentValue.product ? currentValue.product.imageUrl = escape(currentValue.product.imageUrl) : '';
  123. });
  124. this.setData({
  125. onlineCurrentPage: page,
  126. onlineLastPage: lastPage,
  127. loading: false,
  128. onlineExhibitionList: page === 1 ? exhibitionList : tempContent.concat(exhibitionList),
  129. });
  130. wx.stopPullDownRefresh();
  131. }, err => {
  132. this.setData({
  133. loading: false
  134. });
  135. });
  136. },
  137. // 获取线下展讯数据
  138. getOfflineExhibitionList: function (page) {
  139. let { locationName, activeDist, activeType } = this.data;
  140. let loginSessionKey = wx.getStorageSync("token");
  141. let sort = distItems.find(item => item.id === activeDist).idx;
  142. let type = typeItems.find(item => item.id === activeType).idx;
  143. this.setData({
  144. loading: true
  145. });
  146. request["getExhibitionListOffline"]({
  147. page: page,
  148. loginSessionKey,
  149. city: locationName,
  150. sort: sort,
  151. type: type,
  152. lng: this.data.longitude,
  153. lat: this.data.latitude,
  154. }, '', res => {
  155. let tempContent = this.data.offlineExhibitionList || [];
  156. let { last: lastPage, totalElements, content: exhibitionList } = res.data.data;
  157. exhibitionList.forEach((currentValue) => {
  158. currentValue.product ? currentValue.product.link = escape(currentValue.product.link) : '';
  159. });
  160. this.setData({
  161. offlineCurrentPage: page,
  162. offlineLastPage: lastPage,
  163. loading: false,
  164. offlineExhibitionList: page === 1 ? exhibitionList : tempContent.concat(exhibitionList),
  165. exhiNum: totalElements
  166. });
  167. wx.stopPullDownRefresh();
  168. if (this.data.exhiNum <= 3) {
  169. this.offlineRecommend();
  170. }
  171. }, err => {
  172. this.setData({
  173. loading: false
  174. });
  175. });
  176. },
  177. // 下拉刷新
  178. onPullDownRefresh: function () {
  179. if (this.data.activeIndex == 0) {
  180. this.setData({
  181. onlineExhibitionList: [],
  182. onlineCurrentPage: 1
  183. });
  184. this.getOnlineExhibitionList(1);
  185. } else {
  186. this.setData({
  187. offlineExhibitionList: [],
  188. offlineCurrentPage: 1
  189. });
  190. this.getOfflineExhibitionList(1);
  191. }
  192. },
  193. // 上拉加载更多
  194. onReachBottom: function () {
  195. if (this.data.loading) return;
  196. if (this.data.activeIndex == 0) {
  197. if (!this.data.onlineLastPage) {
  198. this.getOnlineExhibitionList(this.data.onlineCurrentPage + 1);
  199. }
  200. } else {
  201. if (!this.data.offlineLastPage) {
  202. this.getOfflineExhibitionList(this.data.offlineCurrentPage + 1);
  203. }
  204. }
  205. },
  206. // 收藏/取消收藏展览
  207. addLike: function (e) {
  208. let { type, id, idx, listType } = e.currentTarget.dataset;
  209. let exhibitionList = listType === 'online' ? this.data.onlineExhibitionList : this.data.offlineExhibitionList;
  210. let { collectedArr } = app.globalData;
  211. let hasItem = true;
  212. if (listType === 'online') {
  213. let onlineLikes = this.data.onlineLikes;
  214. onlineLikes[id] = !onlineLikes[id];
  215. this.setData({
  216. onlineLikes: onlineLikes
  217. });
  218. }
  219. Toast.showToast2('loading');
  220. let loginSessionKey = wx.getStorageSync('token') || "";
  221. request['isCollect']({
  222. loginSessionKey,
  223. exhibitionId: id,
  224. type: Number(type),
  225. }, "post", res => {
  226. if (res.data.code > -1) {
  227. for (let i = 0; i < collectedArr.length; i++) {
  228. if (collectedArr[i].collectedId && id == collectedArr[i].collectedId) {
  229. collectedArr[i] = {
  230. collectedId: id,
  231. status: res.data.data.hasCollect,
  232. };
  233. hasItem = false;
  234. }
  235. }
  236. if (hasItem) {
  237. collectedArr.push({
  238. collectedId: id,
  239. status: res.data.data.hasCollect,
  240. });
  241. }
  242. app.globalData.collectedArr = collectedArr;
  243. app.globalData.collectedChange = true;
  244. exhibitionList[idx].hasCollect = res.data.data.hasCollect;
  245. if (listType === 'online') {
  246. this.setData({
  247. onlineExhibitionList: exhibitionList
  248. });
  249. } else {
  250. this.setData({
  251. offlineExhibitionList: exhibitionList
  252. });
  253. }
  254. }
  255. }, err => {
  256. // 错误处理
  257. }, complete => {
  258. Toast.hideLoading();
  259. });
  260. },
  261. // 获取位置名称
  262. getLocationName: function() {
  263. wx.getLocation({
  264. type: 'wgs84',
  265. success: (res) => {
  266. this.setData({
  267. latitude: res.latitude,
  268. longitude: res.longitude
  269. });
  270. qqmapsdk.reverseGeocoder({
  271. location: {
  272. latitude: res.latitude,
  273. longitude: res.longitude
  274. },
  275. success: (res) => {
  276. let { city: locationName } = res.result.address_component;
  277. locationName = locationName.substring(0, 2);
  278. app.globalData.city = locationName;
  279. this.setData({
  280. locationName,
  281. locationNameDesp: locationName
  282. });
  283. },
  284. fail: function (res) {
  285. this.setData({
  286. locationName: ""
  287. });
  288. }
  289. });
  290. },
  291. fail: () => {
  292. this.setData({
  293. getLocationBtn: true
  294. });
  295. }
  296. });
  297. },
  298. // 线下推荐
  299. offlineRecommend: function () {
  300. wx.getLocation({
  301. type: 'wgs84',
  302. success: (res) => {
  303. this.setData({
  304. latitude: res.latitude,
  305. longitude: res.longitude
  306. });
  307. }
  308. });
  309. let { latitude: lat, longitude: lng } = this.data;
  310. request["offlineRecommend"]({
  311. lat,
  312. lng,
  313. }, "", res => {
  314. this.setData({
  315. ExhibitionPopular: res.data.data
  316. });
  317. }, err => {
  318. // 错误处理
  319. });
  320. },
  321. // 前往搜索页面
  322. to_search: function() {
  323. wx.navigateTo({
  324. url: '../tongcheng/search/index'
  325. });
  326. },
  327. // 展览搜索
  328. to_search_exhibition: function () {
  329. wx.navigateTo({
  330. url: '../yuezhan/search/index'
  331. });
  332. },
  333. // 显示下拉菜单
  334. showDrop: function(e) {
  335. let tag = e.target.dataset.id;
  336. let name = e.target.dataset.test;
  337. if (tag === 'type') {
  338. this.setData({
  339. navItem: {
  340. type: tag,
  341. name: name,
  342. arr: typeItems
  343. },
  344. isShow: !this.data.isShow
  345. });
  346. } else {
  347. this.setData({
  348. navItem: {
  349. type: tag,
  350. name: name,
  351. arr: distItems
  352. },
  353. isShow: !this.data.isShow
  354. });
  355. }
  356. },
  357. // 关闭下拉菜单
  358. onIsclose: function(e) {
  359. this.setData({
  360. isShow: e.detail
  361. });
  362. },
  363. // 选择下拉菜单项
  364. getActiveItem: function(e) {
  365. let { activeDist, activeType } = e.detail;
  366. this.setData({
  367. activeDist,
  368. activeType,
  369. offlineExhibitionList: []
  370. });
  371. this.getOfflineExhibitionList(1);
  372. },
  373. // 关闭确认对话框
  374. closeDialog: function() {
  375. this.setData({
  376. showConfirm: false
  377. });
  378. },
  379. // 获取附近列表
  380. getNearByList: function (page) {
  381. let { locationName, latitude, longitude } = this.data;
  382. let loginSessionKey = wx.getStorageSync("token");
  383. wx.getSetting({
  384. success: res => {
  385. if (!res.authSetting['scope.userLocation']) {
  386. Toast.showToast('tip', "无法获取用户位置", () => {
  387. return;
  388. });
  389. this.setData({
  390. getLocationBtn: true
  391. });
  392. } else {
  393. this.setData({
  394. loading: true,
  395. getLocationBtn: false
  396. });
  397. if (!longitude && !latitude) {
  398. wx.getLocation({
  399. success: res => {
  400. let longitude = res.longitude;
  401. let latitude = res.latitude;
  402. request["getNearByList"]({
  403. page: page,
  404. loginSessionKey,
  405. city: locationName,
  406. lng: longitude,
  407. lat: latitude
  408. }, '', res => {
  409. let tempContent = this.data.offlineExhibitionList || [];
  410. let { last: lastPage, content: exhibitionList } = res.data.data;
  411. this.setData({
  412. offlineCurrentPage: page,
  413. offlineLastPage: lastPage,
  414. loading: false,
  415. longitude,
  416. latitude,
  417. offlineExhibitionList: page === 1 ? exhibitionList : tempContent.concat(exhibitionList),
  418. });
  419. wx.stopPullDownRefresh();
  420. }, err => {
  421. // 错误处理
  422. });
  423. }
  424. });
  425. } else {
  426. request["getNearByList"]({
  427. page: page,
  428. loginSessionKey,
  429. city: locationName,
  430. lng: longitude,
  431. lat: latitude
  432. }, '', res => {
  433. let tempContent = this.data.offlineExhibitionList || [];
  434. let { last: lastPage, content: exhibitionList } = res.data.data;
  435. this.setData({
  436. offlineCurrentPage: page,
  437. offlineLastPage: lastPage,
  438. loading: false,
  439. offlineExhibitionList: page === 1 ? exhibitionList : tempContent.concat(exhibitionList),
  440. });
  441. wx.stopPullDownRefresh();
  442. }, err => {
  443. // 错误处理
  444. });
  445. }
  446. }
  447. }
  448. });
  449. },
  450. onShow: function() {
  451. let { city, clickToSelect, collectedArr, collectedChange } = app.globalData;
  452. if (clickToSelect) {
  453. this.setData({
  454. locationName: city || "珠海",
  455. offlineExhibitionList: [],
  456. offlineCurrentPage: 1
  457. });
  458. if (this.data.activeIndex == 1) {
  459. this.getOfflineExhibitionList(1);
  460. }
  461. }
  462. if (collectedChange) {
  463. // 更新线上看展收藏状态
  464. if (this.data.onlineExhibitionList.length > 0) {
  465. let onlineExhibitionList = this.data.onlineExhibitionList;
  466. for (let i = 0; i < onlineExhibitionList.length; i++) {
  467. for (let j = 0; j < collectedArr.length; j++) {
  468. if (collectedArr[j].collectedId == onlineExhibitionList[i].id) {
  469. onlineExhibitionList[i].hasCollect = collectedArr[j].status;
  470. if (onlineExhibitionList[i].hasCollect) {
  471. onlineExhibitionList[i].collectionsCount += 1;
  472. } else {
  473. onlineExhibitionList[i].collectionsCount -= 1;
  474. }
  475. if (onlineExhibitionList[i].collectionsCount < 0) {
  476. onlineExhibitionList[i].collectionsCount = 0;
  477. }
  478. }
  479. }
  480. }
  481. this.setData({ onlineExhibitionList });
  482. }
  483. // 更新线下展讯收藏状态
  484. if (this.data.offlineExhibitionList.length > 0) {
  485. let offlineExhibitionList = this.data.offlineExhibitionList;
  486. for (let i = 0; i < offlineExhibitionList.length; i++) {
  487. for (let j = 0; j < collectedArr.length; j++) {
  488. if (collectedArr[j].collectedId == offlineExhibitionList[i].id) {
  489. offlineExhibitionList[i].hasCollect = collectedArr[j].status;
  490. if (offlineExhibitionList[i].hasCollect) {
  491. offlineExhibitionList[i].collectionsCount += 1;
  492. } else {
  493. offlineExhibitionList[i].collectionsCount -= 1;
  494. }
  495. if (offlineExhibitionList[i].collectionsCount < 0) {
  496. offlineExhibitionList[i].collectionsCount = 0;
  497. }
  498. }
  499. }
  500. }
  501. this.setData({ offlineExhibitionList });
  502. }
  503. }
  504. app.globalData.clickToSelect = false;
  505. app.globalData.collectedChange = false;
  506. },
  507. onShareAppMessage: function () {
  508. // 分享功能
  509. }
  510. })