提交 beac2f5a authored 作者: 钟是志's avatar 钟是志

2006 评奖评优管理头像上传限制优化

上级 6ec19ed3
import React from 'react';
import { Upload, message, Badge, Icon } from 'antd';
import { Upload, message, Badge, Icon, Modal } from 'antd';
import config from '@/webPublic/one_stop_public/config';
import { zipImage } from '@/webPublic/zyd_public/utils/handlePhoto';
import { getToken } from '@/webPublic/one_stop_public/utils/token';
......@@ -57,16 +57,38 @@ export default class ImgUploadCom extends React.Component {
this.triggerChange('');
};
getImageWidthAndHeight = (file) => { // 解决禅道任务 2006 评奖评优管理头像上传限制优化
return new Promise((resolve, reject) => {
let _URL = window.URL || window.webkitURL;
let image = new Image();
image.src = _URL.createObjectURL(file);
image.onload = function () {
resolve({
width: image.width,
height: image.height,
});
};
});
}
giveMessage = () => {
const { otherProps = {} } = this.props;
Modal.warning({
title: '图片限制',
content: otherProps.limitMessage || '图片格式错误!',
okText: '确定',
});
}
render() {
const {
json,
disabled
} = this.props;
const { json, disabled, otherProps } = this.props;
const { url } = this.state;
return (
<Upload.Dragger
disabled={disabled}
accept={'image/*'}
accept={otherProps.accept || 'image/*'}
url={url}
headers={{
Authorization: `bearer ${getToken()}`,
......@@ -75,6 +97,34 @@ export default class ImgUploadCom extends React.Component {
token: getToken(),
}}
beforeUpload={(file) => {
if(otherProps){
if(otherProps.limitFileType && Array.isArray(otherProps.limitFileType)){ // 限制文件类型
let fileType = file.name.split('.').pop();
if(!otherProps.limitFileType.includes(fileType)){
this.giveMessage();
return false;
}
}
if(otherProps.limitWidth || otherProps.limitHeight){ //限制上传图片的宽高. 解决禅道任务 2006
return new Promise((resolve, reject) => {
this.getImageWidthAndHeight(file).then((res) => {
if(otherProps.limitWidth && res.width !== otherProps.limitWidth){
this.giveMessage();
reject(false);
return false;
}
if(otherProps.limitHeight && res.height !== otherProps.limitHeight){
this.giveMessage();
reject(false);
return false;
}
return resolve(zipImage(file, 4));
});
});
}
}
return zipImage(file, 4);
// 图片压缩函数. 超过fileSizeLimitMb兆的图片 直接压缩成原来的 30% 如果后续有其他需求考虑 做成全局配置项 配置可以压缩的图片的范围
// 禅道bug 23185
......@@ -86,10 +136,10 @@ export default class ImgUploadCom extends React.Component {
multiple={false}
style={{ padding: 0 }}>
{url ? (
<Badge count={<Icon type="close-circle"
style={{ color: 'red' }}
onClick={this.removePicture}
/>}>
<Badge
count={
<Icon type="close-circle" style={{ color: 'red' }} onClick={this.removePicture} />
}>
<img
src={queryFileUrl(url)}
style={{
......
......@@ -3892,7 +3892,7 @@ ${obj[dataColumn.base52]}
message: '请上传图片',
},
],
})(<ImgUploadCom json={json} disabled={disabled} />);
})(<ImgUploadCom json={json} disabled={disabled} otherProps={this.otherProps}/>);
if (
get === 'mobile' &&
((json.isMobileLabel != null && json.isMobileLabel) ||
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论