Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
M
merchant-manage-ui
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ui
merchant-manage-ui
Commits
4185b9b2
Commit
4185b9b2
authored
Jun 09, 2023
by
武广
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: 添加图片校验
parent
d1c26369
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
57 additions
and
15 deletions
+57
-15
env.config.js
config/env.config.js
+1
-1
UploadImage.jsx
src/pages/ServiceGoods/components/UploadImage.jsx
+23
-14
img.valid.js
src/utils/img.valid.js
+33
-0
No files found.
config/env.config.js
View file @
4185b9b2
const
isProduction
=
process
.
env
.
NODE_ENV
===
'
production
'
;
const
isPre
=
process
.
env
.
PRE_ENV
===
'
pre
'
;
const
environment
=
'
yxm
2
'
;
const
environment
=
'
yxm
'
;
const
envAPi
=
{
api
:
`https://security-
${
environment
}
.liangkebang.net`
,
//'https://security-xyqb.liangkebang.net',
kdspOpApi
:
`https://sc-merchant-api-
${
environment
}
.liangkebang.net`
,
...
...
src/pages/ServiceGoods/components/UploadImage.jsx
View file @
4185b9b2
import
{
PlusOutlined
,
DeleteOutlined
,
EyeOutlined
}
from
'
@ant-design/icons
'
;
import
{
validImageType
}
from
'
@/utils/img.valid
'
;
import
{
Modal
,
Upload
,
notification
,
Spin
,
Icon
,
Button
}
from
'
antd
'
;
import
React
,
{
useState
,
useEffect
,
useRef
,
forwardRef
}
from
'
react
'
;
import
lodash
from
'
lodash
'
;
...
...
@@ -99,21 +100,29 @@ const UploadImage = forwardRef((props, ref) => {
warningTip
(
`[
${
file
.
name
}
] 图片不可以大于2MB`
);
resolve
(
null
);
}
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
}
图片上传失败!`
);
const
curType
=
file
.
name
.
substr
(
file
.
name
.
lastIndexOf
(
'
.
'
)
+
1
).
toLowerCase
();
validImageType
(
file
,
curType
).
then
(
res
=>
{
if
(
!
res
)
{
warningTip
(
`[
${
file
.
name
}
] 请上传正确图片!`
);
resolve
(
null
);
});
image
.
src
=
url
;
}
else
{
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
;
});
}
});
});
...
...
src/utils/img.valid.js
0 → 100644
View file @
4185b9b2
/**
* 真实文件格式(前4个字节)
*/
export
const
ImgExt2Hex
=
{
jpg
:
[
'
ffd8ffe0
'
,
'
ffd8ffe1
'
,
'
ffd8ffe2
'
,
'
ffd8ffe3
'
],
jpeg
:
[
'
ffd8ffe0
'
,
'
ffd8ffe1
'
,
'
ffd8ffe2
'
,
'
ffd8ffe3
'
],
png
:
[
'
89504e47
'
],
};
/**
* 检测图片是否是预期的类型
* @param {File} imgFile 图片文件对象
* @param {string} imgExt 文件预期真实类型
* @return {boolean} 文件类型是否和预期一致
*/
export
const
validImageType
=
(
imgFile
,
imgExt
)
=>
{
const
FR
=
new
FileReader
();
return
new
Promise
(
resolve
=>
{
FR
.
onload
=
e
=>
{
const
{
type
}
=
imgFile
;
const
correctExtHex
=
ImgExt2Hex
[
imgExt
];
const
af
=
e
.
target
.
result
;
const
view
=
new
DataView
(
af
);
const
first4Byte
=
view
.
getUint32
(
0
,
false
);
// 获取32bit数
const
hexValue
=
Number
(
first4Byte
).
toString
(
16
);
if
(
!
type
||
!
correctExtHex
)
{
return
resolve
(
false
);
}
return
resolve
(
correctExtHex
.
indexOf
(
hexValue
)
>
-
1
);
};
FR
.
readAsArrayBuffer
(
imgFile
);
});
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment