「小程式JAVA實戰」小程式視訊上傳方法的抽象複用(56)
在使用者中心有視訊上傳,在視訊展示的時候也是視訊上傳,如何將這個js抽象出來是個關鍵,現在咱們嘗試抽離到公共js中,方便呼叫。原始碼https://github.com/limingios/wxProgram.git 中No.15
抽象方法的步驟
- 新建公共js
- 找到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 }
-
需要使用的地方新增方法引入
>定義名稱,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(); } })
PS:目前用到了兩次匯入的方式,第一次第三方搜尋元件的時候,第二次是視訊上傳。
ofollow,noindex" target="_blank" href="http://zhanzhang.baidu.com/sitesubmit/index?sitename=https://idig8.com/2018/09/23/xiaochengxujavashizhanxiaochengxushipinshangchuanfangfadechouxiangfuyong56/">百度未收錄
>>原創文章,歡迎轉載。轉載請註明:轉載自IT人故事會,謝謝!
>>原文連結地址: