Refactor(AI智能):采取分包
This commit is contained in:
@@ -0,0 +1,277 @@
|
||||
<template>
|
||||
<view
|
||||
:class="{
|
||||
'focus': inputOnFocus
|
||||
}"
|
||||
class="fix-page-bottom"
|
||||
>
|
||||
<view v-if="startTaskFlag" class="music-wrap">
|
||||
<view class="item one" />
|
||||
<view class="item two" />
|
||||
<view class="item three" />
|
||||
<view class="item four" />
|
||||
<view class="item five" />
|
||||
<view class="item six" />
|
||||
<view class="item seven" />
|
||||
</view>
|
||||
<view v-if="inputModel === 'input'" class="bottom-body flex-center-between">
|
||||
<view class="history-wrap" @click="historyViewHandle">
|
||||
历史
|
||||
</view>
|
||||
<view class="input-container">
|
||||
<!-- 输入模式切换 -->
|
||||
<view
|
||||
class="input-model"
|
||||
@click="toggleInputModel"
|
||||
>
|
||||
<image
|
||||
:src="webUrl + '/aiChat/microphone.png'"
|
||||
class="icon"
|
||||
mode="widthFix"
|
||||
/>
|
||||
</view>
|
||||
<!-- 输入框 -->
|
||||
<view class="input-wrap flex-center-between">
|
||||
<input
|
||||
v-model.trim="prompt"
|
||||
:show-confirm-bar="false"
|
||||
:disabled="loading"
|
||||
:maxlength="-1"
|
||||
class="inp"
|
||||
@confirm="sendHandle"
|
||||
@focus="promptInputFocusHandle"
|
||||
@blur="promptInputBlurHandle"
|
||||
/>
|
||||
<view v-if="!prompt && !inputOnFocus" class="placeholder-txt">发消息...</view>
|
||||
</view>
|
||||
<view
|
||||
class="send-btn"
|
||||
@click="sendHandle"
|
||||
>
|
||||
<image
|
||||
:src="webUrl + '/aiChat/send.png'"
|
||||
class="icon"
|
||||
mode="widthFix"
|
||||
/>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<!-- 语音输入 -->
|
||||
<view
|
||||
v-if="inputModel === 'sound'"
|
||||
class="sound-input-wrap"
|
||||
@touchstart="startRecord"
|
||||
@touchend="stopRecord"
|
||||
>
|
||||
<image
|
||||
:src="webUrl + '/icon-20.png'"
|
||||
class="icon"
|
||||
mode="widthFix"
|
||||
/>
|
||||
按住说话
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import {
|
||||
makeFileTransTask,
|
||||
getFileTransResult
|
||||
} from '@/api/voiceToText/index'
|
||||
import { VUE_APP_API_URL } from '@/config'
|
||||
import settings from '@/config/baseSetting.js'
|
||||
import cookie from '@/utils/store/cookie'
|
||||
export default {
|
||||
name: 'BottomSend',
|
||||
props: {
|
||||
loading: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
token: cookie.get('login_status'),
|
||||
userInfo: cookie.get('userInfo'),
|
||||
webUrl: this.$VUE_APP_RESOURCES_URL,
|
||||
// 输入模式:input-键盘,sound-语音
|
||||
inputModel: 'input',
|
||||
prompt: '',
|
||||
recordManager: null,
|
||||
// 录音权限是否开通
|
||||
recordPermission: false,
|
||||
taskId: '',
|
||||
taskTimer: null,
|
||||
startTaskFlag: false,
|
||||
// 输入框是否聚焦
|
||||
inputOnFocus: false
|
||||
}
|
||||
},
|
||||
beforeDestroy() {
|
||||
this.clearIntervalFn()
|
||||
},
|
||||
created() {
|
||||
uni.getSetting({
|
||||
success: res => {
|
||||
if (res.authSetting['scope.record']) {
|
||||
this.recordPermission = true
|
||||
}
|
||||
}
|
||||
})
|
||||
},
|
||||
methods: {
|
||||
promptInputFocusHandle() {
|
||||
this.inputOnFocus = true
|
||||
},
|
||||
promptInputBlurHandle() {
|
||||
this.inputOnFocus = false
|
||||
},
|
||||
toggleInputModel() {
|
||||
if (!this.token) {
|
||||
uni.reLaunch({ url: '/pages/auth/login' })
|
||||
return
|
||||
}
|
||||
if (this.loading) return
|
||||
if (this.inputModel === 'input') {
|
||||
uni.getSetting({
|
||||
success: res => {
|
||||
if (!res.authSetting['scope.record']) {
|
||||
uni.authorize({
|
||||
scope: 'scope.record',
|
||||
success: () => {
|
||||
console.log('获取录音权限成功')
|
||||
this.recordPermission = true
|
||||
this.inputModel = 'sound'
|
||||
},
|
||||
fail: () => {
|
||||
console.log('获取录音权限失败')
|
||||
this.recordPermission = false
|
||||
}
|
||||
})
|
||||
} else {
|
||||
this.recordPermission = true
|
||||
this.inputModel = 'sound'
|
||||
}
|
||||
}
|
||||
})
|
||||
return
|
||||
}
|
||||
if (this.inputModel === 'sound') {
|
||||
this.clearIntervalFn()
|
||||
this.inputModel = 'input'
|
||||
return
|
||||
}
|
||||
},
|
||||
startRecord() {
|
||||
if (this.loading) {
|
||||
this.$toast('正在检索')
|
||||
return
|
||||
}
|
||||
// 正在识别,不触发相关动作
|
||||
if (this.startTaskFlag || this.taskTimer) {
|
||||
return
|
||||
}
|
||||
this.recordManager = uni.getRecorderManager()
|
||||
console.log('启动录音')
|
||||
this.startTaskFlag = true
|
||||
this.recordManager.start({
|
||||
duration: 60 * 1000,
|
||||
frameSize: 1,
|
||||
sampleRate: 16000,
|
||||
format: 'wav'
|
||||
})
|
||||
},
|
||||
stopRecord() {
|
||||
const _this = this
|
||||
// 正在识别,不触发相关动作
|
||||
if (this.taskTimer) {
|
||||
return
|
||||
}
|
||||
if (_this.recordManager) {
|
||||
_this.recordManager.stop()
|
||||
console.log('停止录音')
|
||||
_this.startTaskFlag = false
|
||||
_this.recordManager.onStop((res) => {
|
||||
console.log('录音结束------------')
|
||||
console.log(res)
|
||||
const { tempFilePath, fileSize, duration } = res
|
||||
if (tempFilePath && fileSize && duration) {
|
||||
_this.taskId = ''
|
||||
_this.clearIntervalFn()
|
||||
uni.uploadFile({
|
||||
url: `${VUE_APP_API_URL + settings.sysPrefix}/common/file/local/upload`,
|
||||
filePath: tempFilePath,
|
||||
header: {
|
||||
'client-id': 'app',
|
||||
Authorization: 'Bearer ' + (_this.token || '')
|
||||
},
|
||||
name: 'file',
|
||||
success: uploadRes => {
|
||||
console.log(JSON.parse(uploadRes.data))
|
||||
_this.renderTransTask(uploadRes)
|
||||
},
|
||||
fail: err => {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
},
|
||||
renderTransTask(uploadRes) {
|
||||
const _this = this
|
||||
makeFileTransTask({
|
||||
file: `${VUE_APP_API_URL + settings.sysPrefix}` + JSON.parse(uploadRes.data).data.fileUrl
|
||||
}).then(taskRes => {
|
||||
_this.taskId = taskRes.data
|
||||
_this.getTransResultReq()
|
||||
})
|
||||
},
|
||||
getTransResultReq() {
|
||||
const _this = this
|
||||
uni.showLoading({ title: '识别中...' })
|
||||
_this.taskTimer = setInterval(() => {
|
||||
getFileTransResult({
|
||||
taskId: _this.taskId
|
||||
}).then(res => {
|
||||
const { success, data = [] } = res
|
||||
if (success && data) {
|
||||
uni.hideLoading()
|
||||
_this.clearIntervalFn()
|
||||
if (data === '__NULL__') {
|
||||
_this.$toast('豁然没有听清您说了什么,请再说一遍')
|
||||
} else {
|
||||
if (data.length > 0) {
|
||||
const prompt = data[0].Text
|
||||
if (prompt) {
|
||||
_this.$emit('send', { prompt: prompt.replace('。', '') })
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}, 1000)
|
||||
},
|
||||
clearIntervalFn() {
|
||||
this.taskTimer && clearInterval(this.taskTimer)
|
||||
this.taskTimer = null
|
||||
setTimeout(() => {
|
||||
this.startTaskFlag = false
|
||||
}, 300)
|
||||
},
|
||||
sendHandle() {
|
||||
if (this.loading) return
|
||||
if (!this.prompt) {
|
||||
this.$toast('请输入您想问的内容')
|
||||
return
|
||||
}
|
||||
this.$emit('send', { prompt: this.prompt })
|
||||
this.prompt = ''
|
||||
this.inputOnFocus = false
|
||||
},
|
||||
historyViewHandle() {
|
||||
this.$emit('history')
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -0,0 +1,156 @@
|
||||
<template>
|
||||
<view
|
||||
:class="{
|
||||
'show': showLayer
|
||||
}"
|
||||
class="history-wrap"
|
||||
>
|
||||
<view class="layer" @click="closeLayer" />
|
||||
<view
|
||||
:style="{
|
||||
'padding-top': statusBarHeight + 'px'
|
||||
}"
|
||||
class="history-container"
|
||||
>
|
||||
<view class="history-type">
|
||||
<view class="title">
|
||||
<view class="txt">30天内</view>
|
||||
<image
|
||||
:src="webUrl + '/aiChat/delete.png'"
|
||||
class="icon"
|
||||
mode="widthFix"
|
||||
/>
|
||||
</view>
|
||||
<view class="list-cont">
|
||||
<view
|
||||
v-for="(item, index) in list"
|
||||
:key="index"
|
||||
class="item"
|
||||
>
|
||||
<view class="item-txt one-t">
|
||||
{{ item.title }}
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import {
|
||||
getChatList
|
||||
} from '@/api/chat/index'
|
||||
export default {
|
||||
name: 'AiHistoryList',
|
||||
props: {
|
||||
statusBarHeight: {
|
||||
type: Number,
|
||||
default: 0
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
webUrl: this.$VUE_APP_RESOURCES_URL,
|
||||
showLayer: false,
|
||||
list: [],
|
||||
moreList: []
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
showView() {
|
||||
this.showLayer = true
|
||||
getChatList().then(res => {
|
||||
const { success, data } = res
|
||||
if (success) {
|
||||
this.list = data['今天']
|
||||
this.moreList = data['超过30天']
|
||||
}
|
||||
})
|
||||
},
|
||||
closeLayer() {
|
||||
this.showLayer = false
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style scoped lang="less">
|
||||
.history-wrap {
|
||||
position: fixed;
|
||||
left: 0;
|
||||
top: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
z-index: 1001;
|
||||
transform: translateX(-100%);
|
||||
opacity: 0;
|
||||
transition: all 0.3s;
|
||||
.layer {
|
||||
position: absolute;
|
||||
top: 0;
|
||||
left: 0;
|
||||
right: 0;
|
||||
bottom: 0;
|
||||
z-index: 2;
|
||||
background-color: rgba(0, 0, 0, 0.5);
|
||||
}
|
||||
.history-container {
|
||||
position: absolute;
|
||||
left: 0;
|
||||
top: 0;
|
||||
bottom: 0;
|
||||
z-index: 5;
|
||||
width: 75%;
|
||||
padding: 40rpx;
|
||||
box-sizing: border-box;
|
||||
border-top-right-radius: 60rpx;
|
||||
border-bottom-right-radius: 60rpx;
|
||||
overflow-y: auto;
|
||||
background-color: #fff;
|
||||
.history-type + .history-type {
|
||||
padding: 40rpx 0 0 0;
|
||||
}
|
||||
.history-type {
|
||||
.title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
.txt {
|
||||
font-size: 34rpx;
|
||||
line-height: 50rpx;
|
||||
}
|
||||
.icon {
|
||||
display: block;
|
||||
width: 40rpx;
|
||||
height: 40rpx;
|
||||
}
|
||||
}
|
||||
.list-cont {
|
||||
padding: 24rpx 0 0 0;
|
||||
.item + .item {
|
||||
margin: 12rpx 0 0 0;
|
||||
}
|
||||
.item {
|
||||
.item-txt {
|
||||
display: inline-block;
|
||||
max-width: 100%;
|
||||
height: 60rpx;
|
||||
padding: 0 24rpx;
|
||||
box-sizing: border-box;
|
||||
border-radius: 30rpx;
|
||||
color: #333;
|
||||
font-size: 28rpx;
|
||||
line-height: 60rpx;
|
||||
background-color: #F1F2FF;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
&.show {
|
||||
opacity: 1;
|
||||
transform: translateX(0);
|
||||
}
|
||||
}
|
||||
</style>
|
||||
@@ -0,0 +1,289 @@
|
||||
import {
|
||||
getSseConfigV1,
|
||||
getCompletions,
|
||||
deleteChatItemByConversationId
|
||||
} from '@/api/chat/index'
|
||||
import { VUE_APP_API_URL } from '@/config'
|
||||
import settings from '@/config/baseSetting.js'
|
||||
import { formatAiMsgContent } from '../utils/aiChat'
|
||||
import cookie from '@/utils/store/cookie'
|
||||
import { handleLoginFailure } from '@/utils'
|
||||
export const chatMixins = {
|
||||
data() {
|
||||
return {
|
||||
token: cookie.get('login_status'),
|
||||
userInfo: cookie.get('userInfo'),
|
||||
webUrl: this.$VUE_APP_RESOURCES_URL,
|
||||
scrollData: {},
|
||||
scrollTop: 0,
|
||||
isScrollToBottom: false,
|
||||
// 状态栏高度
|
||||
statusBarHeight: 20,
|
||||
fileHttpStr: Object.freeze(VUE_APP_API_URL + settings.sysPrefix),
|
||||
optionsFrom: '',
|
||||
loading: false,
|
||||
chatNumber: '',
|
||||
infoResData: {},
|
||||
chatMessageList: [
|
||||
/*
|
||||
{
|
||||
type: -2,
|
||||
isError: false,
|
||||
showCursor: false,
|
||||
conversationId: '',
|
||||
prompt: '',
|
||||
nodes: '-------------监听 WebSocket 接受到服务器的消息事件',
|
||||
content: '-------------监听 WebSocket 接受到服务器的消息事件',
|
||||
listQuestion: [
|
||||
'监听 WebSocket 接受到服务器的消息事件',
|
||||
'监听 WebSocket 接受到服务器的消息事件',
|
||||
'监听 WebSocket 接受到服务器的消息事件'
|
||||
]
|
||||
}
|
||||
*/
|
||||
],
|
||||
errorMsg: Object.freeze('您提的问题未能理解,云灵思想跑偏啦,重新整理一下问题或者重新提问,让云灵再试试!')
|
||||
}
|
||||
},
|
||||
computed: {
|
||||
chatMessageListLength() {
|
||||
return this.chatMessageList.length
|
||||
}
|
||||
},
|
||||
onPageScroll(e) {
|
||||
this.scrollData = e
|
||||
const scrollTop = e.scrollTop
|
||||
this.scrollTop = scrollTop
|
||||
const query = wx.createSelectorQuery().in(this)
|
||||
query.select('#fixTbabarBody').boundingClientRect(function(rect) {
|
||||
const contentHeight = rect.height
|
||||
const windowInfo = wx.getWindowInfo()
|
||||
const windowHeight = windowInfo.windowHeight
|
||||
if (scrollTop + windowHeight + 30 >= contentHeight) {
|
||||
this.isScrollToBottom = true
|
||||
} else {
|
||||
this.isScrollToBottom = false
|
||||
}
|
||||
}.bind(this)).exec()
|
||||
},
|
||||
created() {
|
||||
const _this = this
|
||||
uni.getSystemInfo({
|
||||
success: (e) => {
|
||||
let statusBar = 0
|
||||
// #ifdef MP-WEIXIN
|
||||
statusBar = e.statusBarHeight
|
||||
const custom = uni.getMenuButtonBoundingClientRect()
|
||||
_this.rightDistance = e.windowWidth - custom.left + 10
|
||||
// #endif
|
||||
// 状态栏高度
|
||||
_this.statusBarHeight = statusBar
|
||||
}
|
||||
})
|
||||
_this.createdCallbak()
|
||||
},
|
||||
methods: {
|
||||
createdCallbak() {
|
||||
console.log('页面初始话回调')
|
||||
},
|
||||
copyContentHandle(data) {
|
||||
uni.setClipboardData({
|
||||
data,
|
||||
success: () =>
|
||||
uni.showToast({
|
||||
title: '已复制'
|
||||
})
|
||||
})
|
||||
},
|
||||
shareHandle() {
|
||||
wx.showShareMenu({
|
||||
withShareTicket: true,
|
||||
menus: ['shareAppMessage', 'shareTimeline'],
|
||||
success: (res) => {
|
||||
console.log(res)
|
||||
},
|
||||
fail: err => {
|
||||
console.log(err)
|
||||
}
|
||||
})
|
||||
},
|
||||
scrollToBottomHandle() {
|
||||
uni.pageScrollTo({
|
||||
duration: 100,
|
||||
selector: '#bottom_postion'
|
||||
})
|
||||
this.isScrollToBottom = true
|
||||
},
|
||||
// 重新生成
|
||||
regenerateHandle(item) {
|
||||
deleteChatItemByConversationId(item.conversationId).then(res => {
|
||||
const { success } = res
|
||||
if (success) {
|
||||
this.chatMessageList.splice(this.chatMessageListLength - 1, 1)
|
||||
this.streamReq({
|
||||
prompt: item.prompt,
|
||||
init: 1
|
||||
})
|
||||
}
|
||||
})
|
||||
},
|
||||
// 建议提问
|
||||
suggestionItemClickHandle(question) {
|
||||
this.streamReq({
|
||||
prompt: question,
|
||||
init: 0
|
||||
})
|
||||
},
|
||||
// 大家都在问
|
||||
topicItemClickHandle(question) {
|
||||
this.streamReq({
|
||||
prompt: question,
|
||||
init: 0
|
||||
})
|
||||
},
|
||||
streamReq(params) {
|
||||
const _this = this
|
||||
// 需要校验登录状态
|
||||
if (!_this.token) {
|
||||
handleLoginFailure()
|
||||
return
|
||||
}
|
||||
_this.initWebSocket(() => {
|
||||
const prompt = params.prompt
|
||||
if (params.init !== 1) {
|
||||
_this.chatMessageList.push({
|
||||
prompt,
|
||||
type: -1
|
||||
})
|
||||
}
|
||||
_this.$nextTick(() => {
|
||||
_this.scrollToBottomHandle()
|
||||
})
|
||||
_this.chatMessageList.push({
|
||||
content: '',
|
||||
nodes: '',
|
||||
isError: false,
|
||||
showCursor: false,
|
||||
conversationId: '',
|
||||
prompt,
|
||||
listQuestion: [],
|
||||
type: -2
|
||||
})
|
||||
_this.loading = true
|
||||
_this.showCursor = false
|
||||
getSseConfigV1({
|
||||
'chatNumber': _this.chatNumber,
|
||||
prompt
|
||||
}).then(resConfig => {
|
||||
const conversationId = resConfig.data.conversationId
|
||||
_this.chatNumber = resConfig.data.chatNumber || ''
|
||||
_this.chatMessageList[_this.chatMessageListLength - 1].showCursor = true
|
||||
_this.chatMessageList[_this.chatMessageListLength - 1].conversationId = conversationId
|
||||
getCompletions({
|
||||
conversationId,
|
||||
ws: true,
|
||||
isNoNeedPublicErrorNotification: 1
|
||||
}).then(sseRes => {
|
||||
console.log(sseRes)
|
||||
// _this.closeWsFn()
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
_this.chatMessageList[_this.chatMessageListLength - 1].isError = true
|
||||
_this.chatMessageList[_this.chatMessageListLength - 1].content = _this.errorMsg
|
||||
_this.chatMessageList[_this.chatMessageListLength - 1].nodes = _this.errorMsg
|
||||
_this.closeWsFn()
|
||||
})
|
||||
}).catch(err => {
|
||||
console.log(err)
|
||||
_this.chatMessageList[_this.chatMessageListLength - 1].isError = true
|
||||
_this.chatMessageList[_this.chatMessageListLength - 1].content = _this.errorMsg
|
||||
_this.chatMessageList[_this.chatMessageListLength - 1].nodes = _this.errorMsg
|
||||
_this.closeWsFn()
|
||||
})
|
||||
})
|
||||
},
|
||||
closeWsFn() {
|
||||
this.loading = false
|
||||
this.chatMessageList[this.chatMessageListLength - 1].showCursor = false
|
||||
uni.closeSocket({
|
||||
success: () => {
|
||||
console.log('-----关闭连接')
|
||||
}
|
||||
})
|
||||
this.$nextTick(() => {
|
||||
this.scrollToBottomHandle()
|
||||
})
|
||||
},
|
||||
initWebSocket(cb) {
|
||||
const _this = this
|
||||
console.log('------------------创建连接')
|
||||
uni.connectSocket({
|
||||
url: VUE_APP_API_URL.replace('https', 'wss') + settings.sysPrefix + `v1/chat/websocket/${_this.userInfo.unionId}`,
|
||||
header: {
|
||||
'content-type': 'application/json'
|
||||
},
|
||||
timeout: 10 * 1000,
|
||||
success: () => {
|
||||
console.log('------------WebSocket初始化成功')
|
||||
},
|
||||
fail: (err) => {
|
||||
uni.showModal({
|
||||
showCancel: false,
|
||||
content: err
|
||||
})
|
||||
}
|
||||
})
|
||||
uni.onSocketMessage(res => {
|
||||
_this.websocketOnmessage(res)
|
||||
})
|
||||
uni.onSocketOpen(() => {
|
||||
cb && cb()
|
||||
_this.websocketOnopen()
|
||||
})
|
||||
uni.onSocketError(err => {
|
||||
_this.websocketOnerror(err)
|
||||
})
|
||||
},
|
||||
websocketOnmessage(res) {
|
||||
console.log('-------------监听 WebSocket 接受到服务器的消息事件')
|
||||
const resData = JSON.parse(res.data)
|
||||
if (resData) {
|
||||
const message = resData.message
|
||||
if (message && this.chatMessageListLength > 1) {
|
||||
const mesContent = message.content
|
||||
this.chatMessageList[this.chatMessageListLength - 1].nodes = formatAiMsgContent(mesContent)
|
||||
this.chatMessageList[this.chatMessageListLength - 1].content = mesContent
|
||||
this.chatMessageList[this.chatMessageListLength - 1].listQuestion = message.listQuestion || []
|
||||
this.$forceUpdate()
|
||||
this.$nextTick(() => {
|
||||
this.scrollToBottomHandle()
|
||||
if (message.finish) {
|
||||
this.closeWsFn()
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
},
|
||||
websocketOnopen() {
|
||||
console.log('------------------监听 WebSocket 连接打开事件')
|
||||
// this.websocketSend(JSON.stringify({ msg: '1' }))
|
||||
},
|
||||
websocketOnerror(e) {
|
||||
console.log('-----------------监听 WebSocket 错误事件')
|
||||
console.log(e)
|
||||
},
|
||||
websocketSend(msg) {
|
||||
// 数据发送
|
||||
try {
|
||||
uni.sendSocketMessage({
|
||||
data: msg,
|
||||
success: () => {
|
||||
console.log('-----------发送消息成功了')
|
||||
}
|
||||
})
|
||||
} catch (err) {
|
||||
console.log('send failed (' + err.code + ')')
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
// 引入markdown-it库
|
||||
import MarkdownIt from './/markdown-it.min.js'
|
||||
// hljs是由 Highlight.js 经兼容性修改后的文件,请勿直接升级。否则会造成uni-app-vue3-Android下有兼容问题
|
||||
import hljs from './highlight/highlight-uni.min.js'
|
||||
|
||||
// 初始化 MarkdownIt库
|
||||
const markdownIt = MarkdownIt({
|
||||
// 在源码中启用 HTML 标签
|
||||
html: true,
|
||||
// 如果结果以 <pre ... 开头,内部包装器则会跳过。
|
||||
highlight: function(str, lang) {
|
||||
// if (lang && hljs.getLanguage(lang)) {
|
||||
// console.error('lang', lang)
|
||||
// try {
|
||||
// return '<pre class="hljs" style="padding: 5px 8px;margin: 5px 0;overflow: auto;display: block;"><code>' +
|
||||
// hljs.highlight('lang', str, true).value +
|
||||
// '</code></pre>';
|
||||
// } catch (__) {}
|
||||
// }
|
||||
// 经过highlight.js处理后的html
|
||||
let preCode = ''
|
||||
try {
|
||||
preCode = hljs.highlightAuto(str).value
|
||||
} catch (err) {
|
||||
preCode = markdownIt.utils.escapeHtml(str)
|
||||
}
|
||||
|
||||
// 以换行进行分割
|
||||
const lines = preCode.split(/\n/).slice(0, -1)
|
||||
// 添加自定义行号
|
||||
let html = lines.map((item, index) => {
|
||||
// 去掉空行
|
||||
if (item === '') {
|
||||
return ''
|
||||
}
|
||||
return '<li><span class="line-num" data-line="' + (index + 1) + '"></span>' + item + '</li>'
|
||||
}).join('')
|
||||
html = '<ol style="padding: 0px 30px;">' + html + '</ol>'
|
||||
let htmlCode = `<div style="background:#0d1117;margin-top: 5px;color: #888;padding:5px 0;border-radius: 5px;">`
|
||||
htmlCode += `<pre class="hljs" style="padding:0 8px;margin-bottom:5px;overflow: auto;display: block;border-radius: 5px;"><code>${html}</code></pre>`
|
||||
htmlCode += '</div>'
|
||||
return htmlCode
|
||||
}
|
||||
})
|
||||
|
||||
export function formatAiMsgContent(val) {
|
||||
if (!val) {
|
||||
return ''
|
||||
}
|
||||
let htmlString = ''
|
||||
// 修改转换结果的htmlString值 用于正确给界面增加鼠标闪烁的效果
|
||||
// 判断markdown中代码块标识符的数量是否为偶数
|
||||
if (val.split('```').length % 2) {
|
||||
let msgContent = val
|
||||
if (msgContent[msgContent.length - 1] !== '\n') {
|
||||
msgContent += '\n'
|
||||
}
|
||||
msgContent += ' <span class="cursor">|</span>'
|
||||
htmlString = markdownIt.render(msgContent)
|
||||
} else {
|
||||
htmlString = markdownIt.render(val)
|
||||
htmlString = markdownIt.render(val) + ' \n <span class="cursor">|</span>'
|
||||
}
|
||||
// console.log(htmlString)
|
||||
return htmlString
|
||||
}
|
||||
|
||||
/**
|
||||
* 简单实现防抖方法
|
||||
*
|
||||
* 防抖(debounce)函数在第一次触发给定的函数时,不立即执行函数,而是给出一个期限值(delay),比如100ms。
|
||||
* 如果100ms内再次执行函数,就重新开始计时,直到计时结束后再真正执行函数。
|
||||
* 这样做的好处是如果短时间内大量触发同一事件,只会执行一次函数。
|
||||
*
|
||||
* @param fn 要防抖的函数
|
||||
* @param delay 防抖的毫秒数
|
||||
* @returns {Function}
|
||||
*/
|
||||
export function simpleDebounce(fn, delay = 100) {
|
||||
let timer = null
|
||||
return function() {
|
||||
const args = arguments
|
||||
if (timer) {
|
||||
clearTimeout(timer)
|
||||
}
|
||||
timer = setTimeout(() => {
|
||||
fn.apply(null, args)
|
||||
}, delay)
|
||||
}
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
pre code.hljs{display:block;overflow-x:auto;padding:1em}code.hljs{padding:3px 5px}/*!
|
||||
Theme: GitHub Dark
|
||||
Description: Dark theme as seen on github.com
|
||||
Author: github.com
|
||||
Maintainer: @Hirse
|
||||
Updated: 2021-05-15
|
||||
|
||||
Outdated base version: https://github.com/primer/github-syntax-dark
|
||||
Current colors taken from GitHub's CSS
|
||||
*/.hljs{color:#c9d1d9;background:#0d1117}.hljs-doctag,.hljs-keyword,.hljs-meta .hljs-keyword,.hljs-template-tag,.hljs-template-variable,.hljs-type,.hljs-variable.language_{color:#ff7b72}.hljs-title,.hljs-title.class_,.hljs-title.class_.inherited__,.hljs-title.function_{color:#d2a8ff}.hljs-attr,.hljs-attribute,.hljs-literal,.hljs-meta,.hljs-number,.hljs-operator,.hljs-selector-attr,.hljs-selector-class,.hljs-selector-id,.hljs-variable{color:#79c0ff}.hljs-meta .hljs-string,.hljs-regexp,.hljs-string{color:#a5d6ff}.hljs-built_in,.hljs-symbol{color:#ffa657}.hljs-code,.hljs-comment,.hljs-formula{color:#8b949e}.hljs-name,.hljs-quote,.hljs-selector-pseudo,.hljs-selector-tag{color:#7ee787}.hljs-subst{color:#c9d1d9}.hljs-section{color:#1f6feb;font-weight:700}.hljs-bullet{color:#f2cc60}.hljs-emphasis{color:#c9d1d9;font-style:italic}.hljs-strong{color:#c9d1d9;font-weight:700}.hljs-addition{color:#aff5b4;background-color:#033a16}.hljs-deletion{color:#ffdcd7;background-color:#67060c}
|
||||
+5256
File diff suppressed because one or more lines are too long
Vendored
+2
File diff suppressed because one or more lines are too long
@@ -0,0 +1,387 @@
|
||||
<template>
|
||||
<view
|
||||
:class="scrollTop > 0 ? 'page-scroll' : ''"
|
||||
class="fix-tabbar-page"
|
||||
>
|
||||
<hx-navbar
|
||||
:back="true"
|
||||
:fixed="true"
|
||||
:statusBar="true"
|
||||
:pageScroll.sync="scrollData"
|
||||
color="#333"
|
||||
transparent="auto"
|
||||
barPlaceholder="hidden"
|
||||
title="云灵AI智能助手"
|
||||
/>
|
||||
<view
|
||||
id="fixTbabarBody"
|
||||
:style="{
|
||||
'padding-top': (44 + statusBarHeight) + 'px'
|
||||
}"
|
||||
class="fix-tabbar-body"
|
||||
>
|
||||
<block v-if="chatMessageListLength < 1">
|
||||
<view class="home-focus-wrap">
|
||||
<view class="focus-guide-wrap">
|
||||
<view class="guide-left">
|
||||
<image
|
||||
:src="webUrl + '/aiChat/logo-01.gif'"
|
||||
class="avatar"
|
||||
mode="widthFix"
|
||||
/>
|
||||
</view>
|
||||
<view class="guide-right">
|
||||
<view class="title">{{ settingInfo.helloMessageTitle }}</view>
|
||||
<view class="content">{{ settingInfo.helloMessageContent }}</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<view class="topic-wrap">
|
||||
<view class="title">
|
||||
<image
|
||||
:src="webUrl + '/aiChat/icon-01.png'"
|
||||
class="icon"
|
||||
mode="widthFix"
|
||||
/>
|
||||
<view class="txt">#大家都在问</view>
|
||||
<image
|
||||
:src="webUrl + '/aiChat/icon-01.png'"
|
||||
class="icon"
|
||||
mode="widthFix"
|
||||
/>
|
||||
</view>
|
||||
<view class="list-wrap">
|
||||
<view class="list-cont">
|
||||
<view
|
||||
v-for="(item, index) in topicList"
|
||||
:key="index"
|
||||
class="list-item"
|
||||
@click="topicItemClickHandle(item.title)"
|
||||
>
|
||||
<image
|
||||
v-if="item.isHot"
|
||||
:src="webUrl + '/aiChat/hot.png'"
|
||||
class="icon"
|
||||
mode="widthFix"
|
||||
/>
|
||||
{{ item.title }}
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</block>
|
||||
<view class="chat-wrap">
|
||||
<view v-if="chatMessageListLength > 0" class="chat-list-wrap">
|
||||
<view
|
||||
v-for="(item, index) in chatMessageList"
|
||||
:key="index"
|
||||
class="chat-message-item"
|
||||
>
|
||||
<view v-if="item.type === -1" class="chat-message-body">
|
||||
<view class="item-right">
|
||||
<view class="txt">
|
||||
{{ item.prompt }}
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<view v-if="item.type === -2" class="chat-message-body">
|
||||
<view class="avatar">
|
||||
<image
|
||||
:src="webUrl + '/aiChat/avatar.png'"
|
||||
class="chat-avatar"
|
||||
mode="widthFix"
|
||||
/>
|
||||
</view>
|
||||
<view class="item-left">
|
||||
<view class="txt">
|
||||
<view
|
||||
ref="rich-text-box"
|
||||
:class="{
|
||||
'show-cursor': item.showCursor
|
||||
}"
|
||||
class="rich-text-box"
|
||||
>
|
||||
<rich-text
|
||||
v-if="item.nodes && item.nodes.length"
|
||||
:nodes="item.nodes"
|
||||
space="nbsp"
|
||||
/>
|
||||
<view v-if="!(item.nodes && item.nodes.length) && loading" class="loader-wrap">
|
||||
<div class="loader" />
|
||||
</view>
|
||||
</view>
|
||||
<view
|
||||
v-if="!item.showCursor && item.nodes && item.nodes.length && !item.isError"
|
||||
class="bottom-btns"
|
||||
>
|
||||
<!-- <button
|
||||
open-type="share"
|
||||
plain="true"
|
||||
class="item"
|
||||
@click="shareHandle"
|
||||
>
|
||||
<image
|
||||
:src="webUrl + '/icon-25.png'"
|
||||
class="icon"
|
||||
mode="widthFix"
|
||||
/>
|
||||
分享
|
||||
</button> -->
|
||||
<view class="item" @click="copyContentHandle(item.content)">
|
||||
<image
|
||||
:src="webUrl + '/aiChat/icon-02.png'"
|
||||
class="icon"
|
||||
mode="widthFix"
|
||||
/>
|
||||
复制
|
||||
</view>
|
||||
<view
|
||||
v-if="index === (chatMessageListLength - 1)"
|
||||
class="item"
|
||||
@click="regenerateHandle(item)"
|
||||
>
|
||||
<image
|
||||
:src="webUrl + '/aiChat/icon-04.png'"
|
||||
class="icon"
|
||||
mode="widthFix"
|
||||
/>
|
||||
重新生成
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<view
|
||||
v-if="index === (chatMessageListLength - 1) && item.listQuestion.length > 0"
|
||||
class="suggestion-wrap"
|
||||
>
|
||||
<view class="suggestion-list">
|
||||
<view
|
||||
v-for="(question, questionIndex) in item.listQuestion"
|
||||
:key="questionIndex"
|
||||
class="item"
|
||||
@click="suggestionItemClickHandle(question)"
|
||||
>
|
||||
<view class="item-cont">
|
||||
<view class="txt">
|
||||
{{ question }}
|
||||
</view>
|
||||
<image
|
||||
:src="webUrl + '/aiChat/icon-03.png'"
|
||||
class="pic"
|
||||
mode="widthFix"
|
||||
/>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
</view>
|
||||
<view id="bottom_postion" class="bottom-postion" />
|
||||
</view>
|
||||
</view>
|
||||
<view class="page-to-bottom">
|
||||
<view
|
||||
v-if="chatNumber"
|
||||
class="new-chat"
|
||||
@click="createNewHandle"
|
||||
>
|
||||
<image
|
||||
:src="webUrl + '/aiChat/icon-05.png'"
|
||||
class="btn-icon"
|
||||
mode="widthFix"
|
||||
/>
|
||||
开启新对话
|
||||
</view>
|
||||
<image
|
||||
v-if="chatMessageListLength > 0 && !isScrollToBottom && !loading"
|
||||
:src="webUrl + '/aiChat/down.png'"
|
||||
class="icon"
|
||||
mode="widthFix"
|
||||
@click="scrollToBottomHandle"
|
||||
/>
|
||||
</view>
|
||||
<bottom-send
|
||||
ref="bottomSend"
|
||||
:loading="loading"
|
||||
@send="streamReq"
|
||||
@add="createNewHandle"
|
||||
@history="historyViewHandle"
|
||||
/>
|
||||
<AiHistoryList
|
||||
ref="history"
|
||||
:status-bar-height="statusBarHeight"
|
||||
/>
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { chatMixins } from '../mixins/chatMixins.js'
|
||||
import {
|
||||
getAiSystemInfoSetting,
|
||||
getAiSystemInfoTopic
|
||||
} from '@/api/public'
|
||||
import AiHistoryList from '../components/historyList'
|
||||
import BottomSend from '../components/bottomSend.vue'
|
||||
export default {
|
||||
name: 'AiChatIndexPage',
|
||||
components: {
|
||||
AiHistoryList,
|
||||
BottomSend
|
||||
},
|
||||
mixins: [chatMixins],
|
||||
data() {
|
||||
return {
|
||||
settingInfo: {},
|
||||
topicList: [],
|
||||
historyList: [],
|
||||
moreList: []
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
createdCallbak() {
|
||||
this.$nextTick(() => {
|
||||
this.scrollToBottomHandle()
|
||||
getAiSystemInfoSetting().then(res => {
|
||||
const { success, data } = res
|
||||
if (success) {
|
||||
this.settingInfo = data
|
||||
}
|
||||
})
|
||||
getAiSystemInfoTopic().then(res => {
|
||||
const { success, data } = res
|
||||
if (success) {
|
||||
this.topicList = data || []
|
||||
}
|
||||
})
|
||||
})
|
||||
},
|
||||
// 创建新会话
|
||||
createNewHandle() {
|
||||
if (!this.chatNumber) return
|
||||
this.chatNumber = ''
|
||||
this.showCursor = false
|
||||
this.loading = false
|
||||
this.getConfigLoading = false
|
||||
this.chatMessageList = []
|
||||
},
|
||||
historyViewHandle() {
|
||||
this.$refs.history.showView()
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
<style lang="scss" scoped>
|
||||
.chat-wrap {
|
||||
padding: 0 0 180rpx 0;
|
||||
.chat-info-wrap {
|
||||
padding: 24rpx;
|
||||
.chat-list-wrap2 {
|
||||
padding: 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
.loading-wrap {
|
||||
text-align: center;
|
||||
color: #666;
|
||||
line-height: 120rpx;
|
||||
}
|
||||
.rich-text-box {
|
||||
max-width: 100%;
|
||||
}
|
||||
.show-cursor .cursor {
|
||||
display: inline-block;
|
||||
color: #3D4CF1;
|
||||
font-weight: bold;
|
||||
animation: blinking 1s infinite;
|
||||
}
|
||||
|
||||
@keyframes blinking {
|
||||
from {
|
||||
opacity: 1.0;
|
||||
}
|
||||
|
||||
to {
|
||||
opacity: 0.0;
|
||||
}
|
||||
}
|
||||
.focus-guide-wrap {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: space-between;
|
||||
padding: 60rpx 40rpx;
|
||||
.guide-left {
|
||||
.avatar {
|
||||
display: block;
|
||||
width: 267rpx;
|
||||
height: 530rpx;
|
||||
}
|
||||
}
|
||||
.guide-right {
|
||||
padding: 30rpx;
|
||||
margin: 0 0 0 40rpx;
|
||||
border-radius: 20rpx;
|
||||
font-size: 24rpx;
|
||||
background-color: #fff;
|
||||
box-shadow: 0rpx 12rpx 30rpx rgba(0,0,0,0.1);
|
||||
.title {
|
||||
font-weight: bold;
|
||||
}
|
||||
.content {
|
||||
padding: 30rpx 0 0 0;
|
||||
color: #999;
|
||||
}
|
||||
}
|
||||
}
|
||||
.topic-wrap {
|
||||
width: 100%;
|
||||
.title {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
.txt {
|
||||
width: 186rpx;
|
||||
height: 46rpx;
|
||||
margin: 0 20rpx;
|
||||
border-radius: 24rpx;
|
||||
text-align: center;
|
||||
line-height: 46rpx;
|
||||
color: #fff;
|
||||
font-size: 26rpx;
|
||||
background-color: #3D4CF1;
|
||||
}
|
||||
.icon {
|
||||
display: flex;
|
||||
width: 40rpx;
|
||||
}
|
||||
}
|
||||
.list-wrap {
|
||||
width: 100%;
|
||||
margin: 60rpx 0 0 0;
|
||||
overflow-x: auto;
|
||||
.list-cont {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
width: 150%;
|
||||
.list-item {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
height: 40rpx;
|
||||
padding: 0 12rpx;
|
||||
margin: 0 30rpx 30rpx 0;
|
||||
border-radius: 20rpx;
|
||||
font-size: 24rpx;
|
||||
color: #666;
|
||||
background: rgba(255,255,255,0.39);
|
||||
box-shadow: 0rpx 6rpx 12rpx rgba(0,0,0,0.1);
|
||||
.icon {
|
||||
display: block;
|
||||
width: 24rpx;
|
||||
height: 24rpx;
|
||||
margin: 0 8rpx 0 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in New Issue
Block a user