You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.8 KiB

5 months ago
<template>
<view>
5 months ago
<tabbar :index="4"></tabbar>
5 months ago
<view class="u-page">
<u-button type="primary" text="测试调用接口" @click="test"></u-button>
<u-divider text="分割线" :dot="true"></u-divider>
<u-button type="primary" text="修改Storage" @click="storage"></u-button>
<u-divider text="分割线" :dot="true"></u-divider>
<u-button type="primary" text="开始录音" @click="startRecord"></u-button>
<u-divider text="分割线" :dot="true"></u-divider>
<u-button type="primary" text="停止录音" @click="endRecord"></u-button>
<u-divider text="分割线" :dot="true"></u-divider>
<u-button type="primary" text="播放录音" @click="playVoice"></u-button>
<u-divider text="分割线" :dot="true"></u-divider>
<u-button type="primary" text="文字转语音" @click="txt2Voice"></u-button>
<u-divider text="分割线" :dot="true"></u-divider>
<u--textarea v-model="voiceText" placeholder="语音转文字内容"></u--textarea>
<u-toast ref="uToast"></u-toast>
</view>
</view>
</template>
<script>
const innerAudioContext = uni.createInnerAudioContext();
innerAudioContext.autoplay = true;
// 翻译事件引入
const plugin = requirePlugin("WechatSI")
const manager = plugin.getRecordRecognitionManager()
import {
getDictByKey
} from '@/config/api.js';
export default {
data() {
return {
title: 'Hello',
voicePath: '',
voiceText: '',
}
},
onLoad() {
manager.onStop = (res) => {
uni.showToast({
title: '结束~',
icon: 'none',
duration: 2000, //持续时间为 2秒
})
this.voicePath = res.tempFilePath;
this.voiceText = res.result;
if (this.voiceText == '') {
this.voiceText = '周围太安静啦~再试试~';
}
};
// 识别错误事件
manager.onError = (err) => {
uni.showToast({
title: '错误(' + err.retcode + ')' + err.msg,
icon: 'none',
duration: 2000, //持续时间为 2秒
})
};
},
methods: {
storage() {
uni.setStorageSync('token', this.voiceText);
},
test() {
const object1 = {}
uni.$u.setProperty(object1, 'userInfo.email', '123456@qq.com')
uni.$u.setProperty(object1, 'userInfo.address.province', '吉林省')
uni.$u.setProperty(object1, 'userInfo.address.city', '长春市')
console.log(object1)
console.log(uni.$u.getProperty(object1, 'userInfo.address'))
5 months ago
let self = this;
5 months ago
uni.login({
provider: 'weixin',
success: function (loginRes) {
console.log(loginRes);
// 获取用户信息
uni.getUserInfo({
provider: 'weixin',
success: function (infoRes) {
console.log(infoRes);
console.log('用户昵称为:' + infoRes.userInfo.nickName);
5 months ago
self.voiceText = infoRes.userInfo.nickName;
5 months ago
}
});
}
});
getDictByKey({
dictCode: "serviceItem"
}).then((res) => {
console.log(res.data)
}).catch((err) => {
console.log(err)
})
},
startRecord() {
console.log('开始录音');
manager.start({
duration: 60000,
lang: "zh_CN"
})
},
endRecord() {
console.log('停止录音');
manager.stop();
},
playVoice() {
if (this.voicePath) {
innerAudioContext.src = this.voicePath;
innerAudioContext.play();
}
},
txt2Voice() {
plugin.textToSpeech({
lang: "zh_CN",
tts: true,
content: "一个常见的需求,一个常见的需求,一个常见的需求,一个常见的需求,一个常见的需求",
success: function (res) {
console.log("succ tts", res.filename)
innerAudioContext.src = res.filename;
innerAudioContext.play();
},
fail: function (res) {
console.log("fail tts", res)
}
})
}
}
}
</script>
<style lang="scss">
</style>