最近接到的需求有,聊天时,当在输入框中匹配到特定的链接时发送一条自定义消息给用户,并且此链接不会显示在输入框中,做法如下
<!-- 聊天输入框 -->
<div class="im-chat-msg-inp">
<textarea v-model="sendInfo" @paste="userPasterFn"></textarea>
</div>
// 检测输入框粘贴事件
userPasterFn(e) {
console.log(e)
console.log(this.sendInfo)
let sendInfoBeforeValue = this.sendInfo
let text = e.clipboardData.getData('Text')
console.log(text)
let _this = this
try {
let a = JSON.parse(text)
if (a.xmMessageType === 102) {
// 发送推荐咨询师自定义消息
_this.isNeedMask = true
_this.isShowRecommendAConsultant = true
_this.soonSendRecomentText = text
// 此处阻止链接显示在聊天框中
e.preventDefault();
}
} catch (error){
console.log(error)
// 不执行发送自定义消息,直接发送字符串信息
}
console.log('粘贴完成时---------------', sendInfoBeforeValue, this.sendInfo)
},