comments.js 4.0 KB

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