头像裁剪 oss

This commit is contained in:
陈红丽 2024-12-23 11:26:21 +08:00
parent 3f238bf4c4
commit 9f5dc0b57a

View File

@ -162,6 +162,8 @@
import CropperImage from './CropperImage.vue';
import { dataURLtoBlob ,base64ToFile} from '@/utils/file/base64Conver';
import { isFunction } from '@/utils/is';
import { getOssConfig } from '@/api/common';
import OSS from 'ali-oss';
import {
UploadOutlined,
ReloadOutlined,
@ -253,8 +255,9 @@
}
const uploadApi = props.uploadApi;
const name = props.name
const file = base64ToFile(previewSource.value,filename);
//
if (uploadApi && isFunction(uploadApi)) {
const file = base64ToFile(previewSource.value,filename);
try {
setSubLoading(true);
const formData = new FormData()
@ -266,8 +269,66 @@
} finally {
setSubLoading(false);
}
} else {
//oss
actionFiles(file)
}
}
const actionFiles = async (fileChild) => {
const configData = await getOssConfig();
let date = new Date();
let year = date.getFullYear();
let month: string | number = date.getMonth() + 1;
month = month < 10 ? '0' + month : month;
let day: string | number = date.getDate();
day = day < 10 ? '0' + day : day;
let h: string | number = date.getHours();
h = h < 10 ? '0' + h : h;
let minute: string | number = date.getMinutes();
minute = minute < 10 ? '0' + minute : minute;
let second: string | number = date.getSeconds();
second = second < 10 ? '0' + second : second;
let time = h + '' + minute + '' + second;
let filterFileName = time + '-' + fileChild.name;
filterFileName = filterFileName.replace(
/[\+\_\,\!\|\~\`\(\)\#\$\%\^\&\*\{\}\:\;\"\<\>\?]/g,
'',
);
let filePath = props.name + '/' + year + '/' + month + '/' + day + '/' + filterFileName;
setSubLoading(true);
try {
const headers = {
'Access-Control-Allow-Origin': '*',
};
const client = new OSS({
// yourRegionBucket1yourRegionoss-cn-hangzhou
endpoint: configData.oss_endpoint,
// STS访AccessKey IDAccessKey Secret
secure: true,
region: configData.oss_region,
accessKeyId: configData.oss_accessKeyId,
accessKeySecret: configData.oss_accessKeySecret,
// Bucket
bucket: configData.oss_bucketName,
timeout: 600000,
});
const res = await client.multipartUpload(filePath, fileChild, {
headers: headers,
//
parallel: 4,
// 1 MB100 KB
partSize: 1024 * 1024,
// headers,
mime: fileChild.type,
});
emit('uploadSuccess', {fileUrl:configData.oss_domain + res.name});
closeModal();
} catch (error) {
emit('uploadSuccess', {});
} finally {
setSubLoading(false);
}
};
function handleClose(){
src.value =''
previewSource.value=''