comments.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. // pages/comments/comments.js
  2. const getReq = require('./../../config.js').getReq
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. comments: [],
  9. hasmore: '',
  10. curpage: 1,
  11. common_id: '',
  12. addtimes: [],
  13. show_carousel: false,
  14. carouselImgs: [],
  15. current: 0,
  16. basicStar: './../image/basic_star.png',
  17. activeStar: '../../image/star.png',
  18. halfStar: '../../image/half_star.png',
  19. },
  20. /**
  21. * 生命周期函数--监听页面加载
  22. */
  23. onLoad: function (options) {
  24. this.setData({
  25. common_id: options.common_id
  26. })
  27. this.getDatas(options.common_id, this.data.curpage)
  28. // this.getDatas(2684, this.data.curpage)
  29. },
  30. getDatas(common_id, curpage) {
  31. wx.showLoading({
  32. title: '加载中'
  33. })
  34. var self = this
  35. getReq({
  36. act: 'goods_common',
  37. op: 'comments',
  38. goods_commonid: common_id,
  39. curpage,
  40. page: 10
  41. }, function (res) {
  42. if (res.code == 200) {
  43. wx.hideLoading()
  44. // self.getAddTimes(res.datas.comments)
  45. let comments = self.createComments(res.datas.comments)
  46. console.log(comments);
  47. self.setData({
  48. comments: self.data.comments.concat(comments),
  49. hasmore: res.datas.mobile_page.hasmore
  50. })
  51. }
  52. else {
  53. wx.showToast({
  54. icon: 'none',
  55. title: res.message,
  56. duration: 2000
  57. })
  58. }
  59. })
  60. },
  61. getAddTimes(comments) {
  62. let arr = []
  63. comments.map(item => {
  64. let addtime = item.addtime || ''
  65. if (addtime) {
  66. let date = new Date(addtime * 1000);
  67. let year = date.getFullYear();
  68. let month = date.getMonth() + 1;
  69. let day = date.getDate();
  70. addtime = `${year}-${month}-${day}`
  71. }
  72. arr.push(addtime)
  73. })
  74. this.setData({
  75. addtimes: this.data.addtimes.concat(arr)
  76. })
  77. },
  78. createComments(basicComments) {
  79. let self = this
  80. let comments = []
  81. comments = basicComments.map(item => {
  82. let addtime = item.addtime || ''
  83. if (addtime) {
  84. let date = new Date(addtime * 1000);
  85. let year = date.getFullYear();
  86. let month = date.getMonth() + 1;
  87. let day = date.getDate();
  88. let transform_addtime = `${year}-${month}-${day}`
  89. item['transform_addtime'] = transform_addtime
  90. }
  91. let starSrcs = self.createStars(item.scores)
  92. item['starSrcs'] = starSrcs
  93. return item
  94. })
  95. return comments
  96. },
  97. createStars(rate) {
  98. let starSrcs = []
  99. for(let i = 0; i < 5; i++) {
  100. if(i < Math.floor(rate)) {
  101. starSrcs.push(this.data.activeStar)
  102. continue
  103. }
  104. if(i < Math.round(rate)) {
  105. starSrcs.push(this.data.halfStar)
  106. continue
  107. }
  108. starSrcs.push(this.data.basicStar)
  109. }
  110. return starSrcs
  111. },
  112. showCarousel(e) {
  113. let carouselImgs = e.currentTarget.dataset.images
  114. let current = e.currentTarget.dataset.current
  115. this.setData({
  116. show_carousel: true,
  117. carouselImgs,
  118. current
  119. })
  120. },
  121. hideCarousel() {
  122. this.setData({
  123. show_carousel: false
  124. })
  125. },
  126. /**
  127. * 生命周期函数--监听页面初次渲染完成
  128. */
  129. onReady: function () {
  130. },
  131. /**
  132. * 生命周期函数--监听页面显示
  133. */
  134. onShow: function () {
  135. },
  136. /**
  137. * 生命周期函数--监听页面隐藏
  138. */
  139. onHide: function () {
  140. },
  141. /**
  142. * 生命周期函数--监听页面卸载
  143. */
  144. onUnload: function () {
  145. },
  146. /**
  147. * 页面相关事件处理函数--监听用户下拉动作
  148. */
  149. onPullDownRefresh: function () {
  150. },
  151. /**
  152. * 页面上拉触底事件的处理函数
  153. */
  154. onReachBottom: function () {
  155. if (!this.data.hasmore) {
  156. return
  157. }
  158. this.data.curpage++
  159. this.getDatas(this.data.common_id, this.data.curpage)
  160. }
  161. })