list.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <template>
  2. <view class="review-list-page client-theme client-subpage">
  3. <view class="client-subpage-hero review-list-hero">
  4. <text class="client-subpage-hero__title">商品评价</text>
  5. <text class="client-subpage-hero__sub">{{ product?.name || '查看买家真实反馈' }}</text>
  6. </view>
  7. <scroll-view
  8. scroll-y
  9. class="client-subpage-scroll review-list-scroll"
  10. :show-scrollbar="true"
  11. @scrolltolower="loadMore"
  12. >
  13. <view class="client-subpage-body review-list-body">
  14. <ClientSkeleton v-if="initialLoading" variant="list" :rows="3" />
  15. <template v-else>
  16. <!-- 商品信息卡片 -->
  17. <view class="product-card">
  18. <ClientImageLoader class="product-image" :src="product?.imageUrl || IMG_DEFAULT_PRODUCT" mode="aspectFill" radius="8rpx" />
  19. <view class="product-info">
  20. <text class="product-name">{{ product?.name || '' }}</text>
  21. <text class="product-price">¥{{ formatPrice(product?.price || 0) }}</text>
  22. </view>
  23. </view>
  24. <!-- 评价统计 -->
  25. <view class="review-stats">
  26. <view class="avg-rating">
  27. <text class="score">{{ reviewStats?.avgRating?.toFixed(1) || '0.0' }}</text>
  28. <text class="label">分</text>
  29. </view>
  30. <view class="rating-bars">
  31. <view v-for="i in 5" :key="i" class="rating-bar-item">
  32. <text class="star-num">{{ 6 - i }}星</text>
  33. <view class="bar-wrapper">
  34. <view class="bar" :style="{ width: getBarWidth(6 - i) }"></view>
  35. </view>
  36. <text class="count">{{ reviewStats?.ratingDistribution?.[String(6 - i)] || 0 }}</text>
  37. </view>
  38. </view>
  39. </view>
  40. <!-- 筛选标签 -->
  41. <view class="filter-tabs">
  42. <view
  43. v-for="tab in tabs"
  44. :key="tab.key"
  45. class="tab-item"
  46. :class="{ active: currentTab === tab.key }"
  47. @click="switchTab(tab.key)"
  48. >
  49. <text>{{ tab.label }}</text>
  50. <text v-if="tab.count !== undefined" class="tab-count">{{ tab.count }}</text>
  51. </view>
  52. </view>
  53. <!-- 评价列表 -->
  54. <view v-for="review in reviewList" :key="review.id" class="review-item" hover-class="review-item--pressed" :hover-stay-time="100">
  55. <!-- 用户信息 -->
  56. <view class="review-header">
  57. <view class="user-info">
  58. <view class="avatar">{{ getAvatarText(review.userName) }}</view>
  59. <view class="user-detail">
  60. <text class="username">{{ review.isAnonymous ? '匿名用户' : review.userName }}</text>
  61. <van-rate :value="review.rating" readonly size="12" />
  62. </view>
  63. </view>
  64. <text class="review-time">{{ formatTime(review.createAt) }}</text>
  65. </view>
  66. <!-- 评价内容 -->
  67. <view class="review-content">
  68. <text v-if="review.ratingTags" class="tags">{{ formatTags(review.ratingTags) }}</text>
  69. <text class="content-text">{{ review.content }}</text>
  70. </view>
  71. <!-- 媒体 -->
  72. <view v-if="review.mediaList?.length" class="review-media">
  73. <view
  74. v-for="media in review.mediaList"
  75. :key="media.id"
  76. class="media-item"
  77. @click="previewImage(media.fileUrl)"
  78. >
  79. <ClientImageLoader v-if="media.mediaType === 'image'" :src="media.fileUrl" mode="aspectFill" class="media-image" />
  80. <video v-else :src="media.fileUrl" controls />
  81. </view>
  82. </view>
  83. <!-- 商家回复 -->
  84. <view v-if="review.replyContent" class="seller-reply">
  85. <text class="reply-label">商家回复:</text>
  86. <text class="reply-text">{{ review.replyContent }}</text>
  87. <text v-if="review.replyTime" class="reply-time">{{ formatTime(review.replyTime) }}</text>
  88. </view>
  89. <!-- 点赞 -->
  90. <view class="review-actions">
  91. <view class="like-btn" @click="handleLike(review)">
  92. <text class="icon">👍</text>
  93. <text class="count">{{ review.likeCount || 0 }}</text>
  94. </view>
  95. </view>
  96. </view>
  97. <!-- 加载更多 -->
  98. <view v-if="loading" class="loading-more">
  99. <van-loading size="20px">加载中...</van-loading>
  100. </view>
  101. <view v-else-if="noMore" class="no-more">
  102. <text>没有更多了</text>
  103. </view>
  104. <!-- 空状态 -->
  105. <view v-if="!loading && reviewList.length === 0" class="empty-state">
  106. <image class="empty-image" :src="IMG_EMPTY" mode="aspectFit" />
  107. <text class="empty-text">暂无评价</text>
  108. </view>
  109. </template>
  110. </view>
  111. </scroll-view>
  112. </view>
  113. </template>
  114. <script setup>
  115. import { ref, computed, onMounted } from 'vue'
  116. import { onLoad, onShow } from '@dcloudio/uni-app'
  117. import { useUserStore } from '@/stores/user'
  118. import { fetchProductReviews, fetchLikeReview, fetchReviewStatistics } from '@/api/reviewApi'
  119. import { formatFenAsYuan } from '@/utils/format'
  120. import { IMG_DEFAULT_PRODUCT, IMG_EMPTY } from '@/utils/staticAsset'
  121. import ClientSkeleton from '@/components/client/client-skeleton.vue'
  122. import ClientImageLoader from '@/components/client/client-image-loader.vue'
  123. const userStore = useUserStore()
  124. // 商品ID
  125. let productId = ''
  126. // 商品信息
  127. const product = ref(null)
  128. // 评价统计
  129. const reviewStats = ref(null)
  130. // 评价列表
  131. const reviewList = ref([])
  132. const loading = ref(false)
  133. const initialLoading = ref(true)
  134. const page = ref(1)
  135. const pageSize = 10
  136. const noMore = ref(false)
  137. // 筛选标签
  138. const currentTab = ref('all')
  139. const tabs = computed(() => [
  140. { key: 'all', label: '全部', count: reviewStats.value?.totalCount },
  141. { key: 'hasMedia', label: '有图', count: undefined }
  142. ])
  143. // 格式化价格
  144. const formatPrice = (fen) => formatFenAsYuan(fen)
  145. // 格式化时间
  146. const formatTime = (time) => {
  147. if (!time) return ''
  148. return time.slice(0, 10)
  149. }
  150. // 格式化标签
  151. const formatTags = (tags) => {
  152. return tags.split(',').filter(Boolean).map((t) => `#${t.trim()}`).join(' ')
  153. }
  154. // 获取头像文字
  155. const getAvatarText = (name) => {
  156. if (!name) return '?'
  157. return name.slice(-2)
  158. }
  159. // 获取评分条宽度
  160. const getBarWidth = (star) => {
  161. const total = reviewStats.value?.totalCount || 1
  162. const count = reviewStats.value?.ratingDistribution?.[String(star)] || 0
  163. return `${(count / total) * 100}%`
  164. }
  165. // 切换标签
  166. const switchTab = async (key) => {
  167. currentTab.value = key
  168. page.value = 1
  169. noMore.value = false
  170. reviewList.value = []
  171. await loadReviews()
  172. }
  173. // 加载评价
  174. const loadReviews = async () => {
  175. if (loading.value || noMore.value) return
  176. loading.value = true
  177. try {
  178. const res = await fetchProductReviews(productId, {
  179. page: page.value,
  180. pageSize,
  181. hasMedia: currentTab.value === 'hasMedia' ? 1 : undefined
  182. })
  183. const list = (res && res.list) || []
  184. if (page.value === 1) {
  185. reviewList.value = list
  186. } else {
  187. reviewList.value = [...reviewList.value, ...list]
  188. }
  189. noMore.value = list.length < pageSize
  190. page.value++
  191. } catch (e) {
  192. console.error('加载评价失败', e)
  193. } finally {
  194. loading.value = false
  195. }
  196. }
  197. // 加载更多
  198. const loadMore = () => {
  199. if (!noMore.value) {
  200. loadReviews()
  201. }
  202. }
  203. // 点赞
  204. const handleLike = async (review) => {
  205. try {
  206. const res = await fetchLikeReview(review.id)
  207. if (res && typeof res.likeCount === 'number') review.likeCount = res.likeCount
  208. } catch (e) {
  209. console.error('点赞失败', e)
  210. }
  211. }
  212. // 预览图片
  213. const previewImage = (url) => {
  214. const images = reviewList.value
  215. .flatMap((r) => r.mediaList || [])
  216. .filter((m) => m.mediaType === 'image')
  217. .map((m) => m.fileUrl)
  218. uni.previewImage({
  219. current: url,
  220. urls: images
  221. })
  222. }
  223. // 加载评价统计
  224. const loadStatistics = async () => {
  225. try {
  226. reviewStats.value = await fetchReviewStatistics(productId)
  227. } catch (e) {
  228. console.error('加载评价统计失败', e)
  229. }
  230. }
  231. const refreshReviewData = async () => {
  232. page.value = 1
  233. noMore.value = false
  234. reviewList.value = []
  235. await Promise.all([loadReviews(), loadStatistics()])
  236. }
  237. onLoad((options) => {
  238. if (options.productId) {
  239. productId = options.productId
  240. }
  241. if (options.product) {
  242. try {
  243. product.value = JSON.parse(decodeURIComponent(options.product))
  244. } catch (e) {
  245. console.error('解析商品信息失败', e)
  246. }
  247. }
  248. })
  249. onMounted(async () => {
  250. userStore.checkLoginStatus()
  251. if (!userStore.isLoggedIn) {
  252. uni.reLaunch({ url: '/pages/login/login' })
  253. return
  254. }
  255. if (!userStore.isClient) {
  256. uni.showToast({ title: '请使用客户账号登录', icon: 'none' })
  257. setTimeout(() => userStore.navigateToHome(), 1200)
  258. return
  259. }
  260. try {
  261. await refreshReviewData()
  262. } finally {
  263. initialLoading.value = false
  264. }
  265. })
  266. onShow(() => {
  267. userStore.checkLoginStatus()
  268. if (!userStore.isLoggedIn || !userStore.isClient) return
  269. void refreshReviewData()
  270. })
  271. </script>
  272. <style lang="scss" scoped>
  273. .review-list-page {
  274. min-height: 100vh;
  275. background: var(--c-bg);
  276. box-sizing: border-box;
  277. }
  278. .review-list-scroll {
  279. flex: 1;
  280. min-height: 0;
  281. height: 0;
  282. }
  283. .review-item--pressed {
  284. opacity: 0.94;
  285. }
  286. .product-card {
  287. display: flex;
  288. padding: 20rpx;
  289. background: var(--c-card);
  290. border: 1rpx solid var(--c-border);
  291. border-radius: var(--c-radius-card);
  292. margin: 0 0 20rpx;
  293. box-sizing: border-box;
  294. .product-image {
  295. width: 120rpx;
  296. height: 120rpx;
  297. border-radius: 8rpx;
  298. background: #f5f7fa;
  299. }
  300. .product-info {
  301. flex: 1;
  302. margin-left: 20rpx;
  303. display: flex;
  304. flex-direction: column;
  305. justify-content: center;
  306. .product-name {
  307. font-size: 28rpx;
  308. color: var(--c-text);
  309. margin-bottom: 10rpx;
  310. }
  311. .product-price {
  312. font-size: 32rpx;
  313. color: var(--c-danger);
  314. font-weight: bold;
  315. }
  316. }
  317. }
  318. .review-stats {
  319. display: flex;
  320. padding: 30rpx 20rpx;
  321. background: var(--c-card);
  322. border: 1rpx solid var(--c-border);
  323. border-radius: var(--c-radius-card);
  324. margin: 0 0 20rpx;
  325. box-sizing: border-box;
  326. .avg-rating {
  327. display: flex;
  328. align-items: baseline;
  329. padding-right: 30rpx;
  330. border-right: 1px solid #ebeef5;
  331. .score {
  332. font-size: 60rpx;
  333. color: var(--c-barley-yellow);
  334. font-weight: bold;
  335. }
  336. .label {
  337. font-size: 24rpx;
  338. color: #909399;
  339. margin-left: 8rpx;
  340. }
  341. }
  342. .rating-bars {
  343. flex: 1;
  344. margin-left: 30rpx;
  345. .rating-bar-item {
  346. display: flex;
  347. align-items: center;
  348. margin-bottom: 10rpx;
  349. .star-num {
  350. font-size: 22rpx;
  351. color: #606266;
  352. width: 60rpx;
  353. }
  354. .bar-wrapper {
  355. flex: 1;
  356. height: 12rpx;
  357. background: #ebeef5;
  358. border-radius: 6rpx;
  359. margin: 0 10rpx;
  360. overflow: hidden;
  361. .bar {
  362. height: 100%;
  363. background: var(--c-barley-yellow);
  364. border-radius: 6rpx;
  365. }
  366. }
  367. .count {
  368. font-size: 22rpx;
  369. color: #909399;
  370. width: 40rpx;
  371. text-align: right;
  372. }
  373. }
  374. }
  375. }
  376. .filter-tabs {
  377. display: flex;
  378. padding: 20rpx;
  379. background: var(--c-card);
  380. border: 1rpx solid var(--c-border);
  381. border-radius: var(--c-radius-card);
  382. margin: 0 0 20rpx;
  383. box-sizing: border-box;
  384. flex-wrap: wrap;
  385. gap: 12rpx;
  386. .tab-item {
  387. padding: 10rpx 30rpx;
  388. font-size: 26rpx;
  389. color: #606266;
  390. border-radius: 20rpx;
  391. background: #f5f7fa;
  392. margin-right: 20rpx;
  393. &.active {
  394. color: #fff;
  395. background: var(--c-brand-green);
  396. }
  397. .tab-count {
  398. margin-left: 8rpx;
  399. }
  400. }
  401. }
  402. .review-item {
  403. background: var(--c-card);
  404. border: 1rpx solid var(--c-border);
  405. border-radius: var(--c-radius-card);
  406. padding: 24rpx;
  407. margin-bottom: 20rpx;
  408. box-sizing: border-box;
  409. .review-header {
  410. display: flex;
  411. justify-content: space-between;
  412. align-items: flex-start;
  413. margin-bottom: 16rpx;
  414. .user-info {
  415. display: flex;
  416. align-items: center;
  417. .avatar {
  418. width: 60rpx;
  419. height: 60rpx;
  420. border-radius: 50%;
  421. background: var(--c-brand-green);
  422. color: #fff;
  423. display: flex;
  424. align-items: center;
  425. justify-content: center;
  426. font-size: 24rpx;
  427. margin-right: 16rpx;
  428. }
  429. .user-detail {
  430. display: flex;
  431. flex-direction: column;
  432. .username {
  433. font-size: 26rpx;
  434. color: var(--c-text);
  435. margin-bottom: 6rpx;
  436. }
  437. }
  438. }
  439. .review-time {
  440. font-size: 22rpx;
  441. color: #909399;
  442. }
  443. }
  444. .review-content {
  445. .tags {
  446. display: inline-block;
  447. font-size: 24rpx;
  448. color: var(--c-brand-green);
  449. background: var(--c-brand-green-soft);
  450. padding: 4rpx 12rpx;
  451. border-radius: 999rpx;
  452. margin-bottom: 12rpx;
  453. }
  454. .content-text {
  455. display: block;
  456. font-size: 26rpx;
  457. color: var(--c-text);
  458. line-height: 1.6;
  459. }
  460. }
  461. .review-media {
  462. display: flex;
  463. flex-wrap: wrap;
  464. gap: 12rpx;
  465. margin-top: 16rpx;
  466. .media-item {
  467. width: 180rpx;
  468. height: 180rpx;
  469. border-radius: 8rpx;
  470. overflow: hidden;
  471. image, video, .media-image {
  472. width: 100%;
  473. height: 100%;
  474. }
  475. }
  476. }
  477. .seller-reply {
  478. margin-top: 16rpx;
  479. padding: 16rpx;
  480. background: #f5f7fa;
  481. border-radius: 8rpx;
  482. font-size: 24rpx;
  483. .reply-label {
  484. color: #909399;
  485. }
  486. .reply-text {
  487. color: var(--c-text);
  488. margin-left: 8rpx;
  489. }
  490. .reply-time {
  491. display: block;
  492. color: #c0c4cc;
  493. margin-top: 8rpx;
  494. font-size: 22rpx;
  495. }
  496. }
  497. .review-actions {
  498. display: flex;
  499. justify-content: flex-end;
  500. margin-top: 16rpx;
  501. padding-top: 16rpx;
  502. border-top: 1px solid #f5f7fa;
  503. .like-btn {
  504. display: flex;
  505. align-items: center;
  506. padding: 8rpx 20rpx;
  507. background: #f5f7fa;
  508. border-radius: 20rpx;
  509. font-size: 24rpx;
  510. color: #606266;
  511. .icon {
  512. margin-right: 8rpx;
  513. }
  514. }
  515. }
  516. }
  517. .loading-more, .no-more {
  518. text-align: center;
  519. padding: 30rpx;
  520. color: #909399;
  521. font-size: 24rpx;
  522. }
  523. .empty-state {
  524. display: flex;
  525. flex-direction: column;
  526. align-items: center;
  527. justify-content: center;
  528. padding: 72rpx 0;
  529. .empty-image {
  530. width: 140rpx;
  531. height: 140rpx;
  532. margin-bottom: 12rpx;
  533. }
  534. .empty-text {
  535. font-size: 28rpx;
  536. color: #909399;
  537. text-align: center;
  538. line-height: 1.2;
  539. }
  540. }
  541. </style>