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 + '\'' +
|
'}';
|
}
|
}
|
}
|