oss-file-upload.ts 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import request from '@/utils/request';
  2. import axios from 'axios';
  3. import COS from 'cos-js-sdk-v5';
  4. export const ossSwitch = 'tencent' as 'ks3' | 'tencent'; // 上传文件服务商
  5. const tencentBucket = 'daya-online-1303457149';
  6. /**
  7. * 管乐团 gyt/
  8. * 酷乐秀 klx/
  9. * 课堂乐器 ktqy/
  10. * 管乐迷 gym/
  11. */
  12. // 定义一个cos 对象
  13. /**
  14. * 获取上传文件签名
  15. * @param params 上传对应参数
  16. * { filename: fileName,
  17. bucketName: props.bucketName,
  18. postData: {
  19. filename: fileName,
  20. acl: 'public-read',
  21. key: fileName,
  22. unknowValueField: []
  23. }}
  24. * @param oss 服务商 ks3 tencent
  25. * @returns ”{'signatur'':'',''kssAccessKeyI'':'',''policy': '' }“
  26. */
  27. export const getUploadSign = async (params: any) => {
  28. const { bucketName, filename, postData } = params;
  29. const ossType = ossSwitch;
  30. let bucket = bucketName;
  31. let file = filename;
  32. // const key = postData.key;
  33. let tempPostData: any = {};
  34. if (ossType === 'tencent') {
  35. bucket = tencentBucket;
  36. file = 'ktqy/' + filename;
  37. tempPostData = {
  38. key: 'ktqy/' + postData.key
  39. };
  40. } else {
  41. tempPostData = postData;
  42. }
  43. return request.post('/edu-app/open/getUploadSign', {
  44. data: {
  45. postData: tempPostData,
  46. pluginName: ossType,
  47. bucketName: bucket,
  48. filename: file
  49. },
  50. params: { pluginName: ossType }
  51. });
  52. };
  53. /**
  54. * 使用组件上传时,调用方法
  55. * @param param0
  56. */
  57. export const onFileUpload = ({
  58. file,
  59. action,
  60. data,
  61. onProgress,
  62. onFinish,
  63. onError
  64. }: any) => {
  65. if (ossSwitch === 'ks3') {
  66. const fileParams = {
  67. policy: data.policy,
  68. signature: data.signature,
  69. key: data.key,
  70. acl: 'public-read',
  71. KSSAccessKeyId: data.KSSAccessKeyId,
  72. name: data.name
  73. } as any;
  74. const formData = new FormData();
  75. for (const key in fileParams) {
  76. formData.append(key, fileParams[key]);
  77. }
  78. formData.append('file', data.file as File);
  79. axios
  80. .post(action as string, formData, {
  81. onUploadProgress: ({ progress }) => {
  82. console.log(progress);
  83. onProgress({ percent: Math.ceil((progress || 0) * 100) });
  84. }
  85. })
  86. .then(() => {
  87. file.url = action + data.key;
  88. onFinish();
  89. })
  90. .catch(error => {
  91. onError(error);
  92. });
  93. } else {
  94. const cos = new COS({
  95. // getAuthorization 必选参数
  96. getAuthorization: async (options, callback: any) => {
  97. callback({ Authorization: data.signature });
  98. }
  99. });
  100. cos
  101. .uploadFile({
  102. Bucket: tencentBucket /* 填写自己的 bucket,必须字段 */,
  103. Region: 'ap-nanjing' /* 存储桶所在地域,必须字段 */,
  104. Key: `ktqy/${data.name}`,
  105. /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */
  106. Body: data.file.file, // 上传文件对象
  107. SliceSize:
  108. 1024 *
  109. 1024 *
  110. 500 /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */,
  111. onProgress: function (progressData) {
  112. onProgress({ percent: Math.ceil((progressData.percent || 0) * 100) });
  113. }
  114. })
  115. .then((res: any) => {
  116. // file.url = 'https://' + res.Location;
  117. if (res.Location?.indexOf('http') >= 0) {
  118. file.url = res.Location;
  119. } else {
  120. file.url = 'https://' + res.Location;
  121. }
  122. onFinish();
  123. })
  124. .catch(error => {
  125. console.log(error, 'error');
  126. onError();
  127. });
  128. }
  129. };
  130. export const onOnlyFileUpload = async (action: string, params: any) => {
  131. if (ossSwitch === 'ks3') {
  132. const fileParams = {
  133. policy: params.policy,
  134. signature: params.signature,
  135. key: params.key,
  136. acl: 'public-read',
  137. KSSAccessKeyId: params.KSSAccessKeyId,
  138. name: params.name
  139. } as any;
  140. const formData = new FormData();
  141. for (const key in fileParams) {
  142. formData.append(key, fileParams[key]);
  143. }
  144. formData.append('file', params.file as File);
  145. let file = '';
  146. let errorObj: any = null;
  147. await axios
  148. .post(action as string, formData, {
  149. // onUploadProgress: ({ progress }) => {
  150. // console.log(progress);
  151. // onProgress({ percent: Math.ceil((progress || 0) * 100) });
  152. // }
  153. })
  154. .then(() => {
  155. file = action + params.key;
  156. })
  157. .catch(error => {
  158. // onError(error);
  159. errorObj = error;
  160. // throw new Error(error);
  161. });
  162. if (file) {
  163. return file;
  164. } else {
  165. throw new Error(errorObj);
  166. }
  167. return file;
  168. } else {
  169. let file = '';
  170. let errorObj: any = null;
  171. const cos = new COS({
  172. // getAuthorization 必选参数
  173. getAuthorization: async (options, callback: any) => {
  174. callback({ Authorization: params.signature });
  175. }
  176. });
  177. await cos
  178. .uploadFile({
  179. Bucket: tencentBucket /* 填写自己的 bucket,必须字段 */,
  180. Region: 'ap-nanjing' /* 存储桶所在地域,必须字段 */,
  181. Key: `ktqy/${params.name}`,
  182. /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */
  183. Body: params.file, // 上传文件对象
  184. SliceSize:
  185. 1024 *
  186. 1024 *
  187. 500 /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */
  188. // onProgress: function (progressData) {
  189. // onProgress({ percent: Math.ceil((progressData.percent || 0) * 100) });
  190. // }
  191. })
  192. .then((res: any) => {
  193. // file.url = 'https://' + res.Location;
  194. // file = 'https://' + res.Location;
  195. if (res.Location?.indexOf('http') >= 0) {
  196. file = res.Location;
  197. } else {
  198. file = 'https://' + res.Location;
  199. }
  200. // onFinish();
  201. })
  202. .catch(error => {
  203. // console.log(error, 'error');
  204. // onError();
  205. // throw new Error(error);
  206. errorObj = error;
  207. });
  208. if (file) {
  209. return file;
  210. } else {
  211. throw new Error(errorObj);
  212. }
  213. }
  214. };