Authored by qinchao

上传身份证

... ... @@ -17,6 +17,9 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
* 用户身份通过银行卡实名认证
... ... @@ -122,6 +125,42 @@ public class RealNameAuthorizeController {
}
/**
* 6.9.1版本 ,上传身份证图片并校验
* 由于调用百度云ocr验证接口,需求file格式
* 因此要求正反面一起上传,两张
* bucket :yohocard
*/
@RequestMapping(params = "method=ufo.user.zhiMaCertUploadPhotoAndCheck")
public ApiResponse zhiMaCertUploadPhotoAndCheck(@RequestParam("files") MultipartFile[] files,
@RequestParam(value = "bucket") String bucket,
@RequestParam(value = "limitType", required = false) Integer limitType,
RealNameAuthorizeReqVO reqVO) throws Exception {
logger.info("zhiMaCertUploadPhotoAndCheck , reqVO [{}], files by files [{}] , and bucket [{}] , limitType [{}]",reqVO , files, bucket, limitType);
try {
//(1) 优先校验请求的参数
if (reqVO == null || reqVO.getUid()<=0){
throw new GatewayException(400, "uid不能为空!");
}
if ( StringUtils.isEmpty(reqVO.getCertNo())|| StringUtils.isEmpty(reqVO.getCertName())){
throw new GatewayException(400, "身份证号、姓名不能为空!");
}
//身份证格式校验IDCardUtils
if(!IDCardUtils.validate(reqVO.getCertNo())){
throw new GatewayException(400, "身份证号格式不正确!");
}
List<String> imgUrls = realNameAuthorizeService.zhiMaCertUploadPhotoAndCheck(files, bucket, limitType,reqVO);
return new ApiResponse(200, "上传成功", imgUrls);
} catch (Exception e) {
e.printStackTrace();
logger.info("upload files fail");
return new ApiResponse(400, "上传失败" + e.getMessage(), null);
}
}
/**
* 6.9.1版本 ,芝麻认证要求上传图片
* 原接口保持不变,芝麻认证用新的接口
* 芝麻认证(初始化并购置返回url)
... ...
... ... @@ -8,6 +8,9 @@ import com.yohoufo.common.exception.GatewayException;
import com.yohoufo.dal.user.model.UserAuthorizeInfo;
import com.yohoufo.dal.user.model.ZhiMaCert;
import com.yohoufo.user.requestVO.RealNameAuthorizeReqVO;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
/**
* 用户身份实名认证
... ... @@ -24,6 +27,8 @@ public interface IRealNameAuthorizeService {
ZhiMaCert getValidZhiMaCert(int uid);
List<String> zhiMaCertUploadPhotoAndCheck(MultipartFile[] multipartFile, String bucket, Integer limitType, RealNameAuthorizeReqVO reqVO) throws Exception;
AuthorizeResultRespVO zhiMaCertWithPhotoInit(RealNameAuthorizeReqVO reqVO);
AuthorizeResultRespVO zhiMaCertInit(RealNameAuthorizeReqVO reqVO);
... ...
... ... @@ -9,8 +9,10 @@ import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.alipay.api.response.ZhimaCustomerCertificationQueryResponse;
import com.yoho.core.common.helpers.ImagesHelper;
import com.yoho.error.exception.ServiceException;
import com.yoho.tools.common.beans.ApiResponse;
import com.yoho.tools.common.service.IQNUploadService;
import com.yohobuy.ufo.model.user.resp.AuthorizeResultRespVO;
import com.yohoufo.common.exception.GatewayException;
import com.yohoufo.common.exception.UfoServiceException;
... ... @@ -25,16 +27,28 @@ import com.yohoufo.user.common.AlipayConfigInfo;
import com.yohoufo.user.helper.HideDataUtil;
import com.yohoufo.user.requestVO.RealNameAuthorizeReqVO;
import com.yohoufo.user.service.IRealNameAuthorizeService;
import lombok.Data;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
... ... @@ -58,6 +72,12 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
@Autowired
private HttpClient httpClient;
@Value("${file.saveDir}")
private String saveDir;
@Autowired
private IQNUploadService iqnUploadService;
@Value("${zhimacert.switch:true}")
private boolean zhiMaCertSwitch;
/* @Autowired
... ... @@ -353,6 +373,123 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
System.out.println(getStringDate());
}
@Override
public List<String> zhiMaCertUploadPhotoAndCheck(MultipartFile[] multipartFiles, String bucket, Integer limitType, RealNameAuthorizeReqVO reqVO) throws Exception{
List<String> imgUrls = new ArrayList<>();
//判断file数组不能为空并且长度大于0
if(multipartFiles==null||multipartFiles.length!=2){
throw new GatewayException(400, "请上传身份证正反面!");
}
//循环获取file数组中得文件
UploadModel frontUploadModel=null;
UploadModel backUploadModel=null;
for (int i = 0; i < multipartFiles.length; i++) {
MultipartFile multipartFile = multipartFiles[i];
//保存文件
UploadModel uploadModel = getUploadImagePath(multipartFile, bucket, String.valueOf(reqVO.getUid()), limitType);
imgUrls.add(uploadModel.getImgUrl());
if(i==0){
frontUploadModel = uploadModel;
}else{
backUploadModel = uploadModel;
}
}
//调用接口,ocr验证
boolean pass = checkCertPhoto(frontUploadModel ,backUploadModel ,reqVO);
if(!pass){
throw new GatewayException(400, "身份证图片校验不通过!");
}
//删除图片
frontUploadModel.getUploadFile().delete();
backUploadModel.getUploadFile().delete();
return imgUrls;
}
@Data
static class UploadModel{
File uploadFile ;
String imgUrl;
}
public UploadModel getUploadImagePath(MultipartFile multipartFile, String bucket, String uid, Integer limitType) throws Exception {
//文件为空
if (multipartFile.isEmpty()) {
logger.warn("upload file is empty");
throw new FileNotFoundException("上传文件为空");
}
// bucket必须为系统已定义常量
if (!"yohocard".equals(bucket)) {
logger.warn("can't find upload bucket {}", bucket);
throw new Exception("上传Bucket:" + bucket + "无效");
}
String imageFileName = multipartFile.getOriginalFilename();
long imageFileSize = multipartFile.getSize();
//产品需求:百度云要求图片大小4M
boolean flag = StringUtils.contains(imageFileName, ".jpeg") ||
StringUtils.contains(imageFileName, ".jpg") || StringUtils.contains(imageFileName, ".png");
if(!flag){
logger.warn("upload file type not correct {}",imageFileName);
throw new FileNotFoundException("上传文件为空");
}
if (imageFileSize >= 4*1024*1024) {
logger.warn("upload images exceeded imageSize {}", imageFileSize);
DecimalFormat decimalFormat = new DecimalFormat("#.00");
throw new Exception("上传图片实际大小:" + decimalFormat.format(imageFileSize / 1024.0) + "KB,超过了图片默认值" );
}
String fileMode = "01";
logger.info("upload image bucket is {}, fileMode is {}", bucket, fileMode);
String saveName = new SimpleDateFormat("/yyyy/MM/dd/HH/").format(new Date());
String fileName = fileMode
+ DigestUtils.md5Hex(uid + "_" + System.currentTimeMillis() + multipartFile.getOriginalFilename())
+ multipartFile.getOriginalFilename().substring(multipartFile.getOriginalFilename().indexOf("."));
// 文件保存路径
String filePath = saveDir + File.separator + fileName;
File uploadFile = new File(filePath);
multipartFile.transferTo(uploadFile);
checkFilePixels(uploadFile, limitType);
String imgUrl = iqnUploadService.upload(uploadFile, bucket, saveName + fileName);
UploadModel uploadModel = new UploadModel();
uploadModel.setUploadFile(uploadFile);
uploadModel.setImgUrl(imgUrl);
///// uploadFile.delete(); 暂时先不删除,等身份证校验完毕再删
return uploadModel;
}
/**
* 根据配置,校验每种场景的图片是否符合规范
*
* @param uploadFile
* @param limitType
*/
private void checkFilePixels(File uploadFile, Integer limitType) throws Exception {
if (null == limitType) {
return;
}
logger.info("method checkFilePixels exceute.");
BufferedImage sourceImg = null;
try {
sourceImg = ImageIO.read(java.nio.file.Files.newInputStream(uploadFile.toPath()));
} catch (IOException e) {
logger.warn("ImageIO read find wrong uploadFile {}",uploadFile , e);
throw new GatewayException( 400,"上传文件失败");
}
}
@Override
public AuthorizeResultRespVO zhiMaCertWithPhotoInit(RealNameAuthorizeReqVO reqVO){
logger.info("real name zhiMaCertWithPhotoInit reqVO {}", reqVO);
... ... @@ -376,11 +513,6 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
logger.info("real name zhiMaCertWithPhotoInit reqVO {} ,justCheckPhoto {}", reqVO,justCheckPhoto);
boolean pass = checkCertPhoto(reqVO);
if(!pass){
throw new UfoServiceException(400, "身份证照片验证不通过!");
}
String imgUrl = reqVO.getFrontImageUrl()+","+reqVO.getBackImageUrl();
if(justCheckPhoto){
... ... @@ -397,7 +529,8 @@ public class RealNameAuthorizeServiceImpl implements IRealNameAuthorizeService {
return vo;
}
private boolean checkCertPhoto(RealNameAuthorizeReqVO reqVO){
private boolean checkCertPhoto(UploadModel frontUploadModel,UploadModel backUploadModel,RealNameAuthorizeReqVO reqVO){
return true;
}
... ...