From 0203a754aeba380d96e8231fcb016ac5eb00bb1c Mon Sep 17 00:00:00 2001 From: chaosBreaking Date: Sun, 13 Oct 2019 01:12:37 +0800 Subject: [PATCH] preview --- src/pages/identity/index.tsx | 46 ++++++++++++++++++++++++----------- src/pages/identity/style.less | 2 ++ 2 files changed, 34 insertions(+), 14 deletions(-) diff --git a/src/pages/identity/index.tsx b/src/pages/identity/index.tsx index 33f74b3..03aabfa 100644 --- a/src/pages/identity/index.tsx +++ b/src/pages/identity/index.tsx @@ -29,7 +29,17 @@ const nations = [ {key:'singapore', value:'新加坡'}, {key:'hk', value:'中国香港'}, ] - +function getObjectURL(file) { + var url = null ; + if (window.createObjectURL!=undefined) { // basic + url = window.createObjectURL(file) ; + } else if (window.URL!=undefined) { // mozilla(firefox) + url = window.URL.createObjectURL(file) ; + } else if (window.webkitURL!=undefined) { // webkit or chrome + url = window.webkitURL.createObjectURL(file) ; + } + return url ; +} const IdentityPage: React.FC = ({ dispatch, user, @@ -39,18 +49,26 @@ const IdentityPage: React.FC }) => { const picCover = React.createRef(); const picBack = React.createRef(); + const pic1url = React.createRef(); + const pic2url = React.createRef(); const [idtypesVisible, setIdtypesVisible] = useState(false); const [nationVisible, setNationVisible] = useState(false); const [realname, setRealname] = useState(); const [idNo, setIdNo] = useState(); - const onPicCover = (key: any) => { - console.log(key); - dispatch({ type: 'user/onUpdateState', payload: key }); - }; - const onPicBack = (key: any) => { - dispatch({ type: 'user/onUpdateState', payload: key }); - }; + const preview = (type) => { + const pic1 = picCover.current; + const pic2 = picBack.current; + const obj = type === 1 ? pic1 : pic2; + // let x = getObjectURL(obj); + // pic1url.current.src = x; + var reader = new FileReader(); + reader.readAsDataURL(obj.files[0]); + reader.onload = function(e) { + type === 1 ? (pic1url.current.src = e.target.result) : (pic2url.current.src = e.target.result); + } + + } const onIdTypeSelected = (key: string) => { setIdtypesVisible(false); dispatch({ type: 'user/onUpdateState', payload: key }); @@ -92,8 +110,8 @@ const IdentityPage: React.FC formData.append('pic2', pic2); formData.append('realname', realname); formData.append('idNo', idNo); - const data = await http.post('/user/verify', formData); - console.log(data); + // const data = await http.post('/user/verify', formData); + // console.log(data); } return (
@@ -111,15 +129,15 @@ const IdentityPage: React.FC
- +
- + preview(1)}/>
- +
- + preview(2)}/>