Commit 4e9de88b authored by zhangzhenbang's avatar zhangzhenbang

与一所健康码对接

parent d22af23c
...@@ -11,10 +11,7 @@ import com.yingxin.beijingvehicleflow.response.Response; ...@@ -11,10 +11,7 @@ import com.yingxin.beijingvehicleflow.response.Response;
import com.yingxin.beijingvehicleflow.service.AuthService; import com.yingxin.beijingvehicleflow.service.AuthService;
import com.yingxin.beijingvehicleflow.service.IdentityInformationService; import com.yingxin.beijingvehicleflow.service.IdentityInformationService;
import com.yingxin.beijingvehicleflow.service.ReservationService; import com.yingxin.beijingvehicleflow.service.ReservationService;
import com.yingxin.beijingvehicleflow.util.AESUtil; import com.yingxin.beijingvehicleflow.util.*;
import com.yingxin.beijingvehicleflow.util.DesUtil;
import com.yingxin.beijingvehicleflow.util.IdCardUtil;
import com.yingxin.beijingvehicleflow.util.PicUtils;
import net.sf.json.JSONException; import net.sf.json.JSONException;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -159,7 +156,7 @@ public class IdentityInfoController { ...@@ -159,7 +156,7 @@ public class IdentityInfoController {
/*生成一所健康二维码*/ /*生成一所健康二维码*/
String qrCode; String qrCode;
try { try {
qrCode = identityInformationService.getFriQRCode(encodeData.getQrCodeData()); qrCode = identityInformationService.generateFriQrCodeBase64(encodeData.getQrCodeData());
} catch (SystemException e) { } catch (SystemException e) {
LOGGER.error(e.getCode()+"->"+e.getMsg(), e); LOGGER.error(e.getCode()+"->"+e.getMsg(), e);
return Response.fail(e.getCode(), e.getMsg()); return Response.fail(e.getCode(), e.getMsg());
...@@ -167,7 +164,6 @@ public class IdentityInfoController { ...@@ -167,7 +164,6 @@ public class IdentityInfoController {
encodeData.setQrCodeData(qrCode); encodeData.setQrCodeData(qrCode);
return new Response<EncodeData>(true,"","", encodeData); return new Response<EncodeData>(true,"","", encodeData);
} }
/** /**
......
...@@ -49,7 +49,9 @@ public interface IdentityInformationService { ...@@ -49,7 +49,9 @@ public interface IdentityInformationService {
IdentityInformation getAllIdentityInfoById(int id); IdentityInformation getAllIdentityInfoById(int id);
String getFriQRCode(String encodedDateStr) throws SystemException; JSONObject getFriQRCoderResponse(String encodedDateStr) throws SystemException;
String validateFriQRCode(String qrCodeData) throws SystemException; String validateFriQRCode(String qrCodeData) throws SystemException;
String generateFriQrCodeBase64(String dataToFriEncode) throws SystemException;
} }
package com.yingxin.beijingvehicleflow.service.impl; package com.yingxin.beijingvehicleflow.service.impl;
import com.yingxin.beijingvehicleflow.constant.Const;
import com.yingxin.beijingvehicleflow.dto.ReservationsDisplayDTO; import com.yingxin.beijingvehicleflow.dto.ReservationsDisplayDTO;
import com.yingxin.beijingvehicleflow.entity.IdentityInformation; import com.yingxin.beijingvehicleflow.entity.IdentityInformation;
import com.yingxin.beijingvehicleflow.entity.Reservation; import com.yingxin.beijingvehicleflow.entity.Reservation;
import com.yingxin.beijingvehicleflow.exception.SystemException; import com.yingxin.beijingvehicleflow.exception.SystemException;
import com.yingxin.beijingvehicleflow.mapper.IdentityInformationMapper; import com.yingxin.beijingvehicleflow.mapper.IdentityInformationMapper;
import com.yingxin.beijingvehicleflow.service.IdentityInformationService; import com.yingxin.beijingvehicleflow.service.IdentityInformationService;
import com.yingxin.beijingvehicleflow.util.PicUtils; import com.yingxin.beijingvehicleflow.util.QRCodeHelper;
import net.sf.json.JSONObject; import net.sf.json.JSONObject;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -29,7 +28,7 @@ import java.util.HashMap; ...@@ -29,7 +28,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
/** /**
* description //TODO * 身份信息相关接口实现类
* *
* @author 226 * @author 226
* @version 1.0 * @version 1.0
...@@ -145,7 +144,7 @@ public class IdentityInformationServiceImpl implements IdentityInformationServic ...@@ -145,7 +144,7 @@ public class IdentityInformationServiceImpl implements IdentityInformationServic
private String friQrCodeGenerateUrl; private String friQrCodeGenerateUrl;
@Override @Override
public String getFriQRCode(String encodedDateStr) throws SystemException{ public JSONObject getFriQRCoderResponse(String encodedDateStr) throws SystemException{
HashMap<String,String> hashMap = new HashMap<>(4); HashMap<String,String> hashMap = new HashMap<>(4);
hashMap.put("bizExtData", encodedDateStr); hashMap.put("bizExtData", encodedDateStr);
...@@ -156,12 +155,14 @@ public class IdentityInformationServiceImpl implements IdentityInformationServic ...@@ -156,12 +155,14 @@ public class IdentityInformationServiceImpl implements IdentityInformationServic
LOGGER.error("一所二维码生成接口异常:" , e); LOGGER.error("一所二维码生成接口异常:" , e);
throw new SystemException("FRI_QR_CODE_GENERATE_FAILED", "一所二维码生成接口调用异常"); throw new SystemException("FRI_QR_CODE_GENERATE_FAILED", "一所二维码生成接口调用异常");
} }
JSONObject responseJson = JSONObject.fromObject(response); try {
return JSONObject.fromObject(response);
if (responseJson.getBoolean("success")) { } catch (Exception e) {
return responseJson.getString("qrCode"); LOGGER.error("一所二维码生成接口响应结果解析异常:" , e);
throw new SystemException("FRI_QR_CODE_RESPONSE_PARES_FAILED", "一所二维码生成接口响应结果解析异常");
} }
throw new SystemException("FRI_QR_CODE_GENERATE_FAILED", responseJson.getString("errorDec"));
} }
@Value("${validate-QR-code-Fri-Url}") @Value("${validate-QR-code-Fri-Url}")
...@@ -187,4 +188,34 @@ public class IdentityInformationServiceImpl implements IdentityInformationServic ...@@ -187,4 +188,34 @@ public class IdentityInformationServiceImpl implements IdentityInformationServic
throw new SystemException("FRI_QR_CODE_GENERATE_FAILED", responseJson.getString("errorDec")); throw new SystemException("FRI_QR_CODE_GENERATE_FAILED", responseJson.getString("errorDec"));
} }
@Override
public String generateFriQrCodeBase64(String dataToFriEncode) throws SystemException{
JSONObject qrCodeResponseJson;
try {
qrCodeResponseJson = getFriQRCoderResponse(dataToFriEncode);
} catch (SystemException e) {
LOGGER.error(e.getCode()+"->"+e.getMsg(), e);
throw new SystemException(e.getCode(), e.getMsg());
}
if (!qrCodeResponseJson.getBoolean("success")) {
throw new SystemException(qrCodeResponseJson.getString("code"), qrCodeResponseJson.getString("errorDec"));
}
String qrStream;
int width;
try {
qrStream = qrCodeResponseJson.getString("qrCode");
width = qrCodeResponseJson.getInt("width");
} catch (Exception e) {
LOGGER.error("二维码生成接口响应结果解析异常: ", e);
throw new SystemException("QR-FRI-RESPONSE-PARSE-ERROR", "二维码生成接口响应结果解析异常");
}
try {
return QRCodeHelper.imgStreamToBase64Image(qrStream, width);
} catch (Exception e) {
LOGGER.error("二维码流转Base64异常: ", e);
throw new SystemException("QR-TO-BASE64-ERROR", "二维码流转Base64异常");
}
}
} }
package com.yingxin.beijingvehicleflow.util;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
/**
* @auther 李晓阳
* @email lixy@yingxininfo.net
* @date 2020/2/26 17:12
*/
public class QRCodeHelper {
private static final Logger logger = LoggerFactory.getLogger(QRCodeHelper.class);
private static final int multi = 4;
//白边为4个模块,是标准,下面做图片放大时候会自动放大
private static final int WIDTH_BLOCK = 4;
/**
* 二维码图流转化为图片对象
* @param imageData
* @param width
* @return
* @throws IOException
*/
public static String imgStreamToBase64Image(String imageData, int width) throws IOException {
//白色背景 whiteBlock*2 上下两个
int canvasWidth = (width + WIDTH_BLOCK * 2) * multi;
BufferedImage image = new BufferedImage(canvasWidth, canvasWidth, BufferedImage.TYPE_INT_ARGB);
image.createGraphics();
Graphics2D graphics = (Graphics2D) image.getGraphics();
graphics.fillRect(0, 0, canvasWidth, canvasWidth);
//base64解码
byte[] sourceBytes = new BASE64Decoder().decodeBuffer(imageData);
//填充数据
int n = width;
int wid = (n + 7) / 8;
for (int i = 0; i < n; i++) {
int ln = i * wid;
for (int j = 0; j < n; j++) {
int k = ln + j / 8;
Color color = (((byte) 0) == (sourceBytes[k] & ((byte) 0x01 << (7 - (j % 8))))) ? Color.WHITE : Color.BLACK;
graphics.setColor(color);
graphics.fillRect((i + WIDTH_BLOCK) * multi, (j + WIDTH_BLOCK) * multi, multi, multi);
}
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();//io流
ImageIO.write(image, "png", baos);//写入流中
byte[] bytes = baos.toByteArray();//转换成字节
BASE64Encoder encoder = new BASE64Encoder();
String png_base64 = encoder.encodeBuffer(bytes).trim();//转换成base64串
png_base64 = png_base64.replaceAll("\n", "").replaceAll("\r", "");//删除 \r\n
try {
if (baos != null) {
baos.close();
}
} catch (IOException e) {
logger.error("二维码流关闭异常:", e);
}
return png_base64;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment