功能:完善评论测试与优惠券最优选择

This commit is contained in:
xiaowang
2026-05-16 14:28:42 +08:00
parent 1f05e0ab44
commit 4563dd2538
31 changed files with 1130 additions and 581 deletions
+24
View File
@@ -0,0 +1,24 @@
const request = require('../utils/request')
module.exports = {
listByProduct(productId) {
return request({
url: `/comment/list/product/${productId}`
})
},
create(data) {
return request({
url: '/comment/create',
method: 'POST',
data
})
},
delete(id) {
return request({
url: `/comment/delete/${id}`,
method: 'DELETE'
})
}
}
+3 -4
View File
@@ -242,11 +242,10 @@ Page({
}
if (selectedCouponKey === NO_COUPON_KEY) {
const vipCoupons = list
.filter(item => item.sourceType === 'SYSTEM_VIP')
const bestCoupons = list
.sort((left, right) => calculateCouponDiscount(right, subtotalAmount) - calculateCouponDiscount(left, subtotalAmount))
if (vipCoupons.length) {
selectedCouponKey = vipCoupons[0].key
if (bestCoupons.length) {
selectedCouponKey = bestCoupons[0].key
}
}
+64 -1
View File
@@ -1,6 +1,7 @@
const cartApi = require('../../api/cart')
const productApi = require('../../api/product')
const productLikeApi = require('../../api/product-like')
const commentApi = require('../../api/comment')
const shopApi = require('../../api/shop')
const wishlistApi = require('../../api/wishlist')
const actionGuard = require('../../utils/action-guard')
@@ -65,6 +66,9 @@ Page({
product: null,
productImages: [],
shopInfo: null,
comments: [],
commentText: '',
replyTarget: null,
isFavorite: false,
isLiked: false,
artistCountText: '门店安排',
@@ -122,8 +126,9 @@ Page({
earliestBookableText: estimateEarliestBookable(shopInfo && shopInfo.businessHours)
})
await this.loadComments()
if (auth.isLoggedIn()) {
this.loadInteractionStatus()
await this.loadInteractionStatus()
} else {
this.setData({
isFavorite: false,
@@ -159,6 +164,22 @@ Page({
} catch (error) {}
},
async loadComments() {
const product = this.data.product
if (!product || !product.id) {
this.setData({ comments: [] })
return
}
try {
const comments = await commentApi.listByProduct(product.id)
this.setData({
comments: Array.isArray(comments) ? comments : []
})
} catch (error) {
this.setData({ comments: [] })
}
},
handleBack() {
navigation.goBack('/pages/products/index')
},
@@ -323,4 +344,46 @@ Page({
})
}
}
,
handleCommentInput(event) {
this.setData({
commentText: event.detail.value
})
},
handleReply(event) {
const comment = event.currentTarget.dataset.comment
this.setData({
replyTarget: comment || null
})
},
async handleSubmitComment() {
const product = this.data.product
const content = String(this.data.commentText || '').trim()
if (!product || !product.id || !navigation.ensureLogin('发表评论')) {
return
}
if (!content) {
wx.showToast({ title: '请输入评论内容', icon: 'none' })
return
}
try {
await commentApi.create({
productId: product.id,
shopId: product.shopId,
parentCommentId: this.data.replyTarget ? this.data.replyTarget.id : null,
commentContent: content
})
this.setData({
commentText: '',
replyTarget: null
})
await this.loadComments()
wx.showToast({ title: '评论已发布', icon: 'none' })
} catch (error) {
wx.showToast({ title: error.message || '评论发布失败', icon: 'none' })
}
}
})
@@ -78,6 +78,35 @@
<view class="desc-text">{{product.descriptionText}}</view>
</view>
</view>
<view class="page-card comment-card">
<view class="comment-head">
<view class="section-title">作品评论</view>
<text class="comment-count">{{comments.length}} 条</text>
</view>
<view class="comment-editor">
<textarea class="comment-input" maxlength="300" auto-height placeholder="{{replyTarget ? '回复 ' + (replyTarget.userNickname || '用户') : '说说你的看法'}}" value="{{commentText}}" bindinput="handleCommentInput"></textarea>
<view class="comment-actions">
<view wx:if="{{replyTarget}}" class="comment-cancel" bindtap="handleReply">取消回复</view>
<view class="comment-submit" bindtap="handleSubmitComment">发布</view>
</view>
</view>
<view wx:if="{{!comments.length}}" class="comment-empty">还没有评论,来写第一条吧</view>
<view wx:for="{{comments}}" wx:key="id" class="comment-item">
<view class="comment-main">
<text class="comment-name">{{item.userNickname || '用户'}}</text>
<text class="comment-time">{{item.createTime}}</text>
</view>
<view class="comment-text">{{item.commentContent}}</view>
<view class="comment-reply-btn" data-comment="{{item}}" bindtap="handleReply">回复</view>
<view wx:if="{{item.replies && item.replies.length}}" class="reply-list">
<view wx:for="{{item.replies}}" wx:for-item="reply" wx:key="id" class="reply-item">
<text class="reply-name">{{reply.userNickname || '用户'}}:</text>
<text>{{reply.commentContent}}</text>
</view>
</view>
</view>
</view>
</view>
<view class="footer-bar">
@@ -157,6 +157,116 @@
color: #5d4b55;
}
.comment-card {
margin-top: 24rpx;
padding: 30rpx;
}
.comment-head,
.comment-main,
.comment-actions {
display: flex;
align-items: center;
justify-content: space-between;
gap: 16rpx;
}
.comment-count,
.comment-time {
font-size: 22rpx;
color: #9b8d94;
}
.comment-editor {
margin-top: 20rpx;
padding: 20rpx;
border-radius: 20rpx;
background: #fff8fa;
}
.comment-input {
width: 100%;
min-height: 120rpx;
font-size: 26rpx;
line-height: 1.7;
color: #2d2430;
}
.comment-actions {
margin-top: 16rpx;
justify-content: flex-end;
}
.comment-cancel,
.comment-submit,
.comment-reply-btn {
font-size: 24rpx;
color: #c6597d;
font-weight: 700;
}
.comment-submit {
min-width: 116rpx;
height: 56rpx;
line-height: 56rpx;
text-align: center;
border-radius: 999rpx;
color: #ffffff;
background: #c6597d;
}
.comment-empty {
padding: 28rpx 0 8rpx;
font-size: 24rpx;
color: #9b8d94;
text-align: center;
}
.comment-item {
padding: 24rpx 0;
border-bottom: 1rpx solid #f2e7eb;
}
.comment-item:last-child {
border-bottom: 0;
}
.comment-name {
font-size: 26rpx;
font-weight: 700;
color: #2d2430;
}
.comment-text {
margin-top: 12rpx;
font-size: 26rpx;
line-height: 1.8;
color: #5d4b55;
}
.comment-reply-btn {
margin-top: 10rpx;
}
.reply-list {
margin-top: 14rpx;
padding: 16rpx;
border-radius: 18rpx;
background: #faf4f6;
}
.reply-item {
padding: 6rpx 0;
font-size: 24rpx;
line-height: 1.7;
color: #5d4b55;
}
.reply-name {
color: #2d2430;
font-weight: 700;
}
.footer-bar {
position: fixed;
left: 24rpx;
+1
View File
@@ -38,6 +38,7 @@ function isWhitelistedPath(url) {
'/user/reset-password-by-phone',
'/product/get/',
'/product/list/',
'/comment/list/product/',
'/shop/get/',
'/shop/list/public',
'/shop/list/simple',