Procházet zdrojové kódy

纳里对接可能用到的类

gaodm před 5 roky
rodič
revize
2bb50ba29b

+ 11 - 1
config-server/src/main/resources/shared/xlcs-service-dev.yml

@@ -101,4 +101,14 @@ io.github.lvyahui8.spring:
 
 weixin:
   appid: wxedd53be102996426
-  secret: c164e280950342f166a933dd7aa6daf7
+  secret: c164e280950342f166a933dd7aa6daf7
+
+#nali:
+#  apiUrl: http://116.62.170.131:8096/openapi-devtest/gateway
+#  appKey: ngari5e34e52511dc394f
+#  appSecret: 11dc394fc05518c1
+
+nali:
+  apiUrl: https://ssltest.ngarihealth.com/openapi-prerelease/gateway
+  appKey: ngari5e48d9ec11dc3978
+  appSecret: 11dc397869c2d24a

+ 11 - 1
config-server/src/main/resources/shared/xlcs-service-local.yml

@@ -101,4 +101,14 @@ io.github.lvyahui8.spring:
 
 weixin:
   appid: wxedd53be102996426
-  secret: c164e280950342f166a933dd7aa6daf7
+  secret: c164e280950342f166a933dd7aa6daf7
+
+#nali:
+#  apiUrl: http://116.62.170.131:8096/openapi-devtest/gateway
+#  appKey: ngari5e34e52511dc394f
+#  appSecret: 11dc394fc05518c1
+
+nali:
+  apiUrl: https://ssltest.ngarihealth.com/openapi-prerelease/gateway
+  appKey: ngari5e48d9ec11dc3978
+  appSecret: 11dc397869c2d24a

+ 6 - 1
config-server/src/main/resources/shared/xlcs-service-pre.yml

@@ -101,4 +101,9 @@ io.github.lvyahui8.spring:
 
 weixin:
   appid: wxe4e2b88ec9c578bd
-  secret: 16ae52c464d2a201c7ede0820dadb4a9
+  secret: 16ae52c464d2a201c7ede0820dadb4a9
+
+nali:
+  apiUrl: https://openapi.ngarihealth.com/openapi/gateway
+  appKey: ngari5e3517480e980e6e
+  appSecret: 0e980e6e502ab074

+ 6 - 1
config-server/src/main/resources/shared/xlcs-service-pro.yml

@@ -101,4 +101,9 @@ io.github.lvyahui8.spring:
 
 weixin:
   appid: wxe4e2b88ec9c578bd
-  secret: 16ae52c464d2a201c7ede0820dadb4a9
+  secret: 16ae52c464d2a201c7ede0820dadb4a9
+
+nali:
+  apiUrl: https://openapi.ngarihealth.com/openapi/gateway
+  appKey: ngari5e3517480e980e6e
+  appSecret: 0e980e6e502ab074

+ 11 - 1
config-server/src/main/resources/shared/xlcs-service-test.yml

@@ -101,4 +101,14 @@ io.github.lvyahui8.spring:
 
 weixin:
   appid: wxe4e2b88ec9c578bd
-  secret: 16ae52c464d2a201c7ede0820dadb4a9
+  secret: 16ae52c464d2a201c7ede0820dadb4a9
+
+#nali:
+#  apiUrl: http://116.62.170.131:8096/openapi-devtest/gateway
+#  appKey: ngari5e34e52511dc394f
+#  appSecret: 11dc394fc05518c1
+
+nali:
+  apiUrl: https://ssltest.ngarihealth.com/openapi-prerelease/gateway
+  appKey: ngari5e48d9ec11dc3978
+  appSecret: 11dc397869c2d24a

+ 67 - 0
xlcs-service/src/main/java/com/diagbot/entity/Client.java

@@ -0,0 +1,67 @@
+
+package com.diagbot.entity;
+
+import com.diagbot.util.AESUtils;
+import com.diagbot.util.JSONUtils;
+import com.diagbot.util.MessageDigestUtil;
+import com.diagbot.util.OpenApiUtils;
+import com.diagbot.util.SignUtil;
+import org.apache.commons.lang3.StringUtils;
+
+import java.util.UUID;
+
+/**
+ * Client
+ */
+public class Client {
+
+    private String apiUrl;
+    private String appKey;
+    private String appSecret;
+    private String encodingAesKey;
+
+    public Client(String apiUrl, String appKey, String appSecret, String encodingAesKey) {
+        this.apiUrl = apiUrl;
+        this.appKey = appKey;
+        this.appSecret = appSecret;
+        this.encodingAesKey = encodingAesKey;
+    }
+
+    /**
+     * 发送请求
+     *
+     * @param request request对象
+     * @return Response
+     * @throws Exception
+     */
+    public Response execute(Request request) throws Exception {
+        request.setApiUrl(this.apiUrl);
+        request.setAppKey(this.appKey);
+        request.setAppSecret(this.appSecret);
+        request.setEncodingAesKey(this.encodingAesKey);
+        request.addHeader(SystemHeader.X_CA_KEY, request.getAppKey());
+        request.addHeader(SystemHeader.X_CA_NONCE, UUID.randomUUID().toString());
+        request.addHeader(SystemHeader.X_CA_TIMESTAMP, String.valueOf(System.currentTimeMillis()));
+        String jsonStr = JSONUtils.toString(request.getBodys());
+        String encryptStr;
+        if (StringUtils.isNotEmpty(request.getEncodingAesKey())) {
+            encryptStr = AESUtils.encrypt(jsonStr, request.getEncodingAesKey());
+        } else {
+            encryptStr = jsonStr;
+        }
+        String contentMd5 = MessageDigestUtil.base64AndMD5(encryptStr);
+        request.addHeader(SystemHeader.X_CONTENT_MD5, contentMd5);
+        String signature = SignUtil.sign(request.getAppSecret(), request.getHeaders());
+        request.addHeader(SystemHeader.X_CA_SIGNATURE, signature);
+        request.setStringBody(encryptStr);
+        return OpenApiUtils.post(request);
+    }
+
+    public static void main(String[] args) throws Exception {
+        String body = "[1]";
+        String encryptStr = AESUtils.encrypt(body, "1234567890123456");
+        String contentMd5 = MessageDigestUtil.base64AndMD5(encryptStr.getBytes());
+        System.out.println(contentMd5);
+
+    }
+}

+ 47 - 0
xlcs-service/src/main/java/com/diagbot/entity/Constants.java

@@ -0,0 +1,47 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.diagbot.entity;
+
+/**
+ * 通用常量
+ */
+public class Constants {
+    //签名算法HmacSha256
+    public static final String HMAC_SHA256 = "HmacSHA256";
+    //编码UTF-8
+    public static final String ENCODING = "UTF-8";
+    //UserAgent
+    public static final String USER_AGENT = "demo/aliyun/java";
+    //换行符
+    public static final String LF = "\n";
+    //串联符
+    public static final String SPE1 = ",";
+    //示意符
+    public static final String SPE2 = ":";
+    //连接符
+    public static final String SPE3 = "&";
+    //赋值符
+    public static final String SPE4 = "=";
+    //问号符
+    public static final String SPE5 = "?";
+    //默认请求超时时间,单位毫秒
+    public static final int DEFAULT_TIMEOUT = 1000;
+    //参与签名的系统Header前缀,只有指定前缀的Header才会参与到签名中
+    public static final String CA_HEADER_TO_SIGN_PREFIX_SYSTEM = "X-Ca-";
+}

+ 53 - 0
xlcs-service/src/main/java/com/diagbot/entity/JSONResponseBean.java

@@ -0,0 +1,53 @@
+package com.diagbot.entity;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class JSONResponseBean {
+	private int code = 200;
+	private String msg;
+	private Object body;
+	private Map<String, Object> properties;
+	
+	public int getCode() {
+		return code;
+	}
+	
+	public void setCode(int code) {
+		this.code = code;
+	}
+	
+	public String getMsg() {
+		return msg;
+	}
+	
+	public void setMsg(String msg) {
+		this.msg = msg;
+	}
+	
+	public Object getBody() {
+		return body;
+	}
+	
+	public void setBody(Object body) {
+		this.body = body;
+	}
+	
+	public Map<String, Object> getProperties() {
+		return properties;
+	}
+	
+	public Object getProperty(String nm){
+		if(properties == null || properties.isEmpty()){
+			return null;
+		}
+		return properties.get(nm);
+	}
+	
+	public void setProperty(String name, Object val){
+		if(properties == null){
+			properties = new HashMap<>();
+		}
+		properties.put(name, val);
+	}
+}

+ 142 - 0
xlcs-service/src/main/java/com/diagbot/entity/Request.java

@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.diagbot.entity;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Request
+ */
+public class Request {
+
+    public Request() {
+    }
+
+    public Request(String serviceId, String method, List<Object> bodys) {
+        this.appKey = appKey;
+        this.appSecret = appSecret;
+        this.bodys=bodys;
+        headers = new HashMap<>();
+        headers.put(SystemHeader.X_CA_KEY, appKey);
+        headers.put(SystemHeader.X_SERVICE_ID, serviceId);
+        headers.put(SystemHeader.X_SERVICE_METHOD, method);
+    }
+    /**
+     * (必选)服务请求地址
+     */
+    private String apiUrl;
+
+
+
+    /**
+     * (必选)APP KEY
+     */
+    private String appKey;
+
+    /**
+     * (必选)APP密钥
+     */
+    private String appSecret;
+
+    private String encodingAesKey;
+
+
+    /**
+     * (必选) HTTP头
+     */
+    private Map<String, String> headers;
+    
+
+
+    /**
+     * (必选)请求参数
+     */
+    private List<Object> bodys;
+
+    /**
+     * (可选)字符串Body体
+     */
+    private String stringBody;
+
+
+    public String getAppKey() {
+        return appKey;
+    }
+
+    public void setAppKey(String appKey) {
+        this.appKey = appKey;
+    }
+
+    public String getAppSecret() {
+        return appSecret;
+    }
+
+    public void setAppSecret(String appSecret) {
+        this.appSecret = appSecret;
+    }
+
+
+    public Map<String, String> getHeaders() {
+        return headers;
+    }
+
+    public void setHeaders(Map<String, String> headers) {
+        this.headers = headers;
+    }
+
+    public List<Object> getBodys() {
+        return bodys;
+    }
+
+    public void setBodys(List<Object> bodys) {
+        this.bodys = bodys;
+    }
+
+    public String getStringBody() {
+        return stringBody;
+    }
+
+    public void setStringBody(String stringBody) {
+        this.stringBody = stringBody;
+    }
+
+    public String getEncodingAesKey() {
+        return encodingAesKey;
+    }
+
+    public void setEncodingAesKey(String encodingAesKey) {
+        this.encodingAesKey = encodingAesKey;
+    }
+
+    public String getApiUrl() {
+        return apiUrl;
+    }
+
+    public void setApiUrl(String apiUrl) {
+        this.apiUrl = apiUrl;
+    }
+    public void addHeader(String name, String value){
+        if(headers==null){
+            headers = new HashMap<>();
+        }
+        headers.put(name, value);
+    }
+}

