张世豪
昨天 00f4e4fc6e53a26cf3dc67d57d8b00536634d707
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package login;
 
 
import com.fasterxml.jackson.databind.ObjectMapper;
 
/**
 * 找回密码工具类
 * 用于用户找回密码功能
 */
public class PasswordReset {
    
    private static final String RESET_PASSWORD_API = "/api/auth/reset-password";
    
    private final HttpClientLoca httpClient;
    private final ApiConfig config;
    private final ObjectMapper objectMapper;
    
    /**
     * 使用默认配置创建PasswordReset
     */
    public PasswordReset() {
        this.config = ApiConfig.getDefault();
        this.httpClient = new HttpClientLoca(config);
        this.objectMapper = new ObjectMapper();
    }
    
    /**
     * 使用自定义配置创建PasswordReset
     */
    public PasswordReset(ApiConfig config) {
        this.config = config != null ? config : ApiConfig.getDefault();
        this.httpClient = new HttpClientLoca(this.config);
        this.objectMapper = new ObjectMapper();
    }
    
    /**
     * 找回密码(实例方法)
     * 
     * @param email 邮箱地址
     * @param code 验证码(6位数字)
     * @param newPassword 新密码
     * @param confirmPassword 确认密码
     * @return ResetPasswordResponse 包含响应状态码和响应内容的对象
     */
    public ResetPasswordResponse resetPasswordInstance(String email, String code, String newPassword, String confirmPassword) {
        // 参数校验
        String validationError = validateParams(email, code, newPassword, confirmPassword);
        if (validationError != null) {
            return ResetPasswordResponse.failure(400, validationError);
        }
        
        try {
            // 构建请求体JSON
            ResetPasswordRequest request = new ResetPasswordRequest(email, code, newPassword, confirmPassword);
            String jsonData = objectMapper.writeValueAsString(request);
            
            // 发送POST请求
            String url = config.getBaseUrl() + RESET_PASSWORD_API;
            ApiResponse apiResponse = httpClient.postJson(url, jsonData);
            
            // 解析响应并判断是否成功
            boolean success = false;
            String message = "密码重置失败";
            
            if (apiResponse.getResponseBody() != null && !apiResponse.getResponseBody().isEmpty()) {
                try {
                    ResetPasswordApiResponse resetResponse = objectMapper.readValue(
                            apiResponse.getResponseBody(), ResetPasswordApiResponse.class);
                    success = resetResponse.getCode() == 200;
                    message = resetResponse.getMessage();
                } catch (Exception e) {
                    // JSON解析失败,使用HTTP状态码判断
                    success = apiResponse.isSuccess();
                    message = success ? "密码重置成功" : "密码重置失败";
                }
            } else {
                success = apiResponse.isSuccess();
                message = apiResponse.getErrorMessage() != null ? 
                        apiResponse.getErrorMessage() : "密码重置失败";
            }
            
            if (success) {
                return ResetPasswordResponse.success(apiResponse.getStatusCode(), 
                        message, apiResponse.getResponseBody());
            } else {
                return ResetPasswordResponse.failure(apiResponse.getStatusCode(), message);
            }
            
        } catch (Exception e) {
            return ResetPasswordResponse.failure(500, "请求异常: " + e.getMessage());
        }
    }
    
    /**
     * 参数校验
     */
    private String validateParams(String email, String code, String newPassword, String confirmPassword) {
        if (email == null || email.trim().isEmpty()) {
            return "邮箱地址不能为空";
        }
        if (code == null || code.trim().isEmpty()) {
            return "验证码不能为空";
        }
        if (newPassword == null || newPassword.trim().isEmpty()) {
            return "新密码不能为空";
        }
        if (confirmPassword == null || confirmPassword.trim().isEmpty()) {
            return "确认密码不能为空";
        }
        if (!newPassword.equals(confirmPassword)) {
            return "新密码和确认密码不一致";
        }
        return null;
    }
    
    /**
     * 静态方法:使用默认配置重置密码(保持向后兼容)
     */
    public static ResetPasswordResponse resetPassword(String email, String code, String newPassword, String confirmPassword) {
        return new PasswordReset().resetPasswordInstance(email, code, newPassword, confirmPassword);
    }
    
    /**
     * 重置密码请求对象
     */
    @SuppressWarnings("unused")
    private static class ResetPasswordRequest {
        private String email;
        private String code;
        private String newPassword;
        private String confirmPassword;
        
        public ResetPasswordRequest(String email, String code, String newPassword, String confirmPassword) {
            this.email = email;
            this.code = code;
            this.newPassword = newPassword;
            this.confirmPassword = confirmPassword;
        }
        
        public String getEmail() {
            return email;
        }
        
        public void setEmail(String email) {
            this.email = email;
        }
        
        public String getCode() {
            return code;
        }
        
        public void setCode(String code) {
            this.code = code;
        }
        
        public String getNewPassword() {
            return newPassword;
        }
        
        public void setNewPassword(String newPassword) {
            this.newPassword = newPassword;
        }
        
        public String getConfirmPassword() {
            return confirmPassword;
        }
        
        public void setConfirmPassword(String confirmPassword) {
            this.confirmPassword = confirmPassword;
        }
    }
    
    /**
     * 重置密码响应对象(对应接口文档的响应格式)
     */
    @SuppressWarnings("unused")
    private static class ResetPasswordApiResponse {
        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 ResetPasswordResponse {
        private final boolean success;
        private final int statusCode;
        private final String message;
        private final String responseBody;
        
        private ResetPasswordResponse(boolean success, int statusCode, String message, String responseBody) {
            this.success = success;
            this.statusCode = statusCode;
            this.message = message;
            this.responseBody = responseBody;
        }
        
        /**
         * 创建成功响应
         */
        public static ResetPasswordResponse success(int statusCode, String message, String responseBody) {
            return new ResetPasswordResponse(true, statusCode, message, responseBody);
        }
        
        /**
         * 创建失败响应
         */
        public static ResetPasswordResponse failure(int statusCode, String message) {
            return new ResetPasswordResponse(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 "ResetPasswordResponse{" +
                    "success=" + success +
                    ", statusCode=" + statusCode +
                    ", message='" + message + '\'' +
                    ", responseBody='" + responseBody + '\'' +
                    '}';
        }
    }
}