第三方对接-CA对接

package cc.ewell.mem.prescription.api.ca;

import cc.ewell.mem.prescription.comm.ApolloGener;
import cc.ewell.mem.prescription.jdbc.sign.CaSignYw;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import co.faao.plugin.esb.GogApi;
import co.faao.plugin.key.CommonKeyUtil;
import co.faao.plugin.starter.properties.PropertiesValue;
import com.google.gson.JsonObject;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
@GogApi("CA签名API类")
@Slf4j
public class CaVerification {

    //卫生计生组织机构代码,每个医院均有独立编码  测试请使用""
    String businessOrgCode = PropertiesValue.getProperties("businessOrgCode");
//    String businessOrgCode = ApolloGener.businessOrgCode;
    //CA 业务系统编码
    String businessSystemCode = PropertiesValue.getProperties("businessSystemCode");
//    String businessSystemCode = ApolloGener.businessSystemCode;
    //业务系统应用 ID,业务系统的唯一标识号   ,测试请使用""
    String businessSystemAppID = PropertiesValue.getProperties("businessSystemAppID");
//    String businessSystemAppID = ApolloGener.businessSystemAppID;
    //CA 服务地址 http://localhost:55668/
    //http://localhost:55668/CaApi2/Tools/SZWJ_VerifyEncryptedToken
    String caUrl = PropertiesValue.getProperties("caUrl");
//    String caUrl = ApolloGener.caurl;


    /**
     * 开始签名
     *
     * @param response HttpServletResponse response,
     * @param staffCode
     * @param signType     分为1免密,2二维码,3本地用户密码: 免密为以前流程,二维码需要前端请求getSignTxt获取扫码签名状态
     * @param type
     * @param businContent
     */
    @GogApi("CA数据签名")
    @RequestMapping(value = "/caSign/signTxt", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "staffCode", value = "签名医生工号(长工号)", paramType = "query", required = true),
            @ApiImplicitParam(name = "patId", value = "患者id", paramType = "query", required = true),
            @ApiImplicitParam(name = "businessTypeCode", value = "CA 业务类型编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "signType", value = "签名方式 1,免密签章 2,口令登录 3,本地用户名密码签章", paramType = "query", allowableValues = "1,2,3", allowEmptyValue = true),
            @ApiImplicitParam(name = "type", value = "业务类型", paramType = "query", allowableValues = "病历保存,病历删除、处方提交、检验提交,检查提交,处方撤销"),
            @ApiImplicitParam(name = "businContent", value = "内容", paramType = "query"),
            @ApiImplicitParam(name = "encryptedToken", value = "加密令牌", paramType = "query")})
    @ResponseBody
    public String signTxt( String staffCode, String patId, String businessTypeCode,String signType, String type,String businContent,String encryptedToken) {
        if (ApolloGener.caignore) {
            String bizSn = CommonKeyUtil.generString();
            CaSignYw.inserCaSign(bizSn, "{\"" + staffCode + "\":\"" + "签名开关关闭" + "\"}", businContent,null);
            return "{\"state\":\"success\",\"data\":\"" + bizSn + "\"}";
        }else{
            if (StrUtil.equals("3", signType)){//本地登录
                String bizSn = CommonKeyUtil.generString();
                CaSignYw.inserCaSign(bizSn, "{\"" + staffCode + "\":\"" + "本地验证,不走签名服务器" + "\"}", businContent, null);
                return "{\"state\":\"success\",\"data\":\"" + bizSn + "\"}";
            }else if (StrUtil.equals("2",signType)) {//口令登录 账号+验证码登录
                //校验证书是否过期
                Boolean verifyEncryptedToken = VerifyEncryptedToken(encryptedToken);
                if (verifyEncryptedToken){//证书未过期
                    //3.20、 数据签名(关联患者) SZWJ_SignDataWithPatient
                    //http://localhost:55668/CaApi2/SZWJ_SignDataWithPatient
                    String bizSn = CommonKeyUtil.generString();
                    Boolean signDataWithPatient = SignDataWithPatient(bizSn,encryptedToken, patId, businContent,businessTypeCode);
                    if (signDataWithPatient){
                        return "{\"state\":\"success\",\"data\":\"" + bizSn + "\"}";
                    }
                    return "{\"state\":\"fail\",\"data\":\"获取签名失败\"}";
                }
                //过期返回失败
                return "{\"state\":\"fail\",\"data\":\"证书已过期\"}";
            }
            return "{\"state\":\"fail\",\"data\":\"签名失败\"}";
        }
    }


    /**
     * 数据签名(关联患者)
     * @param bizSn caId 存库主键
     * @param encryptedToken 加密令牌 门户前端获取
     * @param patientId  患者id
     * @param data 签名数据 需要进行base64编码
     * @return Boolean
     * bizId 业务系统id 第三方自定义的
     *
     */
    public Boolean SignDataWithPatient(String bizSn,String encryptedToken,String patientId,String data,String businessTypeCode){
        try {
            //对签名原文进行base64编码
            String base64Value = Base64Encode(data);
            //判断编码是否成功
            if(StringUtils.isEmpty(base64Value)){
                return false;
            }
            //深圳市 CA 业务类型编码
            //值 值含义
            //001 登录 002 处方  003 医嘱  004 检验  005 检查  006 病历 007 审批 998 测试  999 其他
//            String businessTypeCode="002";

            CloseableHttpClient httpClient = HttpClients.createDefault();
            //3.20、数据签名
            String url=caUrl+"CaApi2/SZWJ_SignDataWithPatient";
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("businessSystemAppID", businessSystemAppID);
            jsonObject.addProperty("businessSystemCode", businessSystemCode);
            jsonObject.addProperty("businessOrgCode", businessOrgCode);
            jsonObject.addProperty("businessTypeCode", businessTypeCode);
            jsonObject.addProperty("encryptedToken", encryptedToken);
            jsonObject.addProperty("patientId", patientId);//患者id
            jsonObject.addProperty("bizId", "");//暂时没有值
            jsonObject.addProperty("data", base64Value);//需要进行base64编码 请求315接口进行编码
            jsonObject.addProperty("withTsa", true);//是否进行时间戳签名
            HttpPost httpPost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom().
                    setConnectTimeout(180 * 1000).setConnectionRequestTimeout(180 * 1000)
                    .setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build();
            httpPost.setConfig(requestConfig);
            httpPost.setHeader("Content-Type", "application/json");
            StringEntity requestEntity = new StringEntity(jsonObject.toString(), "utf-8");
            log.info(jsonObject.toString());
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setEntity(requestEntity);
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(response.getEntity());
                log.info("数据签名(关联患者)结果,result:" + result);
                String statusCode = JSONUtil.parse(result).getByPath("statusCode").toString();
                if (StringUtils.isNotEmpty(statusCode)&&"0".equals(statusCode)) {
                    String signedData = JSONUtil.parse(result).getByPath("eventValue.signedData").toString();
                    String timestamp = JSONUtil.parse(result).getByPath("eventValue.timestamp").toString();
                    CaSignYw.inserCaSign(bizSn,signedData, data,timestamp);
                    return true;
                }
            }
            return false;
        }catch (Exception e) {
            log.error("调用数据签名(关联患者)接口异常:", e);
            return false;
        }
    }

    /**
     *  3.15、 Base64 编码
     *  http://localhost:55668/CaApi2/Tools/SZWJ_Base64Encode
     * @param data 签名原文
     * @return base64编码后的签名原文
     */
    public String Base64Encode(String data){
        try{
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //3.15、Base64 编码
            //http://localhost:55668/CaApi2/Tools/SZWJ_Base64Encode
            String url=caUrl+"CaApi2/Tools/SZWJ_Base64Encode";
            HttpPost httpPost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom().
                    setConnectTimeout(180 * 1000).setConnectionRequestTimeout(180 * 1000)
                    .setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build();
            httpPost.setConfig(requestConfig);
            httpPost.setHeader("Content-Type", "text/html");
            StringEntity requestEntity = new StringEntity(data, "utf-8");
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setEntity(requestEntity);
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(response.getEntity());
                log.info("Base64 编码结果,result:" + result);
                String statusCode = JSONUtil.parse(result).getByPath("statusCode").toString();
                if (StringUtils.isNotEmpty(statusCode)&&"0".equals(statusCode)) {
                    //返回base64Value
                    return JSONUtil.parse(result).getByPath("eventValue.base64Value").toString();
                }
            }
            return null;
        }catch(Exception e){
            log.error("调用Base64 编码接口异常:", e);
            return null;
        }
    }




    /**
     * 动态令牌校验 判断encryptedToken是否过期
     * @param encryptedToken 加密令牌 门户获取
     * @return Boolean
     */
    public Boolean VerifyEncryptedToken(String encryptedToken){
        try {
            CloseableHttpClient httpClient = HttpClients.createDefault();
            //3.13、动态令牌验证
            String url=caUrl+"CaApi2/Tools/SZWJ_VerifyEncryptedToken";
            JsonObject jsonObject = new JsonObject();
            jsonObject.addProperty("businessSystemAppID", businessSystemAppID);
            jsonObject.addProperty("businessSystemCode", businessSystemCode);
            jsonObject.addProperty("businessOrgCode", businessOrgCode);
            jsonObject.addProperty("encryptedToken", encryptedToken);
            HttpPost httpPost = new HttpPost(url);
            RequestConfig requestConfig = RequestConfig.custom().
                    setConnectTimeout(180 * 1000).setConnectionRequestTimeout(180 * 1000)
                    .setSocketTimeout(180 * 1000).setRedirectsEnabled(true).build();
            httpPost.setConfig(requestConfig);
            httpPost.setHeader("Content-Type", "application/json");
            StringEntity requestEntity = new StringEntity(jsonObject.toString(), "utf-8");
            log.info(jsonObject.toString());
            requestEntity.setContentEncoding("UTF-8");
            httpPost.setEntity(requestEntity);
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null && response.getStatusLine().getStatusCode() == 200) {
                String result = EntityUtils.toString(response.getEntity());
                log.info("动态令牌校验结果,result:" + result);
                String statusCode = JSONUtil.parse(result).getByPath("statusCode").toString();
                if (StringUtils.isNotEmpty(statusCode)&&"0".equals(statusCode)) {//0表示成功 1表示失败
                    return true;
                }
            }
            return false;
        } catch (Exception e){
            log.error("调用动态令牌校验接口异常:", e);
            return false;
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值