身份证扫描信息添加
parent
2801ad7ded
commit
1ed5729d50
|
@ -0,0 +1,73 @@
|
|||
package com.ruoyi.mybasic.common.domain;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import lombok.Data;
|
||||
import org.springframework.format.annotation.DateTimeFormat;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 身份证信息
|
||||
* @author Lou_Zs
|
||||
*/
|
||||
@Data
|
||||
public class Card {
|
||||
|
||||
/**
|
||||
* 用户编号
|
||||
*/
|
||||
private Integer userId;
|
||||
|
||||
/**
|
||||
* 身份证正面
|
||||
*/
|
||||
private String cardFace;
|
||||
/**
|
||||
* 身份证反面
|
||||
*/
|
||||
private String cardBack;
|
||||
|
||||
/**
|
||||
* 身份证号
|
||||
*/
|
||||
private String num;
|
||||
/**
|
||||
* 姓名
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 所属地
|
||||
*/
|
||||
private String address;
|
||||
/**
|
||||
* 性别
|
||||
*/
|
||||
private String sex;
|
||||
/**
|
||||
* 生日日期
|
||||
*/
|
||||
private String birth;
|
||||
/**
|
||||
* 籍贯
|
||||
*/
|
||||
private String nationality;
|
||||
/**
|
||||
* 有效期开始时间
|
||||
*/
|
||||
private String startDate;
|
||||
/**
|
||||
* 有效期结束时间
|
||||
*/
|
||||
private String endDate;
|
||||
/**
|
||||
* 签发机关
|
||||
*/
|
||||
private String issue;
|
||||
/**
|
||||
* 绑定时间
|
||||
*/
|
||||
@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
|
||||
private Date createTime;
|
||||
|
||||
}
|
|
@ -0,0 +1,54 @@
|
|||
package com.ruoyi.mybasic.common.domain.response;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 身份证返回值
|
||||
* @author Lou_Zs
|
||||
*/
|
||||
@Data
|
||||
public class ResponseCard {
|
||||
|
||||
//<--正面-->
|
||||
/**
|
||||
* 地址详情
|
||||
*/
|
||||
private String address;
|
||||
/**
|
||||
* 姓名
|
||||
*/
|
||||
private String name;
|
||||
/**
|
||||
* 民族
|
||||
*/
|
||||
private String nationality;
|
||||
/**
|
||||
* 身份证号
|
||||
*/
|
||||
private String num;
|
||||
/**
|
||||
* 性别
|
||||
*/
|
||||
private String sex;
|
||||
/**
|
||||
* 生日
|
||||
*/
|
||||
private String birth;
|
||||
//<--正面-->
|
||||
|
||||
//<--反面-->
|
||||
/**
|
||||
* 起始有效期
|
||||
*/
|
||||
private String startDate;
|
||||
/**
|
||||
* 结束有效期
|
||||
*/
|
||||
private String endDate;
|
||||
/**
|
||||
* 签发机关
|
||||
*/
|
||||
private String issue;
|
||||
//<--反面-->
|
||||
|
||||
}
|
|
@ -0,0 +1,48 @@
|
|||
package com.ruoyi.mybasic.controller;
|
||||
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ruoyi.common.core.domain.R;
|
||||
import com.ruoyi.mybasic.service.CardService;
|
||||
import lombok.extern.log4j.Log4j2;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
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 javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* 上传身份证控制成
|
||||
* @author Lou_Zs
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/cardUpload")
|
||||
@Log4j2
|
||||
public class CardController {
|
||||
|
||||
@Autowired
|
||||
private CardService cardService;
|
||||
@Autowired
|
||||
private HttpServletRequest request;
|
||||
|
||||
|
||||
/**
|
||||
* 上传身份证
|
||||
* @param fileFace
|
||||
* @param fileBack
|
||||
* @return
|
||||
*/
|
||||
@PostMapping("/uploadCardFace")
|
||||
public R uploadCardFace(@RequestParam("fileFace")MultipartFile fileFace,@RequestParam("fileBack")MultipartFile fileBack){
|
||||
log.info("功能介绍:上传身份证,请求方式:{},请求路径:{},请求参数:{}",
|
||||
request.getMethod(),request.getRequestURI(), JSONObject.toJSONString(fileFace+"-"+fileBack));
|
||||
//上传身份证正面
|
||||
cardService.uploadCardFace(fileFace,fileBack);
|
||||
log.info("功能介绍:上传身份证,请求方式:{},请求路径:{},响应结果:{}",
|
||||
request.getMethod(),request.getRequestURI(), JSONObject.toJSONString(R.ok()));
|
||||
return R.ok();
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
package com.ruoyi.mybasic.mapper;
|
||||
|
||||
import com.ruoyi.mybasic.common.domain.Card;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
/**
|
||||
* 上传身份证mapper层
|
||||
*/
|
||||
@Mapper
|
||||
public interface CardMapper {
|
||||
/**
|
||||
* 根据用户id查询身份证信息
|
||||
* @param userId
|
||||
* @return
|
||||
*/
|
||||
Card findUserCard(@Param("userId") Long userId);
|
||||
|
||||
/**
|
||||
* 添加身份证信息
|
||||
* @param addCard
|
||||
* @return
|
||||
*/
|
||||
int insertUserCard(Card addCard);
|
||||
}
|
|
@ -0,0 +1,17 @@
|
|||
package com.ruoyi.mybasic.service;
|
||||
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
/**
|
||||
* 上传身份证实现层
|
||||
* @author Lou_Zs
|
||||
*/
|
||||
public interface CardService {
|
||||
|
||||
/**
|
||||
* 上传身份证
|
||||
* @param fileFace
|
||||
* @param fileBack
|
||||
*/
|
||||
void uploadCardFace(MultipartFile fileFace, MultipartFile fileBack);
|
||||
}
|
|
@ -0,0 +1,133 @@
|
|||
package com.ruoyi.mybasic.service.Impl;
|
||||
|
||||
import com.alibaba.druid.util.StringUtils;
|
||||
import com.ruoyi.common.security.utils.SecurityUtils;
|
||||
import com.ruoyi.mybasic.common.domain.Card;
|
||||
import com.ruoyi.mybasic.common.domain.response.ResponseCard;
|
||||
import com.ruoyi.mybasic.mapper.CardMapper;
|
||||
import com.ruoyi.mybasic.service.CardService;
|
||||
import com.ruoyi.mybasic.util.SdkCardUtils;
|
||||
import com.ruoyi.mybasic.util.UploadUtil;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 上传身份证实现层
|
||||
* @author Lou_Zs
|
||||
*/
|
||||
@Service
|
||||
public class CardServiceImpl implements CardService {
|
||||
|
||||
|
||||
|
||||
@Autowired
|
||||
private CardMapper cardMapper;
|
||||
@Autowired
|
||||
private UploadUtil uploadUtil;
|
||||
@Autowired
|
||||
private SdkCardUtils sdkCardUtils;
|
||||
|
||||
/**
|
||||
* 身份证上传
|
||||
* @param fileFace
|
||||
* @param fileBack
|
||||
*/
|
||||
@Override
|
||||
public void uploadCardFace(MultipartFile fileFace, MultipartFile fileBack) {
|
||||
//获取登入id
|
||||
Long userId = SecurityUtils.getUserId();
|
||||
Card card = cardMapper.findUserCard(userId);
|
||||
if (null != card){
|
||||
throw new RuntimeException("身份证信息已存在!");
|
||||
}
|
||||
//正面上传
|
||||
String uploadFace = uploadUtil.upload(fileFace);
|
||||
if (StringUtils.isEmpty(uploadFace)){
|
||||
throw new RuntimeException("正面身份证信息上传失败");
|
||||
}
|
||||
//将图片文件读取并base64编码
|
||||
String img_base64_face = sdkCardUtils.img_base64(uploadFace);
|
||||
//反面上传
|
||||
//将图片文件读取并base64编码
|
||||
String uploadBack= uploadUtil.upload(fileBack);
|
||||
if (StringUtils.isEmpty(uploadBack)){
|
||||
throw new RuntimeException("反面身份证信息上传失败");
|
||||
}
|
||||
String img_base64_back = sdkCardUtils.img_base64(uploadBack);
|
||||
|
||||
//提取信息
|
||||
//正
|
||||
ResponseCard cardedFace = sdkCardUtils.cardFace(img_base64_face);
|
||||
if (null == cardedFace){
|
||||
throw new RuntimeException("正面身份证信息扫描失败");
|
||||
}
|
||||
//反
|
||||
ResponseCard cardBack = sdkCardUtils.cardBack(img_base64_back);
|
||||
if (null == cardBack){
|
||||
throw new RuntimeException("反面身份证信息扫描失败");
|
||||
}
|
||||
//添加身份证信息
|
||||
//正
|
||||
Card addCard = new Card();
|
||||
addCard.setUserId(Math.toIntExact(userId));
|
||||
addCard.setCardFace(uploadFace);
|
||||
addCard.setCardBack(uploadBack);
|
||||
addCard.setNum(cardedFace.getNum());
|
||||
addCard.setName(cardedFace.getName());
|
||||
addCard.setAddress(cardedFace.getAddress());
|
||||
addCard.setSex(cardedFace.getSex());
|
||||
addCard.setBirth(cardedFace.getBirth());
|
||||
addCard.setNationality(cardedFace.getNationality());
|
||||
///反
|
||||
addCard.setStartDate(cardBack.getStartDate());
|
||||
addCard.setEndDate(cardBack.getEndDate());
|
||||
addCard.setIssue(cardBack.getIssue());
|
||||
addCard.setCreateTime(new Date());
|
||||
//添加身份证信息
|
||||
int i = cardMapper.insertUserCard(addCard);
|
||||
if (i < 0){
|
||||
throw new RuntimeException("身份证信息填充失败!");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// /**
|
||||
// * 上传身份证正面
|
||||
// * @param file
|
||||
// */
|
||||
// @Override
|
||||
// public void uploadCardFace(MultipartFile file) {
|
||||
// //获取当前登入人
|
||||
// Long userId = SecurityUtils.getUserId();
|
||||
// //上传文件
|
||||
// String upload = uploadUtil.upload(file);
|
||||
// if (StringUtils.isEmpty(upload)){
|
||||
// throw new RuntimeException("图片上传失败!");
|
||||
// }
|
||||
// //查询用户是否存在身份证信息
|
||||
// Card card = cardMapper.findUserCard(userId);
|
||||
// if (null != card){
|
||||
// //存在身份证信息
|
||||
// if (card.getCardFace()!=null && !card.getCardFace().equals("")){
|
||||
// //存在身份证正面信息
|
||||
// throw new RuntimeException("身份证正面信息已存在!");
|
||||
// }
|
||||
// }
|
||||
// if (null == card){
|
||||
// //创建身份证信息表
|
||||
// Card addCard = new Card();
|
||||
// addCard.setUserId(Math.toIntExact(userId));
|
||||
// addCard.setCardFace(upload);
|
||||
// int i = cardMapper.insertCard(addCard);
|
||||
// }
|
||||
// //将图片文件读取并base64编码
|
||||
// String img_base64 = sdkCardUtils.img_base64(upload);
|
||||
// //读取身份证信息
|
||||
// ResponseCard responseCard = sdkCardUtils.cardBack(img_base64);
|
||||
// //添加身份证信息详情
|
||||
//
|
||||
// }
|
||||
}
|
|
@ -0,0 +1,159 @@
|
|||
package com.ruoyi.mybasic.util;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.ruoyi.mybasic.common.domain.response.ResponseCard;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.util.EntityUtils;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.IOException;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 身份证上传工具类
|
||||
* @author Lou_Zs
|
||||
*/
|
||||
@Component
|
||||
public class SdkCardUtils {
|
||||
public String img_base64(String path) {
|
||||
/**
|
||||
* 对path进行判断,如果是本地文件就二进制读取并base64编码,如果是url,则返回
|
||||
*/
|
||||
String imgBase64="";
|
||||
if (path.startsWith("http")){
|
||||
imgBase64 = path;
|
||||
}else {
|
||||
try {
|
||||
File file = new File(path);
|
||||
byte[] content = new byte[(int) file.length()];
|
||||
FileInputStream finputstream = new FileInputStream(file);
|
||||
finputstream.read(content);
|
||||
finputstream.close();
|
||||
imgBase64 = new String(Base64.encodeBase64(content));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
return imgBase64;
|
||||
}
|
||||
}
|
||||
return imgBase64;
|
||||
}
|
||||
|
||||
public ResponseCard cardFace(String file) {
|
||||
String host = "https://cardnumber.market.alicloudapi.com";
|
||||
String path = "/rest/160601/ocr/ocr_idcard.json";
|
||||
String appcode = "6b430f0ea246474c9245b8b35388f7a9";
|
||||
String imgFile = file;
|
||||
String method = "POST";
|
||||
|
||||
Map<String, String> headers = new HashMap<String, String>();
|
||||
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
|
||||
headers.put("Authorization", "APPCODE " + appcode);
|
||||
//根据API的要求,定义相对应的Content-Type
|
||||
headers.put("Content-Type", "application/json; charset=UTF-8");
|
||||
|
||||
Map<String, String> querys = new HashMap<String, String>();
|
||||
// 对图像进行base64编码
|
||||
String imgBase64 = img_base64(imgFile);
|
||||
|
||||
//configure配置
|
||||
JSONObject configObj = new JSONObject();
|
||||
configObj.put("side", "face");
|
||||
|
||||
String config_str = configObj.toString();
|
||||
|
||||
// 拼装请求body的json字符串
|
||||
JSONObject requestObj = new JSONObject();
|
||||
requestObj.put("image", imgBase64);
|
||||
if(configObj.size() > 0) {
|
||||
requestObj.put("configure", config_str);
|
||||
}
|
||||
String bodys = requestObj.toString();
|
||||
|
||||
try {
|
||||
/**
|
||||
* 重要提示如下:
|
||||
* HttpUtils请从
|
||||
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
|
||||
* 下载
|
||||
*
|
||||
* 相应的依赖请参照
|
||||
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
|
||||
*/
|
||||
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
|
||||
int stat = response.getStatusLine().getStatusCode();
|
||||
if(stat != 200){
|
||||
System.out.println("Http code: " + stat);
|
||||
System.out.println("http header error msg: "+ response.getFirstHeader("X-Ca-Error-Message"));
|
||||
System.out.println("Http body error msg:" + EntityUtils.toString(response.getEntity()));
|
||||
}
|
||||
String res = EntityUtils.toString(response.getEntity());
|
||||
ResponseCard responseCard = JSON.parseObject(res, ResponseCard.class);
|
||||
return responseCard;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public ResponseCard cardBack(String file) {
|
||||
String host = "https://cardnumber.market.alicloudapi.com";
|
||||
String path = "/rest/160601/ocr/ocr_idcard.json";
|
||||
String appcode = "6b430f0ea246474c9245b8b35388f7a9";
|
||||
String imgFile = file;
|
||||
String method = "POST";
|
||||
|
||||
Map<String, String> headers = new HashMap<String, String>();
|
||||
//最后在header中的格式(中间是英文空格)为Authorization:APPCODE 83359fd73fe94948385f570e3c139105
|
||||
headers.put("Authorization", "APPCODE " + appcode);
|
||||
//根据API的要求,定义相对应的Content-Type
|
||||
headers.put("Content-Type", "application/json; charset=UTF-8");
|
||||
|
||||
Map<String, String> querys = new HashMap<String, String>();
|
||||
// 对图像进行base64编码
|
||||
String imgBase64 = img_base64(imgFile);
|
||||
|
||||
//configure配置
|
||||
JSONObject configObj = new JSONObject();
|
||||
configObj.put("side", "back");
|
||||
|
||||
String config_str = configObj.toString();
|
||||
|
||||
// 拼装请求body的json字符串
|
||||
JSONObject requestObj = new JSONObject();
|
||||
requestObj.put("image", imgBase64);
|
||||
if(configObj.size() > 0) {
|
||||
requestObj.put("configure", config_str);
|
||||
}
|
||||
String bodys = requestObj.toString();
|
||||
|
||||
try {
|
||||
/**
|
||||
* 重要提示如下:
|
||||
* HttpUtils请从
|
||||
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/src/main/java/com/aliyun/api/gateway/demo/util/HttpUtils.java
|
||||
* 下载
|
||||
*
|
||||
* 相应的依赖请参照
|
||||
* https://github.com/aliyun/api-gateway-demo-sign-java/blob/master/pom.xml
|
||||
*/
|
||||
HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
|
||||
int stat = response.getStatusLine().getStatusCode();
|
||||
if(stat != 200){
|
||||
System.out.println("Http code: " + stat);
|
||||
System.out.println("http header error msg: "+ response.getFirstHeader("X-Ca-Error-Message"));
|
||||
System.out.println("Http body error msg:" + EntityUtils.toString(response.getEntity()));
|
||||
}
|
||||
String res = EntityUtils.toString(response.getEntity());
|
||||
ResponseCard responseCard = JSON.parseObject(res, ResponseCard.class);
|
||||
return responseCard;
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -1,6 +1,6 @@
|
|||
# Tomcat
|
||||
server:
|
||||
port: 9001
|
||||
port: 9785
|
||||
|
||||
# Spring
|
||||
spring:
|
||||
|
|
|
@ -0,0 +1,46 @@
|
|||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE mapper
|
||||
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
|
||||
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.ruoyi.mybasic.mapper.CardMapper">
|
||||
|
||||
<insert id="insertUserCard">
|
||||
INSERT INTO
|
||||
t_card (
|
||||
user_id,
|
||||
card_face,
|
||||
card_back,
|
||||
num,
|
||||
name,
|
||||
address,
|
||||
sex,
|
||||
birth,
|
||||
nationality,
|
||||
start_date,
|
||||
end_date,
|
||||
issue,
|
||||
create_time)
|
||||
VALUES
|
||||
(
|
||||
#{userId},
|
||||
#{cardFace},
|
||||
#{cardBack},
|
||||
#{num},
|
||||
#{name},
|
||||
#{address},
|
||||
#{sex},
|
||||
#{birth},
|
||||
#{nationality},
|
||||
#{startDate},
|
||||
#{endDate},
|
||||
#{issue},
|
||||
#{createTime}
|
||||
)
|
||||
|
||||
</insert>
|
||||
|
||||
|
||||
<select id="findUserCard" resultType="com.ruoyi.mybasic.common.domain.Card">
|
||||
select * from t_card where user_id = #{userId}
|
||||
</select>
|
||||
</mapper>
|
Loading…
Reference in New Issue