+ 120 - 0
xlcs-service/src/main/java/com/diagbot/entity/Response.java

@@ -0,0 +1,120 @@
+package com.diagbot.entity;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Response {
+    /**
+     * http 请求状态码
+     */
+    private int statusCode;
+    private String contentType;
+    /**
+     * 请求唯一 ID,请求一旦进入 API 网关应用后,API 网关就会生成请求 ID 并通过响应头返回给客户端,
+     * 建议客户端与后端服务都记录此请求 ID,可用于问题排查与跟踪。
+     */
+    private String requestId;
+    /**
+     * API网关会将服务端的StringToSign放到HTTP应答的Header中返回到客户端,Key为:X-Ca-Error-Message
+     * 只需要将本地计算的StringToSign与服务端返回的StringToSign进行对比即可找到问题;
+     * 如果服务端与客户端的签名串是一致的,请检查用于签名计算的密钥是否正确;
+     */
+    private String caErrorMsg;
+    /**
+     * 业务接口返回的错误信息
+     */
+    private String errorMessage;
+    private Map<String, String> headers;
+    private String body;
+    private JSONResponseBean jsonResponseBean;
+
+    public boolean isSuccess() {
+        return (200 == statusCode && jsonResponseBean != null && 200 == jsonResponseBean.getCode());
+    }
+
+    public Response() {
+
+    }
+
+    public int getStatusCode() {
+        return statusCode;
+    }
+
+    public void setStatusCode(int statusCode) {
+        this.statusCode = statusCode;
+    }
+
+    public String getContentType() {
+        return contentType;
+    }
+
+    public void setContentType(String contentType) {
+        this.contentType = contentType;
+    }
+
+    public String getRequestId() {
+        return requestId;
+    }
+
+    public void setRequestId(String requestId) {
+        this.requestId = requestId;
+    }
+
+    public String getErrorMessage() {
+        if (jsonResponseBean != null && !"200".equals(jsonResponseBean.getCode())) {
+            return jsonResponseBean.getMsg();
+        }
+        return null;
+    }
+
+    public void setErrorMessage(String errorMessage) {
+        this.errorMessage = errorMessage;
+    }
+
+    public Map<String, String> getHeaders() {
+        return headers;
+    }
+
+    public String getHeader(String key) {
+        if (null != headers) {
+            return headers.get(key);
+        } else {
+            return null;
+        }
+    }
+
+    public void setHeaders(Map<String, String> headers) {
+        this.headers = headers;
+    }
+
+    public void setHeader(String key, String value) {
+        if (null == this.headers) {
+            this.headers = new HashMap<String, String>();
+        }
+        this.headers.put(key, value);
+    }
+
+    public String getBody() {
+        return body;
+    }
+
+    public void setBody(String body) {
+        this.body = body;
+    }
+
+    public JSONResponseBean getJsonResponseBean() {
+        return jsonResponseBean;
+    }
+
+    public void setJsonResponseBean(JSONResponseBean jsonResponseBean) {
+        this.jsonResponseBean = jsonResponseBean;
+    }
+
+    public String getCaErrorMsg() {
+        return caErrorMsg;
+    }
+
+    public void setCaErrorMsg(String caErrorMsg) {
+        this.caErrorMsg = caErrorMsg;
+    }
+}

+ 29 - 0
xlcs-service/src/main/java/com/diagbot/entity/SystemHeader.java

@@ -0,0 +1,29 @@
+
+package com.diagbot.entity;
+
+/**
+ * 系统HTTP头常量
+ */
+public class SystemHeader {
+    //签名Header
+    public static final String X_CA_SIGNATURE = "X-Ca-Signature";
+    //所有参与签名的Header
+    public static final String X_CA_SIGNATURE_HEADERS = "X-Ca-Signature-Headers";
+    //请求时间戳
+    public static final String X_CA_TIMESTAMP = "X-Ca-Timestamp";
+    //请求放重放Nonce,15分钟内保持唯一,建议使用UUID
+    public static final String X_CA_NONCE = "X-Ca-Nonce";
+    //APP KEY
+    public static final String X_CA_KEY = "X-Ca-Key";
+    //服务id
+    public static final String X_SERVICE_ID = "X-Service-Id";
+    //具体method
+    public static final String X_SERVICE_METHOD = "X-Service-Method";
+    //MD5加密的请求体信息
+    public static final String X_CONTENT_MD5 = "X-Content-MD5";
+
+    //MD5加密的请求体信息
+    public static final String X_CA_REQUESTID = "X-Ca-RequestId";
+    //API网关会将服务端的StringToSign放到HTTP应答的Header中返回到客户端,Key为:X-Ca-Error-Message
+    public static final String X_CA_ERROR_MESSAGE = "X-Ca-Error-Message";
+}

+ 79 - 0
xlcs-service/src/main/java/com/diagbot/facade/InquiryInfoFacade.java

@@ -4,14 +4,19 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
 import com.diagbot.dto.InquiryDTO;
 import com.diagbot.dto.InquiryPatDTO;
+import com.diagbot.entity.Client;
 import com.diagbot.entity.InquiryDetail;
 import com.diagbot.entity.InquiryInfo;
+import com.diagbot.entity.JSONResponseBean;
+import com.diagbot.entity.Request;
+import com.diagbot.entity.Response;
 import com.diagbot.enums.IdTypeEnum;
 import com.diagbot.enums.IsDeleteEnum;
 import com.diagbot.service.impl.InquiryDetailServiceImpl;
 import com.diagbot.service.impl.InquiryInfoServiceImpl;
 import com.diagbot.util.BeanUtil;
 import com.diagbot.util.DateUtil;
+import com.diagbot.util.FastJsonUtils;
 import com.diagbot.util.HttpUtils;
 import com.diagbot.util.IdCard;
 import com.diagbot.util.IntegerUtil;
@@ -21,12 +26,18 @@ import com.diagbot.vo.InquiryDetailVO;
 import com.diagbot.vo.InquiryPatInquiryVO;
 import com.diagbot.vo.InquiryPatVO;
 import com.diagbot.vo.SaveInquiryVO;
+import com.google.common.collect.Lists;
+import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
+import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Component;
 
+import java.util.ArrayList;
 import java.util.Date;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 /**
  * @Description:
@@ -34,6 +45,7 @@ import java.util.List;
  * @time: 2020/2/1 23:33
  */
 @Component
+@Slf4j
 public class InquiryInfoFacade extends InquiryInfoServiceImpl {
 
 
@@ -46,6 +58,13 @@ public class InquiryInfoFacade extends InquiryInfoServiceImpl {
     @Qualifier("inquiryDetailServiceImpl")
     private InquiryDetailServiceImpl inquiryDetailService;
 
+    @Value("${nali.apiUrl}")
+    private String apiUrl;
+    @Value("${nali.appKey}")
+    private String appKey;
+    @Value("${nali.appSecret}")
+    private String appSecret;
+
     public void saveInquiry(SaveInquiryVO saveInquiryVO) {
         if (StringUtil.isBlank(saveInquiryVO.getHospitalCode())) {
             saveInquiryVO.setHospitalCode("朗通通用");
@@ -139,4 +158,64 @@ public class InquiryInfoFacade extends InquiryInfoServiceImpl {
     }
 
 
+//    private List<String> getUrl(SaveInquiryVO saveInquiryVO) {
+//        List<String> ret = Lists.newArrayList();
+//        String retUrl = null;
+//        Request request = null;
+//        Response response = null;
+//        try {
+//            String encodingAesKey = "";
+//            Client client = new Client(apiUrl, appKey, appSecret, encodingAesKey);
+//            request = getltcs(saveInquiryVO);
+//            response = client.execute(request);
+//            log.info("request:" + FastJsonUtils.getBeanToJson(request));
+//            log.info("requestBodys:" + FastJsonUtils.getBeanToJson(request.getBodys()));
+//            log.info("response:" + FastJsonUtils.getBeanToJson(response));
+//            if (response.isSuccess()) {
+//                JSONResponseBean result = response.getJsonResponseBean();
+//                retUrl = result.getBody().toString();
+//            }
+//        } catch (Exception e) {
+//        }
+//        ret.add(retUrl);
+//        ret.add(FastJsonUtils.getBeanToJson(response));
+//        ret.add(FastJsonUtils.getBeanToJson(request));
+//        ret.add(FastJsonUtils.getBeanToJson(request.getBodys()));
+//        return ret;
+//    }
+//
+//    private Request getltcs(SaveInquiryVO saveInquiryVO) {
+//        List bodyList = new ArrayList<>();
+//
+//        List<Map<String, Object>> details = new ArrayList<>();
+//        saveInquiryVO.getDetailList().forEach(i -> {
+//            Map<String, Object> detail = new HashMap<>();
+//            detail.put("question", i.getQuestion());
+//            detail.put("answer", i.getAnswer());
+//            List<Map<String, Object>> optionMapList = Lists.newArrayList();
+//            i.getOptions().forEach(option -> {
+//                optionMapList.add(FastJsonUtils.getJsonToMap(option));
+//            });
+//            detail.put("options", optionMapList);
+//            details.add(detail);
+//        });
+//
+//        Map<String, Object> body = new HashMap<>();
+//        body.put("details", details);
+//        body.put("doctorId", Integer.parseInt(saveInquiryVO.getDoctorId()));
+//        body.put("mpiId", saveInquiryVO.getMpiId());
+//        body.put("appId", saveInquiryVO.getAppId());
+//        body.put("fromId", saveInquiryVO.getFrom());
+//        body.put("sources", "langtong");
+//        body.put("supplement", saveInquiryVO.getSupplement());
+//        body.put("pushList", saveInquiryVO.getPushList());
+//        body.put("inquiryId", saveInquiryVO.getInquiryId());
+//        bodyList.add(body);
+//
+//        //X-Service-Id对应的值
+//        String serviceId = "consult.questionRecordService";
+//        //X-Service-Method对应的值
+//        String method = "saveQuestionRecord";
+//        return new Request(serviceId, method, bodyList);
+//    }
 }

+ 95 - 0
xlcs-service/src/main/java/com/diagbot/util/AESUtils.java

@@ -0,0 +1,95 @@
+package com.diagbot.util;
+
+import com.diagbot.entity.Constants;
+import org.apache.commons.codec.binary.Base64;
+
+import javax.crypto.Cipher;
+import javax.crypto.KeyGenerator;
+import javax.crypto.SecretKey;
+import javax.crypto.spec.SecretKeySpec;
+import java.security.Key;
+
+public class AESUtils {
+
+    private static final String KEY_ALGORITHM = "AES";
+    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
+
+    public static byte[] initSecretKey() throws Exception {
+        KeyGenerator kg = KeyGenerator.getInstance(KEY_ALGORITHM);
+        kg.init(128);
+        SecretKey secretKey = kg.generateKey();
+
+        return secretKey.getEncoded();
+    }
+
+    public static Key toKey(byte[] key) {
+        return new SecretKeySpec(key, KEY_ALGORITHM);
+    }
+
+    public static String encrypt(String data, String key) throws Exception {
+        return Base64.encodeBase64String(encrypt(data.getBytes(Constants.ENCODING), toKey(key.getBytes()), DEFAULT_CIPHER_ALGORITHM));
+    }
+
+    public static String decrypt(String data, String key) throws Exception {
+        return new String(decrypt(Base64.decodeBase64(data.getBytes(Constants.ENCODING)), toKey(key.getBytes()), DEFAULT_CIPHER_ALGORITHM));
+    }
+
+    public static byte[] encrypt(byte[] data, Key key) throws Exception {
+        return encrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
+    }
+
+    public static byte[] encrypt(byte[] data, byte[] key) throws Exception {
+        return encrypt(data, toKey(key), DEFAULT_CIPHER_ALGORITHM);
+    }
+
+    public static byte[] encrypt(byte[] data, Key key, String cipherAlgorithm) throws Exception {
+        Cipher cipher = Cipher.getInstance(cipherAlgorithm);
+        cipher.init(Cipher.ENCRYPT_MODE, key);
+        return cipher.doFinal(data);
+    }
+
+    public static byte[] decrypt(byte[] data, byte[] key) throws Exception {
+        return decrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
+    }
+
+    public static byte[] decrypt(byte[] data, Key key) throws Exception {
+        return decrypt(data, key, DEFAULT_CIPHER_ALGORITHM);
+    }
+
+    public static byte[] decrypt(byte[] data, byte[] key, String cipherAlgorithm) throws Exception {
+        Key k = toKey(key);
+        return decrypt(data, k, cipherAlgorithm);
+    }
+
+    public static byte[] decrypt(byte[] data, Key key, String cipherAlgorithm) throws Exception {
+        Cipher cipher = Cipher.getInstance(cipherAlgorithm);
+        cipher.init(Cipher.DECRYPT_MODE, key);
+        return cipher.doFinal(data);
+    }
+
+    private static String showByteArray(byte[] data) {
+        if (null == data) {
+            return null;
+        }
+        StringBuilder sb = new StringBuilder("{");
+        for (byte b : data) {
+            sb.append(b).append(",");
+        }
+        sb.deleteCharAt(sb.length() - 1);
+        sb.append("}");
+        return sb.toString();
+    }
+
+
+    public static void main(String[] args) throws Exception {
+        String original = "[1]";
+        String key = "1234567890123456";
+        System.out.println("original: " + original);
+        String encrypt = encrypt(original, key);
+        System.out.println("encrypt: " + encrypt + ", key: " + key);
+        String decrypt = decrypt(encrypt, key);
+        System.out.println("decrypt: " + decrypt);
+
+    }
+
+}

+ 71 - 0
xlcs-service/src/main/java/com/diagbot/util/Demo.java

@@ -0,0 +1,71 @@
+package com.diagbot.util;
+
+import com.diagbot.entity.Client;
+import com.diagbot.entity.JSONResponseBean;
+import com.diagbot.entity.Request;
+import com.diagbot.entity.Response;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class Demo {
+
+    public static void main(String[] args) throws Exception {
+        //必填 apiUrl
+        String apiUrl = "http://116.62.170.131:8096/openapi-devtest/gateway";
+        //必填
+        String appKey = "ngari5e34e52511dc394f";
+        //必填
+        String appSecret = "11dc394fc05518c1";
+        //如果开启加密,则必填
+        String encodingAesKey = "";
+        Client client = new Client(apiUrl, appKey, appSecret, encodingAesKey);
+
+        //调用相应的接口请求
+        Response response = client.execute(getltcs());
+//        System.out.println(JSONUtils.toString(response));
+        if (response.isSuccess()) {
+            //接口返回的结果
+            JSONResponseBean result = response.getJsonResponseBean();
+//            System.out.println(JSONUtils.toString(result));
+            System.out.println(result.getBody().toString());
+
+        } else {
+            System.out.println(response.getCaErrorMsg());
+            System.out.println(response.getErrorMessage());
+        }
+
+    }
+
+    public static Request getltcs() {
+        //入参赋值,注意最外层是个json数组
+        List bodyList = new ArrayList<>();
+
+        List<Map<String, Object>> details = new ArrayList<>();
+        Map<String, Object> detail1 = new HashMap<>();
+        detail1.put("question", "问题1");
+        detail1.put("answer", "答案1");
+        Map<String, Object> detail2 = new HashMap<>();
+        detail2.put("question", "问题2");
+        detail2.put("answer", "答案2");
+        details.add(detail1);
+        details.add(detail2);
+
+        Map<String, Object> body = new HashMap<>();
+        body.put("details", details);
+        body.put("doctorId", 1182);
+        body.put("mpiId", "2c90818958b4f5f10158b50289020001");
+        body.put("appId", "wx6a80dd109228fd4b");
+        body.put("diagnosis", "Summary");
+
+        bodyList.add(body);
+
+        //X-Service-Id对应的值
+        String serviceId = "consult.questionRecordService";
+        //X-Service-Method对应的值
+        String method = "saveQuestionRecord";
+        return new Request(serviceId, method, bodyList);
+    }
+}

+ 139 - 0
xlcs-service/src/main/java/com/diagbot/util/JSONUtils.java

@@ -0,0 +1,139 @@
+package com.diagbot.util;
+
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
+import org.apache.commons.lang3.StringUtils;
+
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Reader;
+import java.io.Writer;
+import java.text.SimpleDateFormat;
+
+public class JSONUtils {
+	private static ObjectMapper mapper =  new ObjectMapper();
+
+	static{
+		mapper.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);
+		mapper.configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true);
+		mapper.configure(JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES, true);
+
+		mapper.configure(SerializationFeature.WRITE_ENUMS_USING_TO_STRING,true);
+		mapper.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false);
+		mapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
+		mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
+		mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
+
+	}
+
+	public static <T> T parse(String value, Class<T> clz){
+
+		if (StringUtils.isEmpty(value)) {
+			return null;
+		}
+		try {
+			return mapper.readValue(value, clz);
+		}
+		catch (Exception e) {
+			throw new IllegalStateException(e);
+		}
+	}
+
+	public static <T> T parse(byte[] bytes, Class<T> clz){
+		try {
+			return mapper.readValue(bytes, clz);
+		}
+		catch (Exception e) {
+			throw new IllegalStateException(e);
+		}
+	}
+
+
+	public static <T> T parse(InputStream ins, Class<T> clz){
+		try {
+			return mapper.readValue(ins, clz);
+		}
+		catch (Exception e) {
+			throw new IllegalStateException(e);
+		}
+	}
+
+
+	public static <T> T  parse(Reader reader, Class<T> clz){
+		try {
+			return mapper.readValue(reader, clz);
+		}
+		catch (Exception e) {
+			throw new IllegalStateException(e);
+		}
+	}
+
+	@SuppressWarnings("unchecked")
+	public static <T> T update(String value, T object) {
+		try {
+			return (T) mapper.readerForUpdating(object).readValue(value);
+		}
+		catch (Exception e) {
+			throw new IllegalStateException(e);
+		}
+	}
+
+	public static String writeValueAsString(Object o){
+		try {
+			return mapper.writeValueAsString(o);
+		}
+		catch (Exception e) {
+			throw new IllegalStateException(e);
+		}
+	}
+
+	public static void write(OutputStream outs, Object o){
+		try {
+			mapper.writeValue(outs,o);
+		}
+		catch (Exception e) {
+			throw new IllegalStateException(e);
+		}
+    }
+
+	public static void write(Writer writer, Object o){
+		try {
+			mapper.writeValue(writer,o);
+		}
+		catch (Exception e) {
+			throw new IllegalStateException(e);
+		}
+	}
+
+	public static String toString(Object o){
+		try {
+			return mapper.writeValueAsString(o);
+		}
+		catch (Exception e) {
+			throw new IllegalStateException(e);
+		}
+	}
+
+	public static String toString(Object o, Class<?> clz){
+		try {
+			return mapper.writerFor(clz).writeValueAsString(o);
+		}
+		catch (Exception e) {
+			throw new IllegalStateException(e);
+		}
+	}
+
+	public static byte[] toBytes(Object o){
+		try {
+			return mapper.writeValueAsBytes(o);
+		}
+		catch (Exception e) {
+			throw new IllegalStateException(e);
+		}
+	}
+
+}

