实现短信通知功能
This commit is contained in:
parent
7c666c7bd6
commit
a437c1564f
|
|
@ -23,6 +23,9 @@ public class GlobalConfig {
|
|||
// Redis键前缀
|
||||
public static String redisPrefix = "Like:";
|
||||
|
||||
// 短信验证码
|
||||
public static String redisSmsCode = "smsCode:";
|
||||
|
||||
// 资源访问前缀
|
||||
public static String publicPrefix = "api/uploads";
|
||||
|
||||
|
|
|
|||
|
|
@ -25,7 +25,6 @@ public class NoticeSetting implements Serializable {
|
|||
private String smsNotice; // 短信的通知设置
|
||||
private String oaNotice; // 公众号通知设置
|
||||
private String mnpNotice; // 小程序通知设置
|
||||
private String support; // 支持的发送类型
|
||||
private Integer isDelete; // 是否删除: [0=否, 1=是]
|
||||
private Long createTime; // 创建时间
|
||||
private Long updateTime; // 更新时间
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
|||
import com.mdd.common.entity.notice.NoticeSetting;
|
||||
import com.mdd.common.exception.OperateException;
|
||||
import com.mdd.common.mapper.notice.NoticeSettingMapper;
|
||||
import com.mdd.common.plugin.notice.engine.MpNotice;
|
||||
import com.mdd.common.plugin.notice.engine.OaNotice;
|
||||
import com.mdd.common.plugin.notice.engine.SmsNotice;
|
||||
import com.mdd.common.utils.SpringUtil;
|
||||
import com.mdd.common.utils.StringUtil;
|
||||
|
|
@ -36,16 +34,16 @@ public class NoticeDriver {
|
|||
// 短信通知
|
||||
Map<String, String> smsTemplate = ToolsUtil.jsonToMap(noticeSetting.getSmsNotice());
|
||||
if (StringUtil.isNotEmpty(smsTemplate.get("status")) && Integer.parseInt(smsTemplate.get("status")) == 1) {
|
||||
(new SmsNotice()).send(config, params, smsTemplate);
|
||||
(new SmsNotice()).send(config, params, smsTemplate, noticeSetting);
|
||||
}
|
||||
|
||||
// 小程序订阅通知
|
||||
Map<String, String> mnpTemplate = ToolsUtil.jsonToMap(noticeSetting.getMnpNotice());
|
||||
if (StringUtil.isNotEmpty(mnpTemplate.get("status")) && Integer.parseInt(mnpTemplate.get("status")) == 1) {
|
||||
(new MpNotice()).send(config, params, mnpTemplate);
|
||||
}
|
||||
// 小程序订阅通知 todo
|
||||
// Map<String, String> mnpTemplate = ToolsUtil.jsonToMap(noticeSetting.getMnpNotice());
|
||||
// if (StringUtil.isNotEmpty(mnpTemplate.get("status")) && Integer.parseInt(mnpTemplate.get("status")) == 1) {
|
||||
// (new MpNotice()).send(config, params, mnpTemplate);
|
||||
// }
|
||||
|
||||
// 公众号订阅通知
|
||||
// 公众号订阅通知 todo
|
||||
// Map<String, String> oaTemplate = ToolsUtil.jsonToMap(noticeSetting.getOaNotice());
|
||||
// if (StringUtil.isNotEmpty(oaTemplate.get("status")) && Integer.parseInt(oaTemplate.get("status")) == 1) {
|
||||
// (new OaNotice()).send(config, params, oaTemplate);
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
package com.mdd.common.plugin.notice.engine;
|
||||
|
||||
import com.mdd.common.config.GlobalConfig;
|
||||
import com.mdd.common.entity.notice.NoticeSetting;
|
||||
import com.mdd.common.plugin.sms.SmsDriver;
|
||||
import com.mdd.common.utils.ConfigUtil;
|
||||
import com.mdd.common.utils.RedisUtil;
|
||||
import com.mdd.common.utils.StringUtil;
|
||||
|
||||
import java.util.*;
|
||||
|
|
@ -15,10 +18,12 @@ public class SmsNotice {
|
|||
* @param config 基础配置
|
||||
* @param params 短信参数
|
||||
* @param smsTemplate 短信模板
|
||||
* @param noticeSetting 通知设置
|
||||
*/
|
||||
public void send(Map<String, String> config, Map<String, String> params, Map<String, String> smsTemplate) {
|
||||
public void send(Map<String, String> config, Map<String, String> params, Map<String, String> smsTemplate, NoticeSetting noticeSetting) {
|
||||
String mobile = config.getOrDefault("mobile", "");
|
||||
String scene = config.getOrDefault("scene", "");
|
||||
|
||||
if (StringUtil.isNotEmpty(mobile) && StringUtil.isNotEmpty(scene)) {
|
||||
(new SmsDriver())
|
||||
.setMobile(mobile)
|
||||
|
|
@ -26,6 +31,12 @@ public class SmsNotice {
|
|||
.setTemplateParam(this.getSmsParams(params, smsTemplate))
|
||||
.setSmsContent(this.getContent(params, smsTemplate))
|
||||
.sendSms();
|
||||
|
||||
// 1=业务通知, 2=验证码
|
||||
if (noticeSetting.getType() == 2 && StringUtil.isNotNull(params.get("code"))) {
|
||||
String code = params.get("code").toLowerCase();
|
||||
RedisUtil.set(GlobalConfig.redisSmsCode+mobile, code);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -8,9 +8,6 @@ public class FrontConfig {
|
|||
// 登录缓存键
|
||||
public static final String frontendTokenKey = "frontend:token:";
|
||||
|
||||
// 短信验证码
|
||||
public static final String frontendSmsCode = "frontend:smsCode:";
|
||||
|
||||
// 免登录验证
|
||||
public static String[] notLoginUri = new String[]{
|
||||
"/api/login"
|
||||
|
|
|
|||
|
|
@ -1,7 +1,6 @@
|
|||
package com.mdd.front.controller;
|
||||
|
||||
import com.mdd.common.core.AjaxResult;
|
||||
import com.mdd.common.plugin.notice.NoticeDriver;
|
||||
import com.mdd.common.validator.annotation.IDMust;
|
||||
import com.mdd.front.service.IIndexService;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
|
@ -11,7 +10,6 @@ import org.springframework.web.bind.annotation.RequestParam;
|
|||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
|
|
@ -29,16 +27,6 @@ public class IndexController {
|
|||
*/
|
||||
@GetMapping("/index")
|
||||
public Object index() {
|
||||
Map<String, String> config = new LinkedHashMap<>();
|
||||
config.put("scene", "100");
|
||||
config.put("mobile", "1222");
|
||||
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
params.put("code", "5522");
|
||||
// params.put("order_sn", "27552210565677");
|
||||
(new NoticeDriver()).handle(config, params);
|
||||
|
||||
|
||||
Map<String, Object> detail = IIndexService.index();
|
||||
return AjaxResult.success(detail);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ package com.mdd.front.controller;
|
|||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
import com.mdd.common.core.AjaxResult;
|
||||
import com.mdd.front.service.ILoginService;
|
||||
import com.mdd.front.validate.RegisterParam;
|
||||
import com.mdd.front.validate.RegParam;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
|
|
@ -25,12 +25,12 @@ public class LoginController {
|
|||
* 注册账号
|
||||
*
|
||||
* @author fzr
|
||||
* @param registerParam 参数
|
||||
* @param regParam 参数
|
||||
* @return Object
|
||||
*/
|
||||
@PostMapping("/register")
|
||||
public Object register(@Validated @RequestBody RegisterParam registerParam) {
|
||||
iLoginService.register(registerParam);
|
||||
public Object register(@Validated @RequestBody RegParam regParam) {
|
||||
iLoginService.register(regParam);
|
||||
return AjaxResult.success();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,41 @@
|
|||
package com.mdd.front.controller;
|
||||
|
||||
import com.mdd.common.core.AjaxResult;
|
||||
import com.mdd.common.plugin.notice.NoticeDriver;
|
||||
import com.mdd.common.utils.ToolsUtil;
|
||||
import com.mdd.front.validate.SmsParam;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 短信管理
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/sms")
|
||||
public class SmsController {
|
||||
|
||||
/**
|
||||
* 发送短信
|
||||
*
|
||||
* @author fzr
|
||||
* @param smsParam 参数
|
||||
* @return Object
|
||||
*/
|
||||
@PostMapping("/send")
|
||||
public Object send(@Validated @RequestBody SmsParam smsParam) {
|
||||
Map<String, String> config = new LinkedHashMap<>();
|
||||
config.put("scene", smsParam.getScene());
|
||||
config.put("mobile", smsParam.getMobile());
|
||||
Map<String, String> params = new LinkedHashMap<>();
|
||||
params.put("code", ToolsUtil.randomInt(4));
|
||||
(new NoticeDriver()).handle(config, params);
|
||||
return AjaxResult.success();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -1,6 +1,6 @@
|
|||
package com.mdd.front.service;
|
||||
|
||||
import com.mdd.front.validate.RegisterParam;
|
||||
import com.mdd.front.validate.RegParam;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
|
|
@ -13,9 +13,9 @@ public interface ILoginService {
|
|||
* 账号注册
|
||||
*
|
||||
* @author fzr
|
||||
* @param registerParam 参数
|
||||
* @param regParam 参数
|
||||
*/
|
||||
void register(RegisterParam registerParam);
|
||||
void register(RegParam regParam);
|
||||
|
||||
/**
|
||||
* 微信登录
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
|
|||
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Assert;
|
||||
import com.mdd.common.config.GlobalConfig;
|
||||
import com.mdd.common.entity.user.User;
|
||||
import com.mdd.common.entity.user.UserAuth;
|
||||
import com.mdd.common.enums.ClientEnum;
|
||||
|
|
@ -15,7 +16,7 @@ import com.mdd.common.mapper.user.UserMapper;
|
|||
import com.mdd.common.utils.*;
|
||||
import com.mdd.front.config.FrontConfig;
|
||||
import com.mdd.front.service.ILoginService;
|
||||
import com.mdd.front.validate.RegisterParam;
|
||||
import com.mdd.front.validate.RegParam;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
|
@ -41,13 +42,13 @@ public class LoginServiceImpl implements ILoginService {
|
|||
* 注册账号
|
||||
*
|
||||
* @author fzr
|
||||
* @param registerParam 参数
|
||||
* @param regParam 参数
|
||||
*/
|
||||
@Override
|
||||
public void register(RegisterParam registerParam) {
|
||||
public void register(RegParam regParam) {
|
||||
User model = userMapper.selectOne(new QueryWrapper<User>()
|
||||
.select("id,sn,username")
|
||||
.eq("username", registerParam.getUsername())
|
||||
.eq("username", regParam.getUsername())
|
||||
.eq("is_delete", 0)
|
||||
.last("limit 1"));
|
||||
|
||||
|
|
@ -55,12 +56,12 @@ public class LoginServiceImpl implements ILoginService {
|
|||
|
||||
Integer sn = this.randMakeSn();
|
||||
String salt = ToolsUtil.randomString(5);
|
||||
String pwd = ToolsUtil.makeMd5(registerParam.getPassword()+salt);
|
||||
String pwd = ToolsUtil.makeMd5(regParam.getPassword()+salt);
|
||||
|
||||
User user = new User();
|
||||
user.setSn(sn);
|
||||
user.setNickname("用户"+sn);
|
||||
user.setUsername(registerParam.getUsername());
|
||||
user.setUsername(regParam.getUsername());
|
||||
user.setPassword(pwd);
|
||||
user.setSalt(salt);
|
||||
user.setChannel(0);
|
||||
|
|
@ -187,13 +188,13 @@ public class LoginServiceImpl implements ILoginService {
|
|||
String code = params.get("code").toLowerCase();
|
||||
|
||||
// 校验验证码
|
||||
Object smsCode = RedisUtil.get(FrontConfig.frontendSmsCode+mobile);
|
||||
Object smsCode = RedisUtil.get(GlobalConfig.redisSmsCode+mobile);
|
||||
if (StringUtil.isNull(smsCode) || !smsCode.toString().equals(code)) {
|
||||
throw new OperateException("验证码错误!");
|
||||
}
|
||||
|
||||
// 删除验证码
|
||||
RedisUtil.del(FrontConfig.frontendSmsCode+mobile);
|
||||
RedisUtil.del(GlobalConfig.redisSmsCode+mobile);
|
||||
|
||||
// 查询手机号
|
||||
User user = userMapper.selectOne(new QueryWrapper<User>()
|
||||
|
|
@ -264,13 +265,13 @@ public class LoginServiceImpl implements ILoginService {
|
|||
String password = params.get("password");
|
||||
|
||||
// 校验验证码
|
||||
Object smsCode = RedisUtil.get(FrontConfig.frontendSmsCode+mobile);
|
||||
Object smsCode = RedisUtil.get(GlobalConfig.redisSmsCode+mobile);
|
||||
if (StringUtil.isNull(smsCode) || !smsCode.toString().equals(code)) {
|
||||
throw new OperateException("验证码错误!");
|
||||
}
|
||||
|
||||
// 删除验证码
|
||||
RedisUtil.del(FrontConfig.frontendSmsCode+mobile);
|
||||
RedisUtil.del(GlobalConfig.redisSmsCode+mobile);
|
||||
|
||||
// 查询手机号
|
||||
User user = userMapper.selectOne(new QueryWrapper<User>()
|
||||
|
|
|
|||
|
|
@ -16,7 +16,7 @@ import java.io.Serializable;
|
|||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
public class RegisterParam implements Serializable {
|
||||
public class RegParam implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
|
|
@ -0,0 +1,26 @@
|
|||
package com.mdd.front.validate;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.experimental.Accessors;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import javax.validation.constraints.NotEmpty;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.io.Serializable;
|
||||
|
||||
@Data
|
||||
@EqualsAndHashCode(callSuper = false)
|
||||
@Accessors(chain = true)
|
||||
public class SmsParam implements Serializable {
|
||||
|
||||
@NotNull(message = "scene参数缺失")
|
||||
@NotEmpty(message = "场景不能为空")
|
||||
private String scene;
|
||||
|
||||
@NotNull(message = "mobile参数缺失")
|
||||
@NotEmpty(message = "手机号不能为空")
|
||||
@Length(min = 11, max = 11, message = "手机号只能为11位")
|
||||
private String mobile;
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue