沙盘演示系统应用的微信小程序
zuoxiao
2024-10-18 35cd1fc2e97ee8c8ec93d632cfc6b8b90e7a6e2b
pages/feedback/feedback.js
@@ -1,55 +1,130 @@
const audio = wx.createInnerAudioContext()
const recorderManager = wx.getRecorderManager()
Page({
  data: {
    isRefreshing: false,
    currentTab: 0,
    recordingSrc: '',
    isRecording: false,
    isshowVoiceMask: true, //是否显示录音中
    voiceTime: 2, //录音时长
    isshowVoiceMask: false, //是否显示录音中
    isShowVoiceView: false,
    voiceTime: 0, //录音时长
    maxVoiceTime: 60, //最长录音时间
    contDownTime: 0,
    line2Opcity: 1,
    line3Opcity: 1,
    gridConfig: {
      column: 3,
      width: 160,
      height: 160,
    },
    originFiles: [{
        url: 'https://tdesign.gtimg.com/mobile/demos/example4.png',
        name: 'uploaded1.png',
        type: 'image',
      },
      {
        url: 'https://tdesign.gtimg.com/mobile/demos/example6.png',
        name: 'uploaded2.png',
        type: 'image',
      }
    ]
    originFiles: [
    ],
    feedBackList: Array(19).fill({
      createTime: "2023-05-06 12:36:25",
      responseTime: "2023-05-07 12:36:25"
    }),
    contentValue:''//反馈详情
  },
  onLoad() {
    this.recorderManager = wx.getRecorderManager();
    this.recorderManager.onStart(() => {
      console.log('recorder start');
      this.setData({
        isRecording: true
      });
      this.startWaveformDrawing();
    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);
      }
    this.recorderManager.onStop((res) => {
      console.log('recorder stop', res);
      const {
        tempFilePath
      } = res;
      this.setData({
        recordingSrc: tempFilePath,
        isRecording: false
      });
      this.stopWaveformDrawing();
      //上传录制的音频到服务器
      // wx.uploadFile({
      //  url: '接口地址' + api.voice, //接口地址
      //  name: 'file', //上传文件名
      //  filePath: tempFilePath,
      //  success: function (res) { //后台返回给前端识别后的文字
      //   var model = res.data
      //   var modeljson = JSON.parse(model)
      //   if (modeljson.status_code == 500) {
      //    wx.showToast({
      //     title: '语音转换失败',
      //     image: '/assets/image/icon/fail@2x.png'
      //    })
      //    return false;
      //   }
      //   if (modeljson.meta.status_code === 200 && !modeljson.data.err_msg) {
      //    var saymessage = modeljson.data.message;
      //    wx.setStorageSync('sayinfo', saymessage)
      //    that.setData({
      //     inpvalue: saymessage
      //    })
      //    setTimeout(() =>{
      //     wx.navigateTo({
      //      url: '../loding/loding'
      //     })
      //    },2000)
      //    setTimeout(() => {
      //     wx.hideLoading();
      //    }, 100)
      //   } else if (modeljson.data.err_msg) {
      //    wx.showToast({
      //     title: '请大声说话',
      //     image: '/assets/image/icon/fail@2x.png'
      //    })
      //    return false;
      //   }
      //  }
      // })
    });
    this.recorderManager.onError((res) => {
      console.error(res);
      this.setData({
        isRecording: false
      });
      this.stopWaveformDrawing();
    recorderManager.onStart(() => {
      console.log('录音开始');
    });
    recorderManager.onError((err) => {
      console.log('录音错误', err);
    });
  },
  handleRemove(e) {
@@ -99,136 +174,177 @@
    });
  },
  //按住按钮
  startHandel () {
  startHandel() {
    this.setData({
     isShowVoiceMask:true
      isShowVoiceMask: true,
      contDownTime: this.data.maxVoiceTime,
      voiceTime: 0,
      isShowVoiceView: false
    })
    console.log("开始录音")
    wx.getRecorderManager().start({
     duration: 0
    recorderManager.start({
      duration: 0
    })
   },
   //松开按钮
   endHandle () {
    this.setData({
     isShowVoiceMask:false
    })
    this.startRecordingCountdown();
  },
  //松开按钮
  endHandle() {
    this.stopRecordingCountdown();
    console.log("结束")
    const recorderManager = wx.getRecorderManager()
    //录音停止函数
    var that = this;
    wx.getRecorderManager().onStop((res) => {
      const voiceTime =  Math.floor(res.duration/1000)
      console.log('voiceTime',voiceTime);
      that.setData({
        voiceTime
      })
      console.log('res',res);
     const { tempFilePath } = res; //这里松开按钮 会返回录音本地路径
     audio.src = tempFilePath
     console.log(tempFilePath);
     //上传录制的音频到服务器
     // wx.uploadFile({
     //  url: '接口地址' + api.voice, //接口地址
     //  name: 'file', //上传文件名
     //  filePath: tempFilePath,
     //  success: function (res) { //后台返回给前端识别后的文字
     //   var model = res.data
     //   var modeljson = JSON.parse(model)
     //   if (modeljson.status_code == 500) {
     //    wx.showToast({
     //     title: '语音转换失败',
     //     image: '/assets/image/icon/fail@2x.png'
     //    })
     //    return false;
     //   }
     //   if (modeljson.meta.status_code === 200 && !modeljson.data.err_msg) {
     //    var saymessage = modeljson.data.message;
     //    wx.setStorageSync('sayinfo', saymessage)
     //    that.setData({
     //     inpvalue: saymessage
     //    })
     //    setTimeout(() =>{
     //     wx.navigateTo({
     //      url: '../loding/loding'
     //     })
     //    },2000)
     //    setTimeout(() => {
     //     wx.hideLoading();
     //    }, 100)
     //   } else if (modeljson.data.err_msg) {
     //    wx.showToast({
     //     title: '请大声说话',
     //     image: '/assets/image/icon/fail@2x.png'
     //    })
     //    return false;
     //   }
     //  }
     // })
    })
    //触发录音停止
    wx.getRecorderManager().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
      })
    },300)
    timer3 = setTimeout(()=>{
      console.log('timer3');
        this.setData({
          line2Opcity: 1,
          line3Opcity:0
        })
    },600)
    timer4 = setTimeout(()=>{
      console.log('timer4');
    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
      })
    },900)
    timer5 = setTimeout(()=>{
      console.log('timer5');
        })
        clearInterval(timer6)
        clearTimeout(timer2)
        clearTimeout(timer3)
        clearTimeout(timer4)
        clearTimeout(timer5)
        return
      }
      timer2 = setTimeout(() => {
        console.log('timer2');
        this.setData({
          line2Opcity: 0,
          line3Opcity: 0
        })
    },1200)
  },1200)
  // 倒计时
  timer1 = setInterval(()=>{
    time--;
    if(time <= 0){
      clearInterval(timer1)
      return
    }
},1000)
      }, 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,
   })
  }
});