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