home.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // pages/home/home.js
  2. const { getCategoryAPI, getAllGoods} = require('../../API/appraise')
  3. Page({
  4. /**
  5. * 页面初始数据
  6. */
  7. data: {
  8. //存放分类列表
  9. classifyList: [],
  10. //用于存放页面上划高度
  11. scrollTop: 0,
  12. //存放所有商品
  13. allGoods: [],
  14. page: 1,
  15. pageSize: 6,
  16. flag: true,
  17. isLoading: false
  18. },
  19. userTo() {
  20. wx.switchTab({
  21. url: '/pages/user/user',
  22. })
  23. },
  24. classifyTo() {
  25. wx.switchTab({//tabBar页面跳转要用wx.switchTab
  26. url: '/pages/classify/classify',
  27. })
  28. },
  29. messageTo() {
  30. wx.switchTab({
  31. url: '/pages/message/message',
  32. })
  33. },
  34. searchTo() {
  35. wx.navigateTo({
  36. url: '/pages/search/search',
  37. })
  38. },
  39. //获取点击分类的id
  40. detailClassifyTo({ currentTarget }) {
  41. const id = currentTarget.dataset.num;
  42. const title = currentTarget.dataset.title;
  43. wx.navigateTo({
  44. url: '/pages/classify_detail/classify_detail?classify_id=' + id + '&title=' + title,
  45. })
  46. },
  47. flagChange() {
  48. this.setData({
  49. flag: false
  50. })
  51. },
  52. /**
  53. * 生命周期函数--监听页面加载
  54. */
  55. onLoad(options) {
  56. this.getClassifyList();
  57. this.getGoodsList();
  58. this.setData({
  59. avatarUrl:wx.getStorageSync('avatarUrl'),
  60. nickName: wx.getStorageSync('nickName')
  61. })
  62. },
  63. getClassifyList() {
  64. getCategoryAPI(this.data)
  65. .then((res) => {
  66. const { code, data, mesage } = res.data;
  67. this.setData({
  68. classifyList: data
  69. })
  70. })
  71. },
  72. //获取推荐商品
  73. getGoodsList() {
  74. //加载的loading效果
  75. wx.showLoading({
  76. title: '数据获取中',
  77. })
  78. getAllGoods(this.data).then((res) => {
  79. const { code, data, message } = res.data;
  80. const d = data.data;
  81. this.setData({
  82. allGoods: [...this.data.allGoods, ...d],
  83. isLoading: true
  84. })
  85. })
  86. this.setData({
  87. isLoading: false
  88. })
  89. },
  90. //推荐商品传递数据
  91. detailTo(e) {
  92. console.log(e.currentTarget.dataset);
  93. const id = e.currentTarget.dataset.id;
  94. const title = e.currentTarget.dataset.title;
  95. const price = e.currentTarget.dataset.price;
  96. const comImg = e.currentTarget.dataset.pic;
  97. wx.navigateTo({
  98. url: '/pages/details/details?id=' + id + '&title=' + title + '&price=' + price + '&img=' + comImg,
  99. })
  100. },
  101. /**
  102. * 生命周期函数--监听页面初次渲染完成
  103. */
  104. onReady() {
  105. },
  106. /**
  107. * 生命周期函数--监听页面显示
  108. */
  109. onShow() {
  110. },
  111. /**
  112. * 生命周期函数--监听页面隐藏
  113. */
  114. onHide() {
  115. },
  116. /**
  117. * 生命周期函数--监听页面卸载
  118. */
  119. onUnload() {
  120. },
  121. /**
  122. * 页面相关事件处理函数--监听用户下拉动作
  123. */
  124. onPullDownRefresh() {
  125. },
  126. /**
  127. * 页面上拉触底事件的处理函数
  128. */
  129. onReachBottom() {
  130. if(this.data.isLoading) {
  131. this.setData({
  132. page: this.data.page + 1,
  133. })
  134. this.getGoodsList()
  135. }
  136. },
  137. /**
  138. * 用户点击右上角分享
  139. */
  140. onShareAppMessage() {
  141. },
  142. // /**
  143. // * 监听页面滑动
  144. // */
  145. // onPageScroll(data) {
  146. // // console.log(data.scrollTop);
  147. // this.setData({
  148. // scrollTop: data.scrollTop
  149. // })
  150. // }
  151. })