Feat(云灵AI):界面初始化
This commit is contained in:
@@ -0,0 +1,268 @@
|
||||
<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 class="bottom-body flex-center-between">
|
||||
<!-- 输入模式切换 -->
|
||||
<view
|
||||
class="input-model"
|
||||
@click="toggleInputModel"
|
||||
>
|
||||
输入模式
|
||||
</view>
|
||||
<!-- 输入框 -->
|
||||
<view
|
||||
v-if="inputModel === 'input'"
|
||||
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
|
||||
v-if="inputModel === 'input'"
|
||||
class="send-btn"
|
||||
>
|
||||
<view
|
||||
@click="sendHandle"
|
||||
>
|
||||
发送
|
||||
</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>
|
||||
</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.$webUrl,
|
||||
// 输入模式: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
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user