nebula/pages/chats/_account.vue

384 lines
9.8 KiB
Vue
Raw Normal View History

2021-03-25 21:48:05 +08:00
<template>
<client-only>
<el-row class="wrapper" v-if="user">
<el-col style="padding: 0 .5rem 1rem 0;max-height: 38rem;overflow-y: auto;" id="messagesContent">
<el-col v-for="message in messages" :key="message.dataId" style="max-height: 38rem;">
<el-col v-if="message.from === user.account">
<el-col :span="22" style="text-align: right;">
<div class="from-message">
<div v-html="message.content"></div>
</div>
</el-col>
<el-col :span="2" style="text-align: right;">
2023-03-23 11:04:11 +08:00
<el-avatar :src="user.avatarUrl"></el-avatar>
</el-col>
2020-08-10 00:44:27 +08:00
</el-col>
<el-col v-else>
<el-col :span="2">
2023-03-23 11:04:11 +08:00
<el-avatar :src="to.avatarUrl"></el-avatar>
</el-col>
<el-col :span="22" style="text-align: left;">
<div class="to-message">
<div v-html="message.content"></div>
</div>
</el-col>
2020-08-10 00:44:27 +08:00
</el-col>
</el-col>
<el-col v-if="message">
<el-col :span="2">
<el-avatar :src="to.avatarUrl"></el-avatar>
</el-col>
<el-col :span="22" style="text-align: left;">
<div class="to-message">
<div v-html="message"></div>
</div>
</el-col>
</el-col>
</el-col>
<el-col style="position: fixed;bottom: 5.5rem;max-width: 71rem;left: 30rem;">
<el-col :span="20">
<div id="contentEditor"></div>
</el-col>
<el-col :span="4" style="position: fixed;bottom: 6rem;right: 15.5rem;">
<el-button type="primary" :loading="loading" @click="send" plain>发送</el-button>
</el-col>
2020-08-10 00:44:27 +08:00
</el-col>
</el-row>
</client-only>
2020-08-10 00:44:27 +08:00
</template>
<script>
2021-03-25 21:48:05 +08:00
import Vue from 'vue';
import {mapState} from 'vuex';
2022-05-24 11:07:05 +08:00
import apiConfig from '~/config/api.config';
import 'vditor/dist/css/content-theme/light.css';
2020-08-10 00:44:27 +08:00
2021-03-25 21:48:05 +08:00
export default {
name: "Chat",
computed: {
...mapState({
2022-10-27 23:22:46 +08:00
user: state => state.auth.user
2021-03-25 21:48:05 +08:00
})
},
data() {
return {
contentEditor: null,
tokenURL: {
URL: '',
linkToImageURL: '',
token: ''
},
drawer: false,
direction: 'btt',
initEditor: false,
isShow: true,
loading: false,
to: {},
messages: [],
vueSse: null,
customEvents: null,
message: ''
2021-03-25 21:48:05 +08:00
}
},
watch: {
messages(value) {
console.log(value);
}
},
methods: {
_initEditor(data) {
let _ts = this;
2020-10-18 14:43:02 +08:00
2021-03-25 21:48:05 +08:00
let toolbar = [
'emoji',
'headings',
'bold',
'italic',
'strike',
'link',
'|',
'list',
'ordered-list',
'check',
'outdent',
'indent',
'|',
'quote',
'line',
'code',
'inline-code',
'insert-before',
'insert-after',
'|',
'upload',
// 'record',
'table',
'|',
'undo',
'redo',
'|',
'edit-mode',
{
name: 'more',
toolbar: [
'fullscreen',
'both',
'preview',
'info'
],
}]
return new Vue.Vditor(data.id, {
toolbar,
mode: 'ir',
2021-03-25 21:48:05 +08:00
tab: '\t',
2022-05-24 11:07:05 +08:00
cdn: apiConfig.VDITOR,
2021-03-25 21:48:05 +08:00
cache: {
enable: false
2021-03-25 21:48:05 +08:00
},
after() {
_ts.contentEditor.setValue(data.value ? data.value : '');
},
preview: {
hljs: {
enable: true,
lineNumber: true,
style: 'github'
},
2021-03-25 21:48:05 +08:00
markdown: {
toc: true,
autoSpace: true
},
math: {
inlineDigit: true
2020-08-10 00:44:27 +08:00
},
2021-03-25 21:48:05 +08:00
delay: 500,
mode: data.mode,
/*url: `${process.env.Server}/api/console/markdown`,*/
parse: (element) => {
if (element.style.display === 'none') {
return
2020-08-10 00:44:27 +08:00
}
2021-03-25 21:48:05 +08:00
// LazyLoadImage();
// Vue.Vditor.highlightRender({style:'github'}, element, document);
2022-05-24 11:07:05 +08:00
},
theme: {
cdn: apiConfig.VDITOR_CSS
2021-03-25 21:48:05 +08:00
}
},
upload: {
max: 10 * 1024 * 1024,
url: this.tokenURL.URL,
linkToImgUrl: this.tokenURL.linkToImageURL,
token: this.tokenURL.token,
filename: name => name.replace(/[^(a-zA-Z0-9\u4e00-\u9fa5\.)]/g, '').replace(/[\?\\/:|<>\*\[\]\(\)\$%\{\}@~]/g, '').replace('/\\s/g', '')
},
height: data.height,
counter: 102400,
resize: {
enable: data.resize,
},
lang: this.$store.state.locale,
placeholder: data.placeholder,
})
2020-08-10 00:44:27 +08:00
},
2021-03-25 21:48:05 +08:00
async send() {
2020-08-10 00:44:27 +08:00
let _ts = this;
_ts.message = '';
2021-03-25 21:48:05 +08:00
const message = {
to: _ts.to.account,
from: _ts.user.account,
2021-03-25 21:48:05 +08:00
dataType: 1,
2023-07-16 15:47:59 +08:00
role: 'user',
2021-03-25 21:48:05 +08:00
dataId: new Date().getTime(),
content: await _ts.contentEditor.getHTML()
}
2021-03-25 21:48:05 +08:00
_ts.messages.push(message);
_ts.messages.sort((a, b) => {
return a.dataId - b.dataId;
});
2021-03-25 21:48:05 +08:00
_ts.contentEditor.setValue('')
2023-07-16 15:47:59 +08:00
_ts.$axios.$post('/api/openai/new-chat', _ts.messages).then(async res => {
const html = await Vue.Vditor.md2html(_ts.message, {
cdn: apiConfig.VDITOR
});
2023-03-23 11:04:11 +08:00
_ts.messages.push({
to: _ts.user.account,
from: _ts.to.account,
dataType: 1,
2023-07-16 15:47:59 +08:00
role: 'assistant',
2023-03-23 11:04:11 +08:00
dataId: new Date().getTime(),
content: html
2023-03-23 11:04:11 +08:00
});
_ts.messages.sort((a, b) => {
return a.dataId - b.dataId;
});
_ts.message = '';
2023-03-23 11:04:11 +08:00
});
},
close() {
let _ts = this;
//浏览器关闭SSE连接
_ts.vueSse.disconnect();
_ts.$axios.$get(`/api/sse/close/${_ts.user.idUser}`);
},
init() {
//初始化 vue-sse
let _ts = this;
let vueSse = _ts.vueSse;
//监听 message
vueSse.on('message', this.handleMessage);
//监听 customEvents
vueSse.once('customEvents', this.handleCustomEvents);
//里面的 on、once、off 是用了发布订阅模式,
//源码 once 方法这有点小问题,写文章时改了
//源码但还没提PR主要是没提过不会弄
//执行 connect 返回个Promise
vueSse
.connect()
.then((sse) => {
console.log("We're connected!", sse);
})
.catch((err) => console.error('Failed make initial connection:', err));
},
//message回调
handleMessage(res) {
if (typeof res !== "undefined") {
this.message += res;
}
},
//handleCustomEvents回调
handleCustomEvents(res) {
let {data} = res;
this.customEvents = data;
2021-03-25 21:48:05 +08:00
}
},
async mounted() {
let _ts = this;
2023-03-23 11:04:11 +08:00
_ts.$store.commit('setActiveMenu', 'chat');
2021-03-25 21:48:05 +08:00
let to = {
2023-03-23 11:04:11 +08:00
account: _ts.$route.params?.account,
avatarUrl: 'https://static.rymcu.com/article/1679539451459.jpg'
2021-03-25 21:48:05 +08:00
}
2021-03-25 21:48:05 +08:00
_ts.$set(_ts, 'to', to);
2020-08-10 00:44:27 +08:00
2021-03-25 21:48:05 +08:00
if (_ts.user) {
const responseData = await _ts.$axios.$get('/api/upload/token');
if (responseData) {
_ts.$set(_ts, 'tokenURL', {
token: responseData.uploadToken || '',
URL: responseData.uploadURL || '',
linkToImageURL: responseData.linkToImageURL || ''
})
2020-08-10 00:44:27 +08:00
}
2023-03-23 11:04:11 +08:00
const message = {
to: _ts.user.account,
from: _ts.to.account,
dataType: 1,
2023-07-16 15:47:59 +08:00
role: 'assistant',
2023-03-23 11:04:11 +08:00
dataId: new Date().getTime(),
content: '伟大的"坦格利安家族的风暴降生丹妮莉丝 · 铁王座的合法继承人 · 安达尔人和先民的合法女王 · 七国的守护者 · 草海上的卡丽熙 · 不焚者 · 解放者 · 傲之追猎者 · 悠米"为你服务'
}
_ts.messages.push(message);
_ts.vueSse = _ts.$sse.create({
url: `/api/sse/subscribe/${_ts.user.idUser}`,
format: 'json',
withCredentials: true
});
this.init();
2020-08-10 00:44:27 +08:00
}
2021-03-25 21:48:05 +08:00
if (!_ts.initEditor) {
_ts.$set(_ts, 'initEditor', true);
setTimeout(function () {
_ts.contentEditor = _ts._initEditor({
id: 'contentEditor',
mode: 'both',
height: 160,
2021-03-25 21:48:05 +08:00
placeholder: '', //this.$t('inputContent', this.$store.state.locale)
resize: false,
value: ''
});
}, 500);
}
},
sse: {
//配置后自动添加断开连接事件,源码里面是做了判断,
//然后加在组件 beforeDestroy 生命周期里
cleanup: true,
},
beforeDestroy() {
this.close();
2020-08-10 00:44:27 +08:00
}
2021-03-25 21:48:05 +08:00
}
2020-08-10 00:44:27 +08:00
</script>
<style lang="less">
2023-03-23 11:04:11 +08:00
@import "~vditor/src/assets/less/index.less";
2020-08-10 00:44:27 +08:00
2021-03-25 21:48:05 +08:00
.from-message {
float: right;
width: auto;
min-height: 40px;
margin: 10px;
background-color: skyblue;
border-bottom-color: skyblue;
/*为了给after伪元素自动继承*/
color: #000;
font-size: 14px;
2021-03-25 21:48:05 +08:00
line-height: 18px;
padding: 5px 12px 5px 12px;
box-sizing: border-box;
border-radius: 6px;
position: relative;
word-break: break-all;
}
2020-08-10 00:44:27 +08:00
2021-03-25 21:48:05 +08:00
.to-message {
float: left;
width: auto;
min-height: 40px;
margin: 10px;
background-color: skyblue;
border-bottom-color: skyblue;
/*为了给after伪元素自动继承*/
color: #000;
font-size: 14px;
2021-03-25 21:48:05 +08:00
line-height: 18px;
padding: 5px 12px 5px 12px;
box-sizing: border-box;
border-radius: 6px;
position: relative;
word-break: break-all;
}
2020-08-10 00:44:27 +08:00
2021-03-25 21:48:05 +08:00
.from-message::after {
content: '';
position: absolute;
top: 50%;
right: -5px;
width: 10px;
height: 10px;
margin-top: -5px;
background: inherit;
/*自动继承父元素的背景*/
transform: rotate(45deg);
}
2020-08-10 00:44:27 +08:00
2021-03-25 21:48:05 +08:00
/** 通过对小正方形旋转45度解决 **/
.to-message::before {
content: '';
position: absolute;
top: 50%;
left: -5px;
width: 10px;
height: 10px;
margin-top: -5px;
background: inherit;
/*自动继承父元素的背景*/
transform: rotate(45deg);
}
2020-08-10 00:44:27 +08:00
</style>