Browse Source

修改获取token代码

tags/v1.3.1
wanjing 1 year ago
parent
commit
b07363bb32
2 changed files with 37 additions and 6 deletions
  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 View File

package com.tuoheng.admin.tzhl.service; package com.tuoheng.admin.tzhl.service;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.tuoheng.admin.tzhl.config.TZHLConfig; 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.response.TZHLTokenResponse;
import com.tuoheng.admin.tzhl.service.token.TZHLGetTokenService; import com.tuoheng.admin.tzhl.service.token.TZHLGetTokenService;
import com.tuoheng.common.core.config.common.CommonConfig; import com.tuoheng.common.core.config.common.CommonConfig;
import com.tuoheng.common.core.exception.ServiceException; import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.JsonResult; import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.RedisUtils;
import com.tuoheng.common.core.utils.StringUtils; import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
public class CallTianYiPlatformService { public class CallTianYiPlatformService {


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


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


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


public String callPost(String apiPath, HttpEntity<Object> httpEntity) { public String callPost(String apiPath, HttpEntity<Object> httpEntity) {
// 获取同行令牌token // 获取同行令牌token
String token = tzhlGetTokenService.getToken();
String token = this.getToken();
if (StringUtils.isEmpty(token)) { if (StringUtils.isEmpty(token)) {
throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "token不能为空"); throw new ServiceException(HttpStatus.BAD_REQUEST.value(), "token不能为空");
} }
} }
return JSONObject.toJSONString(response.getBody()); 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 View File

private TZHLGetTokenService tzhlGetTokenService; private TZHLGetTokenService tzhlGetTokenService;


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

Loading…
Cancel
Save