goods_list.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import {
  2. getProductslist,
  3. getProductHot
  4. } from '../../api/store.js';
  5. const app = getApp();
  6. Page({
  7. /**
  8. * 页面的初始数据
  9. */
  10. data: {
  11. productList: [],
  12. parameter: {
  13. 'navbar': '1',
  14. 'return': '1',
  15. 'title': '商品列表',
  16. 'color': true,
  17. 'class': '0'
  18. },
  19. navH: "",
  20. is_switch: true,
  21. where: {
  22. sid: 0,
  23. keyword: '',
  24. priceOrder: '',
  25. salesOrder: '',
  26. news: 0,
  27. page: 1,
  28. limit: 10,
  29. cid: 0,
  30. },
  31. price: 0,
  32. stock: 0,
  33. nows: false,
  34. loadend: false,
  35. loading: false,
  36. loadTitle: '加载更多',
  37. userInfo: {}
  38. },
  39. onLoadFun: function (e) {
  40. this.setData({
  41. userInfo: e.detail
  42. })
  43. },
  44. /**
  45. * 生命周期函数--监听页面加载
  46. */
  47. onLoad: function (options) {
  48. this.setData({
  49. ['where.sid']: options.sid || 0,
  50. title: options.title || '',
  51. ['where.keyword']: options.searchValue || '',
  52. navH: app.globalData.navHeight
  53. });
  54. this.get_product_list();
  55. this.get_host_product();
  56. },
  57. /**
  58. * 商品详情跳转
  59. */
  60. goDetail: function (e) {
  61. let item = e.currentTarget.dataset.items;
  62. if (item.activity && item.activity.type === "1") {
  63. wx.navigateTo({
  64. url: `/pages/activity/goods_seckill_details/index?id=${item.activity.id}&time=${item.activity.time}&status=1`
  65. });
  66. } else if (item.activity && item.activity.type === "2") {
  67. wx.navigateTo({
  68. url: `/pages/activity/goods_bargain_details/index?id=${item.activity.id}&bargain=${this.data.userInfo.uid}`
  69. });
  70. } else if (item.activity && item.activity.type === "3") {
  71. wx.navigateTo({
  72. url: `/pages/activity/goods_combination_details/index?id=${item.activity.id}`
  73. });
  74. } else {
  75. wx.navigateTo({
  76. url: `/pages/goods_details/index?id=${item.id}`
  77. });
  78. }
  79. },
  80. Changswitch: function () {
  81. var that = this;
  82. that.setData({
  83. is_switch: !this.data.is_switch
  84. })
  85. },
  86. searchSubmit: function (e) {
  87. var that = this;
  88. this.setData({
  89. ['where.keyword']: e.detail.value,
  90. loadend: false,
  91. ['where.page']: 1
  92. })
  93. this.get_product_list(true);
  94. },
  95. /**
  96. * 获取我的推荐
  97. */
  98. get_host_product: function () {
  99. var that = this;
  100. getProductHot().then(res => {
  101. that.setData({
  102. host_product: res.data
  103. });
  104. });
  105. },
  106. //点击事件处理
  107. set_where: function (e) {
  108. var dataset = e.target.dataset;
  109. switch (dataset.type) {
  110. case '1':
  111. return wx.navigateBack({
  112. delta: 1,
  113. })
  114. break;
  115. case '2':
  116. if (this.data.price == 0)
  117. this.data.price = 1;
  118. else if (this.data.price == 1)
  119. this.data.price = 2;
  120. else if (this.data.price == 2)
  121. this.data.price = 0;
  122. this.setData({
  123. price: this.data.price,
  124. stock: 0
  125. });
  126. break;
  127. case '3':
  128. if (this.data.stock == 0)
  129. this.data.stock = 1;
  130. else if (this.data.stock == 1)
  131. this.data.stock = 2;
  132. else if (this.data.stock == 2)
  133. this.data.stock = 0;
  134. this.setData({
  135. stock: this.data.stock,
  136. price: 0
  137. });
  138. break;
  139. case '4':
  140. this.setData({
  141. nows: !this.data.nows
  142. });
  143. break;
  144. }
  145. this.setData({
  146. loadend: false,
  147. ['where.page']: 1
  148. });
  149. this.get_product_list(true);
  150. },
  151. //设置where条件
  152. setWhere: function () {
  153. if (this.data.price == 0)
  154. this.data.where.priceOrder = '';
  155. else if (this.data.price == 1)
  156. this.data.where.priceOrder = 'desc';
  157. else if (this.data.price == 2)
  158. this.data.where.priceOrder = 'asc';
  159. if (this.data.stock == 0)
  160. this.data.where.salesOrder = '';
  161. else if (this.data.stock == 1)
  162. this.data.where.salesOrder = 'desc';
  163. else if (this.data.stock == 2)
  164. this.data.where.salesOrder = 'asc';
  165. this.data.where.news = this.data.nows ? 1 : 0;
  166. this.setData({
  167. where: this.data.where
  168. });
  169. },
  170. //查找产品
  171. get_product_list: function (isPage) {
  172. let that = this;
  173. this.setWhere();
  174. if (that.data.loadend) return;
  175. if (that.data.loading) return;
  176. if (isPage === true) that.setData({
  177. productList: []
  178. });
  179. that.setData({
  180. loading: true,
  181. loadTitle: ''
  182. });
  183. getProductslist(that.data.where).then(res => {
  184. let list = res.data;
  185. let productList = app.SplitArray(list, that.data.productList);
  186. let loadend = list.length < that.data.where.limit;
  187. that.setData({
  188. loadend: loadend,
  189. loading: false,
  190. loadTitle: loadend ? '已全部加载' : '加载更多',
  191. productList: productList,
  192. ['where.page']: that.data.where.page + 1,
  193. });
  194. }).catch(err => {
  195. that.setData({
  196. loading: false,
  197. loadTitle: '加载更多'
  198. });
  199. });
  200. },
  201. /**
  202. * 生命周期函数--监听页面初次渲染完成
  203. */
  204. onReady: function () {
  205. },
  206. /**
  207. * 生命周期函数--监听页面显示
  208. */
  209. onShow: function () {
  210. },
  211. /**
  212. * 生命周期函数--监听页面隐藏
  213. */
  214. onHide: function () {
  215. },
  216. /**
  217. * 生命周期函数--监听页面卸载
  218. */
  219. onUnload: function () {
  220. },
  221. /**
  222. * 页面相关事件处理函数--监听用户下拉动作
  223. */
  224. onPullDownRefresh: function () {
  225. this.setData({
  226. ['where.page']: 1,
  227. loadend: false,
  228. productList: []
  229. });
  230. this.get_product_list();
  231. wx.stopPullDownRefresh();
  232. },
  233. /**
  234. * 页面上拉触底事件的处理函数
  235. */
  236. onReachBottom: function () {
  237. this.get_product_list();
  238. },
  239. })