goods_list.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. import { getProductslist, getProductHot } from '../../api/store.js';
  2. const app = getApp();
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. productList:[],
  9. parameter: {
  10. 'navbar': '1',
  11. 'return': '1',
  12. 'title': '商品列表',
  13. 'color': true,
  14. 'class': '0'
  15. },
  16. navH: "",
  17. is_switch:true,
  18. where: {
  19. sid: 0,
  20. keyword: '',
  21. priceOrder: '',
  22. salesOrder: '',
  23. news: 0,
  24. page: 1,
  25. limit: 10,
  26. cid: 0,
  27. },
  28. price:0,
  29. stock:0,
  30. nows:false,
  31. loadend:false,
  32. loading:false,
  33. loadTitle:'加载更多',
  34. },
  35. /**
  36. * 生命周期函数--监听页面加载
  37. */
  38. onLoad: function (options) {
  39. this.setData({
  40. ['where.sid']: options.sid || 0,
  41. title: options.title || '',
  42. ['where.keyword']: options.searchValue || '',
  43. navH: app.globalData.navHeight
  44. });
  45. this.get_product_list();
  46. this.get_host_product();
  47. },
  48. Changswitch:function(){
  49. var that = this;
  50. that.setData({
  51. is_switch: !this.data.is_switch
  52. })
  53. },
  54. searchSubmit: function (e) {
  55. var that = this;
  56. this.setData({ ['where.keyword']: e.detail.value, loadend: false, ['where.page']: 1 })
  57. this.get_product_list(true);
  58. },
  59. /**
  60. * 获取我的推荐
  61. */
  62. get_host_product: function () {
  63. var that = this;
  64. getProductHot().then(res=>{
  65. that.setData({ host_product: res.data });
  66. });
  67. },
  68. //点击事件处理
  69. set_where: function (e) {
  70. var dataset = e.target.dataset;
  71. switch (dataset.type) {
  72. case '1':
  73. return wx.navigateBack({
  74. delta: 1,
  75. })
  76. break;
  77. case '2':
  78. if (this.data.price == 0)
  79. this.data.price = 1;
  80. else if (this.data.price == 1)
  81. this.data.price = 2;
  82. else if (this.data.price == 2)
  83. this.data.price = 0;
  84. this.setData({ price: this.data.price, stock: 0 });
  85. break;
  86. case '3':
  87. if (this.data.stock == 0)
  88. this.data.stock = 1;
  89. else if (this.data.stock == 1)
  90. this.data.stock = 2;
  91. else if (this.data.stock == 2)
  92. this.data.stock = 0;
  93. this.setData({ stock: this.data.stock, price: 0 });
  94. break;
  95. case '4':
  96. this.setData({ nows: !this.data.nows });
  97. break;
  98. }
  99. this.setData({ loadend: false, ['where.page']: 1 });
  100. this.get_product_list(true);
  101. },
  102. //设置where条件
  103. setWhere: function () {
  104. if (this.data.price == 0)
  105. this.data.where.priceOrder = '';
  106. else if (this.data.price == 1)
  107. this.data.where.priceOrder = 'desc';
  108. else if (this.data.price == 2)
  109. this.data.where.priceOrder = 'asc';
  110. if (this.data.stock == 0)
  111. this.data.where.salesOrder = '';
  112. else if (this.data.stock == 1)
  113. this.data.where.salesOrder = 'desc';
  114. else if (this.data.stock == 2)
  115. this.data.where.salesOrder = 'asc';
  116. this.data.where.news = this.data.nows ? 1 : 0;
  117. this.setData({ where: this.data.where });
  118. },
  119. //查找产品
  120. get_product_list: function (isPage) {
  121. let that = this;
  122. this.setWhere();
  123. if (that.data.loadend) return;
  124. if (that.data.loading) return;
  125. if (isPage === true) that.setData({ productList: [] });
  126. that.setData({ loading: true, loadTitle: '' });
  127. getProductslist(that.data.where).then(res=>{
  128. let list = res.data;
  129. let productList = app.SplitArray(list, that.data.productList);
  130. let loadend = list.length < that.data.where.limit;
  131. that.setData({
  132. loadend: loadend,
  133. loading: false,
  134. loadTitle: loadend ? '已全部加载' : '加载更多',
  135. productList: productList,
  136. ['where.page']: that.data.where.page + 1,
  137. });
  138. }).catch(err=>{
  139. that.setData({ loading: false, loadTitle: '加载更多' });
  140. });
  141. },
  142. /**
  143. * 生命周期函数--监听页面初次渲染完成
  144. */
  145. onReady: function () {
  146. },
  147. /**
  148. * 生命周期函数--监听页面显示
  149. */
  150. onShow: function () {
  151. },
  152. /**
  153. * 生命周期函数--监听页面隐藏
  154. */
  155. onHide: function () {
  156. },
  157. /**
  158. * 生命周期函数--监听页面卸载
  159. */
  160. onUnload: function () {
  161. },
  162. /**
  163. * 页面相关事件处理函数--监听用户下拉动作
  164. */
  165. onPullDownRefresh: function () {
  166. this.setData({['where.page']:1,loadend:false,productList:[]});
  167. this.get_product_list();
  168. wx.stopPullDownRefresh();
  169. },
  170. /**
  171. * 页面上拉触底事件的处理函数
  172. */
  173. onReachBottom: function () {
  174. this.get_product_list();
  175. },
  176. })