index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // pages/quality/index/index.js
  2. var app = getApp();
  3. var util = require("../../../utils/util.js");
  4. Page({
  5. /**
  6. * 页面的初始数据
  7. */
  8. data: {
  9. id: null,
  10. tabs: [],
  11. currentTab: 0,
  12. scrollIntoView: null,
  13. pageParams: {
  14. pageSize: 10,
  15. pageNum: 1,
  16. categoryId: 0,
  17. status: true,
  18. },
  19. noMore: false,
  20. list: [],
  21. total: 0,
  22. totalPage: 0,
  23. nodata: util.nodata(),
  24. },
  25. /**
  26. * 生命周期函数--监听页面加载
  27. */
  28. onLoad(options) {},
  29. /**
  30. * 生命周期函数--监听页面初次渲染完成
  31. */
  32. onReady() {},
  33. /**
  34. * 生命周期函数--监听页面显示
  35. */
  36. onShow() {
  37. this.getClassfiy();
  38. // if (!wx.getStorageSync("classfiyId")) {
  39. // this.init(true);
  40. // }
  41. },
  42. getClassfiy() {
  43. app._get("goodscategory/select", {}, (res) => {
  44. if (res.code == 0) {
  45. this.setData({
  46. tabs: [{
  47. name: "全部",
  48. id: 0,
  49. },
  50. ...res.list,
  51. ],
  52. });
  53. const classfiyId = wx.getStorageSync("classfiyId");
  54. if (classfiyId && this.data.tabs.some((el) => el.id == classfiyId)) {
  55. this.data.tabs.map((item, index) => {
  56. if (item.id == classfiyId) {
  57. this.setData({
  58. currentTab: index,
  59. "pageParams.categoryId": classfiyId,
  60. scrollIntoView: 'location' + classfiyId
  61. });
  62. this.init(true);
  63. }
  64. });
  65. } else {
  66. this.setData({
  67. currentTab: 0,
  68. "pageParams.categoryId": 0,
  69. scrollIntoView: 'location' + 0
  70. });
  71. wx.setStorageSync('classfiyId', 0)
  72. this.init(true);
  73. }
  74. }
  75. });
  76. },
  77. init(isRefresh) {
  78. if (!isRefresh && this.data.noMore) {
  79. wx.showToast({
  80. title: "没有更多了~",
  81. icon: "none",
  82. });
  83. return false;
  84. }
  85. this.setData({
  86. list: isRefresh ? [] : this.data.list,
  87. "pageParams.pageNum": isRefresh ? 1 : this.data.pageParams.pageNum + 1,
  88. noMore: isRefresh ? false : this.data.noMore,
  89. });
  90. this.getList();
  91. },
  92. getList() {
  93. wx.showLoading({
  94. title: "努力加载中...",
  95. });
  96. app._get(
  97. "goods/page",
  98. this.data.pageParams,
  99. (res) => {
  100. if (res.code == 0) {
  101. let listData = this.data.list;
  102. listData.push(...res.page.list);
  103. this.setData({
  104. list: listData,
  105. total: res.page.totalCount,
  106. totalPage: res.page.totalPage,
  107. noMore: res.page.totalPage == res.page.currPage,
  108. });
  109. }
  110. },
  111. function () {
  112. wx.hideLoading();
  113. }
  114. );
  115. },
  116. handleClassfiy(e) {
  117. let categoryId = this.data.tabs[e.currentTarget.dataset.index].id;
  118. this.setData({
  119. currentTab: e.currentTarget.dataset.index,
  120. "pageParams.categoryId": categoryId,
  121. scrollIntoView: 'location' + categoryId
  122. });
  123. wx.setStorageSync("classfiyId", categoryId);
  124. if (wx.getStorageSync("classfiyId") || categoryId != null) {
  125. this.init(true);
  126. }
  127. },
  128. goodsDetail(e) {
  129. wx.navigateTo({
  130. url: `/pages/quality/detail/index?id=${e.currentTarget.dataset.id}`,
  131. });
  132. },
  133. /**
  134. * 生命周期函数--监听页面隐藏
  135. */
  136. onHide() {},
  137. /**
  138. * 生命周期函数--监听页面卸载
  139. */
  140. onUnload() {},
  141. /**
  142. * 页面相关事件处理函数--监听用户下拉动作
  143. */
  144. onPullDownRefresh() {
  145. // wx.removeStorageSync('classfiyId')
  146. // this.getClassfiy();
  147. // setTimeout(() => {
  148. // wx.stopPullDownRefresh
  149. // }, 1000);
  150. },
  151. /**
  152. * 页面上拉触底事件的处理函数
  153. */
  154. onReachBottom() {
  155. this.init();
  156. },
  157. /**
  158. * 用户点击右上角分享
  159. */
  160. onShareAppMessage() {},
  161. });