oss-file-upload.ts 6.5 KB

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