|
@@ -1,32 +1,100 @@
|
|
import request from '@/utils/request';
|
|
import request from '@/utils/request';
|
|
import axios from 'axios';
|
|
import axios from 'axios';
|
|
-const ossSwitch = 'ks3' as 'ks3' | 'tencent'; // 上传文件服务商
|
|
|
|
|
|
+import COS from 'cos-js-sdk-v5';
|
|
|
|
+export const ossSwitch = 'ks3' as 'ks3' | 'tencent'; // 上传文件服务商
|
|
|
|
+
|
|
/**
|
|
/**
|
|
- * 获取上传文件签名
|
|
|
|
|
|
+ * 管乐团 gyt/
|
|
|
|
+ * 酷乐秀 klx/
|
|
|
|
+ * 课堂乐器 ktqy/
|
|
|
|
+ * 管乐迷 gym/
|
|
*/
|
|
*/
|
|
-export const getSign = (params: any) => {
|
|
|
|
- return request.post('/edu-app/open/getUploadSign', {
|
|
|
|
- data: params
|
|
|
|
- });
|
|
|
|
-};
|
|
|
|
|
|
|
|
/**
|
|
/**
|
|
* 获取上传文件签名
|
|
* 获取上传文件签名
|
|
* @param params 上传对应参数
|
|
* @param params 上传对应参数
|
|
* { filename: fileName,
|
|
* { filename: fileName,
|
|
- bucketName: props.bucketName,
|
|
|
|
- postData: {
|
|
|
|
- filename: fileName,
|
|
|
|
- acl: 'public-read',
|
|
|
|
- key: fileName,
|
|
|
|
- unknowValueField: []
|
|
|
|
- }}
|
|
|
|
|
|
+ bucketName: props.bucketName,
|
|
|
|
+ postData: {
|
|
|
|
+ filename: fileName,
|
|
|
|
+ acl: 'public-read',
|
|
|
|
+ key: fileName,
|
|
|
|
+ unknowValueField: []
|
|
|
|
+ }}
|
|
* @param oss 服务商 ks3 tencent
|
|
* @param oss 服务商 ks3 tencent
|
|
|
|
+ * @returns ”{'signatur'':'',''kssAccessKeyI'':'',''policy': '' }“
|
|
*/
|
|
*/
|
|
export const getUploadSign = async (params: any, oss?: string) => {
|
|
export const getUploadSign = async (params: any, oss?: string) => {
|
|
- try {
|
|
|
|
- const { data } = await getSign({ params });
|
|
|
|
- } catch (e: any) {
|
|
|
|
- throw new Error(e.message || '获取失败');
|
|
|
|
|
|
+ const { bucketName, filename, ...more } = params;
|
|
|
|
+ const ossType = oss || ossSwitch;
|
|
|
|
+ let bucket = bucketName;
|
|
|
|
+ let file = filename;
|
|
|
|
+ if (ossType === 'tencent') {
|
|
|
|
+ bucket = 'daya-online-1303457149';
|
|
|
|
+ file = 'ktqy/' + filename;
|
|
}
|
|
}
|
|
|
|
+ return request.post('/edu-app/open/getUploadSign', {
|
|
|
|
+ data: { ...more, pluginName: ossType, bucketName: bucket, filename: file }
|
|
|
|
+ });
|
|
};
|
|
};
|
|
|
|
+
|
|
|
|
+export const onFileUpload = ({
|
|
|
|
+ action,
|
|
|
|
+ data,
|
|
|
|
+ onProgress,
|
|
|
|
+ onFinish,
|
|
|
|
+ onError
|
|
|
|
+}: any) => {
|
|
|
|
+ console.log(data, 'data');
|
|
|
|
+ const fileParams = {
|
|
|
|
+ policy: data.policy,
|
|
|
|
+ signature: data.signature,
|
|
|
|
+ key: data.key,
|
|
|
|
+ acl: 'public-read',
|
|
|
|
+ KSSAccessKeyId: data.KSSAccessKeyId,
|
|
|
|
+ name: data.name
|
|
|
|
+ } as any;
|
|
|
|
+ const formData = new FormData();
|
|
|
|
+ for (const key in fileParams) {
|
|
|
|
+ formData.append(key, fileParams[key]);
|
|
|
|
+ }
|
|
|
|
+ formData.append('file', data.file as File);
|
|
|
|
+ axios
|
|
|
|
+ .post(action as string, formData, {
|
|
|
|
+ onUploadProgress: ({ progress }) => {
|
|
|
|
+ console.log(progress);
|
|
|
|
+ onProgress({ percent: Math.ceil((progress || 0) * 100) });
|
|
|
|
+ }
|
|
|
|
+ })
|
|
|
|
+ .then((res: any) => {
|
|
|
|
+ console.log(res, 'res');
|
|
|
|
+ onFinish();
|
|
|
|
+ })
|
|
|
|
+ .catch(error => {
|
|
|
|
+ console.log(error, 'error');
|
|
|
|
+ onError();
|
|
|
|
+ });
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+// cos.uploadFile(
|
|
|
|
+// {
|
|
|
|
+// Bucket: 'daya-online-1303457149' /* 填写自己的 bucket,必须字段 */,
|
|
|
|
+// Region: 'ap-nanjing' /* 存储桶所在地域,必须字段 */,
|
|
|
|
+// Key: 'gyt/1.jpg' /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */,
|
|
|
|
+// Body: file, // 上传文件对象
|
|
|
|
+// SliceSize:
|
|
|
|
+// 1024 *
|
|
|
|
+// 1024 *
|
|
|
|
+// 5 /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */,
|
|
|
|
+// onProgress: function (progressData) {
|
|
|
|
+// console.log(JSON.stringify(progressData));
|
|
|
|
+// }
|
|
|
|
+// },
|
|
|
|
+// function (err, data) {
|
|
|
|
+// if (err) {
|
|
|
|
+// console.log('上传失败', err);
|
|
|
|
+// } else {
|
|
|
|
+// console.log('上传成功');
|
|
|
|
+// }
|
|
|
|
+// }
|
|
|
|
+// );
|