diff --git a/pom.xml b/pom.xml index c91a2fb..9a594a3 100644 --- a/pom.xml +++ b/pom.xml @@ -58,6 +58,22 @@ org.springframework.kafka spring-kafka + + + org.apache.httpcomponents + httpclient + + + + + com.fasterxml.jackson.core + jackson-databind + + + javax.servlet + javax.servlet-api + 9.0.41 + diff --git a/src/main/java/com/lh/bean/dto/TokenDTO.java b/src/main/java/com/lh/bean/dto/TokenDTO.java new file mode 100644 index 0000000..10618db --- /dev/null +++ b/src/main/java/com/lh/bean/dto/TokenDTO.java @@ -0,0 +1,13 @@ +package com.lh.bean.dto; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class TokenDTO { + private int jwcode; + private String username; +} diff --git a/src/main/java/com/lh/controller/VoteController.java b/src/main/java/com/lh/controller/VoteController.java index 7f7c8da..0da9a16 100644 --- a/src/main/java/com/lh/controller/VoteController.java +++ b/src/main/java/com/lh/controller/VoteController.java @@ -1,29 +1,40 @@ package com.lh.controller; import com.lh.bean.RespBean; -import com.lh.bean.Voter; import com.lh.service.VoteService; +import com.lh.until.Utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; +import java.io.IOException; + @CrossOrigin @RestController @RequestMapping public class VoteController { @Autowired private VoteService voteService; - //投票 - @PostMapping("/vote") - public RespBean vote(@RequestBody Voter voter) throws Exception { - String voterJwcode = voter.getJwCode(); - String candidateJwcode = voter.getCandidateJwCode(); - String voterName = voter.getName(); + //投票. + + @PostMapping("/vote/{candidateJwcode}") + public RespBean vote(@PathVariable("candidateJwcode") String candidateJwcode,@RequestBody String token) throws Exception { + token = "token=" + token.substring(0, token.length() - 1); + Utils utils = new Utils(); + String voterJwcode = String.valueOf(utils.getJwcode(token)); + String voterName = utils.getUsername(token); Integer result =voteService.insertVote(voterJwcode, candidateJwcode, voterName); return RespBean.ok("投票成功!今日还可以投" + result + "次"); } //获取所有候选人 - @GetMapping ("/getCandidates/{voterJwcode}") - public RespBean getCandidates(@PathVariable("voterJwcode") String voterJwcode) { + @PostMapping ("/getCandidates") + public RespBean getCandidates(@RequestBody String token) throws IOException { + //System.out.println(token); + ////将token的值分离出来 + //int startIndex = token.indexOf('=') + 1; // 找到等号的位置,并移动到等号后一位 + //String tokenValue = token.substring(startIndex); // 提取等号后面的部分 + token = token.substring(10, token.length() - 2); + System.out.println(token); + String voterJwcode = String.valueOf(new Utils().getJwcode(token)); return RespBean.ok("获取成功",voteService.getCandidates(voterJwcode)); } diff --git a/src/main/java/com/lh/until/Utils.java b/src/main/java/com/lh/until/Utils.java new file mode 100644 index 0000000..9ac6a05 --- /dev/null +++ b/src/main/java/com/lh/until/Utils.java @@ -0,0 +1,72 @@ +package com.lh.until; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.lh.bean.dto.TokenDTO; +import lombok.extern.slf4j.Slf4j; +import org.apache.http.client.methods.CloseableHttpResponse; +import org.apache.http.client.methods.HttpPost; +import org.apache.http.entity.ContentType; +import org.apache.http.entity.StringEntity; +import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.http.impl.client.HttpClients; +import org.apache.http.util.EntityUtils; +import org.springframework.data.redis.core.StringRedisTemplate; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +@Slf4j +@Component +public class Utils { + @Resource + private StringRedisTemplate stringRedisTemplate; + // 获取token中的信息 + public TokenDTO analysisToken(String token) throws IOException { + // 编码Token + String url = "http://39.101.133.168:8828/hljw/api/v2/member/info"; + // 创建HttpClient实例 + try (CloseableHttpClient httpClient = HttpClients.createDefault()) { + // 创建POST请求 + HttpPost postRequest = new HttpPost(url); + postRequest.setHeader("Content-Type", "application/x-www-form-urlencoded"); + System.out.println(token); + // 设置请求体参数 + StringEntity entity = new StringEntity(token, ContentType.APPLICATION_FORM_URLENCODED); + postRequest.setEntity(entity); + // 发送请求并获取响应 + 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; + } + else { + throw new RuntimeException("Failed : HTTP error code : " + responseCode); + } + } + } + } + + // 获取token中的jwcode + public Integer getJwcode(String token) throws IOException { + TokenDTO dto = analysisToken(token); + return dto.getJwcode(); + } + public String getUsername(String token) throws IOException { + TokenDTO dto = analysisToken(token); + return dto.getUsername(); + } + +}