微信小程序存放视频文件到阿里云用到算法js脚本文件

  
 
 
peterhuang007/weixinFileToaliyun: 微信小程序存放视频文件到阿里云用到算法js脚本文件 https://github.com/peterhuang007/weixinFileToaliyun
 
 
 
require('./crypto/hmac.js');
require('./crypto/sha1.js');
const env = require('./config.js');
const Base64 = require('./Base64.js');
const Crypto = require('./crypto/crypto.js');
// const uploadFile = function (filePath, fileW, objectId, successCB, errorCB) {
const uploadFile = function(aliyunLogicDir, filePath, successCB, errorCB) {
  // const uploadFile = function (aliyunLogicDir,filePath) {
  console.log(filePath)
  if (!filePath || filePath.length < 9) {
    wx.showModal({
      title: '视频错误',
      content: '请重试',
      showCancel: false,
    })
    return;
  }
  console.log('上传视频…');
  //const aliyunFileKey = fileW+filePath.replace('wxfile://', ''); 

  const aliyunFileKey = aliyunLogicDir + filePath.split("/")[filePath.split("/").length - 1];
  console.log(aliyunFileKey);
  const aliyunServerURL = env.aliyunServerURL;
  const OSSAccessKeyId = env.OSSAccessKeyId;
  const policyBase64 = getPolicyBase64();
  const signature = getSignature(policyBase64);
  console.log('aliyunFileKey=', aliyunFileKey);
  wx.uploadFile({
    url: aliyunServerURL, //仅为示例,非真实的接口地址
    filePath: filePath,
    name: 'file',
    formData: {
      'key': aliyunFileKey,
      'OSSAccessKeyId': OSSAccessKeyId,
      'policy': policyBase64,
      'Signature': signature,
      'success_action_status': '200',
    },
    success: function(res) {
      console.log(res)
      if (res.statusCode != 200) {
        errorCB(new Error('上传错误:' + JSON.stringify(res)))
        return;
      }
      console.log('上传视频成功', res)
      successCB(aliyunFileKey);
    },
    fail: function(err) {
      console.log(err)

      err.wxaddinfo = aliyunServerURL;
      errorCB(err);
    },
  })
}

const getPolicyBase64 = function() {
  let date = new Date();
  date.setHours(date.getHours() + env.timeout);
  let srcT = date.toISOString();
  const policyText = {
    "expiration": srcT, //设置该Policy的失效时间,超过这个失效时间之后,就没有办法通过这个policy上传文件了 指定了Post请求必须发生在2020年01月01日12点之前("2020-01-01T12:00:00.000Z")。
    "conditions": [
      ["content-length-range", 0, 20 * 1024 * 1024] // 设置上传文件的大小限制,1048576000=1000mb
    ]
  };

  const policyBase64 = Base64.encode(JSON.stringify(policyText));
  return policyBase64;
}

const getSignature = function(policyBase64) {
  const accesskey = env.AccessKeySecret;

  const bytes = Crypto.HMAC(Crypto.SHA1, policyBase64, accesskey, {
    asBytes: true
  });
  const signature = Crypto.util.bytesToBase64(bytes);

  return signature;
}

module.exports = uploadFile;

 

const uploadImage = require('../../utils/uploadAliyun.js');
var gd = getApp().globalData;
var imgUrlApp = gd.imgUrlApp;
var localImgPath = gd.localImgPath;
var apiUrlApp = gd.apiUrlApp;
var searchInput = "";
Page({
  data: {
    apiUrlApp: apiUrlApp,
    imgUrlApp: imgUrlApp,
    localImgPath: localImgPath,
    navbar: ['tab0', 'tab1', 'tab2'],
    currentTab: 0,
  },
  //响应点击导航栏
  navbarTap: function (e) {
    var idx = e.currentTarget.dataset.idx
    this.setData({
      currentTab: idx,
      TypeItem: this.data.navbar[this.data.currentTab],
    })
    switch (idx) {
      case 0:
        this.pubBK();
        break;
      case 1:
        this.myBKItemList();
        break;
      case 2:
      default:
        this.myBKDetailList();
        break;
    }
  },

  upLoad: function (e) {
    wx.chooseVideo({
      success: function (res) {
        console.log(res)
        var tempFilePath = res.tempFilePath
        console.log(tempFilePath)
        uploadImage("logicDir/video/" + wx.getStorageSync("username") + "/", tempFilePath, function () {
          console.log('ok')
        }, function () {
          console.log('NOTok')
        })
      }
    });
  },
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          //动态根据手机分辨率来计算内容的高度(屏幕总高度-顶部筛选栏的高度)
          contentHeight: (res.windowHeight - 72 * res.screenWidth / 750)
        });
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  onShow: function () {
    // 页面显示  
  },
  onHide: function () {
    // 页面隐藏  
  },
  onUnload: function () {
    // 页面关闭  
  },


})

  

 

 

 

 

 

 
 
 
 
<view>
  <image bindtap="upLoad" mode="aspectFit" src="{{localImgPath}}icon-plus-16.svg"></image>
</view>

  

 

 

 
 
 
 
 
 
 
 
 
posted @ 2018-07-21 16:45  papering  阅读(862)  评论(0编辑  收藏  举报