lex 1 år sedan
förälder
incheckning
2410329278

+ 86 - 18
src/helpers/oss-file-upload.ts

@@ -1,32 +1,100 @@
 import request from '@/utils/request';
 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 上传对应参数
  * { 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
+ * @returns ”{'signatur'':'',''kssAccessKeyI'':'',''policy': '' }“
  */
 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('上传成功');
+//     }
+//   }
+// );

+ 32 - 11
src/views/natural-resources/components/my-resources/save-modal/index.tsx

@@ -5,6 +5,7 @@ import {
   NSpace,
   NUpload,
   NUploadDragger,
+  UploadCustomRequestOptions,
   UploadFileInfo,
   useMessage
 } from 'naive-ui';
@@ -13,7 +14,11 @@ import { NaturalTypeEnum, PageEnum } from '/src/enums/pageEnum';
 import { policy } from '/src/components/upload-file/api';
 import { formatUrlType } from '../upload-modal';
 import axios from 'axios';
-import { getUploadSign } from '/src/helpers/oss-file-upload';
+import {
+  getUploadSign,
+  onFileUpload,
+  ossSwitch
+} from '/src/helpers/oss-file-upload';
 
 export default defineComponent({
   name: 'save-modal',
@@ -130,7 +135,6 @@ export default defineComponent({
       // }
 
       try {
-        await getUploadSign({ name: 'upload' });
         btnLoading.value = true;
         const name = file.file.name;
         const suffix = name.slice(name.lastIndexOf('.'));
@@ -145,8 +149,9 @@ export default defineComponent({
             unknowValueField: []
           }
         };
-        const { data } = await policy(obj);
-
+        // const { data } = await policy(obj);
+        const { data } = await getUploadSign(obj);
+        console.log(data);
         state.push({
           id: file.id,
           tempFiileBuffer: file.file,
@@ -293,6 +298,24 @@ export default defineComponent({
       return status || fileListRef.value.length <= 0;
     });
 
+    const onCustomRequest = ({
+      file,
+      data,
+      headers,
+      withCredentials,
+      action,
+      onFinish,
+      onError,
+      onProgress
+    }: UploadCustomRequestOptions) => {
+      const item = state.find((c: any) => {
+        return c.id == file.id;
+      });
+
+      item.file = file;
+      onFileUpload({ action, data: item, onProgress, onFinish, onError });
+    };
+
     const onSubmit = async () => {
       const list: any = [];
       fileListRef.value.forEach((file: any) => {
@@ -311,13 +334,11 @@ export default defineComponent({
         <NUpload
           ref={uploadRef}
           action={ossUploadUrl}
-          data={(file: any) => {
-            const item = state.find((c: any) => {
-              return c.id == file.file.id;
-            });
-            const { id, tempFiileBuffer, ...more } = item;
-            return { ...more };
-          }}
+          // data={(file: any) => {
+
+          //   return { ...more };
+          // }}
+          customRequest={onCustomRequest}
           v-model:fileList={fileListRef.value}
           accept=".jpg,jpeg,.png,audio/mp3,video/mp4"
           multiple={true}