tpupload.js 4.28 KB
// components/tpupload/tpupload.js
import util from '../../utils/util'
import App from '../../utils/api'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    uploadDesc: String,
    tpKey: String,
    tpNum: Number,
    files: Array,
  },

  /**
   * 组件的初始数据
   */
  data: {
    files: [],
    filesHttps: [],
    token: '',
  },
  lifetimes: {
    attached: function () {},
    detached: function () {},
  },
  pageLifetimes: {
    show() {
      App.Qiniu.GetUploadToken().then(res => {
        let token = res.data.token
        this.setData({
          token
        })
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    // 删除图片或者视频
    deleteFun(e) {
      var files = this.data.files;
      var that = this;
      var index = e.currentTarget.dataset.index;
      let filesHttps = this.data.filesHttps;
      util.deletelUpload(filesHttps[index].url, this.data.token, (res => {
        filesHttps.splice(index, 1)
        files.splice(index, 1)
        this.setData({
          filesHttps,
          files
        })
      }))
      that.triggerEvent('tpupload', {
        files,
        filesHttps
      })
    },
    // 预览图片
    previewImg(e) {
      var index = e.currentTarget.dataset.index;
      var files = this.data.files;
      let arr = []
      files.forEach(i => {
        arr.push(i.path)
      })
      wx.previewImage({
        current: files[index].path,
        urls: arr
      })
    },
    // 选择视频  上传视频  chooseMedia方法一次只能上传一个视频
    uploadVideos() {
      var that = this;
      var files = that.data.files;
      wx.chooseMedia({
        mediaType: ['video'],
        count: 1, // 默认9
        sourceType: ['camera', 'album'],
        success: function (res) {
          var tempFilePaths = res.tempFiles;
          if (files.length + tempFilePaths.length > that.data.tpNum) {
            wx.showToast({
              icon: 'none',
              title: "最多可上传" + that.data.tpNum + "张"
            })
            return
          }
          let arr = []
          for (var i = 0; i < tempFilePaths.length; i++) {
            arr.push(tempFilePaths[i].tempFilePath);
          }
          that.addFun('videoNew', arr, 'video', tempFilePaths)
        }
      })
    },
    // 上传文件或者视频
    // path:上传到七牛的路径名称
    // arr:要上传的文件
    // type:video或者image
    // tempFilePaths:上传成功才能给页面加上传的图片或者视频
    addFun(path, arr, type, tempFilePaths) {
      var that = this;
      var files = that.data.files;
      var filesHttps = that.data.filesHttps;
      let uploadPromises = arr.map(i => {
        return util.uploadFile(i, this.data.token, path).then(res => {
          return res;
        });
      });
      Promise.all(uploadPromises).then(results => {
        results.forEach(i => {
          filesHttps.push({
            url: i,
            avinfo: type == 'video' ? true : false
          })
        })
        for (var i = 0; i < tempFilePaths.length; i++) {
          files.push({
            path: tempFilePaths[i].tempFilePath,
            type
          });
        }
        that.triggerEvent('tpupload', {
          files,
          filesHttps
        })
        that.setData({
          filesHttps,
          files
        });
      });
    },
    // 选择图片  上传图片
    uploadImages() {
      var that = this;
      var files = that.data.files;
      wx.chooseMedia({
        mediaType: ['image'],
        count: this.data.tpNum, // 默认9
        sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有
        sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机,默认二者都有
        success: function (res) {
          var tempFilePaths = res.tempFiles;
          if (files.length + tempFilePaths.length > that.data.tpNum) {
            wx.showToast({
              icon: 'none',
              title: "最多可上传" + that.data.tpNum + "张"
            })
            return
          }
          let arr = []
          for (var i = 0; i < tempFilePaths.length; i++) {
            arr.push(tempFilePaths[i].tempFilePath);
          }
          that.addFun('imagesNew', arr, 'image', tempFilePaths)
        }
      });
    },
  }
})