home.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. // pages/home/home.js
  2. const { getCategoryAPI, getAllGoods, getUserInfo, getUserInfoList, getRecommend} = 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: String,
  26. ownerId: [],//发布者ID
  27. ownerMsg: [],
  28. user_id: wx.getStorageSync('uid'),//用户id
  29. recommendNum: 6,//推荐数目
  30. newMsg: [],
  31. },
  32. userTo() {
  33. wx.switchTab({
  34. url: '/pages/user/user',
  35. })
  36. },
  37. classifyTo() {
  38. wx.switchTab({//tabBar页面跳转要用wx.switchTab
  39. url: '/pages/classify/classify',
  40. })
  41. },
  42. messageTo() {
  43. wx.switchTab({
  44. url: '/pages/message/message',
  45. })
  46. },
  47. searchTo() {
  48. wx.navigateTo({
  49. url: '/pages/search/search',
  50. })
  51. },
  52. //获取点击分类的id
  53. detailClassifyTo({ currentTarget }) {
  54. const id = currentTarget.dataset.num;
  55. const title = currentTarget.dataset.title;
  56. wx.navigateTo({
  57. url: '/pages/classify_detail/classify_detail?classify_id=' + id + '&title=' + title,
  58. })
  59. },
  60. /**
  61. * 生命周期函数--监听页面加载
  62. */
  63. onLoad(options) {
  64. this.getClassifyList();
  65. // this.getGoodsList();
  66. this.getRecommendGoods();
  67. this.setData({
  68. avatarUrl:wx.getStorageSync('avatarUrl'),
  69. nickName:wx.getStorageSync('nickName')
  70. })
  71. },
  72. getClassifyList() {
  73. getCategoryAPI(this.data)
  74. .then((res) => {
  75. const { code, data, mesage } = res.data;
  76. this.setData({
  77. classifyList: data
  78. })
  79. })
  80. },
  81. //获取推荐商品
  82. getRecommendGoods() {
  83. var time = formatTime(new Date());
  84. this.setData({
  85. time: time
  86. })
  87. //加载的loading效果
  88. wx.showLoading({
  89. title: '数据获取中',
  90. })
  91. getRecommend(this.data)
  92. .then((res) => {
  93. const { code, data, message } = res.data;
  94. // for (let index = 0; index < data.length; index++) {
  95. // // const ownId = {data: {id: [data[index].ownerId]}};
  96. // // getUserInfoList(ownId).then((res) => {
  97. // // const {code, data, message} = res.data;
  98. // // if(data.users != null) {
  99. // // console.log(data.users[0]);
  100. // // // data[index].avatar = data.users[0].avatar;
  101. // // // data[index].name = data.users[0].name;
  102. // // console.log(data.users[0].name);
  103. // // }
  104. // // }).catch((err) => {
  105. // // console.log(err);
  106. // // })
  107. // }
  108. this.setData({
  109. allGoods: [...this.data.allGoods, ...data],
  110. isLoading: true
  111. })
  112. // console.log(this.data.allGoods);
  113. }).catch((err) => {
  114. console.log(err);
  115. })
  116. this.setData({
  117. isLoading: false
  118. })
  119. },
  120. sendOwnerId() {
  121. },
  122. //推荐商品传递数据
  123. detailTo(e) {
  124. const id = e.currentTarget.dataset.id;
  125. const title = e.currentTarget.dataset.title;
  126. const price = e.currentTarget.dataset.price;
  127. const comImg = e.currentTarget.dataset.pic;
  128. const uid = e.currentTarget.dataset.uid;
  129. // this.setData({
  130. // // uid: parseInt(getOrderOption.data.id)
  131. // })
  132. // const uid = this.data.uid;
  133. // console.log(uid);
  134. wx.navigateTo({
  135. url: '/pages/details/details?id=' + id + '&title=' + title + '&price=' + price + '&img=' + comImg + '&uid=' + uid,
  136. })
  137. },
  138. /**
  139. * 生命周期函数--监听页面初次渲染完成
  140. */
  141. onReady() {
  142. },
  143. /**
  144. * 生命周期函数--监听页面显示
  145. */
  146. onShow() {
  147. },
  148. /**
  149. * 生命周期函数--监听页面隐藏
  150. */
  151. onHide() {
  152. },
  153. /**
  154. * 生命周期函数--监听页面卸载
  155. */
  156. onUnload() {
  157. },
  158. /**
  159. * 页面相关事件处理函数--监听用户下拉动作
  160. */
  161. onPullDownRefresh() {
  162. },
  163. /**
  164. * 页面上拉触底事件的处理函数
  165. */
  166. onReachBottom() {
  167. if(this.data.isLoading) {
  168. this.setData({
  169. recommendNum: this.data.recommendNum + 6
  170. })
  171. this.getRecommendGoods()
  172. }
  173. },
  174. /**
  175. * 用户点击右上角分享
  176. */
  177. onShareAppMessage() {
  178. }
  179. })