|
@@ -0,0 +1,94 @@
|
|
|
+package com.yixin.ms.server;
|
|
|
+
|
|
|
+import cn.hutool.cache.CacheUtil;
|
|
|
+import cn.hutool.cache.impl.TimedCache;
|
|
|
+import com.alibaba.fastjson.JSON;
|
|
|
+import com.google.common.collect.Maps;
|
|
|
+import com.yixin.ms.boot.restful.ServiceException;
|
|
|
+import com.yixin.ms.boot.uitls.DESUtil;
|
|
|
+import com.yixin.ms.server.dto.*;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.beans.factory.annotation.Value;
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
+import org.springframework.web.client.RestTemplate;
|
|
|
+
|
|
|
+import java.util.HashMap;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author: lileilei
|
|
|
+ * @date: 2022/9/4 10:49
|
|
|
+ * @description: 实名三项服务
|
|
|
+ */
|
|
|
+@Component
|
|
|
+@Slf4j
|
|
|
+public class RealNameServer {
|
|
|
+ private static final String TOKEN_HEADERS_KEY = "real:name:token_session_id";
|
|
|
+
|
|
|
+ @Value("${real.name.appid}")
|
|
|
+ private String appid;
|
|
|
+ @Value("${real.name.appsecret}")
|
|
|
+ private String appsecret;
|
|
|
+ @Value("${real.name.deskey}")
|
|
|
+ private String deskey;
|
|
|
+ @Value("${real.name.tokenUrl}")
|
|
|
+ private String getTokenUrl;
|
|
|
+ @Value("${real.name.ctidAuthUrl}")
|
|
|
+ private String ctidAuthUrl;
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RestTemplate restTemplate;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 请求密钥有效时间为 7200s
|
|
|
+ *
|
|
|
+ * @see <a href="https://www.hutool.cn/docs/#/cache/TimedCache">定时缓存</a>
|
|
|
+ */
|
|
|
+ private static TimedCache<String, String> tokenCache = CacheUtil.newTimedCache(7200 * 1000);
|
|
|
+
|
|
|
+ public String getToken() {
|
|
|
+ String token = tokenCache.get(TOKEN_HEADERS_KEY, false);
|
|
|
+ if (token != null) {
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ try {
|
|
|
+ //获取token
|
|
|
+ String body = restTemplate.postForObject(getTokenUrl, new RealNameTokenDTO().setBody(new RealNameTokenBodyDTO().setAppid(appid).setSecret(appsecret)), String.class);
|
|
|
+ log.info("实名三项获取token信息响应内容:{}", body);
|
|
|
+ token = JSON.parseObject(body).getJSONObject("body").get("token").toString();
|
|
|
+ tokenCache.put(TOKEN_HEADERS_KEY, token);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ServiceException("实名三项三方服务异常");
|
|
|
+ }
|
|
|
+
|
|
|
+ return token;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 实名三项验证
|
|
|
+ *
|
|
|
+ * @param dto
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public String ctidAuth(RealNameCtidAuthDTO dto) {
|
|
|
+ RealNameAuthBodyDTO authBodyDTO = new RealNameAuthBodyDTO();
|
|
|
+ HashMap<Object, Object> bodyMap = Maps.newHashMap();
|
|
|
+ bodyMap.put("name", dto.getName());
|
|
|
+ bodyMap.put("idcard", dto.getIdcard());
|
|
|
+ authBodyDTO.setToken(this.getToken()).setBody(DESUtil.encrypt(JSON.toJSONString(bodyMap), deskey));
|
|
|
+ authBodyDTO.setOther(new RealNameAuthOtherDTO().setFaceImg(dto.getFaceImg()));
|
|
|
+
|
|
|
+// log.info("实名三项验证请求入参:{}", JSON.toJSONString(authBodyDTO));
|
|
|
+
|
|
|
+ String body = null;
|
|
|
+ try {
|
|
|
+ body = restTemplate.postForObject(ctidAuthUrl, authBodyDTO, String.class);
|
|
|
+ log.info("实名三项验证响应内容:{}", body);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new ServiceException("实名三项三方服务异常");
|
|
|
+ }
|
|
|
+
|
|
|
+ return body;
|
|
|
+ }
|
|
|
+}
|