uploadFile.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 - successc:成功回调
  11. *@param - failc:失败回调
  12. */
  13. const uploadFile = function(type, filePath, dir, successc, 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. if (res.statusCode != 200) {
  48. // failc(new Error('上传错误:' + JSON.stringify(res)))
  49. console.log('上传错误:' + JSON.stringify(res));
  50. return;
  51. }
  52. successc(aliyunServerURL + "/" + aliyunFileKey);
  53. },
  54. fail: function(err) {
  55. err.wxaddinfo = aliyunServerURL;
  56. console.log('上传错误:' + JSON.stringify(err));
  57. },
  58. })
  59. }
  60. const getPolicyBase64 = function() {
  61. let date = new Date();
  62. date.setHours(date.getHours() + env.timeout);
  63. let srcT = date.toISOString();
  64. const policyText = {
  65. "expiration": srcT, //设置该Policy的失效时间,超过这个失效时间之后,就没有办法通过这个policy上传文件了
  66. "conditions": [
  67. ["content-length-range", 0, 500 * 1024 * 1024] // 设置上传文件的大小限制,5mb
  68. ]
  69. };
  70. const policyBase64 = base64.encode(JSON.stringify(policyText));
  71. console.log(policyBase64);
  72. return policyBase64;
  73. }
  74. const getSignature = function(policyBase64) {
  75. const accesskey = env.AccessKeySecret;
  76. const bytes = Crypto.HMAC(Crypto.SHA1, policyBase64, accesskey, {
  77. asBytes: true
  78. });
  79. const signature = Crypto.util.bytesToBase64(bytes);
  80. // console.log(signature);
  81. return signature;
  82. }
  83. module.exports = uploadFile;