|
|
@ -14,15 +14,38 @@ |
|
|
|
<q-footer elevated class="no-shadow" ref="footer"> |
|
|
|
<q-footer elevated class="no-shadow" ref="footer"> |
|
|
|
<q-toolbar |
|
|
|
<q-toolbar |
|
|
|
class="bg-grey-3 text-black no-shadow row justify-center q-pt-md q-pb-sm" |
|
|
|
class="bg-grey-3 text-black no-shadow row justify-center q-pt-md q-pb-sm" |
|
|
|
|
|
|
|
> |
|
|
|
|
|
|
|
<q-select |
|
|
|
|
|
|
|
bg-color="white" |
|
|
|
|
|
|
|
rounded |
|
|
|
|
|
|
|
outlined |
|
|
|
|
|
|
|
v-model="select_model" |
|
|
|
|
|
|
|
:options="options" |
|
|
|
|
|
|
|
option-value="value" |
|
|
|
|
|
|
|
emit-value |
|
|
|
|
|
|
|
map-options |
|
|
|
|
|
|
|
><q-tooltip>机器人型号</q-tooltip> |
|
|
|
|
|
|
|
<template v-slot:option="{ itemProps, itemEvents, opt }"> |
|
|
|
|
|
|
|
<q-item |
|
|
|
|
|
|
|
v-bind="itemProps" |
|
|
|
|
|
|
|
v-on="itemEvents" |
|
|
|
|
|
|
|
> |
|
|
|
|
|
|
|
<q-item-section> |
|
|
|
|
|
|
|
<q-item-label >{{opt.label}} |
|
|
|
|
|
|
|
<q-tooltip >{{opt.value}}</q-tooltip> |
|
|
|
|
|
|
|
</q-item-label> |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</q-item-section> |
|
|
|
|
|
|
|
</q-item> |
|
|
|
|
|
|
|
</template> |
|
|
|
|
|
|
|
</q-select |
|
|
|
> |
|
|
|
> |
|
|
|
<q-input |
|
|
|
<q-input |
|
|
|
class="WAL__field col-grow q-mr-sm" |
|
|
|
class="WAL__field col-grow q-mr-sm" |
|
|
|
style="max-width: 1000px" |
|
|
|
style="max-width: 1000px" |
|
|
|
bg-color="white" |
|
|
|
bg-color="white" |
|
|
|
bottom-slots |
|
|
|
|
|
|
|
v-model="search_text" |
|
|
|
v-model="search_text" |
|
|
|
label="请输入你的问题" |
|
|
|
label="请输入你的问题" |
|
|
|
counter |
|
|
|
|
|
|
|
rounded |
|
|
|
rounded |
|
|
|
outlined |
|
|
|
outlined |
|
|
|
@keyup.enter.native="sendMsg" |
|
|
|
@keyup.enter.native="sendMsg" |
|
|
@ -36,7 +59,11 @@ |
|
|
|
icon="send" |
|
|
|
icon="send" |
|
|
|
@click="sendMsg" |
|
|
|
@click="sendMsg" |
|
|
|
> |
|
|
|
> |
|
|
|
<q-tooltip anchor="top middle" self="bottom middle" :offset="[10, 10]"> |
|
|
|
<q-tooltip |
|
|
|
|
|
|
|
anchor="top middle" |
|
|
|
|
|
|
|
self="bottom middle" |
|
|
|
|
|
|
|
:offset="[10, 10]" |
|
|
|
|
|
|
|
> |
|
|
|
发送问题给机器人 |
|
|
|
发送问题给机器人 |
|
|
|
</q-tooltip> |
|
|
|
</q-tooltip> |
|
|
|
</q-btn> |
|
|
|
</q-btn> |
|
|
@ -48,7 +75,11 @@ |
|
|
|
icon="close" |
|
|
|
icon="close" |
|
|
|
@click="clearMsgs" |
|
|
|
@click="clearMsgs" |
|
|
|
> |
|
|
|
> |
|
|
|
<q-tooltip anchor="top middle" self="bottom middle" :offset="[10, 10]"> |
|
|
|
<q-tooltip |
|
|
|
|
|
|
|
anchor="top middle" |
|
|
|
|
|
|
|
self="bottom middle" |
|
|
|
|
|
|
|
:offset="[10, 10]" |
|
|
|
|
|
|
|
> |
|
|
|
清空聊天记录 |
|
|
|
清空聊天记录 |
|
|
|
</q-tooltip> |
|
|
|
</q-tooltip> |
|
|
|
</q-btn> |
|
|
|
</q-btn> |
|
|
@ -63,35 +94,56 @@ |
|
|
|
import { sendQuestion } from "../api/chatbot/chatbot"; |
|
|
|
import { sendQuestion } from "../api/chatbot/chatbot"; |
|
|
|
import moment from "moment"; |
|
|
|
import moment from "moment"; |
|
|
|
import { dta } from "../utils/date_utils"; |
|
|
|
import { dta } from "../utils/date_utils"; |
|
|
|
const bot_message_records="bot_message_records" |
|
|
|
const bot_message_records = "bot_message_records"; |
|
|
|
export default { |
|
|
|
export default { |
|
|
|
name: "MainLayout", |
|
|
|
name: "MainLayout", |
|
|
|
data() { |
|
|
|
data() { |
|
|
|
return { |
|
|
|
return { |
|
|
|
|
|
|
|
select_model: "TEXT_DEVINCI_003", |
|
|
|
|
|
|
|
options: [ |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
value: "TEXT_DEVINCI_003", |
|
|
|
|
|
|
|
label: "聪明" |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
value: "TEXT_CURIE_001", |
|
|
|
|
|
|
|
label: "还行" |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
value: "TEXT_BABBAGE_001", |
|
|
|
|
|
|
|
label: "有点呆" |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
value: "TEXT_ADA_001", |
|
|
|
|
|
|
|
label: "憨批" |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
], |
|
|
|
search_text: "", |
|
|
|
search_text: "", |
|
|
|
disable: false, |
|
|
|
disable: false, |
|
|
|
messages: [ |
|
|
|
messages: [ |
|
|
|
{ |
|
|
|
{ |
|
|
|
name: "bot", |
|
|
|
name: "bot", |
|
|
|
text: ["请告诉我你的问题"], |
|
|
|
text: "请告诉我你的问题", |
|
|
|
sent: false, |
|
|
|
sent: false, |
|
|
|
datetime: moment().format(dta) |
|
|
|
datetime: moment().format(dta) |
|
|
|
}, |
|
|
|
} |
|
|
|
], |
|
|
|
] |
|
|
|
}; |
|
|
|
}; |
|
|
|
}, |
|
|
|
}, |
|
|
|
created() { |
|
|
|
created() { |
|
|
|
let records = localStorage.getItem(bot_message_records) |
|
|
|
//查询localStorage中是否存在聊天记录 存在则加载到页面 |
|
|
|
|
|
|
|
let records = localStorage.getItem(bot_message_records); |
|
|
|
if (records && records != "[]") { |
|
|
|
if (records && records != "[]") { |
|
|
|
this.messages=JSON.parse(records) |
|
|
|
this.messages = JSON.parse(records); |
|
|
|
localStorage.removeItem(bot_message_records) |
|
|
|
localStorage.removeItem(bot_message_records); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
//监听pagehide事件 页面关闭时保存聊天记录到localStorage中 |
|
|
|
window.addEventListener("pagehide", () => { |
|
|
|
window.addEventListener("pagehide", () => { |
|
|
|
localStorage.setItem(bot_message_records,JSON.stringify(this.messages)) |
|
|
|
localStorage.setItem(bot_message_records, JSON.stringify(this.messages)); |
|
|
|
}) |
|
|
|
}); |
|
|
|
}, |
|
|
|
}, |
|
|
|
methods: { |
|
|
|
methods: { |
|
|
|
//发送问题 |
|
|
|
//请求机器人接口 |
|
|
|
async sendMsg() { |
|
|
|
async sendMsg() { |
|
|
|
if (!this.search_text || this.disable) { |
|
|
|
if (!this.search_text || this.disable) { |
|
|
|
return; |
|
|
|
return; |
|
|
@ -99,33 +151,42 @@ export default { |
|
|
|
this.disable = true; |
|
|
|
this.disable = true; |
|
|
|
//讲自己的问题加入到msg集合中 |
|
|
|
//讲自己的问题加入到msg集合中 |
|
|
|
|
|
|
|
|
|
|
|
let text = this.search_text |
|
|
|
let text = this.search_text; |
|
|
|
this.search_text = "" |
|
|
|
this.search_text = ""; |
|
|
|
this.addMessage("me", true, text, moment().format(dta)); |
|
|
|
this.addMessage("me", true, text, moment().format(dta)); |
|
|
|
await sendQuestion(text).then((res) => { |
|
|
|
await sendQuestion({question:text,model:this.select_model}) |
|
|
|
//发送问题给机器人 |
|
|
|
.then((res) => { |
|
|
|
|
|
|
|
if(res.code==1){ |
|
|
|
this.addMessage("bot", false, res.data, moment().format(dta)); |
|
|
|
this.addMessage("bot", false, res.data, moment().format(dta)); |
|
|
|
//清空输入框 |
|
|
|
}else{ |
|
|
|
}).catch(()=>{ |
|
|
|
this.$q.notify({ |
|
|
|
|
|
|
|
color:"red", |
|
|
|
|
|
|
|
message:"请求服务器错误", |
|
|
|
|
|
|
|
position:"top", |
|
|
|
|
|
|
|
timeout: 3000 |
|
|
|
|
|
|
|
}) |
|
|
|
this.addMessage("bot", false,"出错了!!!", moment().format(dta)); |
|
|
|
this.addMessage("bot", false,"出错了!!!", moment().format(dta)); |
|
|
|
|
|
|
|
} |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
.catch(() => { |
|
|
|
|
|
|
|
this.addMessage("bot", false, "出错了!!!", moment().format(dta)); |
|
|
|
|
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
this.disable = false; |
|
|
|
this.disable = false; |
|
|
|
}, |
|
|
|
}, |
|
|
|
addMessage(name, sent, text, datetime) { |
|
|
|
addMessage(name, sent, text, datetime) { |
|
|
|
this.messages.push({ |
|
|
|
this.messages.push({ |
|
|
|
name: name, |
|
|
|
name: name, |
|
|
|
text: [text], |
|
|
|
text: text, |
|
|
|
sent: sent, |
|
|
|
sent: sent, |
|
|
|
datetime |
|
|
|
datetime |
|
|
|
}); |
|
|
|
}); |
|
|
|
}, |
|
|
|
}, |
|
|
|
clearMsgs() { |
|
|
|
clearMsgs() { |
|
|
|
this.messages = []; |
|
|
|
this.messages = []; |
|
|
|
localStorage.removeItem(bot_message_records) |
|
|
|
localStorage.removeItem(bot_message_records); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}, |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
</script> |
|
|
|
</script> |
|
|
|
<style lang="css"> |
|
|
|
<style lang="css"> |
|
|
|