oss-file-upload.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. Domain: 'https://oss.dayaedu.com',
  96. Protocol: 'https',
  97. // getAuthorization 必选参数
  98. getAuthorization: async (options, callback: any) => {
  99. callback({ Authorization: data.signature });
  100. }
  101. });
  102. cos
  103. .uploadFile({
  104. Bucket: tencentBucket /* 填写自己的 bucket,必须字段 */,
  105. Region: 'ap-nanjing' /* 存储桶所在地域,必须字段 */,
  106. Key: `ktqy/${data.name}`,
  107. /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */
  108. Body: data.file.file, // 上传文件对象
  109. SliceSize:
  110. 1024 *
  111. 1024 *
  112. 500 /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */,
  113. onProgress: function (progressData) {
  114. onProgress({ percent: Math.ceil((progressData.percent || 0) * 100) });
  115. }
  116. })
  117. .then((res: any) => {
  118. // file.url = 'https://' + res.Location;
  119. if (res.Location?.indexOf('http') >= 0) {
  120. file.url = res.Location;
  121. } else {
  122. file.url = 'https://' + res.Location;
  123. }
  124. onFinish();
  125. })
  126. .catch(error => {
  127. console.log(error, 'error');
  128. onError();
  129. });
  130. }
  131. };
  132. export const onOnlyFileUpload = async (action: string, params: any) => {
  133. if (ossSwitch === 'ks3') {
  134. const fileParams = {
  135. policy: params.policy,
  136. signature: params.signature,
  137. key: params.key,
  138. acl: 'public-read',
  139. KSSAccessKeyId: params.KSSAccessKeyId,
  140. name: params.name
  141. } as any;
  142. const formData = new FormData();
  143. for (const key in fileParams) {
  144. formData.append(key, fileParams[key]);
  145. }
  146. formData.append('file', params.file as File);
  147. let file = '';
  148. let errorObj: any = null;
  149. await axios
  150. .post(action as string, formData, {
  151. // onUploadProgress: ({ progress }) => {
  152. // console.log(progress);
  153. // onProgress({ percent: Math.ceil((progress || 0) * 100) });
  154. // }
  155. })
  156. .then(() => {
  157. file = action + params.key;
  158. })
  159. .catch(error => {
  160. // onError(error);
  161. errorObj = error;
  162. // throw new Error(error);
  163. });
  164. if (file) {
  165. return file;
  166. } else {
  167. throw new Error(errorObj);
  168. }
  169. return file;
  170. } else {
  171. let file = '';
  172. let errorObj: any = null;
  173. const cos = new COS({
  174. Domain: 'https://oss.dayaedu.com',
  175. Protocol: 'https',
  176. // getAuthorization 必选参数
  177. getAuthorization: async (options, callback: any) => {
  178. callback({ Authorization: params.signature });
  179. }
  180. });
  181. await cos
  182. .uploadFile({
  183. Bucket: tencentBucket /* 填写自己的 bucket,必须字段 */,
  184. Region: 'ap-nanjing' /* 存储桶所在地域,必须字段 */,
  185. Key: `ktqy/${params.name}`,
  186. /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */
  187. Body: params.file, // 上传文件对象
  188. SliceSize:
  189. 1024 *
  190. 1024 *
  191. 500 /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */
  192. // onProgress: function (progressData) {
  193. // onProgress({ percent: Math.ceil((progressData.percent || 0) * 100) });
  194. // }
  195. })
  196. .then((res: any) => {
  197. // file.url = 'https://' + res.Location;
  198. // file = 'https://' + res.Location;
  199. if (res.Location?.indexOf('http') >= 0) {
  200. file = res.Location;
  201. } else {
  202. file = 'https://' + res.Location;
  203. }
  204. // onFinish();
  205. })
  206. .catch(error => {
  207. // console.log(error, 'error');
  208. // onError();
  209. // throw new Error(error);
  210. errorObj = error;
  211. });
  212. if (file) {
  213. return file;
  214. } else {
  215. throw new Error(errorObj);
  216. }
  217. }
  218. };