826220679@qq.com
8 小时以前 d58d5e1baf9390d4119f48c4af3fbcf308da10a3
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package login;
 
import com.fasterxml.jackson.databind.ObjectMapper;
 
/**
 * 登录校验工具类
 * 用于验证邮箱验证码和密码
 */
public class LoginVerifier {
    
    private static final String VERIFY_CODE_API = "/api/auth/login";
    
    private final HttpClientLoca httpClient;
    private final ApiConfig config;
    private final ObjectMapper objectMapper;
    
    /**
     * 使用默认配置创建LoginVerifier
     */
    public LoginVerifier() {
        this.config = ApiConfig.getDefault();
        this.httpClient = new HttpClientLoca(config);
        this.objectMapper = new ObjectMapper();
    }
    
    /**
     * 使用自定义配置创建LoginVerifier
     */
    public LoginVerifier(ApiConfig config) {
        this.config = config != null ? config : ApiConfig.getDefault();
        this.httpClient = new HttpClientLoca(this.config);
        this.objectMapper = new ObjectMapper();
    }
    
    /**
     * 验证邮箱验证码和密码(实例方法)
     * 
     * @param email 邮箱地址
     * @param password 密码
     * @return LoginVerifyResponse 包含响应状态码和响应内容的对象
     */
    public LoginVerifyResponse verifyLoginInstance(String email,  String password) {
        // 参数校验
        String validationError = validateParams(email, password);
        if (validationError != null) {
            return LoginVerifyResponse.failure(400, validationError);
        }
        
        try {
            // 构建请求体JSON
            VerifyRequest request = new VerifyRequest(email,  password);
            String jsonData = objectMapper.writeValueAsString(request);
            
            // 发送POST请求
            String url = config.getBaseUrl() + VERIFY_CODE_API;
            ApiResponse apiResponse = httpClient.postJson(url, jsonData);
            
            // 解析响应并判断是否成功
            boolean success = false;
            String message = "验证失败";
            
            if (apiResponse.getResponseBody() != null && !apiResponse.getResponseBody().isEmpty()) {
                try {
                    LoginResponse loginResponse = objectMapper.readValue(
                            apiResponse.getResponseBody(), LoginResponse.class);
                    success = loginResponse.getCode() == 200;
                    message = loginResponse.getMessage();
                } catch (Exception e) {
                    // JSON解析失败,使用HTTP状态码判断
                    success = apiResponse.isSuccess();
                    message = success ? "验证成功" : "验证失败";
                }
            } else {
                success = apiResponse.isSuccess();
                message = apiResponse.getErrorMessage() != null ? 
                        apiResponse.getErrorMessage() : "验证失败";
            }
            
            if (success) {
                return LoginVerifyResponse.success(apiResponse.getStatusCode(), 
                        message, apiResponse.getResponseBody());
            } else {
                return LoginVerifyResponse.failure(apiResponse.getStatusCode(), message);
            }
            
        } catch (Exception e) {
            return LoginVerifyResponse.failure(500, "请求异常: " + e.getMessage());
        }
    }
    
    /**
     * 参数校验
     */
    private String validateParams(String email,  String password) {
        if (email == null || email.trim().isEmpty()) {
            return "邮箱地址不能为空";
        }
 
        if (password == null || password.trim().isEmpty()) {
            return "密码不能为空";
        }
        return null;
    }
    
    /**
     * 静态方法:使用默认配置验证登录(保持向后兼容)
     */
    public static LoginVerifyResponse verifyLogin(String email,  String password) {
        return new LoginVerifier().verifyLoginInstance(email, password);
    }
    
    /**
     * 验证请求对象
     */
    @SuppressWarnings("unused")
    private static class VerifyRequest {
        private String usernameOrEmail;
 
        private String password;
        
        public VerifyRequest(String usernameOrEmail, String password) {
            this.usernameOrEmail = usernameOrEmail;
 
            this.password = password;
        }
        
        public String getUsernameOrEmail() {
            return usernameOrEmail;
        }
        
        public void setEmail(String usernameOrEmail) {
            this.usernameOrEmail = usernameOrEmail;
        }
        
 
        
        public String getPassword() {
            return password;
        }
        
        public void setPassword(String password) {
            this.password = password;
        }
    }
    
    /**
     * 登录响应对象(对应接口文档的响应格式)
     */
    @SuppressWarnings("unused")
    private static class LoginResponse {
        private int code;
        private String message;
        private Object data;
        private long timestamp;
        
        public int getCode() {
            return code;
        }
        
        public void setCode(int code) {
            this.code = code;
        }
        
        public String getMessage() {
            return message;
        }
        
        public void setMessage(String message) {
            this.message = message;
        }
        
        public Object getData() {
            return data;
        }
        
        public void setData(Object data) {
            this.data = data;
        }
        
        public long getTimestamp() {
            return timestamp;
        }
        
        public void setTimestamp(long timestamp) {
            this.timestamp = timestamp;
        }
    }
    
    /**
     * 登录验证响应结果封装类
     */
    public static class LoginVerifyResponse {
        private final boolean success;
        private final int statusCode;
        private final String message;
        private final String responseBody;
        
        private LoginVerifyResponse(boolean success, int statusCode, String message, String responseBody) {
            this.success = success;
            this.statusCode = statusCode;
            this.message = message;
            this.responseBody = responseBody;
        }
        
        /**
         * 创建成功响应
         */
        public static LoginVerifyResponse success(int statusCode, String message, String responseBody) {
            return new LoginVerifyResponse(true, statusCode, message, responseBody);
        }
        
        /**
         * 创建失败响应
         */
        public static LoginVerifyResponse failure(int statusCode, String message) {
            return new LoginVerifyResponse(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 "LoginVerifyResponse{" +
                    "success=" + success +
                    ", statusCode=" + statusCode +
                    ", message='" + message + '\'' +
                    ", responseBody='" + responseBody + '\'' +
                    '}';
        }
    }
}