826220679@qq.com
14 小时以前 d655e19ad18eea30f3627fc1c499ae9fe4200d92
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package login;
 
import java.util.HashMap;
import java.util.Map;
 
/**
 * 邮箱验证码发送工具类
 * 用于向指定邮箱地址发送验证码
 * 邮箱地址通过URL参数传递,请求体为空
 */
public class EmailCodeSender {
    
    private static final String SEND_CODE_API = "/api/email/send-code";
    
    private final HttpClientLoca httpClient;
    private final ApiConfig config;
    
    /**
     * 使用默认配置创建EmailCodeSender
     */
    public EmailCodeSender() {
        this.config = ApiConfig.getDefault();
        this.httpClient = new HttpClientLoca(config);
    }
    
    /**
     * 使用自定义配置创建EmailCodeSender
     */
    public EmailCodeSender(ApiConfig config) {
        this.config = config != null ? config : ApiConfig.getDefault();
        this.httpClient = new HttpClientLoca(this.config);
    }
    
    /**
     * 发送邮箱验证码请求(实例方法)
     * 邮箱地址通过URL参数传递,格式:/api/email/send-code?email=xxx
     * 请求体为空
     * 
     * @param email 邮箱地址
     * @return EmailCodeResponse 包含响应状态码和响应内容的对象
     */
    public EmailCodeResponse sendEmailCodeInstance(String email) {
        // 参数校验
        if (email == null || email.trim().isEmpty()) {
            return EmailCodeResponse.failure(400, "邮箱地址不能为空");
        }
        
        try {
            // 构建URL参数
            Map<String, String> params = new HashMap<>();
            params.put("email", email);
            
            // 发送POST请求(URL参数,请求体为空)
            String url = config.getBaseUrl() + SEND_CODE_API;
            ApiResponse apiResponse = httpClient.post(url, params);
            
            // 转换为EmailCodeResponse
            if (apiResponse.isSuccess()) {
                return EmailCodeResponse.success(apiResponse.getStatusCode(), 
                        "验证码发送成功", apiResponse.getResponseBody());
            } else {
                return EmailCodeResponse.failure(apiResponse.getStatusCode(), 
                        apiResponse.getErrorMessage() != null ? 
                        apiResponse.getErrorMessage() : "验证码发送失败");
            }
        } catch (Exception e) {
            return EmailCodeResponse.failure(500, "请求异常: " + e.getMessage());
        }
    }
    
    /**
     * 静态方法:使用默认配置发送验证码(保持向后兼容)
     */
    public static EmailCodeResponse sendEmailCode(String email) {
        return new EmailCodeSender().sendEmailCodeInstance(email);
    }
    
    /**
     * 邮箱验证码响应结果封装类
     */
    public static class EmailCodeResponse {
        private final boolean success;
        private final int statusCode;
        private final String message;
        private final String responseBody;
        
        private EmailCodeResponse(boolean success, int statusCode, String message, String responseBody) {
            this.success = success;
            this.statusCode = statusCode;
            this.message = message;
            this.responseBody = responseBody;
        }
        
        /**
         * 创建成功响应
         */
        public static EmailCodeResponse success(int statusCode, String message, String responseBody) {
            return new EmailCodeResponse(true, statusCode, message, responseBody);
        }
        
        /**
         * 创建失败响应
         */
        public static EmailCodeResponse failure(int statusCode, String message) {
            return new EmailCodeResponse(false, statusCode, message, null);
        }
        
        public boolean isSuccess() {
            return success;
        }
        
        public int getStatusCode() {
            return statusCode;
        }
        
        public String getMessage() {
            return message;
        }
        
        public String getResponseBody() {
            return responseBody;
        }
        
        @Override
        public String toString() {
            return "EmailCodeResponse{" +
                    "success=" + success +
                    ", statusCode=" + statusCode +
                    ", message='" + message + '\'' +
                    ", responseBody='" + responseBody + '\'' +
                    '}';
        }
    }
}