|
|
@ -1,8 +1,12 @@ |
|
|
|
package com.lh.until; |
|
|
|
|
|
|
|
import cn.hutool.core.util.StrUtil; |
|
|
|
import cn.hutool.json.JSONUtil; |
|
|
|
import com.fasterxml.jackson.databind.JsonNode; |
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper; |
|
|
|
import com.lh.bean.RespBean; |
|
|
|
import com.lh.bean.dto.TokenDTO; |
|
|
|
import com.lh.exception.MyException; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.http.client.methods.CloseableHttpResponse; |
|
|
|
import org.apache.http.client.methods.HttpPost; |
|
|
@ -17,6 +21,7 @@ import org.springframework.stereotype.Component; |
|
|
|
import javax.annotation.Resource; |
|
|
|
import java.io.IOException; |
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Component |
|
|
@ -40,21 +45,24 @@ public class Utils { |
|
|
|
try (CloseableHttpResponse response = httpClient.execute(postRequest)) { |
|
|
|
int responseCode = response.getStatusLine().getStatusCode(); // 获取状态码 |
|
|
|
// 检查响应状态 |
|
|
|
if (responseCode == 200) { |
|
|
|
if (responseCode != 200) { |
|
|
|
throw new MyException("Failed : HTTP error code : " + responseCode); |
|
|
|
} |
|
|
|
|
|
|
|
// 读取响应体 |
|
|
|
String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8); |
|
|
|
// 使用Jackson解析JSON响应体 |
|
|
|
ObjectMapper objectMapper = new ObjectMapper(); |
|
|
|
JsonNode jsonResponse = objectMapper.readTree(responseBody); |
|
|
|
JsonNode dataNode = jsonResponse.get("data"); |
|
|
|
TokenDTO tokenDTO = new TokenDTO(); |
|
|
|
tokenDTO.setJwcode(dataNode.get("jwcode").asInt()); |
|
|
|
tokenDTO.setUsername(dataNode.get("username").asText()); |
|
|
|
return tokenDTO; |
|
|
|
// 获取不到用户状态 |
|
|
|
if (StrUtil.isEmpty(responseBody)) { |
|
|
|
throw new MyException("用户状态获取失败"); |
|
|
|
} |
|
|
|
else { |
|
|
|
throw new RuntimeException("Failed : HTTP error code : " + responseCode); |
|
|
|
|
|
|
|
// 将token进行解析为RespBean |
|
|
|
RespBean bean = JSONUtil.toBean(responseBody, RespBean.class); |
|
|
|
if (bean.getCode() != 200) { |
|
|
|
throw new MyException("用户状态获取失败"); |
|
|
|
} |
|
|
|
|
|
|
|
return JSONUtil.toBean(bean.getData().toString(), TokenDTO.class); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|