feat 解决短信发送问题

This commit is contained in:
damonyuan 2024-10-19 01:11:11 +08:00
parent d41cc4cc5b
commit 19bf821545
11 changed files with 98 additions and 24 deletions

View File

@ -1,6 +1,7 @@
package com.mdd.common.entity.notice;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
@ -31,6 +32,7 @@ public class NoticeRecord implements Serializable {
private Integer sceneId;
@ApiModelProperty("已读状态: [0=未读, 1=已读]")
@TableField(value = "`read`")
private Integer read;
@ApiModelProperty("通知接收对象类型;1-会员;2-商家;3-平台;4-游客(未注册用户)")

View File

@ -3,6 +3,9 @@ package com.mdd.common.enums;
import com.alibaba.fastjson2.JSONObject;
import com.mdd.common.util.StringUtils;
import java.util.ArrayList;
import java.util.List;
/**
* 通知枚举类
*/
@ -75,4 +78,16 @@ public enum NoticeEnum {
return scene.getInteger(tag);
}
/**
* 验证码场景
*/
public static List<Integer> getSmsScene() {
List<Integer> ret = new ArrayList<>();
ret.add(LOGIN_CAPTCHA.getCode());
ret.add(BIND_MOBILE_CAPTCHA.getCode());
ret.add(CHANGE_MOBILE_CAPTCHA.getCode());
ret.add(FIND_LOGIN_PASSWORD_CAPTCHA.getCode());
return ret;
}
}

View File

@ -0,0 +1,42 @@
package com.mdd.common.enums;
/**
* 短信枚举
*/
public enum SmsEnum {
SEND_ING(0, "发送中"),
SEND_SUCCESS(1, "发送成功"),
SEND_FAIL(2, "发送失败");
/**
* 构造方法
*/
private final int code;
private final String msg;
SmsEnum(int code, String msg) {
this.code = code;
this.msg = msg;
}
/**
* 获取状态码
*
* @author fzr
* @return Long
*/
public int getCode() {
return this.code;
}
/**
* 获取提示
*
* @author fzr
* @return String
*/
public String getMsg() {
return this.msg;
}
}

View File

@ -18,7 +18,6 @@ import com.mdd.common.util.StringUtils;
public class NoticeDriver {
public static void handle(NoticeSmsVo noticeSmsVo) {
System.out.println(11123);
// 场景模板
NoticeSettingMapper noticeSettingMapper = SpringUtils.getBean(NoticeSettingMapper.class);
NoticeSetting noticeSetting = noticeSettingMapper.selectOne(
@ -35,7 +34,6 @@ public class NoticeDriver {
smsTemplate.setName(noticeSetting.getSceneName());
smsTemplate.setType(noticeSetting.getType());
smsTemplate.setParams(noticeSetting.getSmsNotice());
System.out.println(1111);
if (StringUtils.isNotNull(smsTemplate.getStatus()) && smsTemplate.getStatus().equals(1)) {
(new SmsNoticeHandle()).send(noticeSmsVo, smsTemplate);
}

View File

@ -28,7 +28,6 @@ public class SmsNoticeHandle {
* @param smsTemplate 短信模板
*/
public void send(NoticeSmsVo noticeSmsVo, SmsTemplate smsTemplate) {
System.out.println("111asd");
// 基础参数
String mobile = noticeSmsVo.getMobile();
Integer scene = noticeSmsVo.getScene();
@ -56,6 +55,7 @@ public class SmsNoticeHandle {
noticeRecord.setRead(NoticeEnum.VIEW_UNREAD.getCode());
noticeRecord.setCreateTime(System.currentTimeMillis() / 1000);
noticeRecord.setUpdateTime(System.currentTimeMillis() / 1000);
noticeRecord.setNoticeType(NoticeEnum.SENDER_SMS.getCode());
noticeRecordMapper.insert(noticeRecord);
//记录到sms log
@ -64,6 +64,7 @@ public class SmsNoticeHandle {
smsLog.setSceneId(noticeSmsVo.getScene());
smsLog.setMobile(noticeSmsVo.getMobile());
smsLog.setContent(this.getContent(params, smsTemplate.getContent()));
smsLog.setCode(noticeSmsVo.getCode());
// 消息发送
if (StringUtils.isNotEmpty(mobile)) {
@ -94,10 +95,13 @@ public class SmsNoticeHandle {
smsLog.setIsVerify(0);
smsLog.setCheckNum(0);
smsLog.setSendStatus(2);
smsLog.setResults(e.getMsg());
smsLog.setSendTime(System.currentTimeMillis() / 1000);
smsLog.setCreateTime(System.currentTimeMillis() / 1000);
smsLog.setUpdateTime(System.currentTimeMillis() / 1000);
smsLogMapper.insert(smsLog);
throw new OperateException(e.getMsg());
}
}
}

View File

@ -28,7 +28,7 @@ public class SmsTemplate {
*/
public void setParams(String smsNotice) {
Map<String, String> config = MapUtils.jsonToMap(smsNotice);
this.setTemplateId(config.getOrDefault("templateId", ""));
this.setTemplateId(config.getOrDefault("template_id", ""));
this.setContent(config.getOrDefault("content", ""));
this.setStatus(Integer.parseInt(config.getOrDefault("status", "0")));
}

View File

@ -12,4 +12,6 @@ public class NoticeSmsVo {
private Integer expire;
private String[] params;
private String code;
}

View File

@ -20,7 +20,7 @@ public class SmsDriver {
* 构造方法
*/
public SmsDriver() {
this.engine = ConfigUtils.get("sms", "default", "");
this.engine = ConfigUtils.get("sms", "engine", "");
this.config = ConfigUtils.getMap("sms", this.engine);
}
@ -69,9 +69,8 @@ public class SmsDriver {
String templateParam = JSON.toJSONString(this.templateParam);
Integer sendResult = 0;
String results = "";
switch (this.engine) {
case "aliyun":
case "ali":
AliSms aliSms = new AliSms(this.config);
results = aliSms.setMobile(this.mobile)
.setTemplateId(this.templateCode)
@ -88,6 +87,8 @@ public class SmsDriver {
.send();
sendResult = tencentSms.getSendResult();
break;
default:
throw new OperateException("短信类型错误");
}
if (sendResult == 2) {

View File

@ -84,7 +84,7 @@ public class AliSms {
* @return String
*/
public String send() {
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", this.config.get("appKey"), this.config.get("secretKey"));
DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", this.config.get("app_key"), this.config.get("secret_key"));
IAcsClient client = new DefaultAcsClient(profile);
CommonRequest request = new CommonRequest();

View File

@ -82,7 +82,7 @@ public class TencentSms {
public String send() {
try {
/*认证对象*/
Credential cred = new Credential(this.config.get("secretId"), config.get("secretKey"));
Credential cred = new Credential(this.config.get("secret_id"), config.get("secret_key"));
HttpProfile httpProfile = new HttpProfile();
httpProfile.setReqMethod("POST");
httpProfile.setConnTimeout(60);
@ -97,7 +97,7 @@ public class TencentSms {
SmsClient client = new SmsClient(cred, "ap-guangzhou",clientProfile);
SendSmsRequest req = new SendSmsRequest();
req.setSignName(config.get("sign"));
req.setSmsSdkAppId(config.get("appId"));
req.setSmsSdkAppId(config.get("app_id"));
req.setTemplateId(this.templateId);
/*手机号码*/
@ -107,9 +107,9 @@ public class TencentSms {
/*模板参数*/
String[] templateParamSet = this.templateParams;
req.setTemplateParamSet(templateParamSet);
/*发起请求*/
SendSmsResponse res = client.SendSms(req);
System.out.println(res);
if (!res.getSendStatusSet()[0].getCode().equals("Ok")) {
this.sendResult = 2;
return res.getSendStatusSet()[0].getMessage();

View File

@ -4,9 +4,13 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mdd.common.aop.NotLogin;
import com.mdd.common.core.AjaxResult;
import com.mdd.common.entity.notice.NoticeRecord;
import com.mdd.common.entity.smsLog.SmsLog;
import com.mdd.common.enums.NoticeEnum;
import com.mdd.common.enums.SmsEnum;
import com.mdd.common.enums.YesNoEnum;
import com.mdd.common.exception.OperateException;
import com.mdd.common.mapper.notice.NoticeRecordMapper;
import com.mdd.common.mapper.smsLog.SmsLogMapper;
import com.mdd.common.plugin.notice.NoticeDriver;
import com.mdd.common.plugin.notice.vo.NoticeSmsVo;
import com.mdd.common.util.StringUtils;
@ -30,32 +34,38 @@ import java.util.Map;
public class SmsController {
@Resource
NoticeRecordMapper noticeRecordMapper;
SmsLogMapper smsLogMapper;
@NotLogin
@PostMapping("/sendCode")
@ApiOperation(value="发送短信")
public AjaxResult<Object> sendSms(@Validated @RequestBody SmsValidate smsValidate) {
NoticeRecord noticeRecord = noticeRecordMapper.selectOne(new QueryWrapper<NoticeRecord>()
.eq("account", smsValidate.getMobile())
.eq("scene", smsValidate.getScene())
.eq("status", Arrays.asList(NoticeEnum.STATUS_WAIT, NoticeEnum.STATUS_OK))
.orderByDesc("id")
.last("limit 1"));
if (StringUtils.isNotNull(noticeRecord)) {
if (noticeRecord.getCreateTime() >= (System.currentTimeMillis() / 1000 - 60)){
throw new OperateException("操作频繁,请稍后再试!");
QueryWrapper smsLogQueryWrapper = new QueryWrapper<SmsLog>();
smsLogQueryWrapper.eq("mobile", smsValidate.getMobile());
smsLogQueryWrapper.eq("send_status", SmsEnum.SEND_SUCCESS.getCode());
smsLogQueryWrapper.in("scene_id", NoticeEnum.getSmsScene());
smsLogQueryWrapper.eq("is_verify", YesNoEnum.NO.getCode());
if (StringUtils.isNotNull(smsValidate.getScene())) {
smsLogQueryWrapper.eq("scene_id", NoticeEnum.getSceneByTag(smsValidate.getScene()));
}
smsLogQueryWrapper.orderByDesc("send_time");
smsLogQueryWrapper.last("limit 1");
SmsLog smsLog = smsLogMapper.selectOne(smsLogQueryWrapper);
if (StringUtils.isNotNull(smsLog)) {
if (smsLog.getSendTime() + 5 * 60 > System.currentTimeMillis() / 1000 ) {
throw new OperateException("已有短信记录,请勿重复发送");
}
}
String code = ToolUtils.randomInt(4);
NoticeSmsVo params = new NoticeSmsVo()
.setScene(NoticeEnum.getSceneByTag(smsValidate.getScene()))
.setMobile(smsValidate.getMobile())
.setExpire(900)
.setParams(new String[] {
"code:" + ToolUtils.randomInt(4)
});
"code:" + code
})
.setCode(code);
NoticeDriver.handle(params);
return AjaxResult.success();