瑶都万能墙
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

622 lines
13 KiB

  1. <template>
  2. <view class="comment-detail-container">
  3. <navbar :title="pageTitle" leftClick @leftClick="navigateBack"/>
  4. <!-- 加载状态 -->
  5. <view class="loading-container" v-if="loading">
  6. <view class="loading-spinner"></view>
  7. <text class="loading-text">加载中...</text>
  8. </view>
  9. <!-- 错误状态 -->
  10. <view class="error-container" v-if="!loading && hasError">
  11. <text class="error-text">加载失败请重试</text>
  12. <button class="retry-button" @click="loadCommentDetail">重新加载</button>
  13. </view>
  14. <!-- 主评论内容 -->
  15. <view class="main-comment" v-if="!loading && !hasError && mainComment">
  16. <view class="comment-header">
  17. <image class="avatar" :src="mainComment.userHead || '/static/image/center/default-avatar.png'" mode="aspectFill"></image>
  18. <view class="user-info">
  19. <text class="username">{{mainComment.userName}}</text>
  20. <text class="time">{{formatTime(mainComment.createTime)}}</text>
  21. </view>
  22. </view>
  23. <view class="comment-content">
  24. <text>{{mainComment.userValue}}</text>
  25. </view>
  26. <!-- 评论图片 -->
  27. <view class="comment-images" v-if="mainComment.userImage">
  28. <view class="image-grid">
  29. <image
  30. v-for="(img, index) in mainComment.userImage.split(',')"
  31. :key="index"
  32. :src="img"
  33. mode="aspectFill"
  34. @click="previewImage(mainComment.userImage.split(','), index)"
  35. class="comment-image"
  36. ></image>
  37. </view>
  38. </view>
  39. <view class="comment-footer">
  40. <text class="reply-count">{{totalReplies}}条回复</text>
  41. <view class="reply-btn" @click="showReplyInput(mainComment.id, mainComment.userName)">
  42. <uv-icon name="chat" color="#666" size="32rpx"></uv-icon>
  43. <text>回复</text>
  44. </view>
  45. </view>
  46. </view>
  47. <!-- 回复列表 -->
  48. <view class="replies-container" v-if="!loading && !hasError && commentList.length > 0">
  49. <view class="reply-item" v-for="(item, index) in commentList" :key="index">
  50. <view class="reply-header">
  51. <image class="avatar" :src="item.userHead || '/static/image/center/default-avatar.png'" mode="aspectFill"></image>
  52. <view class="user-info">
  53. <text class="username">{{item.userName}}</text>
  54. <text class="time">{{formatTime(item.createTime)}}</text>
  55. </view>
  56. </view>
  57. <view class="reply-content">
  58. <view v-if="item.replyToUserName" class="reply-to">
  59. <text>回复 </text>
  60. <text class="reply-username">@{{item.replyToUserName}}</text>
  61. <text></text>
  62. </view>
  63. <text>{{item.userValue}}</text>
  64. </view>
  65. <!-- 回复图片 -->
  66. <view class="reply-images" v-if="item.userImage">
  67. <view class="image-grid">
  68. <image
  69. v-for="(img, imgIndex) in item.userImage.split(',')"
  70. :key="imgIndex"
  71. :src="img"
  72. mode="aspectFill"
  73. @click="previewImage(item.userImage.split(','), imgIndex)"
  74. class="reply-image"
  75. ></image>
  76. </view>
  77. </view>
  78. <view class="reply-footer">
  79. <view class="reply-info">
  80. <text v-if="item.replyNum > 0" class="sub-reply-count" @click="navigateToSubComment(item)">
  81. {{item.replyNum}}条回复 >
  82. </text>
  83. </view>
  84. <view class="reply-btn" @click="showReplyInput(item.id, item.userName)">
  85. <uv-icon name="chat" color="#666" size="28rpx"></uv-icon>
  86. <text>回复</text>
  87. </view>
  88. </view>
  89. </view>
  90. </view>
  91. <!-- 加载更多 -->
  92. <view class="load-more" v-if="!loading && !hasError && hasMore">
  93. <text @click="loadMoreReplies">加载更多</text>
  94. </view>
  95. <!-- 回复输入框 -->
  96. <view class="reply-input-container" v-if="showInput">
  97. <view class="input-wrapper">
  98. <input
  99. class="reply-input"
  100. v-model="replyContent"
  101. :placeholder="replyPlaceholder"
  102. focus
  103. confirm-type="send"
  104. @confirm="submitReply"
  105. />
  106. <button class="send-btn" :disabled="!replyContent.trim()" @click="submitReply">发送</button>
  107. </view>
  108. </view>
  109. </view>
  110. </template>
  111. <script>
  112. export default {
  113. data() {
  114. return {
  115. commentId: '', // 当前评论ID
  116. parentId: '', // 父评论ID,用于无限层级
  117. sourceType: '', // 评论来源类型(文章、帖子等)
  118. sourceId: '', // 评论来源ID
  119. pageTitle: '评论详情',
  120. loading: true,
  121. hasError: false,
  122. mainComment: null, // 主评论
  123. commentList: [], // 回复列表
  124. page: 1,
  125. pageSize: 20,
  126. hasMore: false,
  127. totalReplies: 0,
  128. // 回复相关
  129. showInput: false,
  130. replyContent: '',
  131. replyToId: '', // 回复的评论ID
  132. replyToUserName: '', // 回复的用户名
  133. replyPlaceholder: '写回复...'
  134. }
  135. },
  136. onLoad(options) {
  137. // 获取参数
  138. if (options.id) {
  139. this.commentId = options.id;
  140. }
  141. if (options.parentId) {
  142. this.parentId = options.parentId;
  143. }
  144. if (options.sourceType) {
  145. this.sourceType = options.sourceType;
  146. }
  147. if (options.sourceId) {
  148. this.sourceId = options.sourceId;
  149. }
  150. // 加载评论详情
  151. this.loadCommentDetail();
  152. // 加载回复列表
  153. this.loadReplies();
  154. },
  155. methods: {
  156. // 加载评论详情
  157. loadCommentDetail() {
  158. this.loading = true;
  159. this.hasError = false;
  160. // 调用API获取评论详情
  161. this.$api('getCommentDetail', {
  162. id: this.commentId
  163. }, res => {
  164. this.loading = false;
  165. if (res.code === 200) {
  166. this.mainComment = res.result;
  167. // 获取回复总数
  168. this.totalReplies = res.result.replyNum || 0;
  169. } else {
  170. this.hasError = true;
  171. uni.showToast({
  172. title: res.message || '加载失败',
  173. icon: 'none'
  174. });
  175. }
  176. });
  177. },
  178. // 加载回复列表
  179. loadReplies() {
  180. // 使用现有的评论列表接口,传入pid参数
  181. this.$api('getCommentPage', {
  182. pid: this.commentId,
  183. page: this.page,
  184. pageSize: this.pageSize
  185. }, res => {
  186. if (res.code === 200) {
  187. if (this.page === 1) {
  188. this.commentList = res.result.records || [];
  189. } else {
  190. this.commentList = [...this.commentList, ...(res.result.records || [])];
  191. }
  192. // 判断是否有更多数据
  193. this.hasMore = this.commentList.length < res.result.total;
  194. } else {
  195. uni.showToast({
  196. title: res.message || '加载回复失败',
  197. icon: 'none'
  198. });
  199. }
  200. });
  201. },
  202. // 加载更多回复
  203. loadMoreReplies() {
  204. this.page++;
  205. this.loadReplies();
  206. },
  207. // 显示回复输入框
  208. showReplyInput(commentId, userName = '') {
  209. this.showInput = true;
  210. this.replyToId = commentId;
  211. this.replyToUserName = userName;
  212. this.replyPlaceholder = userName ? `回复 @${userName}` : '写回复...';
  213. this.replyContent = '';
  214. },
  215. // 提交回复
  216. submitReply() {
  217. if (!this.replyContent.trim()) return;
  218. // 调用API提交回复
  219. this.$api('addComment', {
  220. userValue: this.replyContent,
  221. pid: this.commentId,
  222. replyToId: this.replyToId,
  223. replyToUserName: this.replyToUserName,
  224. type: this.sourceType,
  225. orderId: this.sourceId
  226. }, res => {
  227. if (res.code === 200) {
  228. // 清空输入框
  229. this.replyContent = '';
  230. this.showInput = false;
  231. // 重新加载回复列表
  232. this.page = 1;
  233. this.loadReplies();
  234. // 更新回复总数
  235. this.totalReplies++;
  236. uni.showToast({
  237. title: '回复成功',
  238. icon: 'success'
  239. });
  240. } else {
  241. uni.showToast({
  242. title: res.message || '回复失败',
  243. icon: 'none'
  244. });
  245. }
  246. });
  247. },
  248. // 导航到子评论详情
  249. navigateToSubComment(comment) {
  250. if (comment.replyNum > 0) {
  251. uni.navigateTo({
  252. url: `/pages_order/comment/commentDetail?id=${comment.id}&parentId=${this.commentId}&sourceType=${this.sourceType}&sourceId=${this.sourceId}`
  253. });
  254. }
  255. },
  256. // 返回上一页
  257. navigateBack() {
  258. uni.navigateBack();
  259. },
  260. // 预览图片
  261. previewImage(images, index) {
  262. uni.previewImage({
  263. urls: images,
  264. current: index
  265. });
  266. },
  267. // 格式化时间
  268. formatTime(timestamp) {
  269. if (!timestamp) return '';
  270. const now = new Date().getTime();
  271. const diff = now - timestamp;
  272. // 小于1分钟
  273. if (diff < 60000) {
  274. return '刚刚';
  275. }
  276. // 小于1小时
  277. if (diff < 3600000) {
  278. return Math.floor(diff / 60000) + '分钟前';
  279. }
  280. // 小于24小时
  281. if (diff < 86400000) {
  282. return Math.floor(diff / 3600000) + '小时前';
  283. }
  284. // 小于30天
  285. if (diff < 2592000000) {
  286. return Math.floor(diff / 86400000) + '天前';
  287. }
  288. // 大于30天显示具体日期
  289. const date = new Date(timestamp);
  290. return `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`;
  291. }
  292. }
  293. }
  294. </script>
  295. <style lang="scss" scoped>
  296. .comment-detail-container {
  297. display: flex;
  298. flex-direction: column;
  299. min-height: 100vh;
  300. background-color: #f5f5f5;
  301. padding-bottom: 100rpx;
  302. }
  303. .loading-container {
  304. display: flex;
  305. flex-direction: column;
  306. justify-content: center;
  307. align-items: center;
  308. margin-top: 200rpx;
  309. .loading-spinner {
  310. width: 80rpx;
  311. height: 80rpx;
  312. border: 6rpx solid #f3f3f3;
  313. border-top: 6rpx solid $uni-color-primary;
  314. border-radius: 50%;
  315. animation: spin 1s linear infinite;
  316. margin-bottom: 20rpx;
  317. }
  318. .loading-text {
  319. font-size: 28rpx;
  320. color: #666;
  321. }
  322. @keyframes spin {
  323. 0% { transform: rotate(0deg); }
  324. 100% { transform: rotate(360deg); }
  325. }
  326. }
  327. .error-container {
  328. display: flex;
  329. flex-direction: column;
  330. align-items: center;
  331. margin-top: 200rpx;
  332. .error-text {
  333. font-size: 28rpx;
  334. color: #999;
  335. margin-bottom: 30rpx;
  336. }
  337. .retry-button {
  338. background-color: $uni-color-primary;
  339. color: #fff;
  340. font-size: 28rpx;
  341. padding: 16rpx 40rpx;
  342. border-radius: 40rpx;
  343. border: none;
  344. }
  345. }
  346. .main-comment {
  347. background-color: #fff;
  348. padding: 30rpx;
  349. margin-bottom: 20rpx;
  350. .comment-header {
  351. display: flex;
  352. align-items: center;
  353. margin-bottom: 20rpx;
  354. .avatar {
  355. width: 80rpx;
  356. height: 80rpx;
  357. border-radius: 50%;
  358. margin-right: 20rpx;
  359. background-color: #f0f0f0;
  360. }
  361. .user-info {
  362. flex: 1;
  363. .username {
  364. font-size: 28rpx;
  365. font-weight: bold;
  366. color: #333;
  367. margin-bottom: 6rpx;
  368. }
  369. .time {
  370. font-size: 24rpx;
  371. color: #999;
  372. }
  373. }
  374. }
  375. .comment-content {
  376. font-size: 30rpx;
  377. color: #333;
  378. line-height: 1.6;
  379. margin-bottom: 20rpx;
  380. }
  381. .comment-images {
  382. margin-bottom: 20rpx;
  383. .image-grid {
  384. display: flex;
  385. flex-wrap: wrap;
  386. .comment-image {
  387. width: 200rpx;
  388. height: 200rpx;
  389. margin-right: 10rpx;
  390. margin-bottom: 10rpx;
  391. border-radius: 8rpx;
  392. background-color: #f0f0f0;
  393. }
  394. }
  395. }
  396. .comment-footer {
  397. display: flex;
  398. justify-content: space-between;
  399. align-items: center;
  400. padding-top: 20rpx;
  401. border-top: 1px solid #f0f0f0;
  402. .reply-count {
  403. font-size: 26rpx;
  404. color: #666;
  405. }
  406. .reply-btn {
  407. display: flex;
  408. align-items: center;
  409. text {
  410. font-size: 26rpx;
  411. color: #666;
  412. margin-left: 6rpx;
  413. }
  414. }
  415. }
  416. }
  417. .replies-container {
  418. background-color: #fff;
  419. .reply-item {
  420. padding: 30rpx;
  421. border-bottom: 1px solid #f0f0f0;
  422. &:active {
  423. background-color: #f9f9f9;
  424. }
  425. .reply-header {
  426. display: flex;
  427. align-items: center;
  428. margin-bottom: 16rpx;
  429. .avatar {
  430. width: 70rpx;
  431. height: 70rpx;
  432. border-radius: 50%;
  433. margin-right: 16rpx;
  434. background-color: #f0f0f0;
  435. }
  436. .user-info {
  437. flex: 1;
  438. .username {
  439. font-size: 26rpx;
  440. font-weight: bold;
  441. color: #333;
  442. margin-bottom: 4rpx;
  443. }
  444. .time {
  445. font-size: 22rpx;
  446. color: #999;
  447. }
  448. }
  449. }
  450. .reply-content {
  451. font-size: 28rpx;
  452. color: #333;
  453. line-height: 1.5;
  454. margin-bottom: 16rpx;
  455. padding-left: 86rpx;
  456. .reply-to {
  457. display: inline;
  458. .reply-username {
  459. color: $uni-color-primary;
  460. }
  461. }
  462. }
  463. .reply-images {
  464. padding-left: 86rpx;
  465. margin-bottom: 16rpx;
  466. .image-grid {
  467. display: flex;
  468. flex-wrap: wrap;
  469. .reply-image {
  470. width: 150rpx;
  471. height: 150rpx;
  472. margin-right: 10rpx;
  473. margin-bottom: 10rpx;
  474. border-radius: 8rpx;
  475. background-color: #f0f0f0;
  476. }
  477. }
  478. }
  479. .reply-footer {
  480. display: flex;
  481. justify-content: space-between;
  482. align-items: center;
  483. padding-left: 86rpx;
  484. .reply-info {
  485. .sub-reply-count {
  486. font-size: 24rpx;
  487. color: $uni-color-primary;
  488. }
  489. }
  490. .reply-btn {
  491. display: flex;
  492. align-items: center;
  493. text {
  494. font-size: 24rpx;
  495. color: #666;
  496. margin-left: 4rpx;
  497. }
  498. }
  499. }
  500. }
  501. }
  502. .load-more {
  503. text-align: center;
  504. padding: 30rpx 0;
  505. text {
  506. font-size: 26rpx;
  507. color: #666;
  508. }
  509. }
  510. .reply-input-container {
  511. position: fixed;
  512. bottom: 0;
  513. left: 0;
  514. right: 0;
  515. background-color: #fff;
  516. padding: 20rpx 30rpx;
  517. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.05);
  518. z-index: 100;
  519. padding-bottom: env(safe-area-inset-bottom);
  520. .input-wrapper {
  521. display: flex;
  522. align-items: center;
  523. .reply-input {
  524. flex: 1;
  525. height: 70rpx;
  526. background-color: #f5f5f5;
  527. border-radius: 35rpx;
  528. padding: 0 30rpx;
  529. font-size: 28rpx;
  530. }
  531. .send-btn {
  532. margin-left: 20rpx;
  533. background-color: $uni-color-primary;
  534. color: #fff;
  535. font-size: 28rpx;
  536. height: 70rpx;
  537. line-height: 70rpx;
  538. padding: 0 30rpx;
  539. border-radius: 35rpx;
  540. &[disabled] {
  541. background-color: #cccccc;
  542. }
  543. }
  544. }
  545. }
  546. </style>