猫妈狗爸伴宠师小程序前端代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
3.2 KiB

  1. /**
  2. * 阿里云OSS工具类
  3. */
  4. import OSSConfig from "@/utils/oss-upload/oss/OSSConfig.js"
  5. /**
  6. * 生成一个随机的Key
  7. */
  8. function storeKey() {
  9. let s = [];
  10. let hexDigits = "0123456789abcdef";
  11. for (let i = 0; i < 36; i++) {
  12. s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
  13. }
  14. s[14] = "4";
  15. s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1);
  16. s[8] = s[13] = s[18] = s[23] = "-";
  17. return s.join("");
  18. }
  19. /**
  20. * 根据当天日期在OSS端生成文件夹
  21. */
  22. function storeFolder() {
  23. const date = new Date();
  24. const formatNumber = n => {
  25. n = n.toString()
  26. return n[1] ? n : '0' + n
  27. }
  28. return [date.getFullYear(), date.getMonth() + 1, date.getDate()].map(formatNumber).join('-')
  29. }
  30. /**
  31. * 阿里云OSS上传文件, 所有具体功能的工具函数均基于此
  32. * 注意, resolve时一定为上传成功, 返回OSS上的Key
  33. * @param filePath 待上传文件的URI
  34. * @param key 存储桶中的目标文件名
  35. * @param folder 存储桶中的目标文件夹
  36. */
  37. export function ossUpload(filePath, key = storeKey(), folder = storeFolder()) {
  38. return new Promise((resolve, reject) => {
  39. if (folder && folder?.length > 0) {
  40. if (folder[0] == "/") folder = folder.slice(1, folder.length)
  41. if (folder[folder.length - 1] != "/") folder += "/"
  42. key = folder + key
  43. }
  44. const filePrefixArr = filePath.split(".")
  45. key += `.${filePrefixArr[filePrefixArr.length - 1]}`
  46. let config = {
  47. url: OSSConfig.host,
  48. name: 'file',
  49. filePath,
  50. formData: {
  51. key,
  52. policy: OSSConfig.policyBase64,
  53. OSSAccessKeyId: OSSConfig.accessid,
  54. success_action_status: '200',
  55. signature: OSSConfig.signature,
  56. },
  57. success(res) {
  58. if (res.errMsg.includes("uploadFile:ok")) {
  59. resolve(OSSConfig.host + key)
  60. } else {
  61. reject(res)
  62. }
  63. },
  64. fail(err) {
  65. reject(err)
  66. }
  67. }
  68. uni.uploadFile(config)
  69. })
  70. }
  71. /**
  72. * 阿里云OSS上传图片
  73. * @param {compressed, key, folder, success, fail} compressed: 是否压缩 key: 存储桶中的目标文件名 folder: 存储桶中的目标文件夹
  74. */
  75. export function ossUploadImage({
  76. key,
  77. folder,
  78. compressed = true, //是否压缩
  79. }) {
  80. const sizeType = [compressed ? 'compressed' : 'original']
  81. return new Promise((success, fail) => {
  82. uni.chooseImage({
  83. count: 1,
  84. sizeType,
  85. success(res) {
  86. ossUpload(res.tempFilePaths[0], key, folder).then(success).catch(fail)
  87. },
  88. fail
  89. })
  90. })
  91. }
  92. /**
  93. * 阿里云OSS上传视频
  94. * @param { key, folder, sourceType, compressed, maxDuration, camera, success, fail}
  95. * key: 存储桶中的目标文件名 folder: 存储桶中的目标文件夹 其它参数同uni.chooseVideo(mpWeixin)
  96. */
  97. export function ossUploadVideo({
  98. key,
  99. folder,
  100. sourceType = ['album', 'camera'], //album 从相册选视频, camera 使用相机拍摄
  101. compressed = true, //是否压缩所选的视频源文件
  102. maxDuration = 60, //拍摄视频最长拍摄时间, 单位秒。最长支持 60 秒
  103. camera = 'back', //调用相机方向, 'front'、'back', 默认'back'
  104. }) {
  105. return new Promise((success, fail) => {
  106. uni.chooseVideo({
  107. sourceType,
  108. compressed,
  109. maxDuration,
  110. camera,
  111. success(res) {
  112. ossUpload(res.tempFilePath, key, folder).then(success).catch(fail)
  113. },
  114. fail
  115. })
  116. })
  117. }
  118. const OSS = {
  119. ossUploadVideo,
  120. ossUploadImage,
  121. ossUpload
  122. }
  123. export default OSS;