const audio = wx.createInnerAudioContext() const recorderManager = wx.getRecorderManager() const { BASEURL } = require('../../api/config') const app = getApp() Page({ data: { isRefreshing: false, currentTab: 0, recordingSrc: '', isRecording: false, isshowVoiceMask: false, //是否显示录音中 isShowVoiceView: false, voiceTime: 0, //录音时长 maxVoiceTime: 60, //最长录音时间 contDownTime: 0, line2Opcity: 1, line3Opcity: 1, gridConfig: { column: 3, width: 160, height: 160, }, originFiles: [], feedBackList: Array(19).fill({ createTime: "2023-05-06 12:36:25", responseTime: "2023-05-07 12:36:25" }), contentValue: '', //反馈详情 accSavePath: '', //上传的音频文件接口返回的地址 photoSavePath: [] //上传的照片文件返回的地址 }, onLoad() { wx.getSetting({ success: (res) => { if (!res.authSetting['scope.record']) { // 如果用户没有授权录音权限,发起授权窗口 wx.authorize({ scope: 'scope.record', success() { // 用户已经同意小程序使用录音功能,可以继续操作 console.log('用户已授权录音'); }, fail() { // 用户拒绝授权,可以给出提示或再次发起授权窗口 console.log('用户拒绝了授权录音'); } }); } else { // 用户已经授权录音,可以直接进行录音操作 console.log('用户已授权录音'); } } }); var that = this; recorderManager.onStop((res) => { console.log('录音停止', voiceTime); const voiceTime = Math.floor(res.duration / 1000) if (voiceTime < 2) { this.setData({ isShowVoiceMask: false, contDownTime: this.data.maxVoiceTime, isShowVoiceView: false }) wx.showToast({ title: '录音时间过短', icon: 'error', time: 3000 }) } else { that.setData({ voiceTime: voiceTime, isShowVoiceMask: false, contDownTime: this.data.maxVoiceTime, isShowVoiceView: true }) console.log('res', res); const { tempFilePath } = res; //这里松开按钮 会返回录音本地路径 audio.src = tempFilePath console.log(tempFilePath); that.upACC(tempFilePath); } }); recorderManager.onStart(() => { console.log('录音开始'); }); recorderManager.onError((err) => { console.log('录音错误', err); }); }, //上传音频文件 upACC(tempFilePath) { //上传录制的音频到服务器 wx.uploadFile({ url: BASEURL + "wx/webFile/upPhone", //接口地址 name: 'file', //上传文件名 filePath: tempFilePath, header: { 'tag': app.globalData.tag, 'appId': app.globalData.AppID, }, success: function (res) { //后台返回给前端识别后的文字 console.log('录音上传成功', res); }, fail: (err) => { // 处理上传失败的逻辑 console.log('录音上传失败', err); } }) }, handleRemove(e) { console.log("handleRemove"); const { index } = e.detail; const { originFiles } = this.data; let imgPath = e.detail.file.url; const updatedFiles = this.data.originFiles.map((file) => { //当上传失败点击时重新上传 if (file.url === imgPath && file.uploadTask) { file.uploadTask.abort(); } }); originFiles.splice(index, 1); this.setData({ originFiles, }); }, handleClick(e) { console.log("handleClick"); let imgPath = e.detail.file.url; const updatedFiles = this.data.originFiles.map((file) => { //当上传失败点击时重新上传 if (file.url === imgPath && file.status === "reload") { this.onUploadPhoto(imgPath); } }); }, handleAdd(e) { console.log("handleAdd"); const { files } = e.detail; const { originFiles } = this.data; const updatedList = files.map(item => { return { ...item, status: 'loading', uploadTask: this.onUploadPhoto(e.detail.files[0].url) }; // 保留所有其他字段,并添加 displayText 字段 }); // 更新列表数据 this.setData({ originFiles: [...originFiles, ...updatedList], // 此时设置了 fileList 之后才会展示选择的图片 }); }, handleSuccess(e) { }, /** * 上传图片 */ onUploadPhoto(imgPath) { const uploadTask = wx.uploadFile({ url: BASEURL + "wx/webFile/upPhone", // 仅为示例,非真实的接口地址 filePath: imgPath, name: 'file', header: { 'tag': app.globalData.tag, 'appId': app.globalData.AppID, }, success: (res) => { console.log('图片上传成功', res); }, fail: (err) => { // 处理上传失败的逻辑 const updatedFiles = this.data.originFiles.map((file) => { if (file.url === imgPath) { return { ...file, percent: progress, status: "reload", // 上传失败状态 }; } return file; }); this.setData({ originFiles: updatedFiles, }); } }); if (uploadTask) { uploadTask.onProgressUpdate((res) => { //处理 const progress = Math.round((res.progress / 100) * 100) const updatedFiles = this.data.originFiles.map((file) => { if (file.url === imgPath) { return { ...file, percent: progress, status: progress < 100 ? 'loading' : undefined, // 更新状态 uploadTask: progress < 100 ? file.uploadTask : undefined, // 保持或删除uploadTask }; } return file; }); this.setData({ originFiles: updatedFiles, }); }); } return uploadTask; }, //按住按钮 startHandel() { this.setData({ isShowVoiceMask: true, contDownTime: this.data.maxVoiceTime, voiceTime: 0, isShowVoiceView: false }) console.log("开始录音") recorderManager.start({ duration: 0 }) this.startRecordingCountdown(); }, //松开按钮 endHandle() { this.stopRecordingCountdown(); console.log("结束") //触发录音停止 recorderManager.stop() }, // 播放 handlePlay(e) { // 倒计时 let time = this.data.voiceTime audio.play() let timer1 = -1, timer2 = -1, timer3 = -1, timer4 = -1, timer5 = -1, timer6 = -1; // 第一次播放为0 第二次播放2秒钟 timer6 = setInterval(() => { console.log('时间', time); if (time <= 0) { this.setData({ line2Opcity: 1, line3Opcity: 1 }) clearInterval(timer6) clearTimeout(timer2) clearTimeout(timer3) clearTimeout(timer4) clearTimeout(timer5) return } timer2 = setTimeout(() => { console.log('timer2'); this.setData({ line2Opcity: 0, line3Opcity: 0 }) }, 200) timer3 = setTimeout(() => { console.log('timer3'); this.setData({ line2Opcity: 1, line3Opcity: 0 }) }, 400) timer4 = setTimeout(() => { console.log('timer4'); this.setData({ line2Opcity: 1, line3Opcity: 1 }) }, 600) timer5 = setTimeout(() => { console.log('timer5'); this.setData({ line2Opcity: 0, line3Opcity: 0 }) }, 800) }, 800) // 倒计时 timer1 = setInterval(() => { time--; if (time <= 0) { clearInterval(timer1) return } }, 1000) }, //开启定时器 startRecordingCountdown() { console.log("startRecordingCountdown"); const that = this; this.recordingInterval = setInterval(() => { const newVoiceTime = that.data.voiceTime + 1; const newContDownTime = that.data.contDownTime - 1; that.setData({ voiceTime: newVoiceTime, contDownTime: newContDownTime }); if (this.data.voiceTime >= this.data.maxVoiceTime) { that.stopRecordingCountdown(); that.endHandle(); } }, 1000); }, stopRecordingCountdown() { console.log("stopRecordingCountdown"); if (this.recordingInterval) { clearInterval(this.recordingInterval); this.recordingInterval = null; console.log("Recording countdown stopped"); } }, // 切换 Tabs switchTab: function (e) { const tab = parseInt(e.currentTarget.dataset.tab); this.setData({ currentTab: tab }); }, onPullDownRefresh() { this.setData({ isRefreshing: false }); }, feelBack() { wx.showLoading({ title: '正在提交...', // 加载提示文字 mask: true // 是否显示透明蒙层,防止触摸穿透,默认为 false }); const app = getApp(); const data = { content: this.data.contentValue, //取水口ID image: this.data.vcId, //虚拟卡ID operator: app.globalData.sessionId, //操作员 forceOpen: !!isforce // 使用逻辑非操作符 !! 来确保 isForce 是布尔值 }; post({ url: "operation/feedback/add", data: data }).then(response => { // 处理成功响应 console.log('请求成功:', response); // 加载完成后隐藏加载动画 wx.hideLoading(); //完成后回到首页 wx.reLaunch({ url: '/pages/home/home?param=true' // 首页的路径,根据实际情况填写 }); }).catch(error => { // 加载完成后隐藏加载动画 wx.hideLoading(); // 处理错误响应 console.error('请求失败:', error); // if (error.code === "10005") { // this.setData({ // showDialog: false, // showForceConfirm: true // }) // } else { this.setData({ showErrorDialog: true, errorData: error.msg }) // } }); }, handleDelete() { this.setData({ isShowVoiceView: false, voiceTime: 0, }) }, submit() { if (this.data.contentValue !== "" || this.data.photoSavePath.length>0 || this.data.accSavePath !== "") { wx.showToast({ title: '提交成功', icon: 'success', duration: 2000, success() {} }) setTimeout(() => { wx.navigateBack({ delta: 1 }); }, 2000); } else { wx.showToast({ title: '请输入一项反馈内容', icon: 'error', duration: 2000, success() {} }) } } });