index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. //index.js
  2. const { request, serverName, imgServer } = require('../../utils/services');
  3. const {
  4. newRequestFns,
  5. newServerName
  6. } = require('../../utils/newServices.js');
  7. const { Toast } = require('../../utils/util.js');
  8. const { defaultImg, noExhibitionImg } = require('../../utils/images');
  9. const WxParse = require('../../common/component/wxParse/wxParse.js');
  10. const app = getApp();
  11. Page({
  12. data: {
  13. animationData: {},
  14. loading: false,
  15. isLike: true,
  16. imgServer,
  17. commodityImgs: [],
  18. testImg: '../../imgs/testImg/fdkz.png',
  19. currentPage: 1,
  20. exhibitionList: [],
  21. likes: {},
  22. tag: 1
  23. },
  24. onLoad: function () {
  25. this.setData({
  26. serverName,
  27. defaultImg,
  28. noExhibitionImg,
  29. })
  30. this.getList(1);
  31. },
  32. loadMore: function () {
  33. if (!this.data.lastPage) {
  34. this.getList(this.data.currentPage + 1);
  35. } else {
  36. return;
  37. }
  38. },
  39. onReachBottom: function () {
  40. if (!this.data.loading) {
  41. this.loadMore();
  42. }
  43. },
  44. onShareAppMessage: function () {
  45. },
  46. onShow: function () {
  47. let { collectedArr, collectedChange } = app.globalData;
  48. let { exhibitionList } = this.data
  49. // this.setData({
  50. // exhibitionList: []
  51. // });
  52. // this.getBanner();
  53. // this.getList(1);
  54. if (collectedChange) {
  55. for (let i = 0; i < exhibitionList.length; i++) {
  56. for (let j = 0; j < collectedArr.length; j++) {
  57. if (collectedArr[j].collectedId == exhibitionList[i].id) {
  58. exhibitionList[i].hasCollect = collectedArr[j].status
  59. if (exhibitionList[i].hasCollect) {
  60. exhibitionList[i].collectionsCount += 1;
  61. }
  62. else {
  63. exhibitionList[i].collectionsCount -= 1;
  64. }
  65. if (exhibitionList[i].collectionsCount < 0) {
  66. exhibitionList[i].collectionsCount = 0
  67. }
  68. }
  69. }
  70. }
  71. this.setData({
  72. exhibitionList
  73. })
  74. }
  75. app.globalData.clickToSelect = false;
  76. app.globalData.collectedChange = false;
  77. },
  78. enablePullDownRefresh: function () {
  79. this.setData({
  80. exhibitionList: [],
  81. currentPage: 1
  82. });
  83. this.getList(1);
  84. this.getBanner();
  85. },
  86. onPullDownRefresh: function () {
  87. this.setData({
  88. exhibitionList: [],
  89. currentPage: 1
  90. });
  91. this.getList(1);
  92. },
  93. getList: function (page) {
  94. let type = this.data.tag;
  95. this.setData({
  96. loading: true
  97. })
  98. newRequestFns["getExhibitionList"]({
  99. page: page,
  100. type: type
  101. }, '', res => {
  102. let tempContent = this.data.exhibitionList
  103. ? this.data.exhibitionList
  104. : [];
  105. let { pageData: exhibitionList, total } = res.data.data;
  106. // 判断是否为最后一页(返回空数组表示没有更多数据)
  107. let isLastPage = !exhibitionList || exhibitionList.length === 0;
  108. if (isLastPage) {
  109. this.setData({
  110. loading: false,
  111. lastPage: true // 标记为最后一页
  112. });
  113. wx.stopPullDownRefresh();
  114. return;
  115. }
  116. exhibitionList.forEach((currentValue) => {
  117. currentValue.product ? currentValue.product.link = escape(currentValue.product.link) : '';
  118. currentValue.product ? currentValue.product.imageUrl = escape(currentValue.product.imageUrl) : '';
  119. })
  120. // 拼接新数据
  121. let newExhibitionList = tempContent.concat(exhibitionList);
  122. this.setData({
  123. currentPage: page,
  124. lastPage: false, // 有数据时设为false
  125. loading: false,
  126. exhibitionList: newExhibitionList,
  127. });
  128. wx.stopPullDownRefresh();
  129. // WxParse.wxParseTemArray("replyTemArray", 'reply', replyArr.length, that)
  130. }, err => {
  131. }, complete => {
  132. })
  133. },
  134. addLike: function (e) {
  135. let { type, id, idx } = e.currentTarget.dataset;
  136. let likes = this.data.likes;
  137. likes[id] = !likes[id];
  138. let exhibitionList = this.data.exhibitionList;
  139. let { collectedArr, collectedChange } = app.globalData, hasItem = true;
  140. this.setData({
  141. likes: likes
  142. })
  143. Toast.showToast2('loading');
  144. let loginSessionKey = wx.getStorageSync('token') || "";
  145. // if (loginSessionKey){
  146. newRequestFns['isCollect']({
  147. loginSessionKey,
  148. exhibitionId: id,
  149. type: Number(type),
  150. }, "post", res => {
  151. if (res.data.code > -1) {
  152. for (let i = 0; i < collectedArr.length; i++) {
  153. if (collectedArr[i].collectedId && id == collectedArr[i].collectedId) {
  154. collectedArr[i] = {
  155. collectedId: id,
  156. status: res.data.data.hasCollect,
  157. }
  158. hasItem = false;
  159. }
  160. }
  161. if (hasItem) {
  162. collectedArr.push({
  163. collectedId: id,
  164. status: res.data.data.hasCollect,
  165. })
  166. }
  167. app.globalData.collectedArr = collectedArr;
  168. app.globalData.collectedChange = true;
  169. exhibitionList[idx].hasCollect = res.data.data.hasCollect
  170. this.setData({
  171. exhibitionList: exhibitionList
  172. })
  173. }
  174. }, err => {
  175. }, complete => {
  176. Toast.hideLoading();
  177. })
  178. // }
  179. },
  180. to_search: function () {
  181. wx.navigateTo({
  182. url: './search/index',
  183. success: function (res) { },
  184. fail: function (res) { },
  185. complete: function (res) { },
  186. })
  187. },
  188. })