package cn.com.do1.dsf.modules.zdph.base.service.impl;
import cn.com.do1.dsf.common.utils.StringUtils;
import cn.com.do1.dsf.modules.zdph.base.service.ZdphAccessTokenService;
import cn.com.do1.dsf.modules.zdph.base.service.ZdphRequestService;
import cn.com.do1.dsf.modules.zdph.base.util.ZdphHttpRequestBaseUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sgcc.sm.SM2Utils;
import com.sgcc.sm.SM3Utils;
import com.sgcc.sm.Util;
import lombok.extern.slf4j.Slf4j;
import org.bouncycastle.util.encoders.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
/**
* 中电普华请求Service实现类
* Created by ysq
* time 2020-01-11 11:39
* Copyright (c) 2019 广东道一信息技术股份有限公司 All rights reserved.
*/
@Slf4j
@Service("zdphRequestService")
public class ZdphRequestServiceImpl implements ZdphRequestService {
@Value("${zdph.serverUrl}")
private String serverUrl;
@Value("${zdph.serverPort}")
private String serverPort;
@Value("${zdph.serverScheme}")
private String serverScheme;
@Value("${zdph.publicKey}")
private String publicKey;
@Value("${zdph.privateKey}")
private String privateKey;
@Autowired
private ZdphAccessTokenService zdphAccessTokenService;
/**
* 通用请求
* @param url 接口相对路径
* @param params 参数
* @return 解密后的json
*/
@Override
public String requestPost(String url, Map<String, String> params) {
//封装url
StringBuilder urlBuilder = new StringBuilder()
.append(serverScheme)
.append("://")
.append(serverUrl).append(":")
.append(serverPort)
.append(url);
return commonRequestPost(urlBuilder.toString(),true,params);
}
/**
* 获取accessToken
* @param url access路径
* @param params 参数
* @return json
*/
@Override
public String reqestAccessToken(String url, Map<String, String> params) {
StringBuilder urlBuilder = new StringBuilder()
.append(serverScheme)
.append("://")
.append(serverUrl).append(":")
.append(serverPort)
.append(url);
return commonRequestPost(urlBuilder.toString(),false,params);
}
/**
* 统一加密请求
* @param url
* @param isNeedToken
* @param params
* @return
*/
protected String commonRequestPost(String url,boolean isNeedToken,Map<String, String> params){
String accessToken = "";
//获取accessToken
if(isNeedToken) {
accessToken = zdphAccessTokenService.getAccessToken();
}
try {
Map<String,String> requestParam = new HashMap<>();
String jsonValue;
if(null == params || params.isEmpty()){
jsonValue = "";
}else{
ObjectMapper objectMapper = new ObjectMapper();
jsonValue = objectMapper.writeValueAsString(params);
}
Calendar calendar = Calendar.getInstance(Locale.CHINA);
long timestamp = calendar.getTime().getTime();
//加密对象
byte[] encByte = new SM2Utils().encryptData(Util.decodeHex(publicKey.toCharArray()), Util.encodeHexString(jsonValue.getBytes()).getBytes());
String encryptData = Util.byteToHex(encByte);
//签名生成
StringBuilder signBuilder = new StringBuilder().append(encryptData).append(accessToken).append(timestamp);
byte[] md = new SM3Utils().sign(signBuilder.toString().getBytes());
String sign = new String(Hex.encode(md));
//塞入对象
requestParam.put("encryptData", encryptData);
requestParam.put("timestamp", String.valueOf(timestamp));
requestParam.put("sign", sign);
//请求
String resultJson = ZdphHttpRequestBaseUtil.postJsonData(url,accessToken,requestParam);
if(StringUtils.isEmpty(resultJson)){
return "";
}
JSONObject jsonObject = JSON.parseObject(resultJson);
String encryptDataKey = "encryptData";
if(!jsonObject.containsKey(encryptDataKey)){
return "";
}
encryptData = jsonObject.getString(encryptDataKey);
byte[]decByte = new SM2Utils().decryptData(Hex.decode(privateKey), Hex.decode(encryptData.getBytes()));
String decryptValue = new String(Hex.decode(decByte));
log.info("请求路径={} 请求参数={} 请求结果={}",url,JSON.toJSONString(requestParam),decryptValue);
return decryptValue;
}catch (Exception e){
log.error("请求中电普华结果异常",e);
}
return "";
}
}