Преглед изворни кода

修改获取token代码

tags/v1.3.1
wanjing пре 1 година
родитељ
комит
b07363bb32
2 измењених фајлова са 37 додато и 6 уклоњено
  1. +36
    -5
      tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/tzhl/service/CallTianYiPlatformService.java
  2. +1
    -1
      tuoheng-service/tuoheng-admin/src/test/java/com/tuoheng/admin/tzhl/TZHLGetTokenServiceTest.java

+ 36
- 5
tuoheng-service/tuoheng-admin/src/main/java/com/tuoheng/admin/tzhl/service/CallTianYiPlatformService.java Прегледај датотеку

@@ -1,12 +1,16 @@
package com.tuoheng.admin.tzhl.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.tzhl.config.TZHLConfig;
import com.tuoheng.admin.tzhl.constant.TZHLConstant;
import com.tuoheng.admin.tzhl.request.TZHLGetTokenRequest;
import com.tuoheng.admin.tzhl.response.TZHLTokenResponse;
import com.tuoheng.admin.tzhl.service.token.TZHLGetTokenService;
import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.RedisUtils;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
@@ -21,14 +25,14 @@ import org.springframework.web.client.RestTemplate;
public class CallTianYiPlatformService {

@Autowired
@Qualifier("restTemplate")
private RestTemplate restTemplate;
private RedisUtils redisUtils;

@Autowired
private TZHLGetTokenService tzhlGetTokenService;
@Qualifier("restTemplate")
private RestTemplate restTemplate;

public String callGet(String apiPath, HttpEntity<Object> httpEntity) {
String token = tzhlGetTokenService.getToken();
String token = this.getToken();
if (StringUtils.isEmpty(token)) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "token不能为空");
}
@@ -62,7 +66,7 @@ public class CallTianYiPlatformService {

public String callPost(String apiPath, HttpEntity<Object> httpEntity) {
// 获取同行令牌token
String token = tzhlGetTokenService.getToken();
String token = this.getToken();
if (StringUtils.isEmpty(token)) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "token不能为空");
}
@@ -87,4 +91,31 @@ public class CallTianYiPlatformService {
}
return JSONObject.toJSONString(response.getBody());
}

/**
* 获取token
*
* @return
*/
public String getToken() {
TZHLTokenResponse tokenResponse = (TZHLTokenResponse) redisUtils.get(TZHLConstant.HL_TOKEN_KEY);
if (ObjectUtil.isNotNull(tokenResponse)) {
return tokenResponse.getToken();
}
log.info("redis中不存在泰州海陵区城管token,重新获取");
TZHLGetTokenRequest request = new TZHLGetTokenRequest();
request.setTicket(TZHLConfig.tzhlTicket);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity httpEntity = new HttpEntity(request, headers);
String url = TZHLConfig.tzhlURL + TZHLConstant.TIAN_YI_API_TICKET_LOGIN;
String dataJson = this.callGet(url, httpEntity);
tokenResponse = JSONObject.parseObject(dataJson, TZHLTokenResponse.class);
if (!TZHLConstant.RESPONSE_SUCCESS_CODE.equals(tokenResponse.getCode())) {
log.error("泰州海陵区城管,获取token接口失败,{}", tokenResponse);
throw new ServiceException("泰州海陵区城管,获取token接口失败");
}
redisUtils.set(TZHLConstant.HL_TOKEN_KEY, tokenResponse, 1500); // 过期时间为半小时1800,为防止延迟,缩小一点25分钟失效
return tokenResponse.getToken();
}
}

+ 1
- 1
tuoheng-service/tuoheng-admin/src/test/java/com/tuoheng/admin/tzhl/TZHLGetTokenServiceTest.java Прегледај датотеку

@@ -17,7 +17,7 @@ public class TZHLGetTokenServiceTest {
private TZHLGetTokenService tzhlGetTokenService;

@Test
public void testGetOneById() {
public void testGetToken() {
String token = tzhlGetTokenService.getToken();
System.out.println("token = " + token);
}

Loading…
Откажи
Сачувај