+ 118 - 0
xlcs-service/src/main/java/com/diagbot/util/MessageDigestUtil.java

@@ -0,0 +1,118 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package com.diagbot.util;
+
+import com.diagbot.entity.Constants;
+import org.apache.commons.codec.binary.Base64;
+
+import java.io.UnsupportedEncodingException;
+import java.security.MessageDigest;
+import java.security.NoSuchAlgorithmException;
+
+/**
+ * 消息摘要工具
+ */
+public class MessageDigestUtil {
+    /**
+     * 先进行MD5摘要再进行Base64编码获取摘要字符串
+     *
+     * @param str
+     * @return
+     */
+    public static String base64AndMD5(String str) {
+        if (str == null) {
+            throw new IllegalArgumentException("inStr can not be null");
+        }
+        return base64AndMD5(toBytes(str));
+    }
+
+    /**
+     * 先进行MD5摘要再进行Base64编码获取摘要字符串
+     *
+     * @return
+     */
+    public static String base64AndMD5(byte[] bytes) {
+        if (bytes == null) {
+            throw new IllegalArgumentException("bytes can not be null");
+        }
+        try {
+            final MessageDigest md = MessageDigest.getInstance("MD5");
+            md.reset();
+            md.update(bytes);
+            final Base64 base64 = new Base64();
+            final byte[] enbytes = base64.encode(md.digest());
+            return new String(enbytes);
+        } catch (final NoSuchAlgorithmException e) {
+            throw new IllegalArgumentException("unknown algorithm MD5");
+        }
+    }
+
+    /**
+     * UTF-8编码转换为ISO-9959-1
+     *
+     * @param str
+     * @return
+     */
+    public static String utf8ToIso88591(String str) {
+        if (str == null) {
+            return str;
+        }
+
+        try {
+            return new String(str.getBytes("UTF-8"), "ISO-8859-1");
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e.getMessage(), e);
+        }
+    }
+
+    /**
+     * ISO-9959-1编码转换为UTF-8
+     *
+     * @param str
+     * @return
+     */
+    public static String iso88591ToUtf8(String str) {
+        if (str == null) {
+            return str;
+        }
+
+        try {
+            return new String(str.getBytes("ISO-8859-1"), "UTF-8");
+        } catch (UnsupportedEncodingException e) {
+            throw new RuntimeException(e.getMessage(), e);
+        }
+    }
+
+    /**
+     * String转换为字节数组
+     *
+     * @param str
+     * @return
+     */
+    private static byte[] toBytes(final String str) {
+        if (str == null) {
+            return null;
+        }
+        try {
+            return str.getBytes(Constants.ENCODING);
+        } catch (final UnsupportedEncodingException e) {
+            throw new RuntimeException(e.getMessage(), e);
+        }
+    }
+}

