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.

175 lines
4.4 KiB

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