|
|
@@ -0,0 +1,73 @@ |
|
|
|
package tech.deepq.source.util; |
|
|
|
|
|
|
|
import com.alibaba.fastjson2.JSON; |
|
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
|
import org.springframework.context.annotation.Lazy; |
|
|
|
import org.springframework.data.redis.core.RedisTemplate; |
|
|
|
import org.springframework.scheduling.annotation.Async; |
|
|
|
import org.springframework.stereotype.Component; |
|
|
|
import org.springframework.util.DigestUtils; |
|
|
|
import tech.deepq.common.dto.HttpRequestParams; |
|
|
|
import tech.deepq.common.dto.HttpRes; |
|
|
|
import tech.deepq.source.request.RequestClient; |
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.UUID; |
|
|
|
import java.util.concurrent.TimeUnit; |
|
|
|
|
|
|
|
/** |
|
|
|
* @author yechuan |
|
|
|
* @since 2023/9/1 09:58 |
|
|
|
**/ |
|
|
|
@Component |
|
|
|
public class TokenUtils { |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RequestClient requestClient; |
|
|
|
|
|
|
|
@Autowired |
|
|
|
private RedisTemplate<String, String> redisTemplate; |
|
|
|
|
|
|
|
@Lazy |
|
|
|
@Autowired |
|
|
|
private TokenUtils proxy; |
|
|
|
|
|
|
|
private final String TokenFormQHRBCacheKey = "tokenFormQHRBCacheKey"; |
|
|
|
|
|
|
|
public String getTokenFormQHRB(String partnerId, String partnerNtr) { |
|
|
|
proxy.checkTokenFormQHRB(partnerId, partnerNtr); |
|
|
|
return redisTemplate.opsForValue() |
|
|
|
.get(TokenFormQHRBCacheKey); |
|
|
|
} |
|
|
|
|
|
|
|
@Async |
|
|
|
public void checkTokenFormQHRB(String partnerId, String partnerNtr) { |
|
|
|
Long expire = redisTemplate.getExpire(TokenFormQHRBCacheKey, TimeUnit.MINUTES); |
|
|
|
if (expire == null) { |
|
|
|
expire = Long.MIN_VALUE; |
|
|
|
} |
|
|
|
if (expire.compareTo(15L) > 0) { |
|
|
|
return; |
|
|
|
} |
|
|
|
Map<String, Object> body = new HashMap<>(); |
|
|
|
String randomNum = UUID.randomUUID().toString().replaceAll("-", ""); |
|
|
|
randomNum = randomNum.substring(randomNum.length() - 8); |
|
|
|
body.put("partnerId", partnerId); |
|
|
|
body.put("randomNum", randomNum); |
|
|
|
body.put("partnerNtr", DigestUtils.md5DigestAsHex((partnerNtr + randomNum).getBytes(StandardCharsets.UTF_8))); |
|
|
|
HttpRequestParams httpRequestParams = new HttpRequestParams(); |
|
|
|
httpRequestParams.setUrl("https://app.qhrb.com.cn/api/user/partner/getTicket"); |
|
|
|
httpRequestParams.setBody(body); |
|
|
|
httpRequestParams.setMethod("POST"); |
|
|
|
|
|
|
|
HttpRes res = requestClient.execute(httpRequestParams); |
|
|
|
if (!res.getStatus().is2xxSuccessful()) { |
|
|
|
return; |
|
|
|
} |
|
|
|
redisTemplate.opsForValue().set(TokenFormQHRBCacheKey, JSON.parseObject(res.getBody()).getString("dataPoints"), 120, TimeUnit.MINUTES); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |