1012 lines
28 KiB
Vue
1012 lines
28 KiB
Vue
<template>
|
|
<view class="chat-page" v-if="mentor" :style="{background: `#f7f7fb url(${mentor.bgImage}) no-repeat center bottom`}">
|
|
<u-navbar @leftClick="toMsg" placeholder title="首页">
|
|
<!-- <template #center>-->
|
|
<!-- <view class="flex flex-col justify-center items-center">-->
|
|
<!-- <text>我是玄衡</text>-->
|
|
<!-- <text class="mt-10 text-gray text-25">知五行 破内卷 助你关键决策</text>-->
|
|
<!-- </view>-->
|
|
<!-- </template>-->
|
|
</u-navbar>
|
|
<scroll-view class="chat-body" :scroll-into-view="scrollIntoId" scroll-y>
|
|
<view class="text-25 text-gray text-center my-10 w-100p">该内容由人工智能生成</view>
|
|
<view class="mentor-card" style="max-width: 70%" v-if="mentor">
|
|
<image class="mentor-avatar" :src="mentor.avatar ? mentor.avatar : ''"
|
|
mode="aspectFill"></image>
|
|
<view class="mentor-info">
|
|
<text class="mentor-name">{{ mentor.roleName }}</text>
|
|
</view>
|
|
</view>
|
|
<view class="mentor-card" style="max-width: 80%; padding-left: 40rpx; border-radius: 20rpx"
|
|
v-if="mentor && mentor.roleDesc">
|
|
<text class="mentor-desc">简介: {{ mentor.roleDesc }}</text>
|
|
</view>
|
|
<view class="m-20" v-if="article && article.image">
|
|
<view
|
|
class="discover-card"
|
|
@click="toArticle"
|
|
>
|
|
<image class="card-cover" :src="`${article.image}`" mode="aspectFill"></image>
|
|
<view class="card-title">{{ article.title }}</view>
|
|
<view class="card-desc">{{ article.source }}</view>
|
|
</view>
|
|
<!-- <u-image radius="15" width="400rpx" height="200rpx" :src="article.image"-->
|
|
<!-- @click="toArticle"/>-->
|
|
</view>
|
|
<!-- <view class="flex flex-col justify-start items-start ml-20 mb-10">-->
|
|
<!-- <text>我是玄衡</text>-->
|
|
<!-- <text class="mt-10 text-gray-weight text-25">知五行 破内卷 助你关键决策</text>-->
|
|
<!-- </view>-->
|
|
<!-- <view class="flex flex-col justify-start items-start">-->
|
|
<!-- <view class="mentor-card welcome-card" v-for="(item, index) in welcomeWords" :key="index"-->
|
|
<!-- @click="send(item.dictDataCode, false)">-->
|
|
<!-- {{ item.dictDataCode }}-->
|
|
<!-- </view>-->
|
|
<!-- </view>-->
|
|
<view v-for="(msg, index) in list" :key="index" :id="`msg-${index}`" class="message-row" :class="msg.type">
|
|
<view v-if="!msg.isMine" class="flex justify-start items-start">
|
|
<u-icon :name="mentor.avatar ? mentor.avatar: ''" size="60rpx"/>
|
|
<view v-if="msg.loading" class="p-15">
|
|
<u-loading-icon mode="semicircle"></u-loading-icon>
|
|
</view>
|
|
<view v-else class="bubble" :class="msg.type" style="background-color: #555aa0; color: white">
|
|
<view v-if="msg.voice" class="voice-tag">
|
|
<u-icon name="play-left-fill" size="26" color="#5c627f"></u-icon>
|
|
<text>{{ msg.voice }}"</text>
|
|
</view>
|
|
<zero-markdown-view :markdown="msg.content" themeColor="white"></zero-markdown-view>
|
|
<view class="my-10 text-24 text-gray">本回答由AI生成,仅供参考,请仔细甄别</view>
|
|
<view class="flex justify-end items-center">
|
|
<view
|
|
class="voice-play-btn mr-10"
|
|
v-if="msg.content && msg.done"
|
|
@click="playVoice(msg, index)"
|
|
>
|
|
<u-icon
|
|
:name="voicePlayingIndex === index ? 'pause-circle-fill' : 'play-circle-fill'"
|
|
size="50rpx"
|
|
color="#f7f7fb"
|
|
></u-icon>
|
|
<!-- <text class="ml-10">{{ voicePlayingIndex === index ? '停止' : '语音' }}</text>-->
|
|
</view>
|
|
<u-icon @click="copy(msg.content)"
|
|
name="/static/icon/复制.png"
|
|
size="50rpx"
|
|
color="#f7f7fb"
|
|
></u-icon>
|
|
</view>
|
|
</view>
|
|
</view>
|
|
<view v-else class="flex justify-end items-start w-100p">
|
|
<view class="shadow p-15 text-white mr-10 pos-r"
|
|
style="border-radius: 15rpx 0 15rpx 15rpx; max-width: 80%; background-color: #FADEB3; color: black; padding-right: 30rpx">
|
|
<view>{{ msg.content }}</view>
|
|
<view class="copy-btn" @click="copy(msg.content)">
|
|
<u-icon
|
|
name="/static/icon/复制-black.png"
|
|
size="30rpx"
|
|
color="#f7f7fb"
|
|
></u-icon>
|
|
</view>
|
|
</view>
|
|
<u-avatar :src="userData.avatar || ''" size="60rpx"/>
|
|
</view>
|
|
</view>
|
|
<u-gap height="160rpx"/>
|
|
<u-gap :height="`${inputBottom}px`"/>
|
|
<view id="chat-bottom"></view>
|
|
</scroll-view>
|
|
|
|
<view class="chat-input" :style="{bottom: `${inputBottom}px`}">
|
|
<view class="m-40 flex justify-between items-center w-100p">
|
|
<view class="ml-15">
|
|
<u-icon :name="voiceMode ? '/static/icon/键盘.png' : '/static/icon/话筒.png'" size="60rpx"
|
|
@click="toggleVoiceMode"/>
|
|
</view>
|
|
<view class="input-wrap" v-if="!voiceMode">
|
|
<textarea
|
|
:adjust-position="false"
|
|
class="textarea"
|
|
v-model="content"
|
|
placeholder="迷茫困惑的话和我聊聊吧"
|
|
maxlength="-1"
|
|
placeholder-style="text-align:center;"
|
|
auto-height
|
|
fixed
|
|
@keyboardheightchange="onKeyboardChange"
|
|
:show-confirm-bar="false"
|
|
/>
|
|
</view>
|
|
<view class="record-wrap" v-else>
|
|
<view
|
|
class="record-btn"
|
|
@longpress="startRecord"
|
|
@touchend="stopRecord"
|
|
@touchcancel="stopRecord"
|
|
>
|
|
{{ recording ? '录音中...' : recordBtnText }}
|
|
</view>
|
|
</view>
|
|
<!-- <button class="voice-toggle" :class="{active: voiceMode}" @click="toggleVoiceMode">-->
|
|
<!-- {{ voiceMode ? '文本' : '录音' }}-->
|
|
<!-- </button>-->
|
|
<button class="send-btn" @click="send()" v-if="!answering">发送</button>
|
|
<button class="send-btn stop-btn" @click="stopAI" v-else>停止</button>
|
|
</view>
|
|
</view>
|
|
<Login ref="Login" @done="getUserData"/>
|
|
<!-- <view class="stop" v-if="answering" @click="stopAI">-->
|
|
<!-- <u-icon name="/static/stop.png" width="80rpx" height="80rpx"/>-->
|
|
<!-- </view>-->
|
|
<u-popup :show="show" @close="show = false" round="15" mode="center" :safe-area-inset-bottom="false">
|
|
<view class="p-30">
|
|
<view class="pb-30">分享可得金币{{ shareGetCoin }}个,是否立即分享?</view>
|
|
<view class="flex justify-center items-center py-10 border-top">
|
|
<view class="flex-1" style="height: 50rpx">
|
|
<button class="share-btn text-30 text-main" open-type="share">分享</button>
|
|
</view>
|
|
<view class="flex-1" style="height: 50rpx">
|
|
<button class="share-btn text-30" @click="show = false">取消</button>
|
|
</view>
|
|
</view>
|
|
</view>
|
|
</u-popup>
|
|
</view>
|
|
</template>
|
|
|
|
<script>
|
|
import {WS_API, MAIN_API} from "@/config";
|
|
import {addCoinLogReq, userInfoReq} from "@/api/user";
|
|
import {getUserInfo} from "@/util/user";
|
|
import Login from "@/components/Login.vue";
|
|
import {
|
|
addChatHistoryReq,
|
|
chatRoleInfoReq,
|
|
listChatHistoryReq,
|
|
makeTransReq,
|
|
makeVoiceReq,
|
|
sendChatReq,
|
|
stopChatReq,
|
|
voiceGetResReq
|
|
} from "@/api/chat";
|
|
import {dictDataReq, dictListReq, pageArticleReq} from "@/api/common";
|
|
import {coinAddReq} from "@/api/coin";
|
|
|
|
export default {
|
|
components: {Login},
|
|
data() {
|
|
return {
|
|
statusBarPadding: '40rpx',
|
|
mentor: {},
|
|
list: [],
|
|
sendWord: '',
|
|
content: '',
|
|
scrollIntoId: '',
|
|
timer: null,
|
|
randomUid: null,
|
|
userData: null,
|
|
chatId: null,
|
|
conversationId: null,
|
|
role: null,
|
|
answering: false,
|
|
chatHeight: '500px',
|
|
welcomeWords: [],
|
|
article: null,
|
|
inputBottom: 0,
|
|
voiceMode: false,
|
|
recording: false,
|
|
recordBtnText: '按住说话',
|
|
recorder: null,
|
|
voicePollingTimer: null,
|
|
maxVoicePolling: 15,
|
|
voicePlayingIndex: null,
|
|
audioCtx: null,
|
|
voiceType: 'default',
|
|
show: false,
|
|
shareGetCoin: 5,
|
|
taskId: null,
|
|
taskIdOrigin: null,
|
|
}
|
|
},
|
|
methods: {
|
|
goBack() {
|
|
uni.navigateBack().catch(() => {
|
|
uni.switchTab({url: '/pages/msg/index'})
|
|
})
|
|
},
|
|
copy(data) {
|
|
uni.setClipboardData({
|
|
data,
|
|
success: () => {
|
|
uni.hideToast();
|
|
this.$nextTick(() => {
|
|
uni.$u.toast('复制成功')
|
|
})
|
|
}
|
|
})
|
|
},
|
|
onKeyboardChange(e) {
|
|
console.log(e)
|
|
const _this = this
|
|
setTimeout(() => {
|
|
_this.$nextTick(() => {
|
|
_this.inputBottom = e.detail.height
|
|
if (_this.inputBottom > 0) _this.scrollToBottom()
|
|
})
|
|
}, parseInt(e.detail.duration * 1000))
|
|
},
|
|
async getWelcomeWords() {
|
|
const {data} = await dictDataReq({
|
|
dictId: 167
|
|
})
|
|
this.welcomeWords = data
|
|
},
|
|
async getShareGetCoin() {
|
|
const {data} = await dictDataReq({
|
|
dictId: 170
|
|
})
|
|
this.shareGetCoin = data[0].dictDataCode
|
|
},
|
|
async getArticleList() {
|
|
const {data} = await pageArticleReq({
|
|
page: 1,
|
|
limit: 1
|
|
})
|
|
if (data && data.list && data.list.length) {
|
|
this.article = data.list[0]
|
|
}
|
|
// this.welcomeWords = data
|
|
},
|
|
async getRoleInfo(id) {
|
|
const {data} = await chatRoleInfoReq(id)
|
|
this.mentor = data
|
|
uni.setNavigationBarTitle({title: this.mentor.roleName})
|
|
},
|
|
async getUserData() {
|
|
const {data} = await userInfoReq()
|
|
this.userData = data
|
|
this.isLogin = true
|
|
this.onLoginDone()
|
|
},
|
|
toArticle() {
|
|
uni.navigateTo({
|
|
url: `/pages/index/detail?id=${this.article.articleId}`
|
|
})
|
|
},
|
|
toggleVoiceMode() {
|
|
uni.authorize({
|
|
scope: 'scope.record',
|
|
success() {
|
|
console.log('授权成功')
|
|
},
|
|
fail() {
|
|
console.log('授权失败')
|
|
},
|
|
complete(e) {
|
|
console.log('授权完成', e)
|
|
}
|
|
})
|
|
if (this.voiceMode) {
|
|
if (this.recorder && this.recording) {
|
|
this.recorder.stop()
|
|
}
|
|
this.recording = false
|
|
this.voiceMode = false
|
|
this.recordBtnText = '按住说话'
|
|
return
|
|
}
|
|
this.voiceMode = true
|
|
this.recordBtnText = '按住说话'
|
|
typeof uni.hideKeyboard === 'function' && uni.hideKeyboard()
|
|
this.initRecorder()
|
|
},
|
|
initRecorder() {
|
|
if (this.recorder) return
|
|
this.recorder = uni.getRecorderManager()
|
|
this.recorder.onStart(() => {
|
|
this.recording = true
|
|
this.recordBtnText = '录音中...'
|
|
})
|
|
this.recorder.onStop((res) => {
|
|
this.recording = false
|
|
const path = res && res.tempFilePath
|
|
if (path) this.processVoiceFile(path)
|
|
else this.recordBtnText = '按住说话'
|
|
})
|
|
this.recorder.onError(() => {
|
|
this.recording = false
|
|
this.recordBtnText = '按住说话'
|
|
uni.showToast({title: '录音失败', icon: 'none'})
|
|
})
|
|
},
|
|
startRecord() {
|
|
if (!this.voiceMode) return
|
|
this.initRecorder()
|
|
if (this.recording) return
|
|
this.recording = true
|
|
this.recordBtnText = '录音中...'
|
|
try {
|
|
this.recorder.start({
|
|
duration: 60000,
|
|
format: 'mp3',
|
|
sampleRate: 16000,
|
|
numberOfChannels: 1,
|
|
encodeBitRate: 48000
|
|
})
|
|
} catch (e) {
|
|
this.recording = false
|
|
this.recordBtnText = '按住说话'
|
|
uni.showToast({title: '无法开始录音', icon: 'none'})
|
|
}
|
|
},
|
|
stopRecord() {
|
|
if (!this.voiceMode || !this.recording || !this.recorder) return
|
|
this.recording = false
|
|
this.recorder.stop()
|
|
},
|
|
async processVoiceFile(filePath) {
|
|
this.recordBtnText = '处理中...'
|
|
uni.showLoading({title: '识别中...'})
|
|
try {
|
|
const {url} = await this.uploadVoiceFile(filePath)
|
|
if (!url) throw new Error('no file')
|
|
// const file = 'https://guoxue.gxwebsoft.com/api/file/20251202/97e5e082becb44e88ece0e9cbd050dca.mp3'
|
|
// const {data: taskId} = await makeTransReq({file})
|
|
const {data: taskId} = await makeTransReq({file: url})
|
|
if (taskId) {
|
|
this.startVoicePolling(taskId)
|
|
} else {
|
|
throw new Error('no task')
|
|
}
|
|
} catch (err) {
|
|
uni.hideLoading()
|
|
uni.showToast({title: '语音识别失败', icon: 'none'})
|
|
this.recordBtnText = '按住说话'
|
|
}
|
|
},
|
|
uploadVoiceFile(filePath) {
|
|
const {token} = getUserInfo()
|
|
return new Promise((resolve, reject) => {
|
|
uni.uploadFile({
|
|
url: `${MAIN_API}/api/file/upload`,
|
|
name: 'file',
|
|
filePath,
|
|
header: {
|
|
Authorization: token,
|
|
TenantId: 10543
|
|
},
|
|
success: (res) => {
|
|
try {
|
|
const result = JSON.parse(res.data)
|
|
const url = result?.data?.url || result?.data
|
|
resolve({url})
|
|
} catch (e) {
|
|
reject(e)
|
|
}
|
|
},
|
|
fail: reject
|
|
})
|
|
})
|
|
},
|
|
startVoicePolling(taskId) {
|
|
this.clearVoicePolling()
|
|
let attempt = 0
|
|
this.voicePollingTimer = setInterval(async () => {
|
|
attempt += 1
|
|
try {
|
|
const {data} = await voiceGetResReq(taskId)
|
|
const result = typeof data === 'string' ? data : (data?.text || '')
|
|
if (result && result !== '__NULL__') {
|
|
this.clearVoicePolling()
|
|
uni.hideLoading()
|
|
this.recordBtnText = '按住说话'
|
|
const wordResult = JSON.parse(data)
|
|
// console.log('wordResult', wordResult)
|
|
if (wordResult.Sentences && wordResult.Sentences.length)
|
|
this.send(wordResult.Sentences[0].Text)
|
|
else uni.showToast({title: '未识别到内容', icon: 'none'})
|
|
} else if (result === '__NULL__') {
|
|
this.clearVoicePolling()
|
|
uni.hideLoading()
|
|
this.recordBtnText = '按住说话'
|
|
uni.showToast({title: '未识别到内容', icon: 'none'})
|
|
} else if (attempt >= this.maxVoicePolling) {
|
|
this.clearVoicePolling()
|
|
uni.hideLoading()
|
|
this.recordBtnText = '按住说话'
|
|
uni.showToast({title: '识别超时', icon: 'none'})
|
|
}
|
|
} catch (err) {
|
|
this.clearVoicePolling()
|
|
uni.hideLoading()
|
|
this.recordBtnText = '按住说话'
|
|
uni.showToast({title: '识别失败', icon: 'none'})
|
|
}
|
|
}, 2000)
|
|
},
|
|
clearVoicePolling() {
|
|
if (this.voicePollingTimer) {
|
|
clearInterval(this.voicePollingTimer)
|
|
this.voicePollingTimer = null
|
|
}
|
|
},
|
|
async playVoice(msg, index) {
|
|
if (!msg || !msg.content || msg.loading) return
|
|
if (this.voicePlayingIndex === index) {
|
|
this.stopAudio()
|
|
return
|
|
}
|
|
this.initAudioContext()
|
|
try {
|
|
uni.showLoading({title: '生成语音...'})
|
|
let voiceUrl = msg.voiceUrl
|
|
if (!voiceUrl) {
|
|
const {data} = await makeVoiceReq({
|
|
content: msg.content,
|
|
voice: this.mentor.voice
|
|
})
|
|
console.log(data)
|
|
voiceUrl = data?.url || data
|
|
if (!voiceUrl) throw new Error('no url')
|
|
this.$set(this.list[index], 'voiceUrl', voiceUrl)
|
|
}
|
|
uni.hideLoading()
|
|
this.audioCtx && this.audioCtx.stop()
|
|
this.audioCtx.src = voiceUrl
|
|
this.voicePlayingIndex = index
|
|
this.audioCtx.play()
|
|
} catch (err) {
|
|
console.log(err)
|
|
uni.hideLoading()
|
|
this.voicePlayingIndex = null
|
|
uni.showToast({title: '语音播放失败', icon: 'none'})
|
|
}
|
|
},
|
|
initAudioContext() {
|
|
if (this.audioCtx) return
|
|
this.audioCtx = uni.createInnerAudioContext()
|
|
this.audioCtx.obeyMuteSwitch = false
|
|
this.audioCtx.onEnded(() => {
|
|
this.voicePlayingIndex = null
|
|
})
|
|
this.audioCtx.onStop(() => {
|
|
this.voicePlayingIndex = null
|
|
})
|
|
this.audioCtx.onError((e) => {
|
|
console.log('audio err', e)
|
|
// this.voicePlayingIndex = null
|
|
// uni.showToast({title: '播放出错', icon: 'none'})
|
|
})
|
|
},
|
|
stopAudio() {
|
|
if (this.audioCtx) {
|
|
try {
|
|
this.audioCtx.stop()
|
|
} catch (e) {
|
|
}
|
|
this.voicePlayingIndex = null
|
|
if (typeof this.audioCtx.destroy === 'function') {
|
|
this.audioCtx.destroy()
|
|
}
|
|
this.audioCtx = null
|
|
}
|
|
},
|
|
async send(content = null, reduceCoin = true) {
|
|
const contentItem = content || this.content
|
|
console.log(contentItem, this.content)
|
|
if (!contentItem.trim()) return this.$toast('请输入提问内容')
|
|
if (this.list.length > 0 && !this.list[this.list.length - 1].done) return this.$toast('请等待回答完毕再继续提问')
|
|
let checkRes = true
|
|
if (reduceCoin) {
|
|
checkRes = await addCoinLogReq({
|
|
type: '-',
|
|
num: 1,
|
|
desc: '聊天扣除'
|
|
}).catch(() => {
|
|
this.show = true
|
|
// uni.showModal({
|
|
// title: '提示',
|
|
// content: '分享获取5个币,是否立即分享',
|
|
// success: async (res) => {
|
|
// if (res.confirm) {
|
|
// await uni.navigateTo({
|
|
// url: '/pages/user/coin'
|
|
// })
|
|
// }
|
|
// }
|
|
// })
|
|
})
|
|
}
|
|
if (!checkRes) return
|
|
this.list.push({
|
|
content: contentItem,
|
|
isMine: true,
|
|
loading: false,
|
|
done: true,
|
|
})
|
|
this.scrollToBottom()
|
|
this.answering = true
|
|
this.list.push({
|
|
content: '',
|
|
isMine: false,
|
|
loading: true,
|
|
done: false,
|
|
})
|
|
// this.taskId = null
|
|
sendChatReq({
|
|
query: contentItem,
|
|
roleName: this.mentor.roleName,
|
|
roleDesc: this.mentor.roleDesc,
|
|
chatId: this.chatId,
|
|
conversationId: this.conversationId || this.taskId,
|
|
}).then(() => {
|
|
this.list[this.list.length - 1].done = true
|
|
this.answering = false
|
|
})
|
|
this.content = ''
|
|
},
|
|
scrollToBottom() {
|
|
this.scrollIntoId = ''
|
|
this.$nextTick(() => {
|
|
this.scrollIntoId = 'chat-bottom'
|
|
})
|
|
},
|
|
async stopAI() {
|
|
await stopChatReq({
|
|
taskId: this.taskIdOrigin
|
|
})
|
|
this.answering = false
|
|
// uni.closeSocket()
|
|
},
|
|
onLoginDone() {
|
|
uni.hideLoading()
|
|
// this.list.push({
|
|
// content: '您好!我是法律机器人,请问您想咨询什么问题呢?',
|
|
// isMine: false,
|
|
// loading: false,
|
|
// done: true,
|
|
// })
|
|
const _this = this
|
|
const user = getUserInfo()
|
|
this.socket = uni.connectSocket({
|
|
url: `${WS_API}/${user.uid}`,
|
|
success: () => {
|
|
_this.ping()
|
|
},
|
|
})
|
|
uni.onSocketOpen(res => {
|
|
if (_this.sendWord) this.send(_this.sendWord, false)
|
|
})
|
|
uni.onSocketMessage(res => {
|
|
if (res.data === '连接成功') return
|
|
const aiData = JSON.parse(res.data)
|
|
this.list[this.list.length - 1].loading = false
|
|
if (aiData.answer !== '__END__') {
|
|
if (aiData.loading && aiData.loading === 'true') {
|
|
this.list[this.list.length - 1].loading = true
|
|
} else {
|
|
this.list[this.list.length - 1].loading = false
|
|
this.list[this.list.length - 1].content += aiData.answer
|
|
this.list[this.list.length - 1].content = this.list[this.list.length - 1].content.replace('搜索中...', '')
|
|
}
|
|
} else if (aiData.answer === '__END__') {
|
|
console.log(this.list)
|
|
addChatHistoryReq({
|
|
content: this.list[this.list.length - 2].content,
|
|
reply: this.list[this.list.length - 1].content,
|
|
roleName: this.mentor.name,
|
|
roleDesc: this.mentor.desc,
|
|
chatId: this.chatId,
|
|
conversationId: aiData.taskId,
|
|
})
|
|
}
|
|
this.taskId = aiData.taskId
|
|
this.taskIdOrigin = aiData.taskIdOrigin
|
|
this.scrollToBottom()
|
|
})
|
|
},
|
|
ping() {
|
|
this.timer = setInterval(() => {
|
|
if (this.socket) {
|
|
this.socket.send({
|
|
data: 'ping'
|
|
})
|
|
}
|
|
}, 10000)
|
|
},
|
|
async getList() {
|
|
const {data} = await listChatHistoryReq({
|
|
chatId: this.chatId,
|
|
})
|
|
this.list = []
|
|
data.reverse()
|
|
data.map(item => {
|
|
this.list.push({
|
|
content: item.content,
|
|
isMine: true,
|
|
loading: false,
|
|
done: true,
|
|
})
|
|
this.list.push({
|
|
content: item.reply,
|
|
isMine: false,
|
|
loading: false,
|
|
done: true,
|
|
})
|
|
})
|
|
setTimeout(() => {
|
|
this.$nextTick(() => {
|
|
this.scrollToBottom()
|
|
})
|
|
}, 300)
|
|
},
|
|
toMsg() {
|
|
uni.switchTab({url: '/pages/msg/index'})
|
|
}
|
|
},
|
|
async onLoad(option) {
|
|
const info = uni.getSystemInfoSync()
|
|
this.statusBarPadding = `${(info.statusBarHeight || 20) + 10}px`
|
|
this.chatHeight = `${info.windowHeight - (info.statusBarHeight || 20) - 80}` - uni.upx2px(140) + 'px'
|
|
if (option.chatId) this.chatId = option.chatId
|
|
if (option.roleId) this.getRoleInfo(option.roleId)
|
|
if (option.conversationId) this.conversationId = option.conversationId
|
|
this.getShareGetCoin()
|
|
this.getWelcomeWords()
|
|
this.getList()
|
|
this.getArticleList()
|
|
// if (option.roleName) this.mentor.name = option.roleName
|
|
// if (option.roleDesc) this.mentor.desc = option.roleDesc
|
|
// if (option.avatar) this.mentor.avatar = option.avatar
|
|
// if (!this.mentor.avatar || this.mentor.avatar === 'undefined') this.mentor.avatar = '/static/icon/图片 2 Copy 1@1x.png'
|
|
if (option.word) this.sendWord = option.word
|
|
const _this = this
|
|
// uni.onKeyboardHeightChange(res => {
|
|
// setTimeout(() => {
|
|
// console.log('onKeyboardHeightChange', res.height)
|
|
// _this.$nextTick(() => {
|
|
// _this.inputBottom = res.height
|
|
// if (_this.inputBottom > 0) _this.scrollToBottom()
|
|
// })
|
|
// }, 100)
|
|
// })
|
|
// console.log(this.mentor.avatar)
|
|
},
|
|
onShow() {
|
|
this.inputBottom = 0
|
|
// this.inputBottom = uni.getSystemInfoSync().safeAreaInsets.bottom
|
|
if (getUserInfo().token) this.getUserData()
|
|
else this.$refs.Login.open()
|
|
},
|
|
onUnload() {
|
|
if (this.timer) clearInterval(this.timer)
|
|
uni.closeSocket()
|
|
this.clearVoicePolling()
|
|
this.stopAudio()
|
|
this.recordBtnText = '按住说话'
|
|
uni.hideLoading()
|
|
this.stopAI()
|
|
uni.offKeyboardHeightChange()
|
|
},
|
|
onHide() {
|
|
if (this.timer) clearInterval(this.timer)
|
|
uni.closeSocket()
|
|
this.clearVoicePolling()
|
|
this.stopAudio()
|
|
this.recordBtnText = '按住说话'
|
|
uni.hideLoading()
|
|
this.stopAI()
|
|
uni.offKeyboardHeightChange()
|
|
},
|
|
onShareTimeline() {
|
|
coinAddReq({
|
|
type: '+',
|
|
desc: '分享获得',
|
|
num: 1
|
|
})
|
|
return {
|
|
title: '简幸',
|
|
path: '/pages/index/index'
|
|
}
|
|
},
|
|
onShareAppMessage() {
|
|
coinAddReq({
|
|
type: '+',
|
|
desc: '分享获得',
|
|
num: 1
|
|
})
|
|
return {
|
|
title: '简幸',
|
|
path: '/pages/index/index'
|
|
}
|
|
},
|
|
}
|
|
</script>
|
|
|
|
<style lang="scss" scoped>
|
|
.chat-page {
|
|
min-height: 100vh;
|
|
background-size: cover;
|
|
display: flex;
|
|
flex-direction: column;
|
|
}
|
|
|
|
.chat-header {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: flex-start;
|
|
padding: 20rpx 30rpx 10rpx;
|
|
}
|
|
|
|
.chat-title {
|
|
font-size: 34rpx;
|
|
color: #2c2f55;
|
|
}
|
|
|
|
.pill {
|
|
background: rgba(255, 255, 255, 0.7);
|
|
padding: 10rpx 26rpx;
|
|
border-radius: 40rpx;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 12rpx;
|
|
}
|
|
|
|
.chat-body {
|
|
//flex: 1;
|
|
height: calc(100vh - 170rpx);
|
|
//overflow-y: auto;
|
|
//padding: 20rpx 30rpx;
|
|
}
|
|
|
|
.mentor-card {
|
|
display: flex;
|
|
align-items: center;
|
|
background: #555aa0;
|
|
border-radius: 80rpx;
|
|
padding: 20rpx;
|
|
color: #fff;
|
|
margin: 20rpx;
|
|
width: 90%;
|
|
|
|
image {
|
|
flex: 0 0 120rpx;
|
|
}
|
|
}
|
|
|
|
.welcome-card {
|
|
background-color: #f1f2ff;
|
|
width: auto;
|
|
max-width: 80%;
|
|
display: inline-flex;
|
|
align-self: flex-start;
|
|
color: #000 !important;
|
|
margin: 10rpx 20rpx;
|
|
}
|
|
|
|
.mentor-avatar {
|
|
width: 120rpx;
|
|
height: 120rpx;
|
|
border-radius: 60rpx;
|
|
margin-right: 20rpx;
|
|
}
|
|
|
|
.mentor-name {
|
|
font-size: 30rpx;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.message-row {
|
|
display: flex;
|
|
margin-bottom: 30rpx;
|
|
padding: 0 20rpx;
|
|
//width: 90%;
|
|
}
|
|
|
|
.message-row.user {
|
|
justify-content: flex-end;
|
|
}
|
|
|
|
.bubble {
|
|
max-width: 70%;
|
|
padding: 24rpx 30rpx;
|
|
border-radius: 28rpx;
|
|
box-shadow: 0 16rpx 30rpx rgba(98, 109, 179, 0.2);
|
|
margin-left: 10rpx;
|
|
background-color: white;
|
|
}
|
|
|
|
.bubble.mentor {
|
|
background: #3a4483;
|
|
color: #fff;
|
|
}
|
|
|
|
.bubble.user {
|
|
background: #f2c892;
|
|
color: #5a3c0e;
|
|
}
|
|
|
|
.voice-tag {
|
|
display: flex;
|
|
align-items: center;
|
|
margin-bottom: 12rpx;
|
|
color: #d5d8ef;
|
|
}
|
|
|
|
.voice-play-btn {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: flex-end;
|
|
//margin-top: 20rpx;
|
|
color: #d5d8ef;
|
|
}
|
|
|
|
.chat-input {
|
|
width: 100%;
|
|
padding: 20rpx 0 40rpx;
|
|
//padding: 20rpx 30rpx 40rpx;
|
|
align-items: center;
|
|
gap: 20rpx;
|
|
background: #eee;
|
|
display: flex;
|
|
justify-content: center;
|
|
position: fixed;
|
|
left: 0;
|
|
bottom: 0;
|
|
}
|
|
|
|
.chat-input .input-wrap {
|
|
flex: 1;
|
|
border-radius: 10rpx;
|
|
background: #fff;
|
|
color: #000;
|
|
padding: 15rpx 30rpx;
|
|
margin: 0 10rpx;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
//height: 100rpx;
|
|
min-height: 40rpx;
|
|
}
|
|
|
|
.chat-input .textarea {
|
|
width: 100%;
|
|
//height: 100%;
|
|
background: none !important;
|
|
color: #000 !important;
|
|
text-align: left;
|
|
line-height: 40rpx;
|
|
padding: 0;
|
|
min-height: 35rpx;
|
|
}
|
|
|
|
.record-wrap {
|
|
flex: 1;
|
|
border-radius: 10rpx;
|
|
//background: linear-gradient(120deg, #fbc889, #f37a5b);
|
|
background: #fff;
|
|
color: #000;
|
|
padding: 15rpx 30rpx;
|
|
margin: 0 10rpx;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
min-height: 40rpx;
|
|
}
|
|
|
|
.record-btn {
|
|
width: 100%;
|
|
text-align: center;
|
|
color: #000;
|
|
}
|
|
|
|
.voice-toggle {
|
|
width: 120rpx;
|
|
height: 100rpx;
|
|
border-radius: 999rpx;
|
|
background: #dfe1f2;
|
|
color: #1f2654;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
margin: 0 10rpx;
|
|
border: none;
|
|
}
|
|
|
|
.voice-toggle.active {
|
|
background: #21284f;
|
|
color: #fff;
|
|
}
|
|
|
|
.send-btn {
|
|
width: 120rpx;
|
|
height: 70rpx;
|
|
border-radius: 20rpx;
|
|
//background: linear-gradient(120deg, #fbc889, #f37a5b);
|
|
background-color: white;
|
|
border: 1px solid white;
|
|
color: #000;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
margin: 0 15rpx 0 0;
|
|
font-size: 25rpx;
|
|
}
|
|
|
|
.send-btn:after {
|
|
border: none;
|
|
}
|
|
|
|
.stop-btn {
|
|
//background: linear-gradient(120deg, #fb8989, #8b0000);
|
|
//color: #fff;
|
|
}
|
|
|
|
|
|
.stop {
|
|
position: fixed;
|
|
right: 10rpx;
|
|
bottom: 320rpx;
|
|
width: 100rpx;
|
|
height: 100rpx;
|
|
border-radius: 100rpx;
|
|
background: rgba(255, 255, 255, 0.4);
|
|
box-shadow: 1px 1px 5px #3c9cff;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
}
|
|
|
|
|
|
.discover-card {
|
|
width: calc(50% - 12rpx);
|
|
margin-bottom: 30rpx;
|
|
border-radius: 26rpx;
|
|
background-color: rgba(255, 255, 255, 0.92);
|
|
box-shadow: 0 10rpx 30rpx rgba(133, 149, 255, 0.2);
|
|
padding: 16rpx 18rpx 26rpx;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
.card-cover {
|
|
width: 100%;
|
|
height: 260rpx;
|
|
border-radius: 20rpx;
|
|
margin-bottom: 18rpx;
|
|
}
|
|
|
|
.card-title {
|
|
font-size: 28rpx;
|
|
color: #1f1f1f;
|
|
font-weight: 600;
|
|
line-height: 1.4;
|
|
margin-bottom: 12rpx;
|
|
}
|
|
|
|
.card-desc {
|
|
font-size: 24rpx;
|
|
color: #6b6f7a;
|
|
line-height: 1.4;
|
|
}
|
|
|
|
.copy-btn {
|
|
background-color: rgba(250, 222, 179, 1);
|
|
width: 35rpx;
|
|
height: 35rpx;
|
|
padding: 10rpx;
|
|
border-radius: 35rpx;
|
|
position: absolute;
|
|
right: -20rpx;
|
|
bottom: -20rpx;
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
box-shadow: 1px 1px 10rpx #ccc;
|
|
}
|
|
</style>
|
|
|
|
<style>
|
|
.wx-button::after {
|
|
border: none;
|
|
}
|
|
</style> |