<template>
	<view>
		<tabbar :index="4"></tabbar>
		<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'))

			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.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>
