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