diff --git a/pom.xml b/pom.xml
index baa2950..5270bc1 100644
--- a/pom.xml
+++ b/pom.xml
@@ -69,6 +69,11 @@
javax.servlet
javax.servlet-api
+
+ cn.hutool
+ hutool-all
+ 5.8.25
+
diff --git a/src/main/java/com/lh/controller/VoteController.java b/src/main/java/com/lh/controller/VoteController.java
index 777bc0b..cb4eef6 100644
--- a/src/main/java/com/lh/controller/VoteController.java
+++ b/src/main/java/com/lh/controller/VoteController.java
@@ -9,6 +9,7 @@ import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.net.URLEncoder;
+import java.util.Map;
@CrossOrigin
@RestController
@@ -29,12 +30,14 @@ public class VoteController {
}
//获取所有候选人
@PostMapping ("/getCandidates")
- public RespBean getCandidates(@RequestBody String token) throws IOException {
+ public RespBean getCandidates(@RequestBody Map query) throws IOException {
+ String token = query.get("token");
//System.out.println(token);
////将token的值分离出来
//int startIndex = token.indexOf('=') + 1; // 找到等号的位置,并移动到等号后一位
//String tokenValue = token.substring(startIndex); // 提取等号后面的部分
- token = "token=" + URLEncoder.encode(token.substring(10, token.length() - 2), "UTF-8");
+// token = "token=" + URLEncoder.encode(token.substring(10, token.length() - 2), "UTF-8");
+ token = "token=" + URLEncoder.encode(token, "UTF-8");
System.out.println(token);
String voterJwcode = String.valueOf(new Utils().getJwcode(token));
diff --git a/src/main/java/com/lh/exception/MyException.java b/src/main/java/com/lh/exception/MyException.java
index d4e1f5b..44416ed 100644
--- a/src/main/java/com/lh/exception/MyException.java
+++ b/src/main/java/com/lh/exception/MyException.java
@@ -1,7 +1,7 @@
package com.lh.exception;
-public class MyException extends Exception{
- public MyException(String msg){
+public class MyException extends RuntimeException {
+ public MyException(String msg) {
super(msg);
}
}
\ No newline at end of file
diff --git a/src/main/java/com/lh/until/Utils.java b/src/main/java/com/lh/until/Utils.java
index 9ac6a05..eafd283 100644
--- a/src/main/java/com/lh/until/Utils.java
+++ b/src/main/java/com/lh/until/Utils.java
@@ -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) {
- // 读取响应体
- 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 (responseCode != 200) {
+ throw new MyException("Failed : HTTP error code : " + responseCode);
}
- else {
- throw new RuntimeException("Failed : HTTP error code : " + responseCode);
+
+ // 读取响应体
+ String responseBody = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
+ // 获取不到用户状态
+ if (StrUtil.isEmpty(responseBody)) {
+ throw new MyException("用户状态获取失败");
+ }
+
+ // 将token进行解析为RespBean
+ RespBean bean = JSONUtil.toBean(responseBody, RespBean.class);
+ if (bean.getCode() != 200) {
+ throw new MyException("用户状态获取失败");
}
+
+ return JSONUtil.toBean(bean.getData().toString(), TokenDTO.class);
}
}
}