index.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. // pages/user/my_comment/index.js
  2. const {
  3. request,
  4. serverName
  5. } = require('../../../utils/services');
  6. const {
  7. defaultImg,
  8. noExhibitionImg
  9. } = require('../../../utils/images');
  10. const {
  11. Toast
  12. } = require('../../../utils/util.js');
  13. Page({
  14. /**
  15. * 页面的初始数据
  16. */
  17. data: {
  18. myComment: [],
  19. loading: false,
  20. currentPage: 1,
  21. full_star_img: "../../../imgs/icon/full.png",
  22. empty_star_img: "../../../imgs/icon/empty.png",
  23. half_star_img: "../../../imgs/icon/half.png",
  24. exhibitionId: "",
  25. sortType: 0,
  26. tapNav: [{
  27. name: '热门',
  28. sortType: 0
  29. }, {
  30. name: '时间',
  31. sortType: 1
  32. }]
  33. },
  34. fix_starImg: function (points) {
  35. let strPoints = String(Number(points).toFixed(2));
  36. let show_Arr = [];
  37. let ten = strPoints.split('.')[0] || 0;
  38. let unit = strPoints.split('.')[1] || 0;
  39. let surPlus = 0;
  40. if (strPoints < 0) {
  41. return
  42. }
  43. if (ten) {
  44. for (let i = 0; i < Number(ten); i++) {
  45. show_Arr.push({
  46. 'img': this.data.full_star_img
  47. })
  48. }
  49. surPlus = 5 - Number(ten);
  50. }
  51. if (unit && surPlus > 0) {
  52. let numUnit = Number(unit.substr(0, 1)) || 0
  53. if (numUnit > 0) {
  54. // let fix_unit = Math.round(numUnit);
  55. // switch (true) {
  56. // case numUnit > 5:
  57. // show_Arr.push({
  58. // 'img': this.data.half_star_img
  59. // })
  60. // break;
  61. // case numUnit <= 5:
  62. // show_Arr.push({
  63. // 'img': this.data.half_star_img
  64. // })
  65. // break;
  66. // default:
  67. // break
  68. // }
  69. show_Arr.push({
  70. 'img': this.data.half_star_img
  71. })
  72. } else if (numUnit == 0) {
  73. show_Arr.push({
  74. 'img': this.data.empty_star_img
  75. })
  76. } else {
  77. return
  78. }
  79. }
  80. if (surPlus > 0) {
  81. for (let i = 0; i < surPlus - 1; i++) {
  82. show_Arr.push({
  83. 'img': this.data.empty_star_img
  84. })
  85. }
  86. }
  87. return show_Arr
  88. },
  89. /**
  90. * 生命周期函数--监听页面加载
  91. */
  92. onLoad: function(options) {
  93. this.setData({
  94. defaultImg,
  95. exhibitionId: options.id,
  96. serverName
  97. })
  98. this.getComment(1)
  99. },
  100. /**
  101. * 生命周期函数--监听页面初次渲染完成
  102. */
  103. onReady: function() {
  104. },
  105. addCommentLike: function(e) {
  106. let {
  107. type,
  108. id,
  109. idx
  110. } = e.currentTarget.dataset;
  111. console.log(idx, type, id)
  112. let {
  113. myComment
  114. } = this.data;
  115. Toast.showToast2('loading');
  116. let loginSessionKey = wx.getStorageSync('token') || "";
  117. // if (loginSessionKey){
  118. request['commentLike']({
  119. loginSessionKey,
  120. commentId: id,
  121. type: Number(type),
  122. }, "post", res => {
  123. if (res.data.code > -1) {
  124. console.log("asdw", myComment)
  125. myComment[idx].hasLike = res.data.data.hasLike
  126. if (res.data.data.hasLike) {
  127. myComment[idx].likeCount += 1;
  128. } else {
  129. myComment[idx].likeCount -= 1;
  130. }
  131. this.setData({
  132. myComment: myComment
  133. })
  134. }
  135. }, err => {
  136. }, complete => {
  137. Toast.hideLoading();
  138. })
  139. // }
  140. },
  141. loadMore: function() {
  142. if (!this.data.lastPage) {
  143. console.log(this.data.currentPage + 1)
  144. this.getComment(this.data.currentPage + 1);
  145. } else {
  146. return;
  147. }
  148. },
  149. onReachBottom: function() {
  150. if (!this.data.loading) {
  151. this.loadMore();
  152. console.log('reach Bottom');
  153. }
  154. },
  155. tapToComment: function() {
  156. let {
  157. exhibitionId
  158. } = this.data;
  159. wx.navigateTo({
  160. url: `../../zl_detail/create_evaluation/index?id=${exhibitionId}`,
  161. success: function(res) {},
  162. fail: function(res) {},
  163. complete: function(res) {},
  164. })
  165. },
  166. getComment: function(page) {
  167. let loginSessionKey = wx.getStorageSync("token") || "";
  168. let {
  169. exhibitionId,
  170. sortType
  171. } = this.data
  172. request["getCommentslist"]({
  173. exhibitionId,
  174. loginSessionKey,
  175. page: page,
  176. sortType
  177. }, "", res => {
  178. if (res.data.code > -1) {
  179. let tempContent = this.data.myComment ?
  180. this.data.myComment :
  181. [];
  182. let {
  183. exhibition,
  184. comments,
  185. score
  186. } = res.data.data
  187. let {
  188. last: lastPage,
  189. totalPages,
  190. content: myComment
  191. } = comments;
  192. if (myComment){
  193. for (let i = 0; i < myComment.length; i++) {
  194. let comments_star = this.fix_starImg(myComment[i].score || '0.0')
  195. myComment[i]['imgObj'] = comments_star
  196. }
  197. }
  198. let score_img = this.fix_starImg(score)
  199. console.log(score_img)
  200. // console.log(res)
  201. this.setData({
  202. currentPage: res.data.data.number + 1,
  203. lastPage,
  204. loading: false,
  205. myComment: tempContent.concat(myComment),
  206. exhibition,
  207. score_img,
  208. score
  209. });
  210. console.log(myComment)
  211. wx.stopPullDownRefresh();
  212. }
  213. }, err => {
  214. },
  215. complete => {
  216. })
  217. },
  218. changeNav: function(e) {
  219. let {
  220. type
  221. } = e.currentTarget.dataset;
  222. this.setData({
  223. sortType: type,
  224. myComment: []
  225. })
  226. this.getComment(1)
  227. // console.log(e)
  228. },
  229. /**
  230. * 生命周期函数--监听页面显示
  231. */
  232. onShow: function() {
  233. },
  234. /**
  235. * 生命周期函数--监听页面隐藏
  236. */
  237. onHide: function() {
  238. },
  239. /**
  240. * 生命周期函数--监听页面卸载
  241. */
  242. onUnload: function() {
  243. },
  244. /**
  245. * 页面相关事件处理函数--监听用户下拉动作
  246. */
  247. onPullDownRefresh: function() {
  248. },
  249. /**
  250. * 页面上拉触底事件的处理函数
  251. */
  252. onReachBottom: function() {
  253. },
  254. /**
  255. * 用户点击右上角分享
  256. */
  257. onShareAppMessage: function() {
  258. }
  259. })