index.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. import { fetchComments } from '../../../services/comments/fetchComments';
  2. import { fetchCommentsCount } from '../../../services/comments/fetchCommentsCount';
  3. import dayjs from 'dayjs';
  4. const layoutMap = {
  5. 0: 'vertical',
  6. };
  7. Page({
  8. data: {
  9. pageLoading: false,
  10. commentList: [],
  11. pageNum: 1,
  12. myPageNum: 1,
  13. pageSize: 10,
  14. total: 0,
  15. myTotal: 0,
  16. hasLoaded: false,
  17. layoutText: layoutMap[0],
  18. loadMoreStatus: 0,
  19. myLoadStatus: 0,
  20. spuId: '1060004',
  21. commentLevel: '',
  22. hasImage: '',
  23. commentType: '',
  24. totalCount: 0,
  25. countObj: {
  26. badCount: '0',
  27. commentCount: '0',
  28. goodCount: '0',
  29. middleCount: '0',
  30. hasImageCount: '0',
  31. uidCount: '0',
  32. },
  33. },
  34. onLoad(options) {
  35. this.getCount(options);
  36. this.getComments(options);
  37. },
  38. async getCount(options) {
  39. try {
  40. const result = await fetchCommentsCount(
  41. {
  42. spuId: options.spuId,
  43. },
  44. {
  45. method: 'POST',
  46. },
  47. );
  48. this.setData({
  49. countObj: result,
  50. });
  51. // const { data, code = '' } = result;
  52. // if (code.toUpperCase() === 'SUCCESS') {
  53. // wx.setNavigationBarTitle({
  54. // title: `全部评价(${data.commentCount})`,
  55. // });
  56. // this.setData({
  57. // countObj: data,
  58. // });
  59. // } else {
  60. // wx.showToast({
  61. // title: '查询失败,请稍候重试',
  62. // });
  63. // }
  64. } catch (error) {}
  65. },
  66. generalQueryData(reset) {
  67. const { hasImage, pageNum, pageSize, spuId, commentLevel } = this.data;
  68. const params = {
  69. pageNum: 1,
  70. pageSize: 30,
  71. queryParameter: {
  72. spuId,
  73. },
  74. };
  75. if (
  76. Number(commentLevel) === 3 ||
  77. Number(commentLevel) === 2 ||
  78. Number(commentLevel) === 1
  79. ) {
  80. params.queryParameter.commentLevel = Number(commentLevel);
  81. }
  82. if (hasImage && hasImage === '1') {
  83. params.queryParameter.hasImage = true;
  84. } else {
  85. delete params.queryParameter.hasImage;
  86. }
  87. // 重置请求
  88. if (reset) return params;
  89. return {
  90. ...params,
  91. pageNum: pageNum + 1,
  92. pageSize,
  93. };
  94. },
  95. async init(reset = true) {
  96. const { loadMoreStatus, commentList = [] } = this.data;
  97. const params = this.generalQueryData(reset);
  98. // 在加载中或者无更多数据,直接返回
  99. if (loadMoreStatus !== 0) return;
  100. this.setData({
  101. loadMoreStatus: 1,
  102. });
  103. try {
  104. const data = await fetchComments(params, {
  105. method: 'POST',
  106. });
  107. const code = 'SUCCESS';
  108. if (code.toUpperCase() === 'SUCCESS') {
  109. const { pageList, totalCount = 0 } = data;
  110. pageList.forEach((item) => {
  111. // eslint-disable-next-line no-param-reassign
  112. item.commentTime = dayjs(Number(item.commentTime)).format(
  113. 'YYYY/MM/DD HH:mm',
  114. );
  115. });
  116. if (Number(totalCount) === 0 && reset) {
  117. this.setData({
  118. commentList: [],
  119. hasLoaded: true,
  120. total: totalCount,
  121. loadMoreStatus: 2,
  122. });
  123. return;
  124. }
  125. const _commentList = reset ? pageList : commentList.concat(pageList);
  126. const _loadMoreStatus =
  127. _commentList.length === Number(totalCount) ? 2 : 0;
  128. this.setData({
  129. commentList: _commentList,
  130. pageNum: params.pageNum || 1,
  131. totalCount: Number(totalCount),
  132. loadMoreStatus: _loadMoreStatus,
  133. });
  134. } else {
  135. wx.showToast({
  136. title: '查询失败,请稍候重试',
  137. });
  138. }
  139. } catch (error) {}
  140. this.setData({
  141. hasLoaded: true,
  142. });
  143. },
  144. getScoreArray(score) {
  145. var array = [];
  146. for (let i = 0; i < 5; i++) {
  147. if (i < score) {
  148. array.push(2);
  149. } else {
  150. array.push(0);
  151. }
  152. }
  153. return array;
  154. },
  155. getComments(options) {
  156. const { commentLevel = -1, spuId, hasImage = '' } = options;
  157. if (commentLevel !== -1) {
  158. this.setData({
  159. commentLevel: commentLevel,
  160. });
  161. }
  162. this.setData({
  163. hasImage: hasImage,
  164. commentType: hasImage ? '4' : '',
  165. spuId: spuId,
  166. });
  167. this.init(true);
  168. },
  169. changeTag(e) {
  170. var { commenttype } = e.currentTarget.dataset;
  171. var { commentType } = this.data;
  172. if (commentType === commenttype) return;
  173. this.setData({
  174. loadMoreStatus: 0,
  175. commentList: [],
  176. total: 0,
  177. myTotal: 0,
  178. myPageNum: 1,
  179. pageNum: 1,
  180. });
  181. if (commenttype === '' || commenttype === '5') {
  182. this.setData({
  183. hasImage: '',
  184. commentLevel: '',
  185. });
  186. } else if (commenttype === '4') {
  187. this.setData({
  188. hasImage: '1',
  189. commentLevel: '',
  190. });
  191. } else {
  192. this.setData({
  193. hasImage: '',
  194. commentLevel: commenttype,
  195. });
  196. }
  197. if (commenttype === '5') {
  198. this.setData({
  199. myLoadStatus: 1,
  200. commentType: commenttype,
  201. });
  202. this.getMyCommentsList();
  203. } else {
  204. this.setData({
  205. myLoadStatus: 0,
  206. commentType: commenttype,
  207. });
  208. this.init(true);
  209. }
  210. },
  211. onReachBottom() {
  212. const { total = 0, commentList } = this.data;
  213. if (commentList.length === total) {
  214. this.setData({
  215. loadMoreStatus: 2,
  216. });
  217. return;
  218. }
  219. this.init(false);
  220. },
  221. });