// mycomponent/yz_upVideo/yz_upVideo.js
var app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    width:{
      value:"100%",
      type:String
    },
    height:{
      value:"100%",
      type:String
    },
    videoUrl:{
      value:"",
      type:String
    },
    limit:{
      value:0,
      type:Number
    }
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    onReadVideo() {
      //iOS中选择上传视频不能超过60秒
      wx.chooseVideo({
        sourceType: ["album", "camera"],
        maxDuration: 60,
        camera: "back",
        success: (res) => {
          this.getDuration(res.duration, res.tempFilePath);
        },
      });
    },
    getDuration(durationData, videoData) {
      let duration = durationData;
  
      //获取视频或者音频时长
      this.data.duration = duration;
      console.log("视频时长：", this.data.duration, this.data.limit + 1, this.data.limit && this.data.duration > this.data.limit + 1);
      if (this.data.duration < 1) {
        this.setData({
          videoUrl: null,
        });
        return app.tips("视频时长太短了！");
      } else if (this.data.limit != 0 && this.data.duration > parseInt(this.data.limit) + 1) {
        this.setData({
          videoUrl: null,
        });
        return app.tips(`视频时长不能超过${this.data.limit}秒`);
      } else {
        this.setData({
          videoUrl: videoData,
        });
        this.setSrcAndCaptureImage(videoData);
        return;
      }
    },
    setSrcAndCaptureImage(videoData) {
      this.triggerEvent('beforeUp', videoData);
      this.uploadAudioM(videoData); //上传视频 --------------
    },
    uploadAudioM(videoData) {
      let urlStr = app.getNetAddresss("upload.uploadPic");
      urlStr += "&upload_type=video";
      wx.uploadFile({
        url: urlStr, //仅为示例，非真实的接口地址
        filePath: videoData,
        name: "file",
        formData: {},
        success:(resdata)=> {
          let res = JSON.parse(resdata.data);
          if(res.result!=1){
            this.setData({videoUrl:null});
          }
          this.triggerEvent('successUp', {res});
        },
      });
    },
  }
});
