沙盘演示系统应用的微信小程序
zuoxiao
2024-10-28 eb64e401aa8f22f510914a21ec3cf89e1594f6cb
pages/feedback/feedback.js
@@ -1,5 +1,9 @@
const audio = wx.createInnerAudioContext()
const recorderManager = wx.getRecorderManager()
const {
  BASEURL
} = require('../../api/config')
const app = getApp()
Page({
  data: {
    isRefreshing: false,
@@ -18,13 +22,15 @@
      width: 160,
      height: 160,
    },
    originFiles: [
    ],
    originFiles: [],
    feedBackList: Array(19).fill({
      createTime: "2023-05-06 12:36:25",
      responseTime: "2023-05-07 12:36:25"
    }),
    contentValue:''//反馈详情
    contentValue: '', //反馈详情
    accSavePath: '', //上传的音频文件接口返回的地址
    photoSavePath: [] //上传的照片文件返回的地址
  },
  onLoad() {
@@ -65,7 +71,7 @@
          time: 3000
        })
      } else {
        that.setData({
          voiceTime: voiceTime,
          isShowVoiceMask: false,
@@ -78,47 +84,9 @@
        } = res; //这里松开按钮 会返回录音本地路径
        audio.src = tempFilePath
        console.log(tempFilePath);
        that.upACC(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;
      //   }
      //  }
      // })
    });
    recorderManager.onStart(() => {
      console.log('录音开始');
@@ -126,6 +94,26 @@
    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");
@@ -135,13 +123,28 @@
    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() {
  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");
@@ -151,27 +154,74 @@
    const {
      originFiles
    } = this.data;
    this.setData({
      originFiles: [...originFiles, ...files], // 此时设置了 fileList 之后才会展示选择的图片
    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) {
  },
  /**
   * 上传图片
   */
  onUpload() {
    wx.uploadFile({
      url: 'https://example.weixin.qq.com/upload', // 仅为示例,非真实的接口地址
      filePath: file.url,
  onUploadPhoto(imgPath) {
    const uploadTask = wx.uploadFile({
      url: BASEURL + "wx/webFile/upPhone", // 仅为示例,非真实的接口地址
      filePath: imgPath,
      name: 'file',
      formData: {
        user: 'test'
      header: {
        'tag': app.globalData.tag,
        'appId': app.globalData.AppID,
      },
      success: () => {
        this.setData({
          [`fileList[${length}].status`]: 'done',
      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() {
@@ -296,7 +346,7 @@
      isRefreshing: false
    });
  },
  feelBack(){
  feelBack() {
    wx.showLoading({
      title: '正在提交...', // 加载提示文字
      mask: true // 是否显示透明蒙层,防止触摸穿透,默认为 false
@@ -340,11 +390,34 @@
      // }
    });
  },
  handleDelete(){
   this.setData({
    isShowVoiceView:false,
    voiceTime:0,
   })
  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() {}
      })
    }
  }
});