goods.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const pages = require('../../mixin/pages')
  2. const { getProductCategoryList } = require('../../api/common')
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. ...pages.data(),
  9. listUrl: '/api/user/caigou/product/list',
  10. searchForm: {
  11. key_words: '',
  12. category_id: ''
  13. },
  14. categoryList: [],
  15. navScrollLeft: -1,
  16. leftList: [],
  17. rightList: []
  18. },
  19. ...pages.methods,
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. async onLoad(options) {
  24. await this.fetchProductCategoryList()
  25. this.fetchOrderList()
  26. },
  27. /**
  28. * 生命周期函数--监听页面初次渲染完成
  29. */
  30. onReady() {
  31. },
  32. /**
  33. * 生命周期函数--监听页面显示
  34. */
  35. onShow() {
  36. },
  37. /**
  38. * 生命周期函数--监听页面隐藏
  39. */
  40. onHide() {
  41. },
  42. /**
  43. * 生命周期函数--监听页面卸载
  44. */
  45. onUnload() {
  46. },
  47. /**
  48. * 页面相关事件处理函数--监听用户下拉动作
  49. */
  50. onPullDownRefresh() {
  51. if (this.data.freshing) {
  52. return
  53. }
  54. this.setData({
  55. freshing: true
  56. })
  57. this.bindCallBack()
  58. },
  59. /**
  60. * 页面上拉触底事件的处理函数
  61. */
  62. onReachBottom() {
  63. this.fetchOrderList()
  64. },
  65. bindCallBack() {
  66. this.refreshOrderList()
  67. },
  68. async fetchProductCategoryList() {
  69. try {
  70. const { status, data, msg } = await getProductCategoryList()
  71. if (status && Array.isArray(data) && data.length > 0) {
  72. const temp = data.map(item => {
  73. return {
  74. ...item,
  75. text: item.category_name
  76. }
  77. })
  78. temp.unshift({
  79. category_img_url: 'https://lanman-shining.oss-cn-hangzhou.aliyuncs.com/images/user/bashi635cd28a12481.png',
  80. category_name: '全部',
  81. id: 0
  82. })
  83. this.setData({
  84. categoryList: temp,
  85. 'searchForm.category_id': temp[0].id
  86. })
  87. } else {
  88. wx.showToast({
  89. title: msg,
  90. icon: 'none'
  91. })
  92. }
  93. } catch (err) {}
  94. },
  95. handleNav(e) {
  96. const { item } = e.currentTarget.dataset
  97. if (this.data.searchForm.category_id !== item.id) {
  98. this.setData({
  99. 'searchForm.category_id': item.id,
  100. navScrollLeft: 0
  101. }, () => {
  102. this.refreshOrderList()
  103. })
  104. }
  105. },
  106. async isLeft(list) {
  107. const {
  108. leftList,
  109. rightList
  110. } = this.data
  111. list.forEach((item, index) => {
  112. if (index % 2 === 0) {
  113. leftList.push(item)
  114. } else {
  115. rightList.push(item)
  116. }
  117. })
  118. this.setData({
  119. leftList,
  120. rightList
  121. })
  122. }
  123. })