|
@@ -0,0 +1,167 @@
|
|
|
+package com.rongwei.bscommon.sys.utils;
|
|
|
+
|
|
|
+import cn.hutool.http.ContentType;
|
|
|
+import cn.hutool.http.HttpRequest;
|
|
|
+import cn.hutool.json.JSONConfig;
|
|
|
+import cn.hutool.json.JSONObject;
|
|
|
+import cn.hutool.json.JSONUtil;
|
|
|
+import com.rongwei.bsentity.dto.ApiCallDto;
|
|
|
+import com.rongwei.bsentity.dto.ApiReturnDto;
|
|
|
+import com.rongwei.commonservice.service.ApiLogService;
|
|
|
+import com.rongwei.commonservice.service.RedisService;
|
|
|
+import com.rongwei.commonservice.service.SysConfigService;
|
|
|
+import com.rongwei.rwcommon.base.exception.CustomException;
|
|
|
+import com.rongwei.rwcommon.utils.Constants;
|
|
|
+import com.rongwei.rwcommon.utils.SecurityUtil;
|
|
|
+import com.rongwei.rwcommon.utils.StringUtils;
|
|
|
+import com.rongwei.rwcommonentity.commonservers.domain.ApiLogDo;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.Date;
|
|
|
+import java.util.HashMap;
|
|
|
+import java.util.Map;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 考勤考核系统对接SDK
|
|
|
+ */
|
|
|
+@Component
|
|
|
+public class AttendanceAssessmentSdk {
|
|
|
+
|
|
|
+
|
|
|
+ public static final SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
|
|
|
+ public static final String REDIS_KEY_NAME = "attendance_assessment_token";
|
|
|
+ public static final int TOKEN_EXPIRED_SECONDS = 1700;
|
|
|
+ private static final Logger logger = LoggerFactory.getLogger(AttendanceAssessmentSdk.class);
|
|
|
+ // 客户端ID
|
|
|
+ private String appId;
|
|
|
+ // 客户端密钥
|
|
|
+ private String secret;
|
|
|
+ // 固定值
|
|
|
+ private String orgCode;
|
|
|
+ // 服务地址
|
|
|
+ private String clientIp;
|
|
|
+ // token的虚拟目录
|
|
|
+ private String tokenVirtualDirectory;
|
|
|
+ private Map<String, String> businessDataMap = new HashMap<>(2);
|
|
|
+ // api路由
|
|
|
+ private Map<String, String> apiVirtualDirectoryMap = null;
|
|
|
+ @Autowired
|
|
|
+ private SysConfigService sysConfigService;
|
|
|
+ @Autowired
|
|
|
+ private ApiLogService apiLogService;
|
|
|
+ @Autowired
|
|
|
+ private RedisService redisService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 黑名单系统对接配置初始化
|
|
|
+ *
|
|
|
+ * @author chen
|
|
|
+ */
|
|
|
+ public void init() {
|
|
|
+ String configContent = sysConfigService.getContentByConfigCode("sdk_config");
|
|
|
+ JSONObject apiReturnJson = JSONUtil.parseObj(configContent);
|
|
|
+ // 黑名单系统IP
|
|
|
+ this.appId = apiReturnJson.getStr("app_id");
|
|
|
+ this.secret = apiReturnJson.getStr("secret");
|
|
|
+ this.orgCode = apiReturnJson.getStr("client_secret");
|
|
|
+ this.clientIp = apiReturnJson.getStr("ip");
|
|
|
+ // 获取tokenIp
|
|
|
+ this.tokenVirtualDirectory = apiReturnJson.getStr("virtual_directory");
|
|
|
+ this.businessDataMap.put("APPID", this.appId);
|
|
|
+ this.businessDataMap.put("SECRET", this.secret);
|
|
|
+ this.apiVirtualDirectoryMap = JSONUtil.toBean(apiReturnJson.getJSONObject("api_virtual_directory"), Map.class);
|
|
|
+ System.out.println("黑名单系统对接配置初始化完毕");
|
|
|
+ }
|
|
|
+
|
|
|
+ public ApiReturnDto apiCall(ApiCallDto paramVo) {
|
|
|
+ logger.info("接口调用开始:", paramVo.getApiCode());
|
|
|
+ // 请求地址
|
|
|
+ String url = this.clientIp + this.apiVirtualDirectoryMap.get(paramVo.getApiCode());
|
|
|
+ logger.info("api地址:{}", url);
|
|
|
+ JSONConfig config = new JSONConfig();
|
|
|
+ config.setIgnoreNullValue(false);
|
|
|
+ String jsonStr = JSONUtil.toJsonStr(paramVo.getData(), config);
|
|
|
+ logger.info("入参:{}", jsonStr);
|
|
|
+ String apiResult = HttpRequest.post(url).headerMap(setHeadMap(), true).body(jsonStr).execute().body();
|
|
|
+ logger.info("接口返回报文:{}", apiResult);
|
|
|
+ ApiReturnDto apiReturnDto = JSONUtil.toBean(apiResult, ApiReturnDto.class);
|
|
|
+ // 接口调用结束后续逻辑处理
|
|
|
+ Thread td = new Thread(() -> afterAll(paramVo, jsonStr, apiResult));
|
|
|
+ td.start();
|
|
|
+ return apiReturnDto;
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getToken() {
|
|
|
+ logger.info("获取token开始");
|
|
|
+ if (redisService.hasKey(REDIS_KEY_NAME)) {
|
|
|
+ logger.info("token已存在");
|
|
|
+ return redisService.getRedisCatchObj(REDIS_KEY_NAME).toString();
|
|
|
+ }
|
|
|
+ String url = this.clientIp + this.tokenVirtualDirectory;
|
|
|
+ /**********************组装请求参数**************************************/
|
|
|
+ Map<String, Map<String, String>> postData = new HashMap<>(2);
|
|
|
+ postData.put("STANDARD_DATA", getRequestData());
|
|
|
+ postData.put("BUSINESS_DATA", this.businessDataMap);
|
|
|
+
|
|
|
+ logger.info("请求url: {}", url);
|
|
|
+ String body = HttpRequest.post(url).body(postData.toString()).execute().body();
|
|
|
+ logger.info("接口返回: {}", body);
|
|
|
+ if (StringUtils.isEmptyStr(body)) {
|
|
|
+ throw new CustomException("请求token失败");
|
|
|
+ }
|
|
|
+ ApiReturnDto response = JSONUtil.toBean(body, ApiReturnDto.class);
|
|
|
+ String token = response.getToken();
|
|
|
+ // 将 token 放入redis;
|
|
|
+ redisService.redisCatchInit(REDIS_KEY_NAME, token, TOKEN_EXPIRED_SECONDS);
|
|
|
+ logger.info("获取token接口返回的信息: {}", response);
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 自定义header
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private Map<String, String> setHeadMap() {
|
|
|
+ Map<String, String> headers = new HashMap<>();
|
|
|
+ headers.put("Content-Type", ContentType.JSON.getValue());
|
|
|
+ headers.put("jsonContent-Type", ContentType.JSON.getValue());
|
|
|
+ headers.put("Authorization", getToken());
|
|
|
+ return headers;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 接口调用结束后续逻辑处理
|
|
|
+ */
|
|
|
+ private void afterAll(ApiCallDto paramVo, String jsonStr, String apiResult) {
|
|
|
+ // 返回数据
|
|
|
+ JSONObject apiReturnJson = JSONUtil.parseObj(apiResult);
|
|
|
+ // 接口日志保存
|
|
|
+ ApiLogDo apiLogDo = new ApiLogDo();
|
|
|
+ apiLogDo.setId(SecurityUtil.getUUID());
|
|
|
+ apiLogDo.setApiip(this.clientIp);
|
|
|
+ apiLogDo.setApisyscode("blackDataQuery");
|
|
|
+ apiLogDo.setApicode(paramVo.getApiCode());
|
|
|
+ apiLogDo.setApiroute(this.apiVirtualDirectoryMap.get("blackApiUrl"));
|
|
|
+ apiLogDo.setCalltype(Constants.APILOG_CALLTYPE_OUT);
|
|
|
+ apiLogDo.setApisenddata(jsonStr);
|
|
|
+ apiLogDo.setApireturndata(apiReturnJson.toJSONString(0));
|
|
|
+ apiLogService.save(apiLogDo);
|
|
|
+ }
|
|
|
+
|
|
|
+ private String getRequestTime() {
|
|
|
+ return sdf.format(new Date());
|
|
|
+ }
|
|
|
+
|
|
|
+ private Map<String, String> getRequestData() {
|
|
|
+ Map<String, String> standardDataMap = new HashMap<>(3);
|
|
|
+ standardDataMap.put("ZINSTID", SecurityUtil.getUUID());
|
|
|
+ standardDataMap.put("ZZREQTIME", getRequestTime());
|
|
|
+ standardDataMap.put("ORGCODE", this.orgCode);
|
|
|
+ return standardDataMap;
|
|
|
+ }
|
|
|
+}
|