update:auto scroll ai page

master
xhhing 1 month ago
parent 1a8a62f598
commit d5e35e6dbb

@ -1,29 +1,27 @@
<template>
<!-- 背景图片-->
<image class="background" src="@/static/homepages/puppy_chat/pictures/chat_image.png"></image>
<scroll-view scroll-y="true" class="chat_scroll" :scroll-into-view="scrollInto">
<view class="chat">
<!-- 渲染聊天消息气泡 -->
<view v-for="(message, index) in send_messages" :id="'s'+index" :key="index" :class="['chat_area', message.sender === 'user' ? 'user_message' : 'ai_message']">
<!-- 头像部分放在气泡外部 -->
<view v-if="message.sender === 'ai'" class="avatar_container ai_avatar_container">
<image class="ai_avatar" src="@/static/homepages/puppy_chat/pictures/ai_avatar.png"></image>
</view>
<!-- 消息内容 -->
<view :class="['message_content', message.sender === 'user' ? 'user_message_content' : 'ai_message_content']">
<!-- <text>{{ message.message }}</text> -->
<zero-markdown-view :markdown="message.message"></zero-markdown-view>
</view>
<view v-if="message.sender === 'user'" class="avatar_container user_avatar_container">
<image class="user_avatar" src="@/static/homepages/puppy_chat/pictures/user_avatar.png"></image>
<!-- 背景图片-->
<image class="background" src="@/static/homepages/puppy_chat/pictures/chat_image.png"></image>
<scroll-view scroll-y="true" id="chat_scroll" :scroll-top="scrollTop" scroll-with-animation="true">
<view class="chat">
<!-- 渲染聊天消息气泡 -->
<view v-for="(message, index) in send_messages" :key="index" :class="['chat_area', message.sender === 'user' ? 'user_message' : 'ai_message']">
<!-- 头像部分放在气泡外部 -->
<view v-if="message.sender === 'ai'" class="avatar_container ai_avatar_container">
<image class="ai_avatar" src="@/static/homepages/puppy_chat/pictures/ai_avatar.png"></image>
</view>
<!-- 消息内容 -->
<view :class="['message_content', message.sender === 'user' ? 'user_message_content' : 'ai_message_content']">
<!-- <text>{{ message.message }}</text> -->
<zero-markdown-view :markdown="message.message"></zero-markdown-view>
</view>
<view v-if="message.sender === 'user'" class="avatar_container user_avatar_container">
<image class="user_avatar" src="@/static/homepages/puppy_chat/pictures/user_avatar.png"></image>
</view>
</view>
</view>
</view>
</view>
</scroll-view>
</scroll-view>
@ -49,7 +47,9 @@ export default {
send_messages: [], //
conversation_id: 'e97ec0f2-2a94-42e7-b6db-b04883d349e8',//id
answer: '', // AI
scrollInto:"", // scroll-into-view
scrollTop : 0, //
totalHeight:0,
scrollViewHeight:0,
};
},
onLoad() {
@ -81,23 +81,40 @@ export default {
sender: 'ai', //
message:"我是fit journey的专属AI小助手有什么问题都可以问我哦~"
});
},
mounted() {
//
const that = this
const query = uni.createSelectorQuery().in(that)
query.select('#chat_scroll').boundingClientRect()
query.exec((res)=>{
that.scrollViewHeight = res[0].height
})
},
methods: {
setScroll(){
//TODO:
const len = this.send_messages.length;
this.scrollInto = 's'+(len-1);
console.log('scrollInto'+this.scrollInto)
setScrollTop(){
//
setTimeout(()=>{ //
const that = this
const query = uni.createSelectorQuery().in(that)
query.selectAll('.chat_area').boundingClientRect()
query.exec((res)=>{
res[0].forEach((item)=>{
that.totalHeight = that.totalHeight+item.height
})
if (that.totalHeight > that.scrollViewHeight) { //
that.scrollTop = that.totalHeight - that.scrollViewHeight
}
})
},100)
},
handleSendMessage() {
//
const userMessageCount = this.send_messages.filter(msg => msg.sender === 'user').length;
if (userMessageCount >= 3) {
if (userMessageCount >= 1) {
// 2
uni.showToast({
title: "AI 可是要交钱的哦,最多发送1条消息,刷新之后再试试吧~",
@ -119,7 +136,7 @@ export default {
this.send_message=this.input_message;
this.input_message = ""; //
this.setScroll() //
this.setScrollTop() //
// AI
this.fetchAIResponse();
@ -150,7 +167,7 @@ export default {
},
success: (res) => {
console.log("请求成功!请求数据是", res.data.data);
// console.log("", res.data.data);
//
this.answer = res.data.data;
// AI
@ -159,7 +176,7 @@ export default {
sender: 'ai', // AI
message:this.answer
});
this.setScroll() //
this.setScrollTop() //
},
error: (res) => {
console.log("请求失败!请求数据是", res);

@ -7,7 +7,7 @@
}
.chat_scroll{
#chat_scroll{
position: absolute;
width: 100%;
top: 12%;

Loading…
Cancel
Save