|
@@ -1,7 +1,8 @@
|
|
|
import request from '@/utils/request';
|
|
|
import axios from 'axios';
|
|
|
import COS from 'cos-js-sdk-v5';
|
|
|
-export const ossSwitch = 'ks3' as 'ks3' | 'tencent'; // 上传文件服务商
|
|
|
+export const ossSwitch = 'tencent' as 'ks3' | 'tencent'; // 上传文件服务商
|
|
|
+const tencentBucket = 'daya-online-1303457149';
|
|
|
|
|
|
/**
|
|
|
* 管乐团 gyt/
|
|
@@ -10,6 +11,7 @@ export const ossSwitch = 'ks3' as 'ks3' | 'tencent'; // 上传文件服务商
|
|
|
* 管乐迷 gym/
|
|
|
*/
|
|
|
|
|
|
+// 定义一个cos 对象
|
|
|
/**
|
|
|
* 获取上传文件签名
|
|
|
* @param params 上传对应参数
|
|
@@ -25,16 +27,33 @@ export const ossSwitch = 'ks3' as 'ks3' | 'tencent'; // 上传文件服务商
|
|
|
* @returns ”{'signatur'':'',''kssAccessKeyI'':'',''policy': '' }“
|
|
|
*/
|
|
|
export const getUploadSign = async (params: any, oss?: string) => {
|
|
|
- const { bucketName, filename, ...more } = params;
|
|
|
+ console.log(params, 'params');
|
|
|
+ const { bucketName, filename, postData } = params;
|
|
|
const ossType = oss || ossSwitch;
|
|
|
let bucket = bucketName;
|
|
|
let file = filename;
|
|
|
+ const key = postData.key;
|
|
|
+ let tempPostData: any = {};
|
|
|
+ console.log(postData, '`');
|
|
|
if (ossType === 'tencent') {
|
|
|
- bucket = 'daya-online-1303457149';
|
|
|
+ bucket = tencentBucket;
|
|
|
file = 'ktqy/' + filename;
|
|
|
+ // key = 'ktqy/' + ;
|
|
|
+
|
|
|
+ tempPostData = {
|
|
|
+ key: 'ktqy/' + postData.key
|
|
|
+ };
|
|
|
+ } else {
|
|
|
+ tempPostData = postData;
|
|
|
}
|
|
|
return request.post('/edu-app/open/getUploadSign', {
|
|
|
- data: { ...more, pluginName: ossType, bucketName: bucket, filename: file }
|
|
|
+ data: {
|
|
|
+ postData: tempPostData,
|
|
|
+ pluginName: ossType,
|
|
|
+ bucketName: bucket,
|
|
|
+ filename: file
|
|
|
+ },
|
|
|
+ params: { pluginName: ossType }
|
|
|
});
|
|
|
};
|
|
|
|
|
@@ -46,34 +65,70 @@ export const onFileUpload = ({
|
|
|
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) });
|
|
|
+
|
|
|
+ if (ossSwitch === 'ks3') {
|
|
|
+ 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();
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ const cos = new COS({
|
|
|
+ // getAuthorization 必选参数
|
|
|
+ getAuthorization: async (options, callback: any) => {
|
|
|
+ console.log(options, { Authorization: data.signature });
|
|
|
+ callback({ Authorization: data.signature });
|
|
|
}
|
|
|
- })
|
|
|
- .then((res: any) => {
|
|
|
- console.log(res, 'res');
|
|
|
- onFinish();
|
|
|
- })
|
|
|
- .catch(error => {
|
|
|
- console.log(error, 'error');
|
|
|
- onError();
|
|
|
});
|
|
|
+
|
|
|
+ console.log(data.file, 'data.file', `ktqy/${data.name}`);
|
|
|
+ cos
|
|
|
+ .uploadFile({
|
|
|
+ Bucket: tencentBucket /* 填写自己的 bucket,必须字段 */,
|
|
|
+ Region: 'ap-nanjing' /* 存储桶所在地域,必须字段 */,
|
|
|
+ Key: `ktqy/${data.name}`,
|
|
|
+ /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */
|
|
|
+ Body: data.file.file, // 上传文件对象
|
|
|
+ // SliceSize:
|
|
|
+ // 1024 *
|
|
|
+ // 1024 *
|
|
|
+ // 5 /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */,
|
|
|
+ onProgress: function (progressData) {
|
|
|
+ console.log(JSON.stringify(progressData));
|
|
|
+ }
|
|
|
+ })
|
|
|
+ .then((res: any) => {
|
|
|
+ console.log(res, 'res');
|
|
|
+ onFinish();
|
|
|
+ })
|
|
|
+ .catch(error => {
|
|
|
+ console.log(error, 'error');
|
|
|
+ onError();
|
|
|
+ });
|
|
|
+ }
|
|
|
};
|
|
|
|
|
|
// cos.uploadFile(
|