|
|
@@ -632,13 +632,65 @@
|
|
|
return true;
|
|
|
};
|
|
|
|
|
|
- // 上传头像方法
|
|
|
- const afterRead = async (file: any) => {
|
|
|
- // 创建FormData对象
|
|
|
- const formData = new FormData();
|
|
|
- formData.append('file', file.file);
|
|
|
+ // 图片压缩函数(与用户信息修改页面保持一致)
|
|
|
+ const compressImage = (file: File): Promise<File> => {
|
|
|
+ return new Promise((resolve, reject) => {
|
|
|
+ const img = new Image();
|
|
|
+ const reader = new FileReader();
|
|
|
+
|
|
|
+ reader.onload = (e: any) => {
|
|
|
+ img.src = e.target.result;
|
|
|
+ img.onload = () => {
|
|
|
+ const canvas = document.createElement('canvas');
|
|
|
+ const ctx = canvas.getContext('2d');
|
|
|
+
|
|
|
+ // 设置最大尺寸为500px(与用户信息修改页面保持一致)
|
|
|
+ const MAX_SIZE = 500;
|
|
|
+ let width = img.width;
|
|
|
+ let height = img.height;
|
|
|
+
|
|
|
+ if (width > height) {
|
|
|
+ if (width > MAX_SIZE) {
|
|
|
+ height *= MAX_SIZE / width;
|
|
|
+ width = MAX_SIZE;
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ if (height > MAX_SIZE) {
|
|
|
+ width *= MAX_SIZE / height;
|
|
|
+ height = MAX_SIZE;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ canvas.width = width;
|
|
|
+ canvas.height = height;
|
|
|
+ ctx!.drawImage(img, 0, 0, width, height);
|
|
|
+
|
|
|
+ // 转换为JPEG格式并压缩(质量0.8)
|
|
|
+ canvas.toBlob((blob) => {
|
|
|
+ if (blob) {
|
|
|
+ resolve(new File([blob], file.name, { type: 'image/jpeg' }));
|
|
|
+ } else {
|
|
|
+ reject(new Error('图片压缩失败'));
|
|
|
+ }
|
|
|
+ }, 'image/jpeg', 0.8);
|
|
|
+ };
|
|
|
+ img.onerror = reject;
|
|
|
+ };
|
|
|
+ reader.onerror = reject;
|
|
|
+ reader.readAsDataURL(file);
|
|
|
+ });
|
|
|
+ };
|
|
|
|
|
|
+ // 上传头像方法(添加压缩处理)
|
|
|
+ const afterRead = async (file: any) => {
|
|
|
try {
|
|
|
+ // 先压缩图片(与用户信息修改页面保持一致)
|
|
|
+ const compressedFile = await compressImage(file.file);
|
|
|
+
|
|
|
+ // 创建FormData对象
|
|
|
+ const formData = new FormData();
|
|
|
+ formData.append('file', compressedFile);
|
|
|
+
|
|
|
// 使用fetch上传文件
|
|
|
const response = await fetch(uploadUrl, {
|
|
|
method: 'POST',
|