index.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. //index.js
  2. const {
  3. request,
  4. serverName,
  5. imgServer
  6. } = require('../../utils/services');
  7. const {
  8. newRequestFns,
  9. newServerName
  10. } = require('../../utils/newServices.js');
  11. const {
  12. Toast
  13. } = require('../../utils/util.js');
  14. const {
  15. defaultImg,
  16. noExhibitionImg,
  17. closeImg
  18. } = require('../../utils/images');
  19. const WxParse = require('../../common/component/wxParse/wxParse.js');
  20. const app = getApp();
  21. Page({
  22. data: {
  23. // navItem: [{ title: '热门', tag: 0 }, { title: '即将开始', tag: 2 }, { title: '展览中', tag: 4 }, { title: '已结束', tag: 6 }],
  24. indicatorDots: true,
  25. autoplay: true,
  26. interval: 2000,
  27. duration: 1000,
  28. activeIndex: 0,
  29. animationData: {},
  30. loading: false,
  31. isLike: true,
  32. commodityImgs: [],
  33. testImg: '../../imgs/testImg/fdkz.png',
  34. currentPage: 1,
  35. exhibitionList: [],
  36. likes: {},
  37. tag: 0,
  38. latitude: null,
  39. longitude: null,
  40. showRecommend:false,
  41. bombList:[], // 首页弹窗
  42. },
  43. onLoad: function() {
  44. this.setData({
  45. serverName,
  46. defaultImg,
  47. noExhibitionImg,
  48. imgServer,
  49. closeImg,
  50. showRecommend:true
  51. // isLogin
  52. })
  53. // 获取首页弹窗
  54. this.getBombList();
  55. this.getBanner();
  56. this.getList(1);
  57. // wx.hideTabBar()
  58. },
  59. loadMore: function() {
  60. if (!this.data.lastPage) {
  61. console.log(this.data.currentPage + 1)
  62. this.getList(this.data.currentPage + 1);
  63. } else {
  64. return;
  65. }
  66. },
  67. onReachBottom: function() {
  68. if (!this.data.loading) {
  69. this.loadMore();
  70. console.log('reach Bottom');
  71. }
  72. },
  73. onShow: function() {
  74. let {
  75. collectedArr,
  76. collectedChange
  77. } = app.globalData;
  78. let {
  79. exhibitionList
  80. } = this.data
  81. let isLogin = app.globalData.isLogin;
  82. this.setData({
  83. isLogin
  84. })
  85. if (collectedChange) {
  86. for (let i = 0; i < exhibitionList.length; i++) {
  87. for (let j = 0; j < collectedArr.length; j++) {
  88. if (collectedArr[j].collectedId == exhibitionList[i].id) {
  89. exhibitionList[i].hasCollect = collectedArr[j].status
  90. if (exhibitionList[i].hasCollect) {
  91. exhibitionList[i].collectionsCount += 1;
  92. } else {
  93. exhibitionList[i].collectionsCount -= 1;
  94. }
  95. if (exhibitionList[i].collectionsCount < 0) {
  96. exhibitionList[i].collectionsCount = 0
  97. }
  98. }
  99. }
  100. // console.log(collectedArr.status, collectedArr.collectedId)
  101. }
  102. this.setData({
  103. exhibitionList,
  104. })
  105. }
  106. // app.globalData.clickToSelect = false;
  107. app.globalData.collectedChange = false;
  108. },
  109. onPullDownRefresh: function() {
  110. this.setData({
  111. exhibitionList: [],
  112. currentPage: 1
  113. });
  114. this.getBanner();
  115. this.getList(1);
  116. },
  117. hideRecommend:function(){
  118. this.setData({
  119. showRecommend:false
  120. })
  121. wx.showTabBar()
  122. },
  123. fetchData: function (page){
  124. newRequestFns["getExhibitionList"]({
  125. page: page,
  126. type: 0,
  127. lat: this.data.latitude,
  128. lng: this.data.longitude
  129. }, '', res => {
  130. let tempContent = this.data.exhibitionList ?
  131. this.data.exhibitionList :
  132. [];
  133. let {
  134. pageData: exhibitionList,
  135. total
  136. } = res.data.data;
  137. // 判断是否为最后一页(返回空数组表示没有更多数据)
  138. let isLastPage = !exhibitionList || exhibitionList.length === 0;
  139. if (isLastPage) {
  140. this.setData({
  141. loading: false,
  142. lastPage: true // 标记为最后一页
  143. });
  144. return;
  145. }
  146. exhibitionList.forEach((currentValue) => {
  147. currentValue.distance = Math.ceil(currentValue.distance);
  148. if (currentValue.product){
  149. currentValue.product.link = escape(currentValue.product.link);
  150. currentValue.product.imageUrl = escape(currentValue.product.imageUrl);
  151. }
  152. })
  153. // 拼接新数据
  154. let newExhibitionList = tempContent.concat(exhibitionList);
  155. this.setData({
  156. currentPage: page,
  157. lastPage: false, // 有数据时设为false
  158. loading: false,
  159. exhibitionList: newExhibitionList,
  160. });
  161. // WxParse.wxParseTemArray("replyTemArray", 'reply', replyArr.length, that)
  162. }, err => {
  163. }, complete => {
  164. wx.stopPullDownRefresh();
  165. })
  166. },
  167. getList: function(page) {
  168. let type = this.data.tag;
  169. this.setData({
  170. loading: true
  171. })
  172. // 检查是否已有位置信息(页面数据或 globalData)
  173. if (this.data.latitude || this.data.longitude || (app.globalData.latitude && app.globalData.longitude)) {
  174. // 如果页面没有位置信息但 globalData 有,使用 globalData 的值
  175. if (!this.data.latitude && !this.data.longitude && app.globalData.latitude && app.globalData.longitude) {
  176. this.setData({
  177. latitude: app.globalData.latitude,
  178. longitude: app.globalData.longitude
  179. });
  180. // 处理 city 设置
  181. if (app.globalData.locationName) {
  182. app.globalData.city = app.globalData.locationName;
  183. } else {
  184. app.globalData.city = "北京";
  185. }
  186. }
  187. this.fetchData(page)
  188. }
  189. // else {
  190. // wx.getLocation({
  191. // type: 'wgs84',
  192. // success: (res) => {
  193. // // 保存到 globalData
  194. // app.globalData.latitude = res.latitude;
  195. // app.globalData.longitude = res.longitude;
  196. // this.setData({
  197. // latitude: res.latitude,
  198. // longitude: res.longitude
  199. // })
  200. // this.fetchData(page)
  201. // },
  202. // fail: (res) => {
  203. // this.fetchData(page)
  204. // }
  205. // })
  206. // }
  207. },
  208. getBanner: function() {
  209. newRequestFns.getBannerList({}, 'get', res => {
  210. let commodityImgs = res.data.data.pageData;
  211. this.setData({
  212. commodityImgs: commodityImgs || []
  213. });
  214. console.log('banner', commodityImgs)
  215. }, err => {
  216. }, complete => {
  217. wx.stopPullDownRefresh()
  218. })
  219. },
  220. getBombList: function() {
  221. newRequestFns.getBombList({}, 'get', res => {
  222. console.log('bomb', this.data.showRecommend)
  223. if(res.data.data.length && this.data.showRecommend) {
  224. wx.hideTabBar()
  225. }
  226. this.setData({
  227. bombList: res.data.data || []
  228. })
  229. }, err => {
  230. }, complete => {
  231. })
  232. },
  233. addLike: function(e) {
  234. let {
  235. type,
  236. id,
  237. idx
  238. } = e.currentTarget.dataset;
  239. console.log(idx, type, id)
  240. let likes = this.data.likes;
  241. likes[id] = !likes[id];
  242. let exhibitionList = this.data.exhibitionList;
  243. let {
  244. collectedArr,
  245. collectedChange
  246. } = app.globalData, hasItem = true;
  247. console.log('type', type)
  248. this.setData({
  249. likes: likes
  250. })
  251. Toast.showToast2('loading');
  252. let loginSessionKey = wx.getStorageSync('token') || "";
  253. // if (loginSessionKey){
  254. newRequestFns['isCollect']({
  255. loginSessionKey,
  256. exhibitionId: id,
  257. type: Number(type),
  258. }, "post", res => {
  259. if (res.data.code > -1) {
  260. for (let i = 0; i < collectedArr.length; i++) {
  261. if (collectedArr[i].collectedId && id == collectedArr[i].collectedId) {
  262. console.log("true")
  263. collectedArr[i] = {
  264. collectedId: id,
  265. status: res.data.data.hasCollect,
  266. }
  267. hasItem = false;
  268. }
  269. }
  270. if (hasItem) {
  271. collectedArr.push({
  272. collectedId: id,
  273. status: res.data.data.hasCollect,
  274. })
  275. }
  276. app.globalData.collectedArr = collectedArr;
  277. app.globalData.collectedChange = true;
  278. exhibitionList[idx].hasCollect = res.data.data.hasCollect
  279. this.setData({
  280. exhibitionList: exhibitionList
  281. })
  282. }
  283. }, err => {
  284. }, complete => {
  285. Toast.hideLoading();
  286. })
  287. // }
  288. console.log(this.data.exhibitionList)
  289. },
  290. tabClick: function(e) {
  291. // console.log(e.currentTarget.dataset)
  292. // if (e.currentTarget.id == 1) {
  293. // wx.switchTab({
  294. // url: '../swkz/index',
  295. // success: function (res) { },
  296. // fail: function (res) { },
  297. // complete: function (res) { },
  298. // })
  299. // }
  300. // else {
  301. this.setData({
  302. exhibitionList: [],
  303. activeIndex: e.currentTarget.id,
  304. tag: e.currentTarget.dataset.tag,
  305. currentPage: 1
  306. });
  307. this.getList(1);
  308. // }
  309. },
  310. onShareAppMessage: function() {
  311. },
  312. navigatorBomb: function(e) {
  313. let {
  314. url,
  315. pagetype,
  316. urltype,
  317. hasproduct,
  318. haspay,
  319. productlink,
  320. id,
  321. description,
  322. imageUrl,
  323. relayUrl
  324. } = e.currentTarget.dataset;
  325. console.log('dwdwdw', hasproduct, productlink, imageUrl)
  326. if (productlink) {
  327. wx.navigateTo({
  328. url: `/pages/webview/index?url=${encodeURIComponent(productlink)}`
  329. });
  330. } else {
  331. wx.navigateTo({
  332. url: `../zl_detail/index?id=${id}&imageUrl=${imageUrl}`,
  333. success: function(res) {},
  334. fail: function(res) {},
  335. complete: function(res) {},
  336. })
  337. }
  338. },
  339. to_pay: function(e) {
  340. console.log('dwdwdw', e.currentTarget.dataset)
  341. let {
  342. url,
  343. pagetype,
  344. urltype,
  345. hasproduct,
  346. haspay,
  347. productlink,
  348. id,
  349. description,
  350. imageUrl,
  351. relayurl
  352. } = e.currentTarget.dataset;
  353. console.log(relayurl, 6666)
  354. // app.globalData.currentUrl = url
  355. // console.log('dwdwdw', app.globalData.currentUrl)
  356. // console.log('dwdwdwd',wx.getStorageSync('url'))
  357. switch (urltype) {
  358. case 0:
  359. if (productlink) {
  360. wx.navigateTo({
  361. url: `/pages/webview/index?url=${encodeURIComponent(productlink)}&relayUrl=${relayurl||''}`
  362. });
  363. }
  364. break;
  365. case 1:
  366. switch (pagetype) {
  367. case 0:
  368. wx.navigateTo({
  369. url: `../cg_detail/index?id=${url}&imageUrl=${imageUrl}&relayUrl=${relayurl||''}`,
  370. success: function(res) {},
  371. fail: function(res) {},
  372. complete: function(res) {},
  373. })
  374. break;
  375. case 1:
  376. // if (hasproduct) {
  377. // 需要调一下getExhibitionDetail获取足迹
  378. wx.navigateTo({
  379. url: `../wv_page/index?&id=${url}&relayUrl=${relayurl||''}`,
  380. success: function(res) {},
  381. fail: function(res) {},
  382. complete: function(res) {},
  383. })
  384. // } else {
  385. // wx.navigateTo({
  386. // url: `../zl_detail/index?id=${url}`,
  387. // success: function(res) {},
  388. // fail: function(res) {},
  389. // complete: function(res) {},
  390. // })
  391. // }
  392. break;
  393. default:
  394. break;
  395. }
  396. break;
  397. case 2:
  398. app.globalData.bannerHtml = description;
  399. wx.navigateTo({
  400. url: `./html_detail/index?id=${id}&imageUrl=${imageUrl}&relayUrl=${relayurl||''}`,
  401. success: function (res) { },
  402. fail: function (res) { },
  403. complete: function (res) { },
  404. })
  405. break;
  406. default:
  407. break;
  408. }
  409. }
  410. })