uploadFile.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. const env = require('./config.js'); //配置文件,在这文件里配置你的OSS keyId和KeySecret,timeout:87600;
  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 - success:成功回调
  11. *@param - failc:失败回调
  12. */
  13. const uploadFile = function(type, filePath, dir, success, failc) {
  14. console.log('type', type)
  15. if (!filePath ) {
  16. uni.showModal({
  17. title: '图片上传错误',
  18. content: '请重试',
  19. showCancel: false,
  20. })
  21. return;
  22. }
  23. let aliyunFileKey = ''
  24. if (type == 'image') {
  25. aliyunFileKey = dir + new Date().getTime() + Math.floor(Math.random() * 150) + '.png';
  26. } else {
  27. aliyunFileKey = dir + new Date().getTime() + Math.floor(Math.random() * 150) + '.mp4';
  28. }
  29. //图片名字 可以自行定义, 这里是采用当前的时间戳 + 150内的随机数来给图片命名的
  30. // const aliyunFileKey = dir + new Date().getTime() + Math.floor(Math.random() * 150) + '.png';
  31. const aliyunServerURL = env.uploadImageUrl; //OSS地址,需要https
  32. const accessid = env.OSSAccessKeyId;
  33. const policyBase64 = getPolicyBase64();
  34. const signature = getSignature(policyBase64); //获取签名
  35. uni.uploadFile({
  36. url: aliyunServerURL, //开发者服务器 url
  37. filePath: filePath, //要上传文件资源的路径
  38. name: 'file', //必须填file
  39. formData: {
  40. 'key': aliyunFileKey,
  41. 'policy': policyBase64,
  42. 'OSSAccessKeyId': accessid,
  43. 'signature': signature,
  44. 'success_action_status': '200',
  45. },
  46. success: function(res) {
  47. debugger
  48. console.log(res);
  49. if (res.statusCode != 200) {
  50. failc(new Error('上传错误:' + JSON.stringify(res)))
  51. return;
  52. }
  53. success(aliyunServerURL + "/" + aliyunFileKey);
  54. },
  55. fail: function(err) {
  56. err.wxaddinfo = aliyunServerURL;
  57. failc(err);
  58. },
  59. })
  60. }
  61. const getPolicyBase64 = function() {
  62. let date = new Date();
  63. date.setHours(date.getHours() + env.timeout);
  64. let srcT = date.toISOString();
  65. const policyText = {
  66. "expiration": srcT, //设置该Policy的失效时间,超过这个失效时间之后,就没有办法通过这个policy上传文件了
  67. "conditions": [
  68. ["content-length-range", 0, 5 * 1024 * 1024] // 设置上传文件的大小限制,5mb
  69. ]
  70. };
  71. const policyBase64 = base64.encode(JSON.stringify(policyText));
  72. console.log(policyBase64);
  73. return policyBase64;
  74. }
  75. const getSignature = function(policyBase64) {
  76. const accesskey = env.AccessKeySecret;
  77. const bytes = Crypto.HMAC(Crypto.SHA1, policyBase64, accesskey, {
  78. asBytes: true
  79. });
  80. const signature = Crypto.util.bytesToBase64(bytes);
  81. // console.log(signature);
  82. return signature;
  83. }
  84. module.exports = uploadFile;