Browse Source

20220222修改查询userId的接口

ptos
zy_Java 4 years ago
parent
commit
5e37eb1455
  1. 6
      ccmq/pom.xml
  2. 78
      ccmq/src/main/java/com/ccsens/ccmq/lowlevel/service/UserService.java

6
ccmq/pom.xml

@ -130,7 +130,11 @@
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.62</version>
</dependency>
<!-- hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>

78
ccmq/src/main/java/com/ccsens/ccmq/lowlevel/service/UserService.java

@ -1,31 +1,101 @@
package com.ccsens.ccmq.lowlevel.service;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import wiki.tall.ccmq.common.exception.BaseException;
import com.alibaba.fastjson.JSONObject;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Map;
/**
* @author wei
*/
@Service
public class UserService implements IUserService{
@Data
public static class UserInfoByToken{
private String token;
private String deviceId;
private String clientIp;
private Byte clientType;
}
@Data
public static class TokenToUserId{
private Long id;
private String userName;
private String avatarUrl;
private String phone;
private byte authType;
private String token;
private String refreshToken;
}
@Autowired
private RestTemplate restTemplate;
@Override
public String getUserIdByToken(String token){
String userId = null;
// String url = "https://test.tall.wiki/gateway/tall/v1.0/users/claims?token="+token;
String url = "http://localhost:7130/v3.0/users/claims?token="+token;
ResponseEntity<String> response = restTemplate.getForEntity(url,String.class);
//// String url = "https://test.tall.wiki/gateway/tall/v1.0/users/claims?token="+token;
// String url = "http://localhost:7130/v3.0/users/claims?token="+token;
// ResponseEntity<String> response = restTemplate.getForEntity(url,String.class);
// String strBody = null;
// if(response.getStatusCodeValue() == 200){
// strBody = response.getBody();
// }
// if(StrUtil.isNotEmpty(strBody)) {
// userId = strBody;
// }
UserInfoByToken userInfoByToken = new UserInfoByToken();
userInfoByToken.setToken(token);
String url = "https://localhost:7290/users/token";
HttpHeaders httpHeaders = new HttpHeaders();
MediaType type=MediaType.parseMediaType("application/json;charset=UTF-8");
httpHeaders.setContentType(type);
JSONObject json = JSON.parseObject(JSON.toJSONString(userInfoByToken));
HttpEntity<Map<String, Object>> objectHttpEntity = new HttpEntity<>(json,httpHeaders);
URI uri = null;
try {
uri = new URI(url);
}catch (URISyntaxException e) {
throw new BaseException("请求路径转换异常");
}
TokenToUserId tokenToUserId = null;
ResponseEntity<String> response = restTemplate.postForEntity(uri, objectHttpEntity, String.class);
String strBody = null;
if(response.getStatusCodeValue() == 200){
strBody = response.getBody();
}
if(StrUtil.isNotEmpty(strBody)) {
userId = strBody;
try {
tokenToUserId = JSONObject.parseObject(strBody,TokenToUserId.class);
}catch (Exception e) {
throw new BaseException("返回参数异常");
}
}
if(ObjectUtil.isNotNull(tokenToUserId)){
userId = tokenToUserId.getId().toString();
}
return userId;
}

Loading…
Cancel
Save