goods_list.js 5.5 KB

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