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.

163 lines
3.9 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<template>
<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>
</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'))
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);
}
});
}
});
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.src = 'https://dsideal.obs.cn-north-1.myhuaweicloud.com/wb/za.mp3';
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">
.wrap {
padding: 12px;
}
.demo-layout {
height: 25px;
border-radius: 4px;
}
.bg-purple {
background: #CED7E1;
}
.bg-purple-light {
background: #e5e9f2;
}
.bg-purple-dark {
background: #99a9bf;
}
</style>