天天看點

「小程式JAVA實戰」小程式視訊上傳方法的抽象複用(57)

在使用者中心有視訊上傳,在視訊展示的時候也是視訊上傳,如何将這個js抽象出來是個關鍵,現在咱們嘗試抽離到公共js中,友善調用。源碼https://github.com/limingios/wxProgram.git 中No.15

抽象方法的步驟

  • 建立公共js
「小程式JAVA實戰」小程式視訊上傳方法的抽象複用(57)
  • 找到mine中視訊上傳的代碼拷貝到videoUtils.js中,并修改裡面的内容
function uploadVideo() {
  var me = this
  wx.chooseVideo({
    sourceType: ['album', 'camera'],
    success: function (res) {
      console.log(res);
      var tempDuration = res.duration;
      var tempHeight = res.height;
      var tempWidth = res.width;
      var tempSize = res.size;
      var tempFilePath = res.tempFilePath;
      var thumbTempFilePath = res.thumbTempFilePath;
      if (tempDuration > 20) {
        wx.showToast({
          title: "視訊太長了老鐵不穩~",
          icon: 'none',
          duration: 3000
        })
      } else if (tempDuration < 5) {
        wx.showToast({
          title: "視訊太短了不到5秒。老鐵不穩~",
          icon: 'none',
          duration: 3000
        })
      } else {
        wx.navigateTo({
          url: '../chooseBgm/chooseBgm?tempDuration=' + tempDuration
            + '&tempHeight=' + tempHeight
            + '&tempWidth=' + tempWidth
            + '&tempSize=' + tempSize
            + '&tempFilePath=' + tempFilePath
            + '&thumbTempFilePath=' + thumbTempFilePath
        })
      }
    }
  })
}

#導出方法,并關聯方法名稱
module.exports={
  uploadVideo: uploadVideo
}           

複制

「小程式JAVA實戰」小程式視訊上傳方法的抽象複用(57)
  • 需要使用的地方添加方法引入
定義名稱,require引入,在需要的方法裡面直接定義的名稱點導出的方法就可以了。
var videoUtils = require('../../utils/videoUtils.js')
Page({

  data: {
    cover:'cover',
    videoContext:""
  },
  showSearch:function(){
    wx.navigateTo({
      url: '../videoSearch/videoSearch',
    })
  },
  onLoad:function(){
    var me = this;
    me.videoContext = wx.createVideoContext('myVideo', me);

  },
  onShow:function(){
    var me = this;
    me.videoContext.play();
  },
  onHide:function(){
    var me = this;
    me.videoContext.pause();
  },
  upload:function(){
    videoUtils.uploadVideo();
  }
})           

複制

「小程式JAVA實戰」小程式視訊上傳方法的抽象複用(57)

PS:目前用到了兩次導入的方式,第一次第三方搜尋元件的時候,第二次是視訊上傳。