zhitong.yu
2024-12-27 21e0b93688de2a98abe3b7b9c0cfed6efdc21183
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package com.hxzk.controller;
 
import cloud.tianai.captcha.application.ImageCaptchaApplication;
import cloud.tianai.captcha.application.vo.CaptchaResponse;
import cloud.tianai.captcha.application.vo.ImageCaptchaVO;
import cloud.tianai.captcha.common.constant.CaptchaTypeConstant;
import cloud.tianai.captcha.common.response.ApiResponse;
import cloud.tianai.captcha.validator.common.model.dto.ImageCaptchaTrack;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
 
import javax.servlet.http.HttpServletRequest;
import javax.xml.ws.Response;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Collections;
import java.util.concurrent.ThreadLocalRandom;
 
@RestController
@RequestMapping("/")
public class CodeController {
    @Autowired
    private RestTemplate restTemplate;
 
    @PostMapping("/code")
    @ResponseBody
    public CaptchaResponse<ImageCaptchaVO> genCaptcha(HttpServletRequest request, @RequestParam(value = "type", required = false) String type) {
        // 构建请求参数
        HttpEntity<String> entity = new HttpEntity<>("Parameters", null);
        ResponseEntity<CaptchaResponse<ImageCaptchaVO>> response = restTemplate.exchange(
                "http://localhost:8086/gen",
                HttpMethod.GET,
                entity,
                new ParameterizedTypeReference<CaptchaResponse<ImageCaptchaVO>>() {},
                "type", type
        );
 
        // 返回结果
        return response.getBody();
    }
 
    @PostMapping("/checkCode")
    public ApiResponse<?> checkCaptcha(@RequestBody Data data) {
        // 远端 check 服务的 URL
        String url = "http://localhost:8086/check";
 
        // 发送 POST 请求到远端服务
        ApiResponse<?> response = restTemplate.postForObject(url, data, ApiResponse.class);
 
        if (response.isSuccess()) {
            return ApiResponse.ofSuccess(Collections.singletonMap("id", data.getId()));
        }
        return response;
    }
 
 
 
    @lombok.Data
    public static class Data {
        private String  id;
        private ImageCaptchaTrack data;
    }
}