+ 118 - 0
xlcs-service/src/main/java/com/diagbot/util/OpenApiUtils.java

@@ -0,0 +1,118 @@
+package com.diagbot.util;
+
+import com.diagbot.entity.JSONResponseBean;
+import com.diagbot.entity.Request;
+import com.diagbot.entity.Response;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.http.Header;
+import org.apache.http.HttpResponse;
+import org.apache.http.client.HttpClient;
+import org.apache.http.client.methods.HttpPost;
+import org.apache.http.conn.ClientConnectionManager;
+import org.apache.http.conn.scheme.Scheme;
+import org.apache.http.conn.scheme.SchemeRegistry;
+import org.apache.http.conn.ssl.SSLSocketFactory;
+import org.apache.http.entity.ContentType;
+import org.apache.http.entity.StringEntity;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.util.EntityUtils;
+
+import javax.net.ssl.SSLContext;
+import javax.net.ssl.TrustManager;
+import javax.net.ssl.X509TrustManager;
+import java.io.IOException;
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+import java.security.cert.X509Certificate;
+import java.util.Map;
+
+public class OpenApiUtils {
+    public static Response post(Request request) throws Exception {
+        return convert(doPost(request));
+    }
+
+    /**
+     * post form
+     *
+     * @param request
+     * @return
+     * @throws Exception
+     */
+    private static HttpResponse doPost(Request request)
+            throws Exception {
+        HttpClient httpClient = wrapClient(request.getApiUrl());
+
+        HttpPost httpPost = new HttpPost(request.getApiUrl());
+        for (Map.Entry<String, String> e : request.getHeaders().entrySet()) {
+            httpPost.addHeader(e.getKey(), e.getValue());
+        }
+        if (request.getBodys() != null || StringUtils.isNotEmpty(request.getStringBody())) {
+            if (StringUtils.isNotEmpty(request.getStringBody())) {
+                httpPost.setEntity(new StringEntity(request.getStringBody(), ContentType.APPLICATION_JSON));
+            } else {
+                httpPost.setEntity(new StringEntity(JSONUtils.toString(request.getBodys()), ContentType.APPLICATION_JSON));
+            }
+        }
+        return httpClient.execute(httpPost);
+    }
+
+    private static HttpClient wrapClient(String host) {
+        HttpClient httpClient = new DefaultHttpClient();
+        if (host.startsWith("https://")) {
+            sslClient(httpClient);
+        }
+        return httpClient;
+    }
+
+    private static void sslClient(HttpClient httpClient) {
+        try {
+            SSLContext ctx = SSLContext.getInstance("TLS");
+            X509TrustManager tm = new X509TrustManager() {
+                public X509Certificate[] getAcceptedIssuers() {
+                    return null;
+                }
+
+                public void checkClientTrusted(X509Certificate[] xcs, String str) {
+
+                }
+
+                public void checkServerTrusted(X509Certificate[] xcs, String str) {
+
+                }
+            };
+            ctx.init(null, new TrustManager[] { tm }, null);
+            SSLSocketFactory ssf = new SSLSocketFactory(ctx);
+            ssf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
+            ClientConnectionManager ccm = httpClient.getConnectionManager();
+            SchemeRegistry registry = ccm.getSchemeRegistry();
+            registry.register(new Scheme("https", 443, ssf));
+        } catch (KeyManagementException ex) {
+            throw new RuntimeException(ex);
+        } catch (NoSuchAlgorithmException ex) {
+            throw new RuntimeException(ex);
+        }
+    }
+
+    private static Response convert(HttpResponse response) throws IOException {
+        Response res = new Response();
+
+        if (null != response) {
+            res.setStatusCode(response.getStatusLine().getStatusCode());
+            for (Header header : response.getAllHeaders()) {
+                res.setHeader(header.getName(), MessageDigestUtil.iso88591ToUtf8(header.getValue()));
+            }
+            res.setContentType(res.getHeader("Content-Type"));
+            res.setRequestId(res.getHeader("X-Ca-Request-Id"));
+            res.setBody(EntityUtils.toString(response.getEntity()));
+            res.setJsonResponseBean(JSONUtils.parse(res.getBody(), JSONResponseBean.class));
+            res.setCaErrorMsg(res.getHeader("X-Ca-Error-Message"));
+
+        } else {
+            //服务器无回应
+            res.setStatusCode(500);
+            res.setErrorMessage("No Response");
+        }
+
+        return res;
+    }
+}

