【web】 ai对话修改

dev
Wangxin 8 months ago
parent 3ed3fcc8d8
commit 87d27225dc
  1. 188
      ALOps_sys_fe/alops-ui/src/views/QandA/Aiassistant/index.vue

@ -31,10 +31,12 @@
@keyup.enter.native="sendMessage" @keyup.enter.native="sendMessage"
></el-input> ></el-input>
<el-button <el-button
type="primary"
@click="sendMessage" @click="sendMessage"
:disabled="!newMessage.trim()" v-if="!isTyping" type="primary" icon="el-icon-position" circle></el-button>
>发送</el-button> <div class="stop-btn " v-if="isTyping"
@click="showFullAnswer">
<div class="el-icon-stop"></div>
</div>
</div> </div>
</div> </div>
</div> </div>
@ -57,24 +59,32 @@ export default {
data() { data() {
return { return {
newMessage: '', newMessage: '',
messages: [ messages: [],
{
text: '您好!我是AI智能助手,请问有什么可以帮您?',
sender: 'service',
time: this.formatTime(new Date())
}
],
faqs: [ faqs: [
{ question: '如何查询订单状态?', answer: '您可以在"我的订单"页面查看订单的当前状态和物流信息。' }, { question: '如何查询订单状态?', answer: '您可以在"我的订单"页面查看订单的当前状态和物流信息。' },
{ question: '如何办理退货?', answer: '请在订单详情页点击"申请退货",填写相关信息后提交申请。' }, { question: '如何办理退货?', answer: '请在订单详情页点击"申请退货",填写相关信息后提交申请。' },
{ question: '支付方式有哪些?', answer: '我们支持支付宝、微信支付、银联等多种支付方式。' }, { question: '支付方式有哪些?', answer: '我们支持支付宝、微信支付、银联等多种支付方式。' },
{ question: '会员有哪些优惠?', answer: '会员可享受购物折扣、专属优惠券、生日特权等多重优惠。' }, { question: '会员有哪些优惠?', answer: '会员可享受购物折扣、专属优惠券、生日特权等多重优惠。' },
{ question: '商品何时发货?', answer: '一般情况下,我们会在订单支付成功后24小时内安排发货。' } { question: '商品何时发货?', answer: '一般情况下,我们会在订单支付成功后24小时内安排发货。' }
] ],
STORAGE_KEY: {
permanent: 'ai_assistant_messages',
temporary: 'ai_assistant_messages_temp'
},
storageType: 'temporary', // 使
typewriterInterval: null, //
isTyping: false, //
currentMessageIndex: -1 //
}; };
},mounted() { },mounted() {
// localStorage
this.loadMessages();
// //
this.scrollToBottom(); this.scrollToBottom();
//
window.addEventListener('beforeunload', this.handleBeforeUnload);
// 使
this.loadMessages(this.storageType);
}, },
methods: { methods: {
sendMessage() { sendMessage() {
@ -149,12 +159,22 @@ export default {
typeWriter(messageIndex, fullText) { typeWriter(messageIndex, fullText) {
let currentIndex = 0; let currentIndex = 0;
//
this.isTyping = true;
this.currentMessageIndex = messageIndex;
// //
this.$nextTick(() => { this.$nextTick(() => {
this.scrollToBottom(); this.scrollToBottom();
}); });
const typeInterval = setInterval(() => { //
if (this.typewriterInterval) {
clearInterval(this.typewriterInterval);
}
//
this.typewriterInterval = setInterval(() => {
if (currentIndex < fullText.length) { if (currentIndex < fullText.length) {
// //
let typeSpeed = 15; // 15/ let typeSpeed = 15; // 15/
@ -184,11 +204,33 @@ export default {
} }
} else { } else {
// //
clearInterval(typeInterval); this.stopTyping();
}
}, 15); // 15/
},
//
stopTyping() {
if (this.typewriterInterval) {
clearInterval(this.typewriterInterval);
this.typewriterInterval = null;
}
this.isTyping = false;
this.currentMessageIndex = -1;
// //
this.scrollToBottom(); this.scrollToBottom();
},
//
showFullAnswer() {
if (this.isTyping && this.currentMessageIndex >= 0 && this.currentMessageIndex < this.messages.length) {
// service
const currentMessage = this.messages[this.currentMessageIndex];
if (currentMessage && currentMessage.sender === 'service') {
//
this.stopTyping();
//
// 便
}
} }
}, 15); // 15/
}, },
selectFaq(faq) { selectFaq(faq) {
// //
@ -315,12 +357,95 @@ export default {
const hours = date.getHours().toString().padStart(2, '0'); const hours = date.getHours().toString().padStart(2, '0');
const minutes = date.getMinutes().toString().padStart(2, '0'); const minutes = date.getMinutes().toString().padStart(2, '0');
return `${hours}:${minutes}`; return `${hours}:${minutes}`;
},
//
handleBeforeUnload() {
//
if (this.storageType === 'temporary') {
this.clearChatHistory('temporary');
}
},
//
clearChatHistory() {
//
this.messages = [{...this.messages[0]}];
//
this.newMessage = '';
//
this.scrollToBottom();
},
//
loadMessages(type = this.storageType) {
try {
const storage = type === 'permanent' ? localStorage : sessionStorage;
const savedMessages = storage.getItem(this.STORAGE_KEY[type]);
if (savedMessages) {
this.messages = JSON.parse(savedMessages);
} else {
//
this.messages = [{
text: '您好!我是AI智能助手,请问有什么可以帮您?',
sender: 'service',
time: this.formatTime(new Date())
}];
}
} catch (error) {
console.error('加载消息失败:', error);
//
this.messages = [{
text: '您好!我是AI智能助手,请问有什么可以帮您?',
sender: 'service',
time: this.formatTime(new Date())
}];
}
},
//
saveMessages(type = this.storageType) {
try {
const storage = type === 'permanent' ? localStorage : sessionStorage;
storage.setItem(this.STORAGE_KEY[type], JSON.stringify(this.messages));
} catch (error) {
console.error('保存消息失败:', error);
}
},
//
clearChatHistory(type = this.storageType) {
try {
const storage = type === 'permanent' ? localStorage : sessionStorage;
storage.removeItem(this.STORAGE_KEY[type]);
this.messages = [{
text: '您好!我是AI智能助手,请问有什么可以帮您?',
sender: 'service',
time: this.formatTime(new Date())
}];
this.scrollToBottom();
} catch (error) {
console.error('清除消息失败:', error);
}
},
//
setStorageType(type) {
if (['permanent', 'temporary'].includes(type)) {
this.storageType = type;
//
this.loadMessages(type);
return true;
}
return false;
},
//
handleBeforeUnload() {
//
//
// this.clearChatHistory();
} }
}, },
watch: { watch: {
messages: { messages: {
handler() { handler() {
// // localStorage
this.saveMessages();
//
this.scrollToBottom(); this.scrollToBottom();
}, },
deep: true deep: true
@ -347,6 +472,13 @@ export default {
border-top-left-radius: 4px; border-top-left-radius: 4px;
border-top-right-radius: 4px; border-top-right-radius: 4px;
} }
.service-header h2 {
margin: 0 0 5px 0;
}
.service-header p {
margin: 0;
opacity: 0.9;
}
.service-container { .service-container {
display: flex; display: flex;
min-height: 400px; min-height: 400px;
@ -403,9 +535,35 @@ export default {
padding: 15px; padding: 15px;
border-top: 1px solid #e6e6e6; border-top: 1px solid #e6e6e6;
display: flex; display: flex;
gap: 10px;
} }
.chat-input .el-input { .chat-input .el-input {
margin-right: 10px; margin-right: 10px;
flex: 1;
}
.chat-input .el-button {
white-space: nowrap;
}
.stop-btn {
align-items: center;
border: 2px solid rgba(73, 113, 245, .5);
border-radius: 20px;
display: flex;
height: 36px;
justify-content: center;
width: 36px;
cursor: pointer;
transition: all .2s;
}
.stop-btn .el-icon-stop {
background: linear-gradient(0deg, #4971f5, #4971f5), #fff;
border-radius: 4px;
flex-shrink: 0;
height: 13px;
width: 13px;
} }
.faq-item { .faq-item {
padding: 10px; padding: 10px;

Loading…
Cancel
Save