lex 1 tahun lalu
induk
melakukan
b60d66adcb
5 mengubah file dengan 342 tambahan dan 30 penghapusan
  1. 30 0
      package-lock.json
  2. 1 0
      package.json
  3. 42 27
      src/components/col-upload/index.tsx
  4. 249 0
      src/helpers/oss-file-upload.ts
  5. 20 3
      yarn.lock

+ 30 - 0
package-lock.json

@@ -14,6 +14,7 @@
         "browserslist": "^4.20.2",
         "classnames": "^2.3.1",
         "clean-deep": "^3.4.0",
+        "cos-js-sdk-v5": "^1.4.21",
         "dayjs": "^1.10.7",
         "echarts": "^5.3.3",
         "eventemitter3": "^5.0.0",
@@ -2723,6 +2724,14 @@
         }
       }
     },
+    "node_modules/@xmldom/xmldom": {
+      "version": "0.8.10",
+      "resolved": "https://registry.npmmirror.com/@xmldom/xmldom/-/xmldom-0.8.10.tgz",
+      "integrity": "sha512-2WALfTl4xo2SkGCYRt6rDTFfk9R1czmBvUQy12gK2KuRKIpWEhcbbzy8EZXtz/jkRqHX8bFEc6FC1HjX4TUWYw==",
+      "engines": {
+        "node": ">=10.0.0"
+      }
+    },
     "node_modules/acorn": {
       "version": "8.7.1",
       "resolved": "https://registry.npmmirror.com/acorn/-/acorn-8.7.1.tgz",
@@ -3561,6 +3570,14 @@
         "semver": "bin/semver.js"
       }
     },
+    "node_modules/cos-js-sdk-v5": {
+      "version": "1.4.21",
+      "resolved": "https://registry.npmmirror.com/cos-js-sdk-v5/-/cos-js-sdk-v5-1.4.21.tgz",
+      "integrity": "sha512-6cR53IZF2o17uaPr8XJSMa+Q73P9pgDFD5IYGcIfJn06JJaK6hGX43nv5DJ17uQYmwQBIqNeZOF97I7ClrsNdA==",
+      "dependencies": {
+        "@xmldom/xmldom": "^0.8.6"
+      }
+    },
     "node_modules/cross-spawn": {
       "version": "7.0.3",
       "resolved": "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-7.0.3.tgz",
@@ -11722,6 +11739,11 @@
         "vue-demi": "*"
       }
     },
+    "@xmldom/xmldom": {
+      "version": "0.8.10",
+      "resolved": "https://registry.npmmirror.com/@xmldom/xmldom/-/xmldom-0.8.10.tgz",
+      "integrity": "sha512-2WALfTl4xo2SkGCYRt6rDTFfk9R1czmBvUQy12gK2KuRKIpWEhcbbzy8EZXtz/jkRqHX8bFEc6FC1HjX4TUWYw=="
+    },
     "acorn": {
       "version": "8.7.1",
       "resolved": "https://registry.npmmirror.com/acorn/-/acorn-8.7.1.tgz",
@@ -12277,6 +12299,14 @@
         }
       }
     },
+    "cos-js-sdk-v5": {
+      "version": "1.4.21",
+      "resolved": "https://registry.npmmirror.com/cos-js-sdk-v5/-/cos-js-sdk-v5-1.4.21.tgz",
+      "integrity": "sha512-6cR53IZF2o17uaPr8XJSMa+Q73P9pgDFD5IYGcIfJn06JJaK6hGX43nv5DJ17uQYmwQBIqNeZOF97I7ClrsNdA==",
+      "requires": {
+        "@xmldom/xmldom": "^0.8.6"
+      }
+    },
     "cross-spawn": {
       "version": "7.0.3",
       "resolved": "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-7.0.3.tgz",

+ 1 - 0
package.json

@@ -27,6 +27,7 @@
     "browserslist": "^4.20.2",
     "classnames": "^2.3.1",
     "clean-deep": "^3.4.0",
+    "cos-js-sdk-v5": "^1.4.21",
     "dayjs": "^1.10.7",
     "echarts": "^5.3.3",
     "eventemitter3": "^5.0.0",

+ 42 - 27
src/components/col-upload/index.tsx

@@ -8,6 +8,7 @@ import umiRequest from 'umi-request'
 import iconUploader from '@common/images/icon_uploader.png'
 import request from '@/helpers/request'
 import { getOssUploadUrl, state } from '@/state'
+import { getUploadSign, onOnlyFileUpload } from '@/helpers/oss-file-upload'
 
 export default defineComponent({
   name: 'col-upload',
@@ -103,24 +104,34 @@ export default defineComponent({
       // 上传文件
       try {
         // 获取签名
-        const signUrl =
-          state.platformType === 'TEACHER'
-            ? '/api-teacher/getUploadSign'
-            : '/api-student/getUploadSign'
+        // const signUrl =
+        //   state.platformType === 'TEACHER'
+        //     ? '/api-teacher/getUploadSign'
+        //     : '/api-student/getUploadSign'
         const tempName = file.name || ''
         const fileName = tempName && tempName.replace(/ /gi, '_')
         const key = new Date().getTime() + fileName
         console.log(file)
 
-        const res = await request.post(signUrl, {
-          data: {
-            filename: fileName,
-            bucketName: this.bucket,
-            postData: {
-              filename: fileName,
-              acl: 'public-read',
-              key: key
-            }
+        // const res = await request.post(signUrl, {
+        //   data: {
+        //     filename: fileName,
+        //     bucketName: this.bucket,
+        //     postData: {
+        //       filename: fileName,
+        //       acl: 'public-read',
+        //       key: key
+        //     }
+        //   }
+        // })
+        const { data } = await getUploadSign({
+          filename: key,
+          bucketName: this.bucket,
+          postData: {
+            filename: key,
+            acl: 'public-read',
+            key: key,
+            unknowValueField: []
           }
         })
         Toast.loading({
@@ -130,24 +141,28 @@ export default defineComponent({
           duration: 0
         })
         const obj = {
-          policy: res.data.policy,
-          signature: res.data.signature,
+          policy: data.policy,
+          signature: data.signature,
           key: key,
-          KSSAccessKeyId: res.data.kssAccessKeyId,
+          KSSAccessKeyId: data.kssAccessKeyId,
           acl: 'public-read',
           name: fileName
         }
-        const formData = new FormData()
-        for (const key in obj) {
-          formData.append(key, obj[key])
-        }
-        formData.append('file', file, fileName)
-        await umiRequest(getOssUploadUrl(this.bucket), {
-          method: 'POST',
-          data: formData
-        })
-        console.log(getOssUploadUrl(this.bucket) + key)
-        const uploadUrl = getOssUploadUrl(this.bucket) + key
+        // const formData = new FormData()
+        // for (const key in obj) {
+        //   formData.append(key, obj[key])
+        // }
+        // formData.append('file', file, fileName)
+        // await umiRequest(getOssUploadUrl(this.bucket), {
+        //   method: 'POST',
+        //   data: formData
+        // })
+        // console.log(getOssUploadUrl(this.bucket) + key)
+        // const uploadUrl = getOssUploadUrl(this.bucket) + key
+        const uploadUrl = await onOnlyFileUpload(
+          getOssUploadUrl(this.bucket),
+          obj
+        )
         Toast.clear()
         this.$emit('update:modelValue', uploadUrl)
         this.onUploadChange(uploadUrl)

+ 249 - 0
src/helpers/oss-file-upload.ts

@@ -0,0 +1,249 @@
+import request from './request'
+// import axios from 'axios'
+import umiRequest from 'umi-request'
+import COS from 'cos-js-sdk-v5'
+import { state } from '@/state'
+export const ossSwitch = 'tencent' as 'ks3' | 'tencent' // 上传文件服务商
+const tencentBucket = 'daya-online-1303457149'
+
+/**
+ * 管乐团 gyt/
+ * 酷乐秀 klx/
+ * 课堂乐器 ktqy/
+ * 管乐迷 gym/
+ */
+
+// 定义一个cos 对象
+/**
+ * 获取上传文件签名
+ * @param params 上传对应参数
+ * { filename: fileName,
+     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) => {
+  const { bucketName, filename, postData } = params
+  const ossType = ossSwitch
+  let bucket = bucketName
+  let file = filename
+  // const key = postData.key;
+  let tempPostData: any = {}
+  if (ossType === 'tencent') {
+    bucket = tencentBucket
+    file = 'gyt/' + filename
+
+    tempPostData = {
+      key: 'gyt/' + postData.key
+    }
+  } else {
+    tempPostData = postData
+  }
+  return request.post(state.platformApi + '/open/getUploadSign', {
+    data: {
+      postData: tempPostData,
+      pluginName: ossType,
+      bucketName: bucket,
+      filename: file
+    },
+    params: { pluginName: ossType }
+  })
+}
+
+/**
+ * 使用组件上传时,调用方法
+ * @param param0
+ */
+export const onFileUpload = ({
+  file,
+  action,
+  data,
+  onProgress,
+  onFinish,
+  onError
+}: any) => {
+  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(() => {
+    //     file.url = action + data.key
+    //     onFinish()
+    //   })
+    //   .catch((error) => {
+    //     onError(error)
+    //   })
+    umiRequest(action as string, {
+      method: 'POST',
+      data: formData
+    })
+      .then(() => {
+        file.url = action + data.key
+        onFinish()
+      })
+      .catch(error => {
+        onError(error)
+      })
+  } else {
+    const cos = new COS({
+      Domain: 'https://oss.dayaedu.com',
+      Protocol: 'https',
+      // getAuthorization 必选参数
+      getAuthorization: async (options, callback: any) => {
+        callback({ Authorization: data.signature })
+      }
+    })
+    cos
+      .uploadFile({
+        Bucket: tencentBucket /* 填写自己的 bucket,必须字段 */,
+        Region: 'ap-nanjing' /* 存储桶所在地域,必须字段 */,
+        Key: `gyt/${data.name}`,
+        /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */
+        Body: data.file.file, // 上传文件对象
+        SliceSize:
+          1024 *
+          1024 *
+          500 /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */,
+        onProgress: function (progressData) {
+          onProgress({ percent: Math.ceil((progressData.percent || 0) * 100) })
+        }
+      })
+      .then((res: any) => {
+        // file.url = 'https://' + res.Location;
+        if (res.Location?.indexOf('http') >= 0) {
+          file.url = res.Location
+        } else {
+          file.url = 'https://' + res.Location
+        }
+        onFinish()
+      })
+      .catch(error => {
+        console.log(error, 'error')
+        onError()
+      })
+  }
+}
+
+export const onOnlyFileUpload = async (action: string, params: any) => {
+  if (ossSwitch === 'ks3') {
+    const fileParams = {
+      policy: params.policy,
+      signature: params.signature,
+      key: params.key,
+      acl: 'public-read',
+      KSSAccessKeyId: params.KSSAccessKeyId,
+      name: params.name
+    } as any
+    const formData = new FormData()
+    for (const key in fileParams) {
+      formData.append(key, fileParams[key])
+    }
+    formData.append('file', params.file as File)
+    let file = ''
+    let errorObj: any = null
+    // await axios
+    //   .post(action as string, formData, {
+    //     // onUploadProgress: ({ progress }) => {
+    //     //   console.log(progress);
+    //     //   onProgress({ percent: Math.ceil((progress || 0) * 100) });
+    //     // }
+    //   })
+    //   .then(() => {
+    //     file = action + params.key
+    //   })
+    //   .catch((error) => {
+    //     // onError(error);
+    //     errorObj = error
+    //     // throw new Error(error);
+    //   })
+    await umiRequest(action as string, {
+      method: 'POST',
+      data: formData
+    })
+      .then(() => {
+        file = action + params.key
+      })
+      .catch(error => {
+        errorObj = error
+      })
+    if (file) {
+      return file
+    } else {
+      throw new Error(errorObj)
+    }
+    return file
+  } else {
+    let file = ''
+    let errorObj: any = null
+    console.log(params, 'params')
+    const cos = new COS({
+      Domain: 'https://oss.dayaedu.com',
+      // getAuthorization 必选参数
+      getAuthorization: async (options, callback: any) => {
+        callback({ Authorization: params.signature })
+      }
+    })
+
+    // http://daya-online-1303457149.cos.ap-nanjing.myqcloud.com/gyt
+    // http://daya-online-1303457149.cos.ap-nanjing.myqcloud.com/gyt
+    await cos
+      .uploadFile({
+        Bucket: tencentBucket /* 填写自己的 bucket,必须字段 */,
+        Region: 'ap-nanjing' /* 存储桶所在地域,必须字段 */,
+        Key: `gyt/${params.name}`,
+        /* 存储在桶里的对象键(例如:1.jpg,a/b/test.txt,图片.jpg)支持中文,必须字段 */
+        Body: params.file, // 上传文件对象
+        SliceSize:
+          1024 *
+          1024 *
+          500 /* 触发分块上传的阈值,超过5MB使用分块上传,小于5MB使用简单上传。可自行设置,非必须 */
+        // onProgress: function (progressData) {
+        //   onProgress({ percent: Math.ceil((progressData.percent || 0) * 100) });
+        // }
+      })
+      .then((res: any) => {
+        // file.url = 'https://' + res.Location;
+        // file = 'https://' + res.Location;
+        if (res.Location?.indexOf('http') >= 0) {
+          file = res.Location
+        } else {
+          file = 'https://' + res.Location
+        }
+        // onFinish();
+      })
+      .catch(error => {
+        // console.log(error, 'error');
+        // onError();
+        // throw new Error(error);
+        errorObj = error
+      })
+    if (file) {
+      return file
+    } else {
+      throw new Error(errorObj)
+    }
+  }
+}

+ 20 - 3
yarn.lock

@@ -1428,6 +1428,11 @@
   dependencies:
     "vue-demi" "*"
 
+"@xmldom/xmldom@^0.8.6":
+  "integrity" "sha512-2WALfTl4xo2SkGCYRt6rDTFfk9R1czmBvUQy12gK2KuRKIpWEhcbbzy8EZXtz/jkRqHX8bFEc6FC1HjX4TUWYw=="
+  "resolved" "https://registry.npmmirror.com/@xmldom/xmldom/-/xmldom-0.8.10.tgz"
+  "version" "0.8.10"
+
 "acorn-jsx@^5.3.1":
   "integrity" "sha512-K0Ptm/47OKfQRpNQ2J/oIN/3QYiK6FwW+eJbILhsdxh2WTLdl+30o8aGdTbm5JbffpFFAg/g+zi1E+jvJha5ng=="
   "resolved" "https://registry.npmmirror.com/acorn-jsx/-/acorn-jsx-5.3.1.tgz"
@@ -1952,6 +1957,13 @@
   "resolved" "https://registry.npmmirror.com/core-js/-/core-js-3.23.3.tgz"
   "version" "3.23.3"
 
+"cos-js-sdk-v5@^1.4.21":
+  "integrity" "sha512-6cR53IZF2o17uaPr8XJSMa+Q73P9pgDFD5IYGcIfJn06JJaK6hGX43nv5DJ17uQYmwQBIqNeZOF97I7ClrsNdA=="
+  "resolved" "https://registry.npmmirror.com/cos-js-sdk-v5/-/cos-js-sdk-v5-1.4.21.tgz"
+  "version" "1.4.21"
+  dependencies:
+    "@xmldom/xmldom" "^0.8.6"
+
 "cross-spawn@^5.0.1":
   "integrity" "sha512-pTgQJ5KC0d2hcY8eyL1IzlBPYjTkyH72XRZPnLyKus2mBfNjQs3klqbJU2VILqZryAZUt9JOb3h/mWMy23/f5A=="
   "resolved" "https://registry.npmmirror.com/cross-spawn/-/cross-spawn-5.1.0.tgz"
@@ -2193,9 +2205,9 @@
   "resolved" "https://registry.npmmirror.com/es-module-lexer/-/es-module-lexer-0.9.3.tgz"
   "version" "0.9.3"
 
-"esbuild-windows-64@0.14.54":
-  "integrity" "sha512-AoHTRBUuYwXtZhjXZbA1pGfTo8cJo3vZIcWGLiUcTNgHpJJMC1rVA44ZereBHMJtotyN71S8Qw0npiCIkW96cQ=="
-  "resolved" "https://registry.npmmirror.com/esbuild-windows-64/-/esbuild-windows-64-0.14.54.tgz"
+"esbuild-darwin-64@0.14.54":
+  "integrity" "sha512-jtdKWV3nBviOd5v4hOpkVmpxsBy90CGzebpbO9beiqUYVMBtSc0AL9zGftFuBon7PNDcdvNCEuQqw2x0wP9yug=="
+  "resolved" "https://registry.npmmirror.com/esbuild-darwin-64/-/esbuild-darwin-64-0.14.54.tgz"
   "version" "0.14.54"
 
 "esbuild@^0.14.27":
@@ -2593,6 +2605,11 @@
   "resolved" "https://registry.npmmirror.com/fs.realpath/-/fs.realpath-1.0.0.tgz"
   "version" "1.0.0"
 
+"fsevents@~2.3.2":
+  "integrity" "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA=="
+  "resolved" "https://registry.npmmirror.com/fsevents/-/fsevents-2.3.2.tgz"
+  "version" "2.3.2"
+
 "function-bind@^1.1.1":
   "integrity" "sha512-yIovAzMX49sF8Yl58fSCWJ5svSLuaibPxXQJFLmBObTuCr0Mf1KiPopGM9NiFjiYBCbfaa2Fh6breQ6ANVTI0A=="
   "resolved" "https://registry.npmmirror.com/function-bind/-/function-bind-1.1.1.tgz"