home.js 3.9 KB

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