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.

201 lines
3.8 KiB

// pages/dubiji/dubiji.js
var plugin = requirePlugin("WechatSI")
let manager = plugin.getRecordRecognitionManager()
Page({
/**
* 页面的初始数据
*/
data: {
title: '',
text: '',
text_old: '',
recording: false, // 正在录音
recordStatus: 0, // 状态: 0 - 录音中 1- 翻译中 2 - 翻译完成/二次翻译
id: '',
authed:false
},
saveFs(){
this.setData({
create_time:new Date().toLocaleString()
})
wx.cloud.database().collection("note").add({
data:{
title:this.data.title,
text:this.data.text,
content:this.data.text,
create_time:this.data.create_time,
type:1,
Collection:0
}}).then(res=>{
wx.switchTab({
url: '/pages/index/index',
})
}).catch(res=>{
console.log("type=1 的笔记写入失败",res);
})
},
titleInput(e){
this.setData({
title:e.detail.value
})
},
textareaAInput(e){
this.setData({
text:e.detail.value
})
},
streamRecord(){
wx.vibrateShort({
complete: (res) => {},
})
manager.start({duration:30000, lang: "zh_CN"})
},
endStreamRecord(){
// 防止重复触发stop函数
manager.stop()
if (!this.data.recording || this.data.recordStatus != 0) {
console.warn("has finished!")
return
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (e) {
this.initRecord()
this.getRecordAuth()
},
initRecord: function () {
//有新的识别内容返回,则会调用此事件
// manager.onRecognize = (res) => {
// this.setData({
// text: this.data.text_old + res.result,
// })
// }
// 识别结束事件
manager.onStop = (res) => {
let text = res.result
if (text == '') {
this.showRecordEmptyTip()
return
}
this.setData({
text: this.data.text_old +text,
recordStatus: 1,
recording: false,
}),
this.setData({
text_old:String(this.data.text)
})
}
// 识别错误事件
manager.onError = (res) => {
this.setData({
recording: false,
})
}
},
getRecordAuth: function () {
wx.getSetting().then(res=>{
if(res.authSetting['scope.record']){
this.setData({
authed:true
})
}else{
wx.authorize({
scope: 'scope.record',
}).then(res=>{
this.setData({
authed:true
})
}).catch(res=>{
this.cancel_auth()
})
}
})
},
cancel_auth(){
wx.showModal({
title:"提示",
content:"未授权无法录音哦~",
cancelText:"不授权",
confirmText:"去授权",
success:res=>{
if(res.confirm){
wx.openSetting({
success:res=>{
if(res.authSetting['scope.record']){
this.setData({
authed:true
})
}
}
})
}
}
})
},
showRecordEmptyTip(){
wx.showToast({
title: '识别为空',
})
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady: function () {
},
/**
* 生命周期函数--监听页面显示
*/
onShow: function () {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide: function () {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload: function () {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh: function () {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom: function () {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage: function () {
}
})