|  | @ -1,36 +1,31 @@
 | 
	
		
			
				|  |  | package com.yihu.wlyy.service.third.ylz;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSON;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONArray;
 | 
	
		
			
				|  |  | import com.alibaba.fastjson.JSONObject;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.service.third.ylz.model.YLZAccessToken;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.service.third.ylz.model.YLZUser;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.service.third.ylz.model.YLZUserCard;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.util.HttpClientUtil;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.entity.patient.SocialSecurityInfo;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.repository.patient.SocialSecurityInfoDao;
 | 
	
		
			
				|  |  | import com.yihu.wlyy.util.IdCardUtil;
 | 
	
		
			
				|  |  | import com.ylzinfo.ehc.empi.sdk.EhcEmpiClient;
 | 
	
		
			
				|  |  | import com.ylzinfo.ehc.empi.sdk.domain.ResponseParams;
 | 
	
		
			
				|  |  | import com.ylzinfo.ehc.empi.sdk.dto.request.AuthorizationTokenRequest;
 | 
	
		
			
				|  |  | import com.ylzinfo.onepay.sdk.OnepayDefaultClient;
 | 
	
		
			
				|  |  | import com.ylzinfo.onepay.sdk.domain.RequestParams;
 | 
	
		
			
				|  |  | import com.ylzinfo.onepay.sdk.domain.ResponseParams;
 | 
	
		
			
				|  |  | import com.ylzinfo.onepay.sdk.exception.PayException;
 | 
	
		
			
				|  |  | import com.ylzinfo.onepay.sdk.utils.DateUtil;
 | 
	
		
			
				|  |  | import com.ylzinfo.onepay.sdk.utils.SecurityUtil;
 | 
	
		
			
				|  |  | import com.ylzinfo.onepay.sdk.utils.Signature;
 | 
	
		
			
				|  |  | import com.ylzinfo.onepay.sdk.utils.StringUtil;
 | 
	
		
			
				|  |  | import com.ylzinfo.ehc.empi.sdk.dto.request.UserInfoRequest;
 | 
	
		
			
				|  |  | import com.ylzinfo.ehc.empi.sdk.dto.response.AuthorizationTokenResponse;
 | 
	
		
			
				|  |  | import com.ylzinfo.ehc.empi.sdk.dto.response.UserInfoResponse;
 | 
	
		
			
				|  |  | 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.transaction.annotation.Transactional;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | import java.util.Date;
 | 
	
		
			
				|  |  | import java.util.HashMap;
 | 
	
		
			
				|  |  | import java.util.List;
 | 
	
		
			
				|  |  | import java.util.Map;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | /**
 | 
	
		
			
				|  |  |  * Created by chenweida on 2018/5/16 0016.
 | 
	
		
			
				|  |  |  */
 | 
	
		
			
				|  |  | @Service
 | 
	
		
			
				|  |  | public class YLZUserService {
 | 
	
		
			
				|  |  |     Logger logger = LoggerFactory.getLogger(YLZUserService.class);
 | 
	
		
			
				|  |  |     Logger logger = LoggerFactory.getLogger("elasticsearch_logger");
 | 
	
		
			
				|  |  |     //网关地址
 | 
	
		
			
				|  |  |     @Value("${ylz.gateway.url}")
 | 
	
		
			
				|  |  |     private String gatewayURL;
 | 
	
	
		
			
				|  | @ -43,33 +38,46 @@ public class YLZUserService {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Value("${ylz.appId}")
 | 
	
		
			
				|  |  |     private String appId;
 | 
	
		
			
				|  |  |     @Value("${ylz.termId}")
 | 
	
		
			
				|  |  |     private String termId;
 | 
	
		
			
				|  |  |     private String termId = "01";
 | 
	
		
			
				|  |  |     @Value("${ylz.appSecret}")
 | 
	
		
			
				|  |  |     private String appSecret;
 | 
	
		
			
				|  |  |     @Value("${ylz.redirectUrl}")
 | 
	
		
			
				|  |  |     private String redirectUrl;
 | 
	
		
			
				|  |  |     @Value("${server.server_url}")
 | 
	
		
			
				|  |  |     private String server_url;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private SocialSecurityInfoDao socialSecurityInfoDao;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private String signType = "MD5";
 | 
	
		
			
				|  |  |     private String encryptType = "AES";
 | 
	
		
			
				|  |  |     private String responseType = "token";
 | 
	
		
			
				|  |  |     @Autowired
 | 
	
		
			
				|  |  |     private HttpClientUtil httpClientUtil;
 | 
	
		
			
				|  |  |     private final String responseType = "token";
 | 
	
		
			
				|  |  |     private final String version = "1.0";
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public String getAccessToken() {
 | 
	
		
			
				|  |  |     public AuthorizationTokenResponse getAccessToken(String openId) {
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             EhcEmpiClient empiClient = new EhcEmpiClient(gatewayURL, appId, appSecret, termId, "1.0", signType, encryptType);
 | 
	
		
			
				|  |  |             EhcEmpiClient empiClient = new EhcEmpiClient(gatewayURL, appId, appSecret, termId, version, signType, encryptType);
 | 
	
		
			
				|  |  |             logger.info(net.sf.json.JSONObject.fromObject(empiClient).toString());
 | 
	
		
			
				|  |  |             try {
 | 
	
		
			
				|  |  |                 AuthorizationTokenRequest params = new AuthorizationTokenRequest();
 | 
	
		
			
				|  |  |                 params.setRedirectUrl(redirectUrl);
 | 
	
		
			
				|  |  |                 params.setUserName("48");
 | 
	
		
			
				|  |  |                 String url = empiClient.createUrl("gatewayPage", empiUimcAuthorization, params);
 | 
	
		
			
				|  |  |                 String responseStr = httpClientUtil.get(url, "UTF-8");
 | 
	
		
			
				|  |  |                 net.sf.json.JSONObject responseJSON = net.sf.json.JSONObject.fromObject(responseStr);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 Map params = new HashMap();
 | 
	
		
			
				|  |  |                 params.put("response_type", responseType);
 | 
	
		
			
				|  |  |                 params.put("redirect_url", redirectUrl);
 | 
	
		
			
				|  |  |                 params.put("auth_type", "IXM");
 | 
	
		
			
				|  |  |                 params.put("domain_cust_id", "openId");
 | 
	
		
			
				|  |  |                 logger.info(net.sf.json.JSONObject.fromObject(params).toString());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 //params 为自行解析回调参数。 key值参考文档
 | 
	
		
			
				|  |  |                 ResponseParams<AuthorizationTokenResponse> response = empiClient.checkResponseParams(params, AuthorizationTokenResponse.class);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |                 if (empiClient.isSuccessful(response)) {
 | 
	
		
			
				|  |  |                     AuthorizationTokenResponse authorizationTokenResponse = response.getBizContent();
 | 
	
		
			
				|  |  |                     logger.info(net.sf.json.JSONObject.fromObject(authorizationTokenResponse).toString());
 | 
	
		
			
				|  |  |                     return authorizationTokenResponse;
 | 
	
		
			
				|  |  |                 } else {
 | 
	
		
			
				|  |  |                     logger.error(new StringBuffer("调用易联众接口失败: " + empiUimcAuthorization + "," + net.sf.json.JSONObject.fromObject(response)).toString());
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             } catch (Exception e) {
 | 
	
		
			
				|  |  |                 e.printStackTrace();
 | 
	
		
			
				|  |  |             }
 | 
	
	
		
			
				|  | @ -80,61 +88,28 @@ public class YLZUserService {
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private String initParams(RequestParams requestParams) throws PayException {
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String sign = Signature.createSign(requestParams, this.appSecret);
 | 
	
		
			
				|  |  |         requestParams.setSign(sign);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         String requestMessage;
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             logger.info("加密前报文:" + JSONObject.toJSONString(requestParams));
 | 
	
		
			
				|  |  |             requestMessage = SecurityUtil.encrypt(JSONObject.toJSONString(requestParams.getParam()), this.encryptType, this.appSecret, this.appId);
 | 
	
		
			
				|  |  |             logger.info("加密后报文:" + requestMessage);
 | 
	
		
			
				|  |  |             requestParams.setEncryptData(requestMessage);
 | 
	
		
			
				|  |  |         } catch (Exception var11) {
 | 
	
		
			
				|  |  |             throw new PayException("请求报文加密失败");
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |         requestParams.setParam((Object) null);
 | 
	
		
			
				|  |  |         requestMessage = JSON.toJSONString(requestParams);
 | 
	
		
			
				|  |  |         System.out.println("请求参数报文:" + requestMessage);
 | 
	
		
			
				|  |  |         return requestMessage;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     /**
 | 
	
		
			
				|  |  |      * 此方式仅应用到注册回调
 | 
	
		
			
				|  |  |      */
 | 
	
		
			
				|  |  |     public YLZUser getUserByToken(String token) {
 | 
	
		
			
				|  |  |     public UserInfoResponse getUserByToken(String token) {
 | 
	
		
			
				|  |  |         EhcEmpiClient empiClient = new EhcEmpiClient(gatewayURL, appId, appSecret, termId, version, signType, encryptType);
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             String result = null;
 | 
	
		
			
				|  |  |             OnepayDefaultClient onepayClient = new OnepayDefaultClient(
 | 
	
		
			
				|  |  |                     empiUimcQuery,
 | 
	
		
			
				|  |  |                     appId,
 | 
	
		
			
				|  |  |                     appSecret, signType, encryptType);
 | 
	
		
			
				|  |  |             RequestParams requestParams = new RequestParams();
 | 
	
		
			
				|  |  |             requestParams.setAppId(appId);
 | 
	
		
			
				|  |  |             requestParams.setTimestamp(DateUtil.getCurrentDateTime());
 | 
	
		
			
				|  |  |             requestParams.setSignType(signType);
 | 
	
		
			
				|  |  |             requestParams.setEncryptType(encryptType);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             //业务参数
 | 
	
		
			
				|  |  |             JSONObject params = new JSONObject();
 | 
	
		
			
				|  |  |             params.put("token", token);  //注册回调的token
 | 
	
		
			
				|  |  |             requestParams.setParam(params);
 | 
	
		
			
				|  |  |             logger.info("requestParams:" + requestParams.toString());
 | 
	
		
			
				|  |  |             ResponseParams<JSONObject> res = onepayClient.execute(requestParams);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             if (OnepayDefaultClient.isSuccessful(res)) {
 | 
	
		
			
				|  |  |                 result = JSON.toJSONString(res.getParam());
 | 
	
		
			
				|  |  |                 logger.info("易联众返回结果:" + result);
 | 
	
		
			
				|  |  | //             新增保存数据
 | 
	
		
			
				|  |  |                 JSONObject userJson = JSON.parseObject(result).getJSONObject("biz_content");
 | 
	
		
			
				|  |  |                 YLZUser user = getYLZUser(userJson);
 | 
	
		
			
				|  |  |                 YLZUserCard userCard = getYLZUserCard(userJson.getJSONArray("card_info"));
 | 
	
		
			
				|  |  |                 user.setYlzUserCard(userCard);
 | 
	
		
			
				|  |  |                 return user;
 | 
	
		
			
				|  |  |             logger.info(net.sf.json.JSONObject.fromObject(empiClient).toString());
 | 
	
		
			
				|  |  |             UserInfoRequest params = new UserInfoRequest();
 | 
	
		
			
				|  |  |             params.setAccessToken(token);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             logger.info(net.sf.json.JSONObject.fromObject(params).toString());
 | 
	
		
			
				|  |  |             ResponseParams<UserInfoResponse> response = empiClient.queryUserInfo(params);
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             if (empiClient.isSuccessful(response)) {
 | 
	
		
			
				|  |  |                 UserInfoResponse userInfoResponse = response.getBizContent();
 | 
	
		
			
				|  |  |                 logger.info(net.sf.json.JSONObject.fromObject(userInfoResponse).toString());
 | 
	
		
			
				|  |  |                 return userInfoResponse;
 | 
	
		
			
				|  |  |             } else {
 | 
	
		
			
				|  |  |                 logger.error("调用易联众接口失败:" + res.toString());
 | 
	
		
			
				|  |  |                 logger.error(new StringBuffer("调用易联众接口失败: " + empiUimcAuthorization + "," + net.sf.json.JSONObject.fromObject(response)).toString());
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             return null;
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
	
		
			
				|  | @ -143,26 +118,75 @@ public class YLZUserService {
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private YLZUserCard getYLZUserCard(JSONArray card_infos) {
 | 
	
		
			
				|  |  |         YLZUserCard ylzUserCard = null;
 | 
	
		
			
				|  |  |         for (int i = 0; i < card_infos.size(); i++) {
 | 
	
		
			
				|  |  |             JSONObject jo = card_infos.getJSONObject(i);
 | 
	
		
			
				|  |  |             if ("normal ".equals(jo.getString("state"))) {
 | 
	
		
			
				|  |  |                 ylzUserCard = JSON.parseObject(jo.toJSONString(), YLZUserCard.class);
 | 
	
		
			
				|  |  |                 break;
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public String securityURL(String openId) {
 | 
	
		
			
				|  |  |         String url = "";
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             EhcEmpiClient empiClient = new EhcEmpiClient(gatewayURL, appId, appSecret, termId, version, signType, encryptType);
 | 
	
		
			
				|  |  |             try {
 | 
	
		
			
				|  |  |                 logger.info(net.sf.json.JSONObject.fromObject(empiClient).toString());
 | 
	
		
			
				|  |  |                 AuthorizationTokenRequest params = new AuthorizationTokenRequest();
 | 
	
		
			
				|  |  |                 params.setRedirectUrl(server_url + redirectUrl);
 | 
	
		
			
				|  |  |                 params.setAuthType("IXM");
 | 
	
		
			
				|  |  |                 params.setDomainCustId(openId);
 | 
	
		
			
				|  |  |                 params.setResponseType(responseType);
 | 
	
		
			
				|  |  |                 url = empiClient.createUrl("gatewayPage", "empi.uimc.authorization", params);
 | 
	
		
			
				|  |  |                 logger.info(url);
 | 
	
		
			
				|  |  |             } catch (Exception e) {
 | 
	
		
			
				|  |  |                 e.printStackTrace();
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return ylzUserCard;
 | 
	
		
			
				|  |  |         return url;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     private YLZUser getYLZUser(JSONObject userJson) {
 | 
	
		
			
				|  |  |         YLZUser ylzUserCard = JSON.parseObject(userJson.toJSONString(), YLZUser.class);
 | 
	
		
			
				|  |  |         return ylzUserCard;
 | 
	
		
			
				|  |  |     @Transactional
 | 
	
		
			
				|  |  |     public  Map<String, String> synchronousBSInfo(String params) {
 | 
	
		
			
				|  |  |         logger.info(params);
 | 
	
		
			
				|  |  |         EhcEmpiClient empiClient = new EhcEmpiClient(gatewayURL, appId, appSecret, termId, version, signType, encryptType);
 | 
	
		
			
				|  |  |         try {
 | 
	
		
			
				|  |  |             //获取get params 入参 转换 并解密验签
 | 
	
		
			
				|  |  |             ResponseParams<AuthorizationTokenResponse> response = empiClient.checkURLResponseParams(params, AuthorizationTokenResponse.class);
 | 
	
		
			
				|  |  |             UserInfoResponse userInfoResponse = getUserByToken(response.getBizContent().getAccessToken());
 | 
	
		
			
				|  |  |             List<UserInfoResponse.CardInfo> cards = userInfoResponse.getCardInfo();
 | 
	
		
			
				|  |  |             UserInfoResponse.CardInfo ssc = null;
 | 
	
		
			
				|  |  |             for (UserInfoResponse.CardInfo card : cards) {
 | 
	
		
			
				|  |  |                 //如果患者有正常状态的社保卡
 | 
	
		
			
				|  |  |                 if ("01".equals(card.getCardType()) && "normal".equals(card.getState().toLowerCase())) {
 | 
	
		
			
				|  |  |                     ssc = card;
 | 
	
		
			
				|  |  |                     break;
 | 
	
		
			
				|  |  |                 }
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             //同步社保卡号
 | 
	
		
			
				|  |  |             SocialSecurityInfo socialSecurityInfo = socialSecurityInfoDao.findBySfzh18Max(userInfoResponse.getIdNo());
 | 
	
		
			
				|  |  |             //如果我们表没有记录那就新增一条记录
 | 
	
		
			
				|  |  |             if(socialSecurityInfo==null){
 | 
	
		
			
				|  |  |                 socialSecurityInfo=new SocialSecurityInfo();
 | 
	
		
			
				|  |  |                 socialSecurityInfo.setXming0(userInfoResponse.getName());
 | 
	
		
			
				|  |  |                 socialSecurityInfo.setXbie00(IdCardUtil.getSexForIdcard(userInfoResponse.getIdNo()));
 | 
	
		
			
				|  |  |                 socialSecurityInfo.setCsrqi0(IdCardUtil.getBirthdayForIdcardStr(userInfoResponse.getIdNo()));
 | 
	
		
			
				|  |  |                 socialSecurityInfo.setSfzh18(userInfoResponse.getIdNo());
 | 
	
		
			
				|  |  |                 socialSecurityInfo.setGrsfen("8");
 | 
	
		
			
				|  |  |                 socialSecurityInfo.setId0000(userInfoResponse.getIdNo());
 | 
	
		
			
				|  |  |                 socialSecurityInfo.setGzztai("01");
 | 
	
		
			
				|  |  |                 socialSecurityInfo.setCardno(ssc.getCardNo());
 | 
	
		
			
				|  |  |                 socialSecurityInfo.setCard16(userInfoResponse.getIdNo().substring(0,16));
 | 
	
		
			
				|  |  |                 socialSecurityInfoDao.save(socialSecurityInfo);
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             //如果医保卡不一致更新{
 | 
	
		
			
				|  |  |             if(!socialSecurityInfo.getCardno().equals(ssc.getCardNo())){
 | 
	
		
			
				|  |  |                 socialSecurityInfo.setCardno(ssc.getCardNo());
 | 
	
		
			
				|  |  |             }
 | 
	
		
			
				|  |  |             Map<String, String> checkReturnModel=new HashMap<>();
 | 
	
		
			
				|  |  |             checkReturnModel.put("name",userInfoResponse.getName());
 | 
	
		
			
				|  |  |             checkReturnModel.put("mobile",userInfoResponse.getTelephone());
 | 
	
		
			
				|  |  |             checkReturnModel.put("idCard",userInfoResponse.getIdNo());
 | 
	
		
			
				|  |  |             checkReturnModel.put("ssc",ssc.getCardNo());
 | 
	
		
			
				|  |  |             return checkReturnModel;
 | 
	
		
			
				|  |  |         } catch (Exception e) {
 | 
	
		
			
				|  |  |             e.printStackTrace();
 | 
	
		
			
				|  |  |         }
 | 
	
		
			
				|  |  |         return null;
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | 
 | 
	
		
			
				|  |  |     public static void main(String[] args) {
 | 
	
		
			
				|  |  |         String str = "{\"card_no\":\"111111\"}";
 | 
	
		
			
				|  |  |         YLZUserCard ylzUserCard = JSON.parseObject(str, YLZUserCard.class);
 | 
	
		
			
				|  |  |         System.out.println(ylzUserCard);
 | 
	
		
			
				|  |  |     }
 | 
	
		
			
				|  |  | }
 |