/*     */ package cn.com.esgcc.ai.matrix.sdk.request;
/*     */ 
/*     */

import cn.com.esgcc.ai.matrix.sdk.constant.SdkConstant;
import cn.com.esgcc.ai.matrix.sdk.esgccenum.ExceptionEnum;
import cn.com.esgcc.ai.matrix.sdk.exception.ServiceException;
import cn.com.esgcc.ai.matrix.sdk.response.SdkResponse;
import cn.com.esgcc.ai.matrix.sdk.response.SdkTokenResponse;
import cn.com.esgcc.ai.matrix.sdk.util.ParamsUtils;
import cn.com.esgcc.ai.matrix.sdk.util.encrypt.StringUtil;
import cn.com.esgcc.ai.matrix.sdk.util.encrypt.sm2.Sm2KeyVO;
import cn.com.esgcc.ai.matrix.sdk.util.encrypt.sm2.Sm2Utils;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import org.bouncycastle.crypto.InvalidCipherTextException;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class RequestClient
/*     */ {
/*  31 */   Log logger = LogFactory.get();
/*     */   
/*     */   private String clientId;
/*     */   
/*     */   private String clientSecret;
/*     */   
/*     */   private String sm2ClientSecret;
/*     */   
/*     */   private String username;
/*     */   private String password;
/*     */   private String sm2Password;
/*     */   private String deviceId;
/*     */   private Sm2KeyVO sm2KeyVO;
/*     */   private String accessToken;
/*     */   private String ocrServer;
/*  46 */   private int timeout = SdkConstant.DEFAULT_CONNECTION_TIMEOUT;
/*     */ 
/*     */   
/*     */   public RequestClient() {}
/*     */ 
/*     */   
/*     */   public RequestClient(String ocrServer, String deviceId, String clientId, String clientSecret, String username, String password, Sm2KeyVO sm2KeyVO) {
/*  53 */     this.clientId = clientId;
/*  54 */     this.clientSecret = clientSecret;
/*  55 */     this.deviceId = deviceId;
/*  56 */     this.username = username;
/*  57 */     this.password = password;
/*  58 */     this.ocrServer = ocrServer;
/*  59 */     this.sm2KeyVO = sm2KeyVO;
/*     */     try {
/*  61 */       this.sm2ClientSecret = Sm2Utils.encrypt(clientSecret, "04e7a67be95ca3efe0fe970d407af7688ea08cdf0d484b89d7720ea836d2baeea11ada72fbe9fc799251141009ef438d62434f8473e6191b7a3e09a1795c8ca5c7");
/*  62 */       this.sm2Password = Sm2Utils.encrypt(password, "04e7a67be95ca3efe0fe970d407af7688ea08cdf0d484b89d7720ea836d2baeea11ada72fbe9fc799251141009ef438d62434f8473e6191b7a3e09a1795c8ca5c7");
/*  63 */     } catch (InvalidCipherTextException e) {
/*  64 */       e.printStackTrace();
/*  65 */       throw new ServiceException("密码加密失败");
/*     */     } 
/*     */   }
/*     */ 
/*     */   
/*     */   public RequestClient(String ocrServer, String deviceId, String clientId, String clientSecret, String username, String password, Sm2KeyVO sm2KeyVO, Boolean sm2Encrypt) {
/*  71 */     this.clientId = clientId;
/*  72 */     this.sm2ClientSecret = clientSecret;
/*  73 */     this.deviceId = deviceId;
/*  74 */     this.username = username;
/*  75 */     this.sm2Password = password;
/*  76 */     this.ocrServer = ocrServer;
/*  77 */     this.sm2KeyVO = sm2KeyVO;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public SdkResponse esgccOcr(String path, QueryData queryData) {
/*  87 */     return esgccOcr(path, queryData, true);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public SdkResponse esgccOcr(String path, File queryData) {
/*  96 */     return esgccOcr(path, queryData, true);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public SdkResponse esgccOcr(String path, File queryData, boolean decrypt) {
/* 106 */     verifyData();
/* 107 */     String sign = null;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */     
/* 116 */     this.logger.debug("sing:" + sign, new Object[0]);
/* 117 */     Map<String, Object> paramap = new HashMap<>();
/* 118 */     paramap.put("pdfFile", queryData);
/* 119 */     String body = ((HttpRequest)((HttpRequest)((HttpRequest)HttpRequest.post(this.ocrServer + path).header("access-token", this.accessToken)).header("encryption", "false")).header("publicKey", this.sm2KeyVO.getPublicKey())).form(paramap).timeout(this.timeout).execute().body();
/* 120 */     this.logger.debug("body:" + body, new Object[0]);
/* 121 */     JSONObject data = JSONUtil.parseObj(body);
/* 122 */     if (!ExceptionEnum.COMMON_SUCCESS_1B.getResultCode().equals(data.getStr("code"))) {
/* 123 */       this.logger.error("识别失败:" + data.getStr("msg"), new Object[0]);
/* 124 */       return SdkResponse.fail(data.getStr("code"), data.getStr("msg"));
/*     */     } 
/* 126 */     return (SdkResponse)JSONUtil.toBean(data, SdkResponse.class);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public SdkResponse esgccOcr(String path, QueryData queryData, boolean decrypt) {
/* 135 */     verifyData();
/* 136 */     String postData = JSONUtil.toJsonStr(queryData);
/* 137 */     String sign = null;
/* 138 */     if (queryData.isCheckFile()) {
/* 139 */       sign = ParamsUtils.generateReqSign(postData);
/*     */     } else {
/*     */       
/* 142 */       sign = ParamsUtils.generateReqSign(queryData);
/*     */     } 
/*     */ 
/*     */     
/* 146 */     this.logger.debug("sing:" + sign, new Object[0]);
/* 147 */     String body = ((HttpRequest)((HttpRequest)((HttpRequest)((HttpRequest)HttpRequest.post(this.ocrServer + path).header("access-token", this.accessToken)).header("appCode", "ocrAdmin")).header("sign", sign)).header("publicKey", this.sm2KeyVO.getPublicKey())).body(postData).timeout(this.timeout).execute().body();
/* 148 */     this.logger.debug("body:" + body, new Object[0]);
/* 149 */     JSONObject data = JSONUtil.parseObj(body);
/* 150 */     if (!ExceptionEnum.COMMON_SUCCESS_1B.getResultCode().equals(data.getStr("code"))) {
/* 151 */       this.logger.error("识别失败:" + data.getStr("msg"), new Object[0]);
/* 152 */       return SdkResponse.fail(data.getStr("code"), data.getStr("msg"));
/* 153 */     }  if (decrypt) {
/* 154 */       data = ParamsUtils.decrypt(data, getSm2KeyVO().getPrivateKey());
/*     */     }
/* 156 */     this.logger.debug("data:" + data, new Object[0]);
/* 157 */     return (SdkResponse)JSONUtil.toBean(data, SdkResponse.class);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public <T> SdkResponse<T> newEsgccOcr(String path, T queryData, boolean decrypt) {
/* 167 */     verifyData();
/* 168 */     String postData = JSONUtil.toJsonStr(queryData);
/* 169 */     String sign = null;
/*     */     
/* 171 */     sign = ParamsUtils.generateReqSign(postData);
/*     */ 
/*     */     
/* 174 */     this.logger.debug("sing:" + sign, new Object[0]);
/* 175 */     String body = ((HttpRequest)((HttpRequest)((HttpRequest)((HttpRequest)HttpRequest.post(this.ocrServer + path).header("access-token", this.accessToken)).header("appCode", "ocrAdmin")).header("sign", sign)).header("publicKey", this.sm2KeyVO.getPublicKey())).body(postData).timeout(this.timeout).execute().body();
/* 176 */     this.logger.debug("body:" + body, new Object[0]);
/* 177 */     JSONObject data = JSONUtil.parseObj(body);
/* 178 */     if (!ExceptionEnum.COMMON_SUCCESS_1B.getResultCode().equals(data.getStr("code"))) {
/* 179 */       this.logger.error("识别失败:" + data.getStr("msg"), new Object[0]);
/* 180 */       return SdkResponse.fail(data.getStr("code"), data.getStr("msg"));
/* 181 */     }  if (decrypt) {
/* 182 */       data = ParamsUtils.decrypt(data, getSm2KeyVO().getPrivateKey());
/*     */     }
/* 184 */     this.logger.debug("data:" + data, new Object[0]);
/* 185 */     return (SdkResponse<T>)JSONUtil.toBean(data, SdkResponse.class);
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public String decrypt(String data) {
/* 193 */     if (null == this.sm2KeyVO || StringUtil.isEmpty(this.sm2KeyVO.getPrivateKey())) {
/* 194 */       throw new ServiceException("私钥为空");
/*     */     }
/*     */     try {
/* 197 */       return Sm2Utils.decrypt(data, this.sm2KeyVO.getPrivateKey());
/* 198 */     } catch (InvalidCipherTextException e) {
/* 199 */       e.printStackTrace();
/* 200 */       throw new ServiceException("解密失败");
/*     */     } 
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public SdkTokenResponse oauthUserToken() {
/* 210 */     verifyData();
/* 211 */     Map<String, Object> formMap = new HashMap<>();
/* 212 */     formMap.put("clientId", this.clientId);
/* 213 */     formMap.put("clientSecret", this.sm2ClientSecret);
/* 214 */     formMap.put("username", this.username);
/* 215 */     formMap.put("password", this.sm2Password);
/* 216 */     formMap.put("timestamp", System.currentTimeMillis() + "");
/* 217 */     String sign = ParamsUtils.generateReqSign(formMap);
/* 218 */     String body = ((HttpRequest)((HttpRequest)((HttpRequest)HttpRequest.post(this.ocrServer + "/api-auth/oauth/user/token").header("sign", sign)).header("appCode", "ocrAdmin")).header("publicKey", this.sm2KeyVO.getPublicKey())).form(formMap).timeout(this.timeout).execute().body();
/* 219 */     SdkTokenResponse sdkTokenResponse = (SdkTokenResponse)JSONUtil.toBean(body, SdkTokenResponse.class);
/* 220 */     if (ExceptionEnum.COMMON_SUCCESS_1B.getResultCode().equals(sdkTokenResponse.getCode())) {
/* 221 */       this.accessToken = sdkTokenResponse.getAccessToken();
/*     */     } else {
/* 223 */       this.logger.error("登录认证失败:" + sdkTokenResponse.getMsg(), new Object[0]);
/* 224 */       throw new ServiceException("登录认证失败:" + sdkTokenResponse.getMsg());
/*     */     } 
/* 226 */     System.out.println(sdkTokenResponse.getAccessToken());
/* 227 */     return sdkTokenResponse;
/*     */   }
/*     */ 
/*     */ 
/*     */   
/*     */   public void verifyData() {
/* 233 */     if (StringUtil.isEmpty(this.ocrServer))
/* 234 */       throw new ServiceException("ocrServer 不能为空"); 
/* 235 */     if (StringUtil.isEmpty(this.clientId))
/* 236 */       throw new ServiceException("clientId 不能为空"); 
/* 237 */     if (StringUtil.isEmpty(this.sm2ClientSecret))
/* 238 */       throw new ServiceException("sm2ClientSecret 不能为空"); 
/* 239 */     if (StringUtil.isEmpty(this.deviceId))
/* 240 */       throw new ServiceException("deviceId 不能为空"); 
/* 241 */     if (StringUtil.isEmpty(this.username))
/* 242 */       throw new ServiceException("username 不能为空"); 
/* 243 */     if (StringUtil.isEmpty(this.sm2Password))
/* 244 */       throw new ServiceException("sm2Password 不能为空"); 
/* 245 */     if (null == this.sm2KeyVO) {
/* 246 */       throw new ServiceException("sm2KeyVO 不能为空");
/*     */     }
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public String getClientId() {
/* 265 */     return this.clientId;
/*     */   }
/*     */   
/*     */   public void setClientId(String clientId) {
/* 269 */     this.clientId = clientId;
/*     */   }
/*     */   
/*     */   public String getClientSecret() {
/* 273 */     return this.clientSecret;
/*     */   }
/*     */   
/*     */   public void setClientSecret(String clientSecret) {
/* 277 */     this.clientSecret = clientSecret;
/*     */   }
/*     */   
/*     */   public String getSm2ClientSecret() {
/* 281 */     return this.sm2ClientSecret;
/*     */   }
/*     */   
/*     */   public void setSm2ClientSecret(String sm2ClientSecret) {
/* 285 */     this.sm2ClientSecret = sm2ClientSecret;
/*     */   }
/*     */   
/*     */   public String getUsername() {
/* 289 */     return this.username;
/*     */   }
/*     */   
/*     */   public void setUsername(String username) {
/* 293 */     this.username = username;
/*     */   }
/*     */   
/*     */   public String getPassword() {
/* 297 */     return this.password;
/*     */   }
/*     */   
/*     */   public void setPassword(String password) {
/* 301 */     this.password = password;
/*     */   }
/*     */   
/*     */   public String getSm2Password() {
/* 305 */     return this.sm2Password;
/*     */   }
/*     */   
/*     */   public void setSm2Password(String sm2Password) {
/* 309 */     this.sm2Password = sm2Password;
/*     */   }
/*     */   
/*     */   public String getDeviceId() {
/* 313 */     return this.deviceId;
/*     */   }
/*     */   
/*     */   public void setDeviceId(String deviceId) {
/* 317 */     this.deviceId = deviceId;
/*     */   }
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   
/*     */   public Sm2KeyVO getSm2KeyVO() {
/* 325 */     return this.sm2KeyVO;
/*     */   }
/*     */   
/*     */   public void setSm2KeyVO(Sm2KeyVO sm2KeyVO) {
/* 329 */     this.sm2KeyVO = sm2KeyVO;
/*     */   }
/*     */   
/*     */   public String getAccessToken() {
/* 333 */     return this.accessToken;
/*     */   }
/*     */   
/*     */   public void setAccessToken(String accessToken) {
/* 337 */     this.accessToken = accessToken;
/*     */   }
/*     */   
/*     */   public String getOcrServer() {
/* 341 */     return this.ocrServer;
/*     */   }
/*     */   
/*     */   public void setOcrServer(String ocrServer) {
/* 345 */     this.ocrServer = ocrServer;
/*     */   }
/*     */   
/*     */   public long getTimeout() {
/* 349 */     return this.timeout;
/*     */   }
/*     */   
/*     */   public void setTimeout(int timeout) {
/* 353 */     this.timeout = timeout;
/*     */   }
/*     */ }


/* Location:              C:\Users\ningj\Desktop\java-sdk-1.0.0-SNAPSHOT\!\cn\com\esgcc\ai\matrix\sdk\request\RequestClient.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */