Browse Source

增加小图token获取 接口

liangbo.huang 1 month ago
parent
commit
ede00958ba

+ 4 - 0
trade-common/src/main/java/com/trade/common/constant/CacheConstants.java

@@ -35,4 +35,8 @@ public class CacheConstants {
      * 登录账户密码错误次数 redis key
      */
     public static final String PWD_ERR_CNT_KEY = "pwd_err_cnt:";
+    /**
+     * 小图API token redis key
+     */
+    public static final String XIAOTU_ACCESS_TOKEN = "xiaotu_access_token:";
 }

+ 27 - 0
trade-service/src/main/java/com/trade/service/thirdparty/qichacha/configuration/QichachaConfiguration.java

@@ -0,0 +1,27 @@
+package com.trade.service.thirdparty.qichacha.configuration;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Component
+@ConfigurationProperties(prefix = "app.qichacha")
+public class QichachaConfiguration {
+    private String apiKey;
+    private String apiSecret;
+
+    public String getApiKey() {
+        return apiKey;
+    }
+
+    public void setApiKey(String apiKey) {
+        this.apiKey = apiKey;
+    }
+
+    public String getApiSecret() {
+        return apiSecret;
+    }
+
+    public void setApiSecret(String apiSecret) {
+        this.apiSecret = apiSecret;
+    }
+}

+ 7 - 8
trade-service/src/main/java/com/trade/service/thirdparty/qichacha/service/impl/QichachaServiceImpl.java

@@ -5,6 +5,7 @@ import com.trade.common.utils.http.HttpClientUtils;
 import com.trade.common.utils.http.HttpConfig;
 import com.trade.common.utils.http.HttpHeader;
 import com.trade.common.utils.http.StitchingUrlUtil;
+import com.trade.service.thirdparty.qichacha.configuration.QichachaConfiguration;
 import com.trade.service.thirdparty.qichacha.dto.ECIResult;
 import com.trade.service.thirdparty.qichacha.dto.EcBasicDetailDto;
 import com.trade.service.thirdparty.qichacha.dto.EcBasicInfoDto;
@@ -13,9 +14,9 @@ import com.yomahub.liteflow.util.JsonUtil;
 import liquibase.util.MD5Util;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-import org.springframework.beans.factory.annotation.Value;
 import org.springframework.stereotype.Service;
 
+import javax.annotation.Resource;
 import java.time.Instant;
 import java.util.Collections;
 import java.util.HashMap;
@@ -26,10 +27,8 @@ import java.util.Map;
 public class QichachaServiceImpl implements QichachaService {
 
     private static final Logger logger = LoggerFactory.getLogger(QichachaServiceImpl.class);
-    //@Value("${qichacha.api.key}")
-    private String apiKey;
-    //@Value("${qichacha.api.secret}")
-    private String apiSecret;
+    @Resource
+    private QichachaConfiguration qichachaConfiguration;
     // 企业关键字模糊查询接口
     private static final String ECIV4_SEARCH_URL = "http://api.qichacha.com/ECIV4/Search";
     // 410 企业工商照面
@@ -38,7 +37,7 @@ public class QichachaServiceImpl implements QichachaService {
     @Override
     public List<EcBasicInfoDto> findBusinessName(String keyword) {
         Map<String, String> params = new HashMap<String, String>();
-        params.put(ThirdpartyConstants.QCC_KEY, apiKey);
+        params.put(ThirdpartyConstants.QCC_KEY, qichachaConfiguration.getApiKey());
         params.put("keyWord", keyword.replaceAll(" ", ""));
         // 企查查权限验证参数
         String rspStr = HttpClientUtils.get(HttpConfig.custom().url(StitchingUrlUtil.getUrl(ECIV4_SEARCH_URL, params))
@@ -53,7 +52,7 @@ public class QichachaServiceImpl implements QichachaService {
     @Override
     public EcBasicDetailDto getBasicDetailsByName(String keyword) {
         Map<String, String> params = new HashMap<String, String>();
-        params.put(ThirdpartyConstants.QCC_KEY, apiKey);
+        params.put(ThirdpartyConstants.QCC_KEY, qichachaConfiguration.getApiKey());
         params.put("keyword", keyword);
         // 企查查权限验证参数
         String rspStr = HttpClientUtils
@@ -78,7 +77,7 @@ public class QichachaServiceImpl implements QichachaService {
      */
     private String getToken() {
         // token验证加密值(key+Timespan+SecretKey组成的32位md5加密的大写字符串)
-        String token = apiKey + getTimespan() + apiSecret;
+        String token = qichachaConfiguration.getApiKey() + getTimespan() + qichachaConfiguration.getApiSecret();
         token = MD5Util.computeMD5(token);
         return token.toUpperCase();
     }

+ 46 - 0
trade-service/src/main/java/com/trade/service/thirdparty/xiaotu/configuration/XiaotuConfiguration.java

@@ -0,0 +1,46 @@
+package com.trade.service.thirdparty.xiaotu.configuration;
+
+import org.springframework.boot.context.properties.ConfigurationProperties;
+import org.springframework.stereotype.Component;
+
+@Component
+@ConfigurationProperties(prefix = "app.xiaotu")
+public class XiaotuConfiguration {
+    private String gatewayAppKey = "CAYK";
+    private String ftype = "fa";
+    private String accessClientId = "dditSaas";
+    // 获取令牌的url
+    private String getAccessTokenUrl;
+
+    public String getGatewayAppKey() {
+        return gatewayAppKey;
+    }
+
+    public void setGatewayAppKey(String gatewayAppKey) {
+        this.gatewayAppKey = gatewayAppKey;
+    }
+
+    public String getFtype() {
+        return ftype;
+    }
+
+    public void setFtype(String ftype) {
+        this.ftype = ftype;
+    }
+
+    public String getAccessClientId() {
+        return accessClientId;
+    }
+
+    public void setAccessClientId(String accessClientId) {
+        this.accessClientId = accessClientId;
+    }
+
+    public String getGetAccessTokenUrl() {
+        return getAccessTokenUrl;
+    }
+
+    public void setGetAccessTokenUrl(String getAccessTokenUrl) {
+        this.getAccessTokenUrl = getAccessTokenUrl;
+    }
+}

+ 34 - 0
trade-service/src/main/java/com/trade/service/thirdparty/xiaotu/dto/CommonResponse.java

@@ -0,0 +1,34 @@
+package com.trade.service.thirdparty.xiaotu.dto;
+
+/**
+ * 公共返回信息
+ */
+public class CommonResponse {
+
+    private String code;
+    private String msg;
+
+    public String getCode() {
+        return code;
+    }
+
+    public void setCode(String code) {
+        this.code = code;
+    }
+
+    public String getMsg() {
+        return msg;
+    }
+
+    public void setMsg(String msg) {
+        this.msg = msg;
+    }
+
+    /**
+     * 判断是否成功
+     * @return
+     */
+    public boolean isSuccess() {
+        return "200".equals(code);
+    }
+}

+ 29 - 0
trade-service/src/main/java/com/trade/service/thirdparty/xiaotu/dto/RequestTokenDto.java

@@ -0,0 +1,29 @@
+package com.trade.service.thirdparty.xiaotu.dto;
+
+public class RequestTokenDto {
+    // 申报企业UUID
+    private String sbqyuuid;
+    // 用户UUID
+    private String yhuuid1;
+
+    public RequestTokenDto(String sbqyuuid, String yhuuid1) {
+        this.sbqyuuid = sbqyuuid;
+        this.yhuuid1 = yhuuid1;
+    }
+
+    public String getSbqyuuid() {
+        return sbqyuuid;
+    }
+
+    public void setSbqyuuid(String sbqyuuid) {
+        this.sbqyuuid = sbqyuuid;
+    }
+
+    public String getYhuuid1() {
+        return yhuuid1;
+    }
+
+    public void setYhuuid1(String yhuuid1) {
+        this.yhuuid1 = yhuuid1;
+    }
+}

+ 4 - 0
trade-service/src/main/java/com/trade/service/thirdparty/xiaotu/service/XiaotuRequestService.java

@@ -0,0 +1,4 @@
+package com.trade.service.thirdparty.xiaotu.service;
+
+public interface XiaotuRequestService {
+}

+ 69 - 0
trade-service/src/main/java/com/trade/service/thirdparty/xiaotu/service/impl/XiaotuRequestServiceImpl.java

@@ -0,0 +1,69 @@
+package com.trade.service.thirdparty.xiaotu.service.impl;
+
+import cn.hutool.core.util.StrUtil;
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONObject;
+import com.trade.common.constant.CacheConstants;
+import com.trade.common.redis.RedisCache;
+import com.trade.common.utils.http.HttpClientUtils;
+import com.trade.common.utils.http.HttpConfig;
+import com.trade.common.utils.http.HttpHeader;
+import com.trade.service.thirdparty.xiaotu.configuration.XiaotuConfiguration;
+import com.trade.service.thirdparty.xiaotu.dto.CommonResponse;
+import com.trade.service.thirdparty.xiaotu.dto.RequestTokenDto;
+import com.trade.service.thirdparty.xiaotu.service.XiaotuRequestService;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.stereotype.Service;
+
+import javax.annotation.Resource;
+import java.util.concurrent.TimeUnit;
+
+@Service
+public class XiaotuRequestServiceImpl implements XiaotuRequestService {
+
+    private static final Logger logger = LoggerFactory.getLogger(XiaotuRequestServiceImpl.class);
+    private static final String CONTENT_TYPE_JSON = "application/json";
+    @Resource
+    private RedisCache redisCache;
+    @Resource
+    private XiaotuConfiguration xiaotuConfiguration;
+
+    private HttpHeader getCommonHeader() {
+        return HttpHeader.custom().other("accessClientId", xiaotuConfiguration.getAccessClientId())
+                .other("ftype", xiaotuConfiguration.getFtype())
+                .other("gatewaAppKey", xiaotuConfiguration.getAccessClientId());
+    }
+
+    /**
+     * 获取小图accessToken 和小图约定token的有效期为20分钟
+     * 
+     * @param xtUid 小图用户id
+     * @param xtEid 小图企业id
+     * @return 小图accessToken
+     */
+    private String getAccessToken(String xtUid, String xtEid) {
+        String accessToken = redisCache.getCacheObject(CacheConstants.XIAOTU_ACCESS_TOKEN);
+        if (StrUtil.isEmpty(accessToken)) {
+            String resp = HttpClientUtils
+                    .post(HttpConfig.custom().headers(getCommonHeader().contentType(CONTENT_TYPE_JSON).build())
+                            .json(JSON.toJSONString(new RequestTokenDto(xtEid, xtUid)))
+                            .url(xiaotuConfiguration.getGetAccessTokenUrl()));
+            CommonResponse commonResponse = JSON.parseObject(resp, CommonResponse.class);
+            if (commonResponse != null && !commonResponse.isSuccess()) {
+                logger.error("获取小图accessToken失败,resp={}", resp);
+                throw new RuntimeException("获取小图accessToken失败");
+            }
+            JSONObject jsonObject = JSON.parseObject(resp).getJSONObject("body");
+            accessToken = (String) jsonObject.get("ACCESS_TOKEN");
+            if (StringUtils.isEmpty(accessToken)) {
+                logger.error("获取小图accessToken为空,resp={}", resp);
+                throw new RuntimeException("获取小图accessToken为空");
+            }
+            redisCache.setCacheObject(CacheConstants.XIAOTU_ACCESS_TOKEN, accessToken, 10, TimeUnit.MINUTES);
+            return accessToken;
+        }
+        return accessToken;
+    }
+}