Refactor(评论):1、回复内容应去掉@xxx;2、增加逻辑判断

This commit is contained in:
lifizer
2025-07-15 17:17:03 +08:00
parent 2629e5d849
commit e96339e7e2
2 changed files with 117 additions and 97 deletions
+57 -44
View File
@@ -34,16 +34,12 @@
{{ comment.parentReply }}
</view>
<view class="v12-font-28 v12-dark2-text" @click="toInfo(comment)">
<text class="v12-dark1-text">{{ "@" +comment.senderUserNickname }}</text>
{{ ' ' + comment.reply.split(' ')[1] }}
<text class="v12-dark1-text">{{ "@" + comment.senderUserNickname }}</text>
{{ ' ' + comment.reply }}
</view>
<view class="v12-mt-3">
<u-button
plain
icon="chat"
shape="circle"
<u-button
size="normal"
@click.stop="showSendBtn(comment)"
:customStyle="{
backgroundColor: '#eee',
width: '160rpx',
@@ -52,8 +48,12 @@
color: '#777',
margin: 0
}"
plain
icon="chat"
shape="circle"
iconColor="#777"
>回复</u-button>
@click.stop="showSendBtn(comment)"
>回复</u-button>
</view>
</view>
</view>
@@ -63,23 +63,24 @@
<view v-if="showBtn" class="v12-align-center v12-justify-center v12-pa-3 v12-white sticky-bottom">
<view style="width: 100%">
<u-textarea
id="input"
v-model="content"
:maxlength="500"
@blur="onBlur"
@focus="commentInputFocus"
@change="commentInputChange"
:maxheight="200"
:autoHeight="content.length < 40 ? true : false"
:showConfirmBar="false"
:focus="autoFocus"
id="input"
:cursor="cursorIndex"
cursorSpacing="85"
:customStyle="{
backgroundColor: '#f7f7f7',
border: '1px solid #f7f7f7',
borderRadius: '20rpx'
}">
}"
cursorSpacing="85"
@blur="onBlur"
@focus="commentInputFocus"
@change="commentInputChange"
>
<template slot="prefix">
<view class="v12-align-center">
<u-icon :name="'edit-pen'"></u-icon>
@@ -93,7 +94,7 @@
</template>
</u-textarea>
</view>
<view class="v12-primary v12-white-text send-btn" @click="replyMessage">
<view class="v12-primary v12-white-text send-btn" @click="replyMessageHandle">
发送
</view>
</view>
@@ -105,6 +106,7 @@ import { getMessageList, replyMessage } from '@/api/rooms'
export default {
data() {
return {
// 消息列表
list: [
// parentReply string 被回复的原始评论
// reply string 回复评论内容
@@ -126,17 +128,21 @@ export default {
// senderUserNickname: '张三',
// senderTime: '2022-01-01 00:00:00',
// }
], // 消息列表
autoFocus: false, // 是否自动聚焦
webUrl: this.$VUE_APP_RESOURCES_URL, // 资源路径
showBtn: false, // 是否显示发送按钮
],
// 是否自动聚焦
autoFocus: false,
// 资源路径
webUrl: this.$VUE_APP_RESOURCES_URL,
// 是否显示发送按钮
showBtn: false,
content: '',
currentComment: {},
cursorIndex: 0 // 光标位置
// 光标位置
cursorIndex: 0
}
},
onShow() {
this.fetchList() // 查询消息列表
this.fetchList()
},
methods: {
toInfo(comment) {
@@ -160,12 +166,12 @@ export default {
this.currentComment = {}
return
}
// 若删除@用户名,将@用户名整个删除
const usernameLength = `@${this.currentComment.senderUserNickname} `.length
if(!e.includes(`@${this.currentComment.senderUserNickname} `) && this.currentComment.senderUserNickname) {
this.currentComment = {}
this.content = e.slice(usernameLength - 1); // 去除@用户名
// 去除@用户名
this.content = e.slice(usernameLength - 1)
}
},
commentInputFocus(e) {
@@ -175,34 +181,36 @@ export default {
* 发送消息
*/
showSendBtn(item) {
console.log(item);
this.showBtn = true // 显示发送按钮
setTimeout(() => { // 延迟100ms,否则无法聚焦
this.autoFocus = true // 自动聚焦
// 显示发送按钮
this.showBtn = true
// 延迟100ms,否则无法聚焦
setTimeout(() => {
// 自动聚焦
this.autoFocus = true
this.currentComment = item
this.content = '@' + item.senderUserNickname + ' '
this.cursorIndex = this.content.length // 光标位置
this.cursorIndex = this.content.length
}, 100)
},
/**
* 失焦
*/
onBlur() {
this.showBtn = false // 隐藏发送按钮
this.autoFocus = false // 取消自动聚焦
// 隐藏发送按钮
this.showBtn = false
// 取消自动聚焦
this.autoFocus = false
},
/**
* 回复消息
* @param {*} item 消息数据
*/
replyMessage() {
replyMessageHandle() {
if(!this.currentComment.senderUserNickname) {
// 未选择需要回复的用户,提示
uni.showToast({
title: '请选择需要回复的用户',
icon: 'none',
icon: 'none'
})
return
}
@@ -210,33 +218,38 @@ export default {
// 请输入回复内容
uni.showToast({
title: '请输入回复内容',
icon: 'none',
icon: 'none'
})
return
}
// 点击弹出回复框,输入回复内容,点击发送按钮,发送回复消息
uni.showLoading({
mask: true, // 显示透明蒙层,防止触摸穿透,默认:false
mask: true
})
const params = {
contentId: this.currentComment.contentId, // 评论的目标内容ID,必传
contentType: 'hotelNews', // 评论的目标内容类型,必传(店铺资讯传入值hotelNews)
reply: this.content, // 评论内容,必传
atUid: this.currentComment.senderUid, // @的用户ID
parentReplyId: this.currentComment.parentReplyId // 回复的评论ID
// 评论的目标内容ID,必传
contentId: this.currentComment.contentId,
// 评论的目标内容类型,必传(店铺资讯传入值hotelNews)
contentType: 'hotelNews',
// 评论内容,必传
reply: this.content.replace('@' + this.currentComment.senderUserNickname, '').trim(),
// @的用户ID
atUid: this.currentComment.senderUid,
// 回复的评论ID
parentReplyId: this.currentComment.parentReplyId
}
replyMessage(params).then(res => {
this.fetchList() // 查询消息列表
this.fetchList()
}).finally(() => {
uni.hideLoading()
})
},
/**
* 查询消息列表
*/
fetchList() {
const params = {
page: 1, // 页码
page: 1,
limit: 999999
}
getMessageList(params).then(res => {