goods_list.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. userInfo:{}
  35. },
  36. onLoadFun: function (e) {
  37. this.setData({
  38. userInfo: e.detail
  39. })
  40. },
  41. /**
  42. * 生命周期函数--监听页面加载
  43. */
  44. onLoad: function (options) {
  45. this.setData({
  46. ['where.sid']: options.sid || 0,
  47. title: options.title || '',
  48. ['where.keyword']: options.searchValue || '',
  49. navH: app.globalData.navHeight
  50. });
  51. this.get_product_list();
  52. this.get_host_product();
  53. },
  54. /**
  55. * 商品详情跳转
  56. */
  57. goDetail: function (e) {
  58. let item = e.currentTarget.dataset.items;
  59. if (item.activity && item.activity.type === "1") {
  60. wx.navigateTo({
  61. url: `/pages/activity/goods_seckill_details/index?id=${item.activity.id}&time=${item.activity.time}&status=1`
  62. });
  63. } else if (item.activity && item.activity.type === "2") {
  64. wx.navigateTo({ url: `/pages/activity/goods_bargain_details/index?id=${item.activity.id}&bargain=${this.data.userInfo.uid}`});
  65. } else if (item.activity && item.activity.type === "3") {
  66. wx.navigateTo({
  67. url: `/pages/activity/goods_combination_details/index?id=${item.activity.id}`
  68. });
  69. } else {
  70. wx.navigateTo({ url: `/pages/goods_details/index?id=${item.id}` });
  71. }
  72. },
  73. Changswitch:function(){
  74. var that = this;
  75. that.setData({
  76. is_switch: !this.data.is_switch
  77. })
  78. },
  79. searchSubmit: function (e) {
  80. var that = this;
  81. this.setData({ ['where.keyword']: e.detail.value, loadend: false, ['where.page']: 1 })
  82. this.get_product_list(true);
  83. },
  84. /**
  85. * 获取我的推荐
  86. */
  87. get_host_product: function () {
  88. var that = this;
  89. getProductHot().then(res=>{
  90. that.setData({ host_product: res.data });
  91. });
  92. },
  93. //点击事件处理
  94. set_where: function (e) {
  95. var dataset = e.target.dataset;
  96. switch (dataset.type) {
  97. case '1':
  98. return wx.navigateBack({
  99. delta: 1,
  100. })
  101. break;
  102. case '2':
  103. if (this.data.price == 0)
  104. this.data.price = 1;
  105. else if (this.data.price == 1)
  106. this.data.price = 2;
  107. else if (this.data.price == 2)
  108. this.data.price = 0;
  109. this.setData({ price: this.data.price, stock: 0 });
  110. break;
  111. case '3':
  112. if (this.data.stock == 0)
  113. this.data.stock = 1;
  114. else if (this.data.stock == 1)
  115. this.data.stock = 2;
  116. else if (this.data.stock == 2)
  117. this.data.stock = 0;
  118. this.setData({ stock: this.data.stock, price: 0 });
  119. break;
  120. case '4':
  121. this.setData({ nows: !this.data.nows });
  122. break;
  123. }
  124. this.setData({ loadend: false, ['where.page']: 1 });
  125. this.get_product_list(true);
  126. },
  127. //设置where条件
  128. setWhere: function () {
  129. if (this.data.price == 0)
  130. this.data.where.priceOrder = '';
  131. else if (this.data.price == 1)
  132. this.data.where.priceOrder = 'desc';
  133. else if (this.data.price == 2)
  134. this.data.where.priceOrder = 'asc';
  135. if (this.data.stock == 0)
  136. this.data.where.salesOrder = '';
  137. else if (this.data.stock == 1)
  138. this.data.where.salesOrder = 'desc';
  139. else if (this.data.stock == 2)
  140. this.data.where.salesOrder = 'asc';
  141. this.data.where.news = this.data.nows ? 1 : 0;
  142. this.setData({ where: this.data.where });
  143. },
  144. //查找产品
  145. get_product_list: function (isPage) {
  146. let that = this;
  147. this.setWhere();
  148. if (that.data.loadend) return;
  149. if (that.data.loading) return;
  150. if (isPage === true) that.setData({ productList: [] });
  151. that.setData({ loading: true, loadTitle: '' });
  152. getProductslist(that.data.where).then(res=>{
  153. let list = res.data;
  154. let productList = app.SplitArray(list, that.data.productList);
  155. let loadend = list.length < that.data.where.limit;
  156. that.setData({
  157. loadend: loadend,
  158. loading: false,
  159. loadTitle: loadend ? '已全部加载' : '加载更多',
  160. productList: productList,
  161. ['where.page']: that.data.where.page + 1,
  162. });
  163. }).catch(err=>{
  164. that.setData({ loading: false, loadTitle: '加载更多' });
  165. });
  166. },
  167. /**
  168. * 生命周期函数--监听页面初次渲染完成
  169. */
  170. onReady: function () {
  171. },
  172. /**
  173. * 生命周期函数--监听页面显示
  174. */
  175. onShow: function () {
  176. },
  177. /**
  178. * 生命周期函数--监听页面隐藏
  179. */
  180. onHide: function () {
  181. },
  182. /**
  183. * 生命周期函数--监听页面卸载
  184. */
  185. onUnload: function () {
  186. },
  187. /**
  188. * 页面相关事件处理函数--监听用户下拉动作
  189. */
  190. onPullDownRefresh: function () {
  191. this.setData({['where.page']:1,loadend:false,productList:[]});
  192. this.get_product_list();
  193. wx.stopPullDownRefresh();
  194. },
  195. /**
  196. * 页面上拉触底事件的处理函数
  197. */
  198. onReachBottom: function () {
  199. this.get_product_list();
  200. },
  201. })