123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- 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('/api-student/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
- 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)
- }
- }
- }
|