|
|
@ -1,31 +1,117 @@ |
|
|
|
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 com.alibaba.fastjson.JSONArray; |
|
|
|
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 wiki.tall.ccmq.common.util.JsonResponse; |
|
|
|
import wiki.tall.ccmq.common.util.WebConstant; |
|
|
|
|
|
|
|
import java.net.URI; |
|
|
|
import java.net.URISyntaxException; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
/** |
|
|
|
* @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(WebConstant.HEADER_KEY_TOKEN_PREFIX + token); |
|
|
|
|
|
|
|
String url = "http://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(); |
|
|
|
System.out.println("认证接口返回:" + strBody); |
|
|
|
} |
|
|
|
|
|
|
|
JSONObject jsonObject = JSONObject.parseObject(strBody); |
|
|
|
//请求正确返回则,否则无操作
|
|
|
|
Integer code = jsonObject.getInteger("code"); |
|
|
|
if (code == null || code != 200) { |
|
|
|
throw new BaseException("返回参数异常"); |
|
|
|
} |
|
|
|
String data = jsonObject.getString("data"); |
|
|
|
if(ObjectUtil.isNotNull(data)){ |
|
|
|
try { |
|
|
|
tokenToUserId = JSON.parseObject(data, TokenToUserId.class); |
|
|
|
}catch (Exception e){ |
|
|
|
throw new BaseException("返回参数异常"); |
|
|
|
} |
|
|
|
} |
|
|
|
if(StrUtil.isNotEmpty(strBody)) { |
|
|
|
userId = strBody; |
|
|
|
if(ObjectUtil.isNotNull(tokenToUserId)){ |
|
|
|
System.out.println("返回的用户信息:" + tokenToUserId); |
|
|
|
userId = tokenToUserId.getId().toString(); |
|
|
|
} |
|
|
|
return userId; |
|
|
|
} |
|
|
|