const recorderManager = wx.getRecorderManager() const innerAudioContext = wx.createInnerAudioContext(); const options = { duration: 540000,//指定录音的时长,单位 ms sampleRate: 16000,//采样率 numberOfChannels: 1,//录音通道数 encodeBitRate: 24000,//编码码率 format: 'mp3',//音频格式,有效值 aac/mp3 frameSize: 50,//指定帧大小,单位 KB } Page({ data: { id:"", name:"", date: new Date().toLocaleDateString(), RecordPause:"开始录音", num:1, tempFilePath:[], fileSize:"", filePhth:[], status: "", personName:"", toView:"a01", topnum:{"e4":"","f1":"","f3":"","g8":"","g11":"","g13":"","h1":"","h8":"","h10":"","h11":"","h14":"","i1":"","j1":"","t3":"","t4":"","t5":""}, e4:"", f1:"", f3:"", g8:"", g11:"", g13:"", h1:"", h8:"", h10:"", h11:"", h14:"", i1:"", j1:"", t3:"", t4:"", t5:"", bianhao:"", year:"", month:"", condition:false, buttonClicked:false }, scrollToViewFn: function (e) { var _id = e.target.dataset.id; this.setData({ toView:_id }) console.log(this.data.toView) }, //开始录音的时候 start: function () { //点击后 变为暂停 if (this.data.status ==2){ if (this.data.num == 1) { this.setData({ num: 2, RecordPause: "暂停录音", condition: false }) //开始录音 recorderManager.start(options); recorderManager.onStop((res) => { this.data.tempFilePath.push(res.tempFilePath); this.tempFilePath = res.tempFilePath; console.log('停止录音', res.tempFilePath) const { tempFilePath } = res recorderManager.start(options); console.log(this.data.tempFilePath) }); //错误回调 recorderManager.onError((res) => { console.log('停止录音', res.tempFilePath) }) } else if (this.data.num == 2) { //点击后 变为继续 recorderManager.pause(); this.setData({ num: 3, RecordPause: "继续录音" }) } else if (this.data.num == 3) { recorderManager.resume(); this.setData({ num: 2, RecordPause: "暂停录音" }) } } }, //停止录音 stop: function () { recorderManager.stop(); recorderManager.onStop((res) => { this.data.tempFilePath.push(res.tempFilePath); this.tempFilePath = res.tempFilePath; const { tempFilePath } = res; console.log(this.data.tempFilePath); this.setData({ num: 1, RecordPause: "开始录音", condition:true }) }) }, onLoad: function (options) { var that = this; var queryBean = options.id; var username = options.name; var uname = options.username; wx.request({ url: 'https://oa.dcqcdc.com/dccdc/Infection/getSheetId',//请求地址 data: {}, header: {//请求头 "Content-Type": "application/x-www-form-urlencoded" }, method: "POST",//get为默认方法/POST success: function (res) { console.log(res.data);//res.data相当于ajax里面的data,为后台返回的数据 console.log(that.data) that.setData({ bianhao: res.data }) }, fail: function (err) { console.log(err) },//请求失败 complete: function () { }//请求完成后执行的函数 }) wx.createSelectorQuery().select('#e4').boundingClientRect(function (rect) { that.setData({ e4:rect.top }) }).exec(); wx.createSelectorQuery().select('#f1').boundingClientRect(function (rect) { that.setData({ f1: rect.top }) }).exec(); wx.createSelectorQuery().select('#f3').boundingClientRect(function (rect) { that.setData({ f3: rect.top }) }).exec(); wx.createSelectorQuery().select('#g8').boundingClientRect(function (rect) { that.setData({ g8: rect.top }) }).exec(); wx.createSelectorQuery().select('#g13').boundingClientRect(function (rect) { that.setData({ g13: rect.top }) }).exec(); wx.createSelectorQuery().select('#h1').boundingClientRect(function (rect) { that.setData({ h1: rect.top }) }).exec(); wx.createSelectorQuery().select('#h8').boundingClientRect(function (rect) { that.setData({ h8: rect.top }) }).exec(); wx.createSelectorQuery().select('#h10').boundingClientRect(function (rect) { that.setData({ h10: rect.top }) }).exec(); wx.createSelectorQuery().select('#h11').boundingClientRect(function (rect) { that.setData({ h11: rect.top }) }).exec(); wx.createSelectorQuery().select('#h14').boundingClientRect(function (rect) { that.setData({ h14: rect.top }) }).exec(); wx.createSelectorQuery().select('#i1').boundingClientRect(function (rect) { that.setData({ i1: rect.top }) }).exec(); wx.createSelectorQuery().select('#j1').boundingClientRect(function (rect) { that.setData({ j1: rect.top }) }).exec(); wx.createSelectorQuery().select('#t3').boundingClientRect(function (rect) { that.setData({ t3: rect.top }) }).exec(); wx.createSelectorQuery().select('#t4').boundingClientRect(function (rect) { that.setData({ t4: rect.top }) }).exec(); wx.createSelectorQuery().select('#t5').boundingClientRect(function (rect) { that.setData({ t5: rect.top }) }).exec(); that.setData({ id: queryBean, name:username, personName:uname }) recorderManager.start(options); if (that.data.num == 1) { that.setData({ num: 2, RecordPause: "暂停录音", condition: false }) } wx.authorize({ scope: 'scope.record', success() { console.log("录音授权成功"); that.setData({ status: 2, }) // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问 // wx.startRecord(); }, fail() { console.log("第一次录音授权失败"); wx.showModal({ title: '提示', content: '您未授权录音,功能将无法使用', showCancel: true, confirmText: "授权", confirmColor: "#52a2d8", success: function (res) { if (res.confirm) { //确认则打开设置页面(重点) wx.openSetting({ success: (res) => { console.log(res.authSetting); if (!res.authSetting['scope.record']) { //未设置录音授权 console.log("未设置录音授权"); wx.showModal({ title: '提示', content: '您未授权录音,功能将无法使用', showCancel: false, success: function (res) { }, }) } else { //第二次才成功授权 console.log("设置录音授权成功"); that.setData({ status: 2, }) // recorderManager.start(options); } }, fail: function () { console.log("授权设置录音失败"); } }) } else if (res.cancel) { console.log("cancel"); } }, fail: function () { console.log("openfail"); } }) } }) }, bindDateChange:function(e){ var that = this; that.setData({ date:e.detail.value }) }, formSubmit: function (e) {//定义函数名称 var that = this; console.log(that.data.id) that.setData({ buttonClicked: true }) var dataList = e.detail.value; for (var i in dataList){ if (i == "A03") { var list = dataList[i]; dataList[i] = list.replace("-", "/").replace("-", "/"); console.log(dataList[i]) } if (Array.isArray(dataList[i])){ var list =""; for (var n = 0; n < dataList[i].length;n++){ if(n == dataList[i].length-1){ list += dataList[i][n]; }else{ list += dataList[i][n] + ";"; } } dataList[i] = list; } } var dataLists = JSON.stringify(dataList); console.log(dataLists) var filePath = this.data.tempFilePath; for (var a in filePath){ console.log(1+a); wx.uploadFile({ url: "https://oa.dcqcdc.com/dccdc/Infection/SaveData_QuestionAnswer",//演示域名、自行配置 filePath: filePath[a], name: 'file', header: { "Content-Type": "multipart/form-data" }, formData: { id: that.data.id ,//附加信息为用户ID username: that.data.name }, success: function (res) { console.log(res) wx.showToast({ title: '上传成功', icon: 'success', duration: 2000 }) }, fail: function (res) { wx.showToast({ title: '上传失败', icon: 'success', duration: 2000 }) }, complete: function (res) { } }) } console.log(that.data.id) wx.request({ url: 'https://oa.dcqcdc.com/dccdc/Infection/SaveData_QuestionAnswer',//请求地址 data: { id: that.data.id, json: dataLists, username:that.data.name }, header: {//请求头 "Content-Type": "application/x-www-form-urlencoded" }, method: "POST",//get为默认方法/POST success: function (res) { console.log(res) if (res.data == "保存成功!"){ wx.showToast({ title: '上传成功', icon: 'success', duration: 2000, success:function(res){ setTimeout(function () { that.setData({ buttonClicked: false }) }, 500) } }) wx.navigateTo({ url: '../inquiry/inquiry?name=' + that.data.name, success: function (res) { }, fail: function (res) { }, complete: function (res) { }, }) } }, fail: function (err) { console.log(err) },//请求失败 complete: function () { }//请求完成后执行的函数 }) }, close: function () { var that = this; that.stop(); wx.navigateTo({ url: '../inquiry/inquiry?name='+that.data.name, success: function (res) { }, fail: function (res) { }, complete: function (res) { }, }) }, goTop: function (e) { // 锚点 if (wx.pageScrollTo) { wx.pageScrollTo({ scrollTop: e.currentTarget.dataset.topnum }) } else { wx.showModal({ title: '提示', content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。' }) } }, upMonth:function(e){ var that = this; that.setData({ month: e.detail.value }) }, upYear: function (e) { var that = this; that.setData({ year: e.detail.value }) }   })