机器人改为socket

V2.0.0
Tuzki 2 years ago
parent 7c510fffac
commit 35db450358
  1. 296
      ruoyi-ui/src/views/pages/index.vue

@ -78,21 +78,23 @@
<el-dialog v-if="open" :title="title" :visible.sync="open" append-to-body custom-class="v-report-dialog">
<onlyOffice :cont="false" :doc-config="docConfig"></onlyOffice>
</el-dialog>
<el-dialog v-if="opens" :modal="false" :title="title" :visible.sync="opens" append-to-body :fullscreen="true"
<el-dialog v-if="opens" :fullscreen="true" :modal="false" :title="title" :visible.sync="opens" append-to-body
custom-class="v-report-dialog-small-l"
style="width: 65%">
<onlyOffice :cont="false" :doc-config="docConfig"></onlyOffice>
</el-dialog>
<el-dialog v-if="opens" :modal="false" :title="'AI智能分析报告'" :visible.sync="opens" append-to-body
:fullscreen="true"
<el-dialog v-if="opens" :fullscreen="true" :modal="false" :title="'AI智能分析报告'" :visible.sync="opens"
append-to-body
custom-class="v-report-dialog-small-r"
style="
position: absolute;
width: 35%;
left: unset;">
left: unset;"
@close="closeAi">
<!-- {{ context }}-->
<div>{{ fullString }}</div>
<ul ref="messagesList" class="message-list">
<div class="loading-p" v-if="showLoading">
<div v-if="showLoading" class="loading-p">
<div class="loading-box">
<div class="loading-ai"></div>
<div class="loading-text">智能助手分析中请稍后...</div>
@ -106,9 +108,9 @@
</li>
</ul>
<div class="chat-window">
<el-input v-model="message" :disabled="showLoading" class="chat-input" placeholder="输入消息..."
type="text" @keyup.enter.native="sendMessage"/>
<el-button :disabled="showLoading" class="chat-btn" type="primary" @click="sendMessage">发送
<el-input v-model="message" :disabled="btnLoading" class="chat-input" placeholder="输入消息..."
type="text" @keyup.enter.native="sendMessages"/>
<el-button :disabled="btnLoading" class="chat-btn" type="primary" @click="sendMessages">发送
</el-button>
</div>
</el-dialog>
@ -125,6 +127,7 @@ import {downPDF, selectDocByType} from '@/api/report/document'
import {converted, downDoc, openDocument} from "@/api/office/onlyOffice";
import onlyOffice from "@/components/OnlyOffice/index.vue";
import VHeader from "@/views/components/VHeader/VHeader";
import {getToken} from '@/utils/auth'
export default {
name: "index",
@ -132,7 +135,9 @@ export default {
data() {
return {
fullString: '',
showLoading: false,
btnLoading: false,
loading: false,
title: '',
open: false,
@ -198,8 +203,33 @@ export default {
//
startTime: "",
nowTime: '',
socket: null,
randomNumber: null,
baseUrl: null,
baseUrls: process.env.VUE_APP_BASE_API,
token: getToken(),
aimessage: [],
aimessages: [],
}
},
// created() {
// this.connectSocket();
// },
// beforeDestroy() {
// this.disconnectSocket();
// },
watch: {
aimessages:{
handler(val){
console.log(val.length, this.aimessage.length,'======');
if (val.length!=0 && val.length == this.aimessage.length){
this.btnLoading = false
}
}
}
},
computed:{
},
mounted() {
//
this.selectAreaYear();
@ -208,6 +238,68 @@ export default {
this.selectAreaName()
},
methods: {
connectSocket() {
let url = window.location.href;
const ipRegex = /^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$/;
if (ipRegex.test(url)) {
this.baseUrl = url
} else {
this.baseUrl = url.split('/')[2];
}
const token = getToken()
this.randomNumber = Math.floor(Math.random() * 999);
this.socket = new WebSocket(`ws://` + this.baseUrl + this.baseUrls + `/aiws/` + this.randomNumber);
// this.socket = new WebSocket(`ws://192.168.130.154:8060`+this.baseUrls+`/aiws/`+this.randomNumber);
console.log(this.socket, '请求链接');
this.socket.onopen = () => {
console.log('WebSocket连接已打开');
};
// this.socket.onmessage = (event) => {
// const message = event.data;
// console.log(':', message);
// //
// };
this.socket.onerror = (error) => {
console.log('WebSocket连接出错:', error);
let obj = {
type: 'Bot',
content: '',
}
this.$nextTick(() => {
obj.content = '智能助手连接失败,请稍后重试'
this.chatList.push(obj)
this.showLoading = false
this.btnLoading = false
})
};
this.socket.onclose = () => {
console.log('WebSocket连接已关闭');
};
},
disconnectSocket() {
if (this.socket) {
this.socket.close();
this.socket = null;
this.randomNumber = null
}
},
sendMessage(message) {
console.log(this.socket.readyState, WebSocket.OPEN, '俩数');
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
this.socket.send(message);
this.socket.onmessage = (event) => {
const message = JSON.parse(event.data)
console.log('收到服务器的消息:', message);
//
};
} else {
console.log('WebSocket连接未打开,无法发送消息');
}
},
formatDate(data) {
let dataList = data.split('-')
return `${dataList[1]}${dataList[2]}`
@ -405,7 +497,11 @@ export default {
},
aiIt(item) {
this.connectSocket();
this.aimessage = []
this.aimessages = []
this.showLoading = true
this.btnLoading = true
let data = {
time: this.year,
type: item.type
@ -416,7 +512,6 @@ export default {
data.time = this.year + '' + item.time
}
selectDocByType(data).then(res => {
console.log(res, 'iiiii')
this.chatList = []
if (res.code === 200) {
if (item.type == '1') {
@ -434,47 +529,82 @@ export default {
this.opens = true
}
})
let datas = {
message: null,
fileId: res.data.fileId,
dialogId: null
}
let datas = JSON.stringify(
{
message: null,
fileId: res.data.fileId,
dialogId: null,
token: 'Bearer ' + this.token
}
)
let obj = {
type: 'Bot',
content: ''
content: '',
}
chatAi(datas).then(res => {
if (res.code == 200) {
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
this.socket.send(datas);
this.chatList.push(obj)
this.socket.onmessage = (event) => {
const message = JSON.parse(event.data)
console.log('收到服务器的消息:', message);
//
this.$nextTick(() => {
obj.content = res.data.context
this.chatList.push(obj)
this.chatList[this.chatList.length - 1].content += message.context
this.showLoading = false
this.dialogId = res.data.dialogId
this.aimessage.push(message.context)
setTimeout(()=>{
this.aimessages.push(message.context)
},7000)
this.dialogId = message.dialogId
const listElement = this.$refs.messagesList;
setTimeout(() => {
listElement.scrollTop = listElement.scrollHeight;
});
})
} else {
this.$nextTick(() => {
obj.content = '智能助手连接失败,请稍后重试'
this.showLoading = false
this.chatList.push(obj)
})
}
}).catch(err => {
};
} else {
console.log('WebSocket连接未打开,无法发送消息');
this.$nextTick(() => {
obj.content = '智能助手连接失败,请稍后重试'
this.chatList.push(obj)
this.showLoading = false
this.btnLoading = false
})
})
}
// chatAi(datas).then(res => {
// if (res.code == 200) {
// this.$nextTick(() => {
// obj.content = res.data.context
// this.chatList.push(obj)
// this.showLoading = false
// this.dialogId = res.data.dialogId
// const listElement = this.$refs.messagesList;
// setTimeout(() => {
// listElement.scrollTop = listElement.scrollHeight;
// });
// })
// } else {
// this.$nextTick(() => {
// obj.content = ''
// this.showLoading = false
// this.chatList.push(obj)
// })
// }
// }).catch(err => {
// this.$nextTick(() => {
// obj.content = ''
// this.chatList.push(obj)
// this.showLoading = false
// })
// })
}
})
},
sendMessage() {
sendMessages() {
this.showLoading = true
this.btnLoading = true
this.aimessage = []
this.aimessages = []
if (this.message !== '') {
let obj = {
type: 'You',
@ -489,11 +619,14 @@ export default {
});
let datas = {
message: this.message,
fileId: this.fileId,
dialogId: this.dialogId
}
let datas = JSON.stringify(
{
message: this.message,
fileId: this.fileId,
dialogId: this.dialogId,
token: 'Bearer ' + this.token
}
)
//
this.message = '';
@ -502,42 +635,75 @@ export default {
type: 'Bot',
content: ''
}
chatAi(datas).then(res => {
if (res.code == 200) {
this.$nextTick(() => {
objs.content = res.data.context
this.chatList.push(objs)
this.showLoading = false
this.dialogId = res.data.dialogId
})
const listElement = this.$refs.messagesList;
setTimeout(() => {
listElement.scrollTop = listElement.scrollHeight;
});
} else {
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
this.socket.send(datas);
this.chatList.push(objs)
this.socket.onmessage = (event) => {
const message = JSON.parse(event.data)
console.log('收到服务器的消息:', message);
//
this.$nextTick(() => {
objs.content = '智能助手连接失败,请稍后重试'
this.chatList.push(objs)
this.chatList[this.chatList.length - 1].content += message.context
this.showLoading = false
this.aimessage.push(message.context)
setTimeout(()=>{
this.aimessages.push(message.context)
},7000)
this.dialogId = message.dialogId
const listElement = this.$refs.messagesList;
setTimeout(() => {
listElement.scrollTop = listElement.scrollHeight;
});
})
const listElement = this.$refs.messagesList;
setTimeout(() => {
listElement.scrollTop = listElement.scrollHeight;
});
}
}).catch(err => {
};
} else {
console.log('WebSocket连接未打开,无法发送消息');
this.$nextTick(() => {
objs.content = '智能助手连接失败,请稍后重试'
this.chatList.push(objs)
obj.content = '智能助手连接失败,请稍后重试'
this.chatList.push(obj)
this.showLoading = false
this.btnLoading = false
})
const listElement = this.$refs.messagesList;
setTimeout(() => {
listElement.scrollTop = listElement.scrollHeight;
});
})
}
// chatAi(datas).then(res => {
// if (res.code == 200) {
// this.$nextTick(() => {
// objs.content = res.data.context
// this.chatList.push(objs)
// this.showLoading = false
// this.dialogId = res.data.dialogId
// })
// const listElement = this.$refs.messagesList;
// setTimeout(() => {
// listElement.scrollTop = listElement.scrollHeight;
// });
// } else {
// this.$nextTick(() => {
// objs.content = ''
// this.chatList.push(objs)
// this.showLoading = false
//
// })
// const listElement = this.$refs.messagesList;
// setTimeout(() => {
// listElement.scrollTop = listElement.scrollHeight;
// });
// }
// }).catch(err => {
// this.$nextTick(() => {
// objs.content = ''
// this.chatList.push(objs)
// this.showLoading = false
// })
// const listElement = this.$refs.messagesList;
// setTimeout(() => {
// listElement.scrollTop = listElement.scrollHeight;
// });
// })
}
},
closeAi() {
this.disconnectSocket();
}
}
}

Loading…
Cancel
Save