+ 126 - 0
xlcs-service/src/main/java/com/diagbot/util/SignUtil.java

@@ -0,0 +1,126 @@
+
+package com.diagbot.util;
+
+
+import com.diagbot.entity.Constants;
+import com.diagbot.entity.SystemHeader;
+import org.apache.commons.codec.binary.Base64;
+import org.apache.commons.lang3.StringUtils;
+
+import javax.crypto.Mac;
+import javax.crypto.spec.SecretKeySpec;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.TreeMap;
+import java.util.UUID;
+
+/**
+ * 签名工具
+ */
+public class SignUtil {
+    /**
+     * 需要参与签名的请求头
+     */
+    private static final List<String> SIGN_HEADER_LIST = Arrays.asList("X-Service-Id","X-Service-Method","X-Ca-Key",
+            "X-Ca-Nonce","X-Ca-Timestamp","X-Content-MD5");
+    /**
+     * 计算签名
+     *
+     * @param secret APP密钥
+     * @param headers 请求头参数
+     * @return 签名后的字符串
+     */
+    public static String sign(String secret,
+                              Map<String, String> headers) {
+        try {
+            Mac hmacSha256 = Mac.getInstance(Constants.HMAC_SHA256);
+            byte[] keyBytes = secret.getBytes(Constants.ENCODING);
+            hmacSha256.init(new SecretKeySpec(keyBytes, 0, keyBytes.length, Constants.HMAC_SHA256));
+//            String contentMd5 = MessageDigestUtil.base64AndMD5(bodyContent);
+
+            return new String(Base64.encodeBase64(
+                    hmacSha256.doFinal((buildHeaders(headers, SIGN_HEADER_LIST))
+                            .getBytes(Constants.ENCODING))),
+                    Constants.ENCODING);
+        } catch (Exception e) {
+            throw new RuntimeException(e);
+        }
+    }
+
+    /**
+     * 构建待签名Http头
+     *
+     * @param headers 请求中所有的Http头
+     * @param signHeaderList 自定义参与签名Header前缀
+     * @return 待签名Http头
+     */
+    public static String buildHeaders(Map<String, String> headers, List<String> signHeaderList) {
+
+    	List<String> sbList=new ArrayList<>();
+    	if (null != signHeaderList) {
+    		Collections.sort(signHeaderList);
+    		if (null != headers) {
+    			Map<String, String> sortMap = new TreeMap<String, String>();
+    			sortMap.putAll(headers);
+    			StringBuilder signHeadersStringBuilder = new StringBuilder();
+    			for (Map.Entry<String, String> header : sortMap.entrySet()) {
+                    if (isHeaderToSign(header.getKey(), signHeaderList)) {
+                        StringBuilder sb = new StringBuilder();
+                    	sb.append(header.getKey().toLowerCase());
+                    	sb.append(Constants.SPE2);
+                        if (!StringUtils.isBlank(header.getValue())) {
+                        	sb.append(header.getValue());
+                        }
+                        sbList.add(sb.toString());
+                        if (0 < signHeadersStringBuilder.length()) {
+                        	signHeadersStringBuilder.append(Constants.SPE1);
+                        }
+                        signHeadersStringBuilder.append(header.getKey().toLowerCase());
+                    }
+                }
+    			headers.put(SystemHeader.X_CA_SIGNATURE_HEADERS, signHeadersStringBuilder.toString());
+    		}
+    	}
+        return StringUtils.join(sbList,Constants.SPE3);
+    }
+
+    /**
+     * Http头是否参与签名 return
+     */
+    private static boolean isHeaderToSign(String headerName, List<String> signHeaderList) {
+        if (StringUtils.isBlank(headerName)) {
+            return false;
+        }
+
+        if (headerName.startsWith(Constants.CA_HEADER_TO_SIGN_PREFIX_SYSTEM)) {
+            return true;
+        }
+
+        if (null != signHeaderList) {
+            for (String signHeaderPrefix : signHeaderList) {
+                if (headerName.equalsIgnoreCase(signHeaderPrefix)) {
+                    return true;
+                }
+            }
+        }
+
+        return false;
+    }
+
+    public static void main(String[] args) {
+        String secret = "1234567890123456";
+        Map<String, String> headers = new HashMap<>();
+        headers.put("X-Service-Id", "eh.organ");
+        headers.put("X-Service-Method", "");
+        headers.put("X-Ca-Key", "hello");
+        headers.put("X-Ca-Timestamp", String.valueOf(System.currentTimeMillis()));
+        headers.put("X-Ca-Nonce", UUID.randomUUID().toString());
+        headers.put(SystemHeader.X_CONTENT_MD5, UUID.randomUUID().toString());
+        System.out.println(sign(secret, headers));
+
+    }
+}