feat: 短信服务
This commit is contained in:
@@ -0,0 +1,21 @@
|
||||
package com.central;
|
||||
|
||||
import com.central.common.lb.annotation.EnableFeignInterceptor;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
/**
|
||||
* 消息中心启动类
|
||||
* @author system
|
||||
*/
|
||||
@EnableDiscoveryClient
|
||||
@EnableTransactionManagement
|
||||
@EnableFeignInterceptor
|
||||
@SpringBootApplication
|
||||
public class MsgCenterApp {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(MsgCenterApp.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package com.central.msg.config;
|
||||
|
||||
import com.central.sms.config.SmsProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* 短信配置类
|
||||
* @author system
|
||||
*/
|
||||
@Configuration
|
||||
@EnableConfigurationProperties(SmsProperties.class)
|
||||
public class SmsConfig {
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
package com.central.msg.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.common.model.Result;
|
||||
import com.central.msg.model.dto.SmsRequest;
|
||||
import com.central.msg.model.dto.SmsResponse;
|
||||
import com.central.msg.model.entity.SmsSendLog;
|
||||
import com.central.msg.service.MsgSmsService;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.validation.Valid;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
/**
|
||||
* 短信发送Controller
|
||||
* @author system
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/sms")
|
||||
@Tag(name = "短信服务", description = "短信发送相关接口")
|
||||
public class SmsController {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(SmsController.class);
|
||||
|
||||
@Autowired
|
||||
private MsgSmsService msgSmsService;
|
||||
|
||||
/**
|
||||
* 发送短信(使用默认服务商)
|
||||
*/
|
||||
@PostMapping("/send")
|
||||
@Operation(summary = "发送短信", description = "使用默认服务商发送模板短信")
|
||||
public Result<SmsResponse> sendSms(@Valid @RequestBody SmsRequest request) {
|
||||
logger.info("收到短信发送请求: {}", request);
|
||||
|
||||
try {
|
||||
boolean success = msgSmsService.sendSms(
|
||||
request.getPhone(),
|
||||
request.getTemplateId(),
|
||||
request.getParams()
|
||||
);
|
||||
|
||||
if (success) {
|
||||
SmsResponse response = new SmsResponse(true, "短信发送成功");
|
||||
return Result.succeed(response);
|
||||
} else {
|
||||
SmsResponse response = new SmsResponse(false, "短信发送失败");
|
||||
return Result.failed(response,"短信发送失败!");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("短信发送异常: {}", e.getMessage(), e);
|
||||
SmsResponse response = new SmsResponse(false, "短信发送异常: " + e.getMessage());
|
||||
return Result.failed(response,"短信发送失败!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送阿里云短信
|
||||
*/
|
||||
@PostMapping("/aliyun/send")
|
||||
@Operation(summary = "发送阿里云短信", description = "使用阿里云发送模板短信")
|
||||
public Result<SmsResponse> sendAliyunSms(@Valid @RequestBody SmsRequest request) {
|
||||
logger.info("收到阿里云短信发送请求: {}", request);
|
||||
|
||||
try {
|
||||
boolean success = msgSmsService.sendAliyunSms(
|
||||
request.getPhone(),
|
||||
request.getTemplateId(),
|
||||
request.getParams()
|
||||
);
|
||||
|
||||
if (success) {
|
||||
SmsResponse response = new SmsResponse(true, "阿里云短信发送成功");
|
||||
return Result.succeed(response);
|
||||
} else {
|
||||
SmsResponse response = new SmsResponse(false, "阿里云短信发送失败");
|
||||
return Result.failed(response,"短信发送失败!");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("阿里云短信发送异常: {}", e.getMessage(), e);
|
||||
SmsResponse response = new SmsResponse(false, "阿里云短信发送异常: " + e.getMessage());
|
||||
return Result.failed(response,"短信发送失败!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送腾讯云短信
|
||||
*/
|
||||
@PostMapping("/tencent/send")
|
||||
@Operation(summary = "发送腾讯云短信", description = "使用腾讯云发送模板短信")
|
||||
public Result<SmsResponse> sendTencentSms(@Valid @RequestBody SmsRequest request) {
|
||||
logger.info("收到腾讯云短信发送请求: {}", request);
|
||||
|
||||
try {
|
||||
boolean success = msgSmsService.sendTencentSms(
|
||||
request.getPhone(),
|
||||
request.getTemplateId(),
|
||||
request.getParams()
|
||||
);
|
||||
|
||||
if (success) {
|
||||
SmsResponse response = new SmsResponse(true, "腾讯云短信发送成功");
|
||||
return Result.succeed(response);
|
||||
} else {
|
||||
SmsResponse response = new SmsResponse(false, "腾讯云短信发送失败");
|
||||
return Result.failed(response,"短信发送失败!");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("腾讯云短信发送异常: {}", e.getMessage(), e);
|
||||
SmsResponse response = new SmsResponse(false, "腾讯云短信发送异常: " + e.getMessage());
|
||||
return Result.failed(response,"短信发送失败!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送验证码(使用默认服务商)
|
||||
*/
|
||||
@PostMapping("/send-verify-code")
|
||||
@Operation(summary = "发送验证码", description = "使用默认服务商发送验证码短信")
|
||||
public Result<SmsResponse> sendVerifyCode(
|
||||
@Parameter(description = "手机号", required = true) @RequestParam("phone") String phone,
|
||||
@Parameter(description = "验证码", required = true) @RequestParam("code") String code) {
|
||||
logger.info("收到验证码发送请求: phone={}, code={}", phone, code);
|
||||
|
||||
try {
|
||||
boolean success = msgSmsService.sendVerifyCode(phone, code);
|
||||
|
||||
if (success) {
|
||||
SmsResponse response = new SmsResponse(true, "验证码发送成功");
|
||||
return Result.succeed(response);
|
||||
} else {
|
||||
SmsResponse response = new SmsResponse(false, "验证码发送失败");
|
||||
return Result.failed(response,"短信发送失败!");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("验证码发送异常: {}", e.getMessage(), e);
|
||||
SmsResponse response = new SmsResponse(false, "验证码发送异常: " + e.getMessage());
|
||||
return Result.failed(response,"短信发送失败!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送阿里云验证码
|
||||
*/
|
||||
@PostMapping("/aliyun/send-verify-code")
|
||||
@Operation(summary = "发送阿里云验证码", description = "使用阿里云发送验证码短信")
|
||||
public Result<SmsResponse> sendAliyunVerifyCode(
|
||||
@Parameter(description = "手机号", required = true) @RequestParam("phone") String phone,
|
||||
@Parameter(description = "验证码", required = true) @RequestParam("code") String code) {
|
||||
logger.info("收到阿里云验证码发送请求: phone={}, code={}", phone, code);
|
||||
|
||||
try {
|
||||
boolean success = msgSmsService.sendAliyunVerifyCode(phone, code);
|
||||
|
||||
if (success) {
|
||||
SmsResponse response = new SmsResponse(true, "阿里云验证码发送成功");
|
||||
return Result.succeed(response);
|
||||
} else {
|
||||
SmsResponse response = new SmsResponse(false, "阿里云验证码发送失败");
|
||||
return Result.failed(response,"短信发送失败!");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("阿里云验证码发送异常: {}", e.getMessage(), e);
|
||||
SmsResponse response = new SmsResponse(false, "阿里云验证码发送异常: " + e.getMessage());
|
||||
return Result.failed(response,"短信发送失败!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送腾讯云验证码
|
||||
*/
|
||||
@PostMapping("/tencent/send-verify-code")
|
||||
@Operation(summary = "发送腾讯云验证码", description = "使用腾讯云发送验证码短信")
|
||||
public Result<SmsResponse> sendTencentVerifyCode(
|
||||
@Parameter(description = "手机号", required = true) @RequestParam("phone") String phone,
|
||||
@Parameter(description = "验证码", required = true) @RequestParam("code") String code) {
|
||||
logger.info("收到腾讯云验证码发送请求: phone={}, code={}", phone, code);
|
||||
|
||||
try {
|
||||
boolean success = msgSmsService.sendTencentVerifyCode(phone, code);
|
||||
|
||||
if (success) {
|
||||
SmsResponse response = new SmsResponse(true, "腾讯云验证码发送成功");
|
||||
return Result.succeed(response);
|
||||
} else {
|
||||
SmsResponse response = new SmsResponse(false, "腾讯云验证码发送失败");
|
||||
return Result.failed(response,"短信发送失败!");
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.error("腾讯云验证码发送异常: {}", e.getMessage(), e);
|
||||
SmsResponse response = new SmsResponse(false, "腾讯云验证码发送异常: " + e.getMessage());
|
||||
return Result.failed(response,"短信发送失败!");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询短信发送记录
|
||||
*/
|
||||
@GetMapping("/logs")
|
||||
@Operation(summary = "查询短信发送记录", description = "分页查询短信发送记录")
|
||||
public Result<Page<SmsSendLog>> querySmsSendLog(
|
||||
@Parameter(description = "当前页", required = true) @RequestParam(value = "page", defaultValue = "1") int page,
|
||||
@Parameter(description = "每页大小", required = true) @RequestParam(value = "size", defaultValue = "10") int size,
|
||||
@Parameter(description = "手机号") @RequestParam(value = "phone", required = false) String phone,
|
||||
@Parameter(description = "状态: 0-失败, 1-成功, 2-发送中") @RequestParam(value = "status", required = false) Integer status) {
|
||||
logger.info("查询短信发送记录: page={}, size={}, phone={}, status={}", page, size, phone, status);
|
||||
|
||||
try {
|
||||
Page<SmsSendLog> result = msgSmsService.querySmsSendLog(page, size, phone, status);
|
||||
return Result.succeed(result);
|
||||
} catch (Exception e) {
|
||||
logger.error("查询短信发送记录异常: {}", e.getMessage(), e);
|
||||
return Result.failed("查询失败: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.central.msg.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.central.msg.model.entity.SmsSendLog;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
|
||||
/**
|
||||
* 短信发送记录Mapper
|
||||
* @author system
|
||||
*/
|
||||
@Mapper
|
||||
public interface SmsSendLogMapper extends BaseMapper<SmsSendLog> {
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package com.central.msg.model.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import jakarta.validation.constraints.NotBlank;
|
||||
import jakarta.validation.constraints.Pattern;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 短信发送请求DTO
|
||||
* @author system
|
||||
*/
|
||||
@Schema(description = "短信发送请求")
|
||||
public class SmsRequest {
|
||||
|
||||
@Schema(description = "手机号", required = true, example = "13800138000")
|
||||
@NotBlank(message = "手机号不能为空")
|
||||
@Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
|
||||
private String phone;
|
||||
|
||||
@Schema(description = "短信模板ID", required = true, example = "123456")
|
||||
@NotBlank(message = "模板ID不能为空")
|
||||
private String templateId;
|
||||
|
||||
@Schema(description = "模板参数", example = "{\"code\":\"1234\",\"time\":\"5\"}")
|
||||
private Map<String, String> params;
|
||||
|
||||
public String getPhone() {
|
||||
return phone;
|
||||
}
|
||||
|
||||
public void setPhone(String phone) {
|
||||
this.phone = phone;
|
||||
}
|
||||
|
||||
public String getTemplateId() {
|
||||
return templateId;
|
||||
}
|
||||
|
||||
public void setTemplateId(String templateId) {
|
||||
this.templateId = templateId;
|
||||
}
|
||||
|
||||
public Map<String, String> getParams() {
|
||||
return params;
|
||||
}
|
||||
|
||||
public void setParams(Map<String, String> params) {
|
||||
this.params = params;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SmsRequest{" +
|
||||
"phone='" + phone + '\'' +
|
||||
", templateId='" + templateId + '\'' +
|
||||
", params=" + params +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package com.central.msg.model.dto;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
|
||||
/**
|
||||
* 短信发送响应DTO
|
||||
* @author system
|
||||
*/
|
||||
@Schema(description = "短信发送响应")
|
||||
public class SmsResponse {
|
||||
|
||||
@Schema(description = "是否成功")
|
||||
private boolean success;
|
||||
|
||||
@Schema(description = "消息")
|
||||
private String message;
|
||||
|
||||
@Schema(description = "请求ID")
|
||||
private String requestId;
|
||||
|
||||
public SmsResponse() {
|
||||
}
|
||||
|
||||
public SmsResponse(boolean success, String message) {
|
||||
this.success = success;
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public SmsResponse(boolean success, String message, String requestId) {
|
||||
this.success = success;
|
||||
this.message = message;
|
||||
this.requestId = requestId;
|
||||
}
|
||||
|
||||
public boolean isSuccess() {
|
||||
return success;
|
||||
}
|
||||
|
||||
public void setSuccess(boolean success) {
|
||||
this.success = success;
|
||||
}
|
||||
|
||||
public String getMessage() {
|
||||
return message;
|
||||
}
|
||||
|
||||
public void setMessage(String message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
public String getRequestId() {
|
||||
return requestId;
|
||||
}
|
||||
|
||||
public void setRequestId(String requestId) {
|
||||
this.requestId = requestId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "SmsResponse{" +
|
||||
"success=" + success +
|
||||
", message='" + message + '\'' +
|
||||
", requestId='" + requestId + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
package com.central.msg.model.entity;
|
||||
|
||||
import com.baomidou.mybatisplus.annotation.IdType;
|
||||
import com.baomidou.mybatisplus.annotation.TableId;
|
||||
import com.baomidou.mybatisplus.annotation.TableName;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 短信发送记录实体
|
||||
* @author system
|
||||
*/
|
||||
@Data
|
||||
@TableName("sms_send_log")
|
||||
public class SmsSendLog implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 主键ID
|
||||
*/
|
||||
@TableId(value = "id", type = IdType.AUTO)
|
||||
private Long id;
|
||||
|
||||
/**
|
||||
* 手机号
|
||||
*/
|
||||
private String phone;
|
||||
|
||||
/**
|
||||
* 短信模板ID
|
||||
*/
|
||||
private String templateId;
|
||||
|
||||
/**
|
||||
* 模板参数(JSON格式)
|
||||
*/
|
||||
private String templateParams;
|
||||
|
||||
/**
|
||||
* 短信内容
|
||||
*/
|
||||
private String content;
|
||||
|
||||
/**
|
||||
* 发送状态: 0-失败, 1-成功, 2-发送中
|
||||
*/
|
||||
private Integer status;
|
||||
|
||||
/**
|
||||
* 服务商: TENCENT, ALIYUN
|
||||
*/
|
||||
private String provider;
|
||||
|
||||
/**
|
||||
* 服务商返回的请求ID
|
||||
*/
|
||||
private String requestId;
|
||||
|
||||
/**
|
||||
* 服务商返回的状态码
|
||||
*/
|
||||
private String responseCode;
|
||||
|
||||
/**
|
||||
* 服务商返回的消息
|
||||
*/
|
||||
private String responseMessage;
|
||||
|
||||
/**
|
||||
* 错误信息
|
||||
*/
|
||||
private String errorMessage;
|
||||
|
||||
/**
|
||||
* 租户ID
|
||||
*/
|
||||
private String tenantId;
|
||||
|
||||
/**
|
||||
* 创建时间
|
||||
*/
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
* 更新时间
|
||||
*/
|
||||
private Date updateTime;
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
package com.central.msg.service;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.msg.model.entity.SmsSendLog;
|
||||
import com.central.sms.enums.SmsProviderType;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 消息中心短信服务接口
|
||||
* @author system
|
||||
*/
|
||||
public interface MsgSmsService {
|
||||
|
||||
/**
|
||||
* 发送短信(使用默认服务商)
|
||||
* @param phone 手机号
|
||||
* @param templateId 模板ID
|
||||
* @param params 模板参数
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
boolean sendSms(String phone, String templateId, Map<String, String> params);
|
||||
|
||||
/**
|
||||
* 发送短信(指定服务商)
|
||||
* @param phone 手机号
|
||||
* @param templateId 模板ID
|
||||
* @param params 模板参数
|
||||
* @param provider 服务商类型
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
boolean sendSms(String phone, String templateId, Map<String, String> params, SmsProviderType provider);
|
||||
|
||||
/**
|
||||
* 发送阿里云短信
|
||||
* @param phone 手机号
|
||||
* @param templateId 模板ID
|
||||
* @param params 模板参数
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
boolean sendAliyunSms(String phone, String templateId, Map<String, String> params);
|
||||
|
||||
/**
|
||||
* 发送腾讯云短信
|
||||
* @param phone 手机号
|
||||
* @param templateId 模板ID
|
||||
* @param params 模板参数
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
boolean sendTencentSms(String phone, String templateId, Map<String, String> params);
|
||||
|
||||
/**
|
||||
* 发送验证码短信(使用默认服务商)
|
||||
* @param phone 手机号
|
||||
* @param code 验证码
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
boolean sendVerifyCode(String phone, String code);
|
||||
|
||||
/**
|
||||
* 发送验证码短信(指定服务商)
|
||||
* @param phone 手机号
|
||||
* @param code 验证码
|
||||
* @param provider 服务商类型
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
boolean sendVerifyCode(String phone, String code, SmsProviderType provider);
|
||||
|
||||
/**
|
||||
* 发送阿里云验证码
|
||||
* @param phone 手机号
|
||||
* @param code 验证码
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
boolean sendAliyunVerifyCode(String phone, String code);
|
||||
|
||||
/**
|
||||
* 发送腾讯云验证码
|
||||
* @param phone 手机号
|
||||
* @param code 验证码
|
||||
* @return 是否发送成功
|
||||
*/
|
||||
boolean sendTencentVerifyCode(String phone, String code);
|
||||
|
||||
/**
|
||||
* 分页查询短信发送记录
|
||||
* @param page 当前页
|
||||
* @param size 每页大小
|
||||
* @param phone 手机号(可选)
|
||||
* @param status 状态(可选)
|
||||
* @return 分页数据
|
||||
*/
|
||||
Page<SmsSendLog> querySmsSendLog(int page, int size, String phone, Integer status);
|
||||
}
|
||||
+205
@@ -0,0 +1,205 @@
|
||||
package com.central.msg.service.impl;
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
||||
import com.central.msg.mapper.SmsSendLogMapper;
|
||||
import com.central.msg.model.entity.SmsSendLog;
|
||||
import com.central.msg.service.MsgSmsService;
|
||||
import com.central.sms.config.SmsProperties;
|
||||
import com.central.sms.enums.SmsProviderType;
|
||||
import com.central.sms.service.ISmsService;
|
||||
import com.central.sms.service.impl.AliyunSmsService;
|
||||
import com.central.sms.service.impl.TencentSmsService;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 消息中心短信服务实现类
|
||||
* @author system
|
||||
*/
|
||||
@Service
|
||||
public class MsgSmsServiceImpl implements MsgSmsService {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(MsgSmsServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private AliyunSmsService aliyunSmsService;
|
||||
|
||||
@Autowired
|
||||
private TencentSmsService tencentSmsService;
|
||||
|
||||
@Autowired
|
||||
private SmsSendLogMapper smsSendLogMapper;
|
||||
|
||||
@Autowired
|
||||
private SmsProperties smsProperties;
|
||||
|
||||
@Value("${sms.save-log:true}")
|
||||
private boolean saveLog;
|
||||
|
||||
@Override
|
||||
public boolean sendSms(String phone, String templateId, Map<String, String> params) {
|
||||
return sendSms(phone, templateId, params, smsProperties.getProvider());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendSms(String phone, String templateId, Map<String, String> params, SmsProviderType provider) {
|
||||
SmsSendLog log = new SmsSendLog();
|
||||
log.setPhone(phone);
|
||||
log.setTemplateId(templateId);
|
||||
log.setTemplateParams(params != null ? JSON.toJSONString(params) : null);
|
||||
log.setProvider(provider.name());
|
||||
log.setCreateTime(new Date());
|
||||
log.setUpdateTime(new Date());
|
||||
|
||||
try {
|
||||
logger.info("开始发送短信: phone={}, templateId={}, params={}, provider={}",
|
||||
phone, templateId, params, provider);
|
||||
log.setStatus(2); // 发送中
|
||||
|
||||
ISmsService smsService = getSmsService(provider);
|
||||
boolean result = smsService.sendSms(phone, templateId, params);
|
||||
|
||||
if (result) {
|
||||
logger.info("短信发送成功: phone={}, templateId={}, provider={}", phone, templateId, provider);
|
||||
log.setStatus(1); // 成功
|
||||
log.setResponseCode("Ok");
|
||||
log.setResponseMessage("发送成功");
|
||||
} else {
|
||||
logger.warn("短信发送失败: phone={}, templateId={}, provider={}", phone, templateId, provider);
|
||||
log.setStatus(0); // 失败
|
||||
log.setResponseCode("Failed");
|
||||
log.setResponseMessage("发送失败");
|
||||
}
|
||||
|
||||
// 保存发送记录
|
||||
if (saveLog) {
|
||||
saveSendLog(log);
|
||||
}
|
||||
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
logger.error("短信发送异常: phone={}, templateId={}, provider={}, error={}",
|
||||
phone, templateId, provider, e.getMessage(), e);
|
||||
log.setStatus(0); // 失败
|
||||
log.setErrorMessage(e.getMessage());
|
||||
log.setResponseMessage("发送异常: " + e.getMessage());
|
||||
|
||||
// 保存发送记录
|
||||
if (saveLog) {
|
||||
saveSendLog(log);
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendAliyunSms(String phone, String templateId, Map<String, String> params) {
|
||||
return sendSms(phone, templateId, params, SmsProviderType.ALIYUN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendTencentSms(String phone, String templateId, Map<String, String> params) {
|
||||
return sendSms(phone, templateId, params, SmsProviderType.TENCENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendVerifyCode(String phone, String code) {
|
||||
return sendVerifyCode(phone, code, smsProperties.getProvider());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendVerifyCode(String phone, String code, SmsProviderType provider) {
|
||||
String templateId = getVerifyCodeTemplateId(provider);
|
||||
if (templateId == null || templateId.isEmpty()) {
|
||||
logger.error("{}验证码模板ID未配置,无法发送验证码短信", provider.name());
|
||||
return false;
|
||||
}
|
||||
|
||||
Map<String, String> params = new HashMap<>();
|
||||
params.put("code", code);
|
||||
params.put("time", "5"); // 默认5分钟有效期
|
||||
|
||||
return sendSms(phone, templateId, params, provider);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendAliyunVerifyCode(String phone, String code) {
|
||||
return sendVerifyCode(phone, code, SmsProviderType.ALIYUN);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean sendTencentVerifyCode(String phone, String code) {
|
||||
return sendVerifyCode(phone, code, SmsProviderType.TENCENT);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Page<SmsSendLog> querySmsSendLog(int page, int size, String phone, Integer status) {
|
||||
Page<SmsSendLog> pageParam = new Page<>(page, size);
|
||||
|
||||
com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<SmsSendLog> wrapper =
|
||||
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<>();
|
||||
|
||||
// 手机号模糊查询
|
||||
if (phone != null && !phone.isEmpty()) {
|
||||
wrapper.like(SmsSendLog::getPhone, phone);
|
||||
}
|
||||
|
||||
// 状态查询
|
||||
if (status != null) {
|
||||
wrapper.eq(SmsSendLog::getStatus, status);
|
||||
}
|
||||
|
||||
// 按创建时间倒序
|
||||
wrapper.orderByDesc(SmsSendLog::getCreateTime);
|
||||
|
||||
return smsSendLogMapper.selectPage(pageParam, wrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据服务商类型获取短信服务实例
|
||||
*/
|
||||
private ISmsService getSmsService(SmsProviderType provider) {
|
||||
switch (provider) {
|
||||
case ALIYUN:
|
||||
return aliyunSmsService;
|
||||
case TENCENT:
|
||||
default:
|
||||
return tencentSmsService;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取验证码模板ID
|
||||
*/
|
||||
private String getVerifyCodeTemplateId(SmsProviderType provider) {
|
||||
switch (provider) {
|
||||
case ALIYUN:
|
||||
return smsProperties.getAliyun().getVerifyCodeTemplateId();
|
||||
case TENCENT:
|
||||
return smsProperties.getTencent().getVerifyCodeTemplateId();
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存短信发送记录
|
||||
*/
|
||||
private void saveSendLog(SmsSendLog log) {
|
||||
try {
|
||||
smsSendLogMapper.insert(log);
|
||||
} catch (Exception e) {
|
||||
logger.error("保存短信发送记录失败: {}", e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
# 消息中心配置文件
|
||||
spring:
|
||||
datasource:
|
||||
url: jdbc:mysql://${zlt.datasource.ip}/msg-center?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&zeroDateTimeBehavior=convertToNull&serverTimezone=Asia/Shanghai
|
||||
username: ${zlt.datasource.username}
|
||||
password: ${zlt.datasource.password}
|
||||
driver-class-name: com.mysql.cj.jdbc.Driver
|
||||
|
||||
mybatis-plus:
|
||||
mapper-locations: classpath:/mapper/*Mapper.xml
|
||||
type-aliases-package: com.central.msg.model.entity
|
||||
global-config:
|
||||
db-config:
|
||||
id-type: auto
|
||||
|
||||
springdoc:
|
||||
group-configs:
|
||||
- group: 'default'
|
||||
paths-to-match: '/**'
|
||||
packages-to-scan: com.central.msg.controller
|
||||
|
||||
knife4j:
|
||||
enable: true
|
||||
setting:
|
||||
swagger-model-name: 消息中心接口文档
|
||||
|
||||
# 短信服务配置
|
||||
sms:
|
||||
# 短信服务提供商: TENCENT(腾讯云) 或 ALIYUN(阿里云)
|
||||
# 默认提供商,用于未指定提供商的接口
|
||||
provider: ${SMS_PROVIDER:TENCENT}
|
||||
|
||||
# 腾讯云短信配置
|
||||
tencent:
|
||||
# 腾讯云短信应用ID (在腾讯云短信控制台获取)
|
||||
app-id: ${SMS_TENCENT_APP_ID:}
|
||||
# 腾讯云SecretId (在腾讯云控制台获取)
|
||||
secret-id: ${SMS_TENCENT_SECRET_ID:}
|
||||
# 腾讯云SecretKey (在腾讯云控制台获取)
|
||||
secret-key: ${SMS_TENCENT_SECRET_KEY:}
|
||||
# 短信签名 (在腾讯云短信控制台配置)
|
||||
sign-name: ${SMS_TENCENT_SIGN_NAME:}
|
||||
# 验证码模板ID
|
||||
verify-code-template-id: ${SMS_TENCENT_VERIFY_CODE_TEMPLATE_ID:}
|
||||
|
||||
# 阿里云短信配置
|
||||
aliyun:
|
||||
# 阿里云AccessKeyId (在阿里云控制台获取)
|
||||
access-key-id: ${SMS_ALIYUN_ACCESS_KEY_ID:}
|
||||
# 阿里云AccessKeySecret (在阿里云控制台获取)
|
||||
access-key-secret: ${SMS_ALIYUN_ACCESS_KEY_SECRET:}
|
||||
# 短信签名 (在阿里云短信控制台配置)
|
||||
sign-name: ${SMS_ALIYUN_SIGN_NAME:}
|
||||
# 验证码模板ID
|
||||
verify-code-template-id: ${SMS_ALIYUN_VERIFY_CODE_TEMPLATE_ID:}
|
||||
|
||||
# 是否保存短信发送记录到数据库
|
||||
save-log: ${SMS_SAVE_LOG:true}
|
||||
|
||||
# 多租户配置
|
||||
zlt:
|
||||
tenant:
|
||||
enable: true
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
spring:
|
||||
application:
|
||||
name: msg-center
|
||||
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
|
||||
<mapper namespace="com.central.msg.mapper.SmsSendLogMapper">
|
||||
|
||||
<resultMap id="BaseResultMap" type="com.central.msg.model.entity.SmsSendLog">
|
||||
<id column="id" property="id"/>
|
||||
<result column="phone" property="phone"/>
|
||||
<result column="template_id" property="templateId"/>
|
||||
<result column="template_params" property="templateParams"/>
|
||||
<result column="content" property="content"/>
|
||||
<result column="status" property="status"/>
|
||||
<result column="provider" property="provider"/>
|
||||
<result column="request_id" property="requestId"/>
|
||||
<result column="response_code" property="responseCode"/>
|
||||
<result column="response_message" property="responseMessage"/>
|
||||
<result column="error_message" property="errorMessage"/>
|
||||
<result column="tenant_id" property="tenantId"/>
|
||||
<result column="create_time" property="createTime"/>
|
||||
<result column="update_time" property="updateTime"/>
|
||||
</resultMap>
|
||||
|
||||
</mapper>
|
||||
Reference in New Issue
Block a user