index.js 6.9 KB

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