home.js 6.0 KB

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