uploadFile.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. var env = getApp().globalData.env;
  2. const base64 = require('base64.js');//Base64,hmac,sha1,crypto相关算法
  3. require('hmac.js');
  4. require('sha1.js');
  5. const Crypto = require('crypto.js');
  6. /*
  7. *上传文件到阿里云oss
  8. *@param - filePath :图片的本地资源路径
  9. *@param - dir:表示要传到哪个目录下
  10. *@param - successc:成功回调
  11. *@param - failc:失败回调
  12. */
  13. const uploadFile = function (filePath, dir, successc, failc) {
  14. if (!filePath || filePath.length < 9) {
  15. wx.showModal({
  16. title: '图片错误',
  17. content: '请重试',
  18. showCancel: false,
  19. })
  20. return;
  21. }
  22. var scale = null;
  23. wx.getImageInfo({
  24. src: filePath,
  25. success (res) {
  26. scale = (res.width/res.height).toFixed(2);
  27. }
  28. })
  29. console.log('上传图片.....');
  30. // console.log(dir)
  31. //图片名字 可以自行定义, 这里是采用当前的时间戳 + 150内的随机数来给图片命名的
  32. const aliyunFileKey = dir + new Date().getTime() + Math.floor(Math.random() * 150);
  33. // console.log(env)
  34. const aliyunServerURL = env.uploadImageUrl;//OSS地址,需要https
  35. const accessid = env.OSSAccessKeyId;
  36. const securityToken = env.securityToken;
  37. const policyBase64 = getPolicyBase64();
  38. const signature = getSignature(policyBase64);//获取签名
  39. wx.uploadFile({
  40. url: aliyunServerURL,//开发者服务器 url
  41. filePath: filePath,//要上传文件资源的路径
  42. name: 'file',//必须填file
  43. formData: {
  44. 'key': aliyunFileKey,
  45. 'policy': policyBase64,
  46. 'OSSAccessKeyId': accessid,
  47. 'signature': signature,
  48. 'success_action_status': '200',//自定义成功状态200
  49. 'x-oss-security-token': securityToken
  50. },
  51. success: function (res) {
  52. // console.log(res)
  53. if (res.statusCode != 200) {
  54. failc(new Error('上传错误:' + JSON.stringify(res)))
  55. return;
  56. }
  57. successc('https://cdn.toktok.beer/' + aliyunFileKey + '?scale=' + scale);
  58. },
  59. fail: function (err) {
  60. err.wxaddinfo = aliyunServerURL;
  61. failc(err);
  62. },
  63. })
  64. }
  65. const getPolicyBase64 = function () {
  66. let dateobj = new Date();
  67. dateobj.setHours(dateobj.getHours() + env.timeout);
  68. let srcT = dateobj.toISOString();
  69. const policyText = {
  70. "expiration": srcT, //设置该Policy的失效时间,超过这个失效时间之后,就没有办法通过这个policy上传文件了
  71. "conditions": [
  72. ["content-length-range", 0, 5 * 1024 * 1024] // 设置上传文件的大小限制,5mb
  73. ]
  74. };
  75. const policyBase64 = base64.encode(JSON.stringify(policyText));
  76. return policyBase64;
  77. }
  78. const getSignature = function (policyBase64) {
  79. const accesskey = env.AccessKeySecret;
  80. const bytes = Crypto.HMAC(Crypto.SHA1, policyBase64, accesskey, {
  81. asBytes: true
  82. });
  83. const signature = Crypto.util.bytesToBase64(bytes);
  84. return signature;
  85. }
  86. module.exports = uploadFile;