Commit 5302545d authored by 武广's avatar 武广

fix: 外卖商品添加图片校验

parent 4185b9b2
import { PlusOutlined, DeleteOutlined, EyeOutlined } from '@ant-design/icons'; import { PlusOutlined, DeleteOutlined, EyeOutlined } from '@ant-design/icons';
import { Modal, Upload, notification, Spin } from 'antd'; import { Modal, Upload, notification, Spin } from 'antd';
import React, { useState, useEffect, useRef, forwardRef } from 'react'; import React, { useState, useEffect, useRef, forwardRef } from 'react';
import { validImageType } from '@/utils/img.valid';
import lodash from 'lodash'; import lodash from 'lodash';
import { ReactSortable } from 'react-sortablejs'; import { ReactSortable } from 'react-sortablejs';
import ImgCrop from 'antd-img-crop'; import ImgCrop from 'antd-img-crop';
...@@ -92,21 +93,29 @@ const UploadCropImage = forwardRef((props, ref) => { ...@@ -92,21 +93,29 @@ const UploadCropImage = forwardRef((props, ref) => {
warningTip(`[${file.name}] 图片不可以大于2MB`); warningTip(`[${file.name}] 图片不可以大于2MB`);
resolve(null); resolve(null);
} }
getBase64(file, url => { const curType = file.name.substr(file.name.lastIndexOf('.') + 1).toLowerCase();
const image = new Image(); validImageType(file, curType).then(res => {
image.addEventListener('load', () => { if (!res) {
const { width } = image; warningTip(`[${file.name}] 请上传正确图片!`);
const { height } = image;
file.width = width;
file.height = height;
file.LtMB = LtMB;
resolve(file);
});
image.addEventListener('error', () => {
warningTip(`${file.name}图片上传失败!`);
resolve(null); resolve(null);
}); } else {
image.src = url; getBase64(file, url => {
const image = new Image();
image.addEventListener('load', () => {
const { width } = image;
const { height } = image;
file.width = width;
file.height = height;
file.LtMB = LtMB;
resolve(file);
});
image.addEventListener('error', () => {
warningTip(`${file.name}图片上传失败!`);
resolve(null);
});
image.src = url;
});
}
}); });
}); });
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment