|
|
|
|
package com.ynxbd.common.config;
|
|
|
|
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
|
|
import com.tencent.healthcard.impl.HealthCardServerImpl;
|
|
|
|
|
import com.tencent.healthcard.model.CommonIn;
|
|
|
|
|
import com.ynxbd.common.helper.ProperHelper;
|
|
|
|
|
import com.ynxbd.common.helper.common.ErrorHelper;
|
|
|
|
|
import com.ynxbd.common.helper.common.JsonHelper;
|
|
|
|
|
import com.ynxbd.wx.config.WeChatConfig;
|
|
|
|
|
import lombok.Getter;
|
|
|
|
|
import lombok.NoArgsConstructor;
|
|
|
|
|
import lombok.Setter;
|
|
|
|
|
import lombok.ToString;
|
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
import org.apache.commons.lang3.ObjectUtils;
|
|
|
|
|
import org.ehcache.Cache;
|
|
|
|
|
|
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
|
|
// 电子健康卡配置
|
|
|
|
|
|
|
|
|
|
@Slf4j
|
|
|
|
|
public class HealthCardConfig {
|
|
|
|
|
public static final String MINI_CACHE_KEY = "mini_app_token_cache";
|
|
|
|
|
public static final String APP_CACHE_KEY = "app_token_cache";
|
|
|
|
|
|
|
|
|
|
public static synchronized void initCache() {
|
|
|
|
|
if (CACHE == null) { // 创建一个缓存实例(7000s最大存活时间)
|
|
|
|
|
CACHE = EhCacheConfig.createCache(String.class, String.class, "health_card_cache", 1L, 3L, 10L, false, 7000L, null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static final boolean IS_ENABLE; // 是否启用电子健康卡(true:启用, false:禁用)
|
|
|
|
|
public static final boolean IS_UPLOAD_DATA; // 是否允许上传数据(true:启用, false:禁用)
|
|
|
|
|
public static final boolean IS_LOG_REPORT; // 是否打印上报日志(true:启用, false:禁用)
|
|
|
|
|
|
|
|
|
|
// 健康码
|
|
|
|
|
public static final String H_APP_ID;
|
|
|
|
|
public static final String H_APP_SECRET;
|
|
|
|
|
public static final String H_HOSPITAL_ID; // 医院ID(每家医院不一样)
|
|
|
|
|
public static final String H_MINI_HOSPITAL_ID; // 小程序-医院ID(每家医院不一样)
|
|
|
|
|
public static final String H_MINI_APP_ID; // 小程序-appid
|
|
|
|
|
// 万达
|
|
|
|
|
public static final String CARD_URL;
|
|
|
|
|
public static final String CARD_APP_ID;
|
|
|
|
|
public static final String CARD_PUBLIC_KEY;
|
|
|
|
|
public static final String CARD_PRIVATE_KEY;
|
|
|
|
|
|
|
|
|
|
// 缓存
|
|
|
|
|
private static Cache<String, String> CACHE;
|
|
|
|
|
|
|
|
|
|
// 离线WeChartCode 批量领取健康卡用
|
|
|
|
|
public static final String WE_CHART_CODE = "73EFA6796D3869FF82FAE7E81E9814B7";
|
|
|
|
|
|
|
|
|
|
// 公众号不用传,小程序内嵌必传,具体传参请联系平台对接人员分配
|
|
|
|
|
public static final int DOMAIN_CHANNEL = 0;
|
|
|
|
|
|
|
|
|
|
static {
|
|
|
|
|
ProperHelper config = new ProperHelper().read("hcode.properties");
|
|
|
|
|
IS_ENABLE = config.getBoolean("is_enable", false);
|
|
|
|
|
IS_UPLOAD_DATA = config.getBoolean("h.is_upload_data", true);
|
|
|
|
|
IS_LOG_REPORT = config.getBoolean("h.is_log_report", false);
|
|
|
|
|
// 不用对config设置开关,不开启时也需要用到参数
|
|
|
|
|
H_APP_ID = config.getString("h.app_id");
|
|
|
|
|
H_APP_SECRET = config.getString("h.app_secret");
|
|
|
|
|
CARD_APP_ID = config.getString("h.card_app_id");
|
|
|
|
|
CARD_PUBLIC_KEY = config.getString("h.card_public_key");
|
|
|
|
|
CARD_PRIVATE_KEY = config.getString("h.card_private_key");
|
|
|
|
|
CARD_URL = config.getString("h.card_url");
|
|
|
|
|
|
|
|
|
|
H_HOSPITAL_ID = config.getString("h.hospital_id");
|
|
|
|
|
if (ObjectUtils.isEmpty(H_HOSPITAL_ID)) {
|
|
|
|
|
log.info("[电子健康卡]医院id缺失");
|
|
|
|
|
}
|
|
|
|
|
H_MINI_APP_ID = config.getString("h.mini_app_id");
|
|
|
|
|
H_MINI_HOSPITAL_ID = config.getString("h.mini_hospital_id");
|
|
|
|
|
|
|
|
|
|
initCache();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 判断是否开启电子健康卡
|
|
|
|
|
public static boolean isEnable() {
|
|
|
|
|
if (!IS_ENABLE) {
|
|
|
|
|
log.info("[配置][hcode.properties]未开启电子健康卡IS_ENABLE");
|
|
|
|
|
}
|
|
|
|
|
return IS_ENABLE;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public static HealthCardServerImpl createHealthCardService() {
|
|
|
|
|
return new HealthCardServerImpl(HealthCardConfig.H_APP_SECRET);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CommonIn createCommonIn() {
|
|
|
|
|
return createCommonIn(false, null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CommonIn createCommonIn(Boolean isMiniApp, String openid) {
|
|
|
|
|
return createCommonIn(isMiniApp, getAppToken(isMiniApp, true), openid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CommonIn createCommonInOrcInfo(Boolean isMiniApp, String openid) {
|
|
|
|
|
return createCommonIn(isMiniApp, getAppToken(false, false), openid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static CommonIn createCommonIn(Boolean isMiniApp, String appToken, String openid) {
|
|
|
|
|
if (appToken == null) { // 此处不判断空字符串
|
|
|
|
|
log.info("[电子健康卡]appToken为空");
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
if (isMiniApp == null) {
|
|
|
|
|
isMiniApp = false;
|
|
|
|
|
}
|
|
|
|
|
int channelNum = (isMiniApp ? 1 : 0); // 填入代码,0为微信服务号渠道,1为微信小程序渠道,3为刷脸终端,4为扫码终端
|
|
|
|
|
String hospId = (isMiniApp ? H_MINI_HOSPITAL_ID : H_HOSPITAL_ID);
|
|
|
|
|
String requestId = UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();
|
|
|
|
|
String appId = (isMiniApp ? H_MINI_APP_ID : WeChatConfig.APP_ID);
|
|
|
|
|
return new CommonIn(appToken, requestId, hospId, channelNum, appId, openid);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取token
|
|
|
|
|
*
|
|
|
|
|
* @param isEnTokenSwitch [true:必须开启功能才能获取token]
|
|
|
|
|
* @return token
|
|
|
|
|
*/
|
|
|
|
|
public static String getAppToken(Boolean isMiniApp, boolean isEnTokenSwitch) {
|
|
|
|
|
if (isEnTokenSwitch && !isEnable()) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isMiniApp == null) {
|
|
|
|
|
isMiniApp = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String appToken = null;
|
|
|
|
|
if (CACHE == null) {
|
|
|
|
|
initCache();
|
|
|
|
|
}
|
|
|
|
|
if (CACHE != null) {
|
|
|
|
|
appToken = CACHE.get(isMiniApp ? MINI_CACHE_KEY : APP_CACHE_KEY);
|
|
|
|
|
if (appToken != null) {
|
|
|
|
|
return appToken;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
CommonIn commonIn = createCommonIn(isMiniApp, "", null);
|
|
|
|
|
if (commonIn == null) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
JSONObject resultJson = new HealthCardServerImpl(H_APP_SECRET, 5, 10).getAppToken(commonIn, H_APP_ID);
|
|
|
|
|
HCardResult result = new HCardResult(resultJson);
|
|
|
|
|
if (!result.isOk) {
|
|
|
|
|
log.info("[电子健康卡]获取appToken失败: {}", resultJson);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
JSONObject respJson = result.getRsp();
|
|
|
|
|
appToken = respJson.getString("appToken");
|
|
|
|
|
|
|
|
|
|
if (CACHE != null && !ObjectUtils.isEmpty(appToken)) {
|
|
|
|
|
CACHE.put("appToken", appToken);
|
|
|
|
|
}
|
|
|
|
|
} catch (Exception e) {
|
|
|
|
|
ErrorHelper.println(e);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (ObjectUtils.isEmpty(appToken)) {
|
|
|
|
|
log.info("[电子健康卡]获取appToken为空");
|
|
|
|
|
}
|
|
|
|
|
return appToken;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Setter
|
|
|
|
|
@Getter
|
|
|
|
|
@ToString
|
|
|
|
|
@NoArgsConstructor
|
|
|
|
|
public static class HCardResult { // 数据解析
|
|
|
|
|
private JSONObject commonOut;
|
|
|
|
|
private JSONObject rsp;
|
|
|
|
|
private String resultCode;
|
|
|
|
|
private String errMsg;
|
|
|
|
|
public boolean isOk;
|
|
|
|
|
|
|
|
|
|
public String getJsonCommon() {
|
|
|
|
|
return JsonHelper.toJsonString(this.commonOut);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public String getErrMsg() {
|
|
|
|
|
return this.errMsg == null ? "" : this.errMsg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public HCardResult(JSONObject resultJsonObj) {
|
|
|
|
|
if (resultJsonObj == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.rsp = resultJsonObj.getJSONObject("rsp");
|
|
|
|
|
this.commonOut = resultJsonObj.getJSONObject("commonOut");
|
|
|
|
|
if (this.commonOut == null) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.resultCode = commonOut.getString("resultCode");
|
|
|
|
|
this.isOk = "0".equals(this.resultCode);
|
|
|
|
|
|
|
|
|
|
if (!this.isOk) {
|
|
|
|
|
this.errMsg = commonOut.getString("errMsg");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|