迁移 user 模块的发送短信功能到 system 模块

This commit is contained in:
YunaiV
2020-04-17 21:41:06 +08:00
parent 4ffc2cb815
commit f4a698bc57
19 changed files with 262 additions and 49 deletions

View File

@@ -27,13 +27,7 @@ public enum UserErrorCodeEnum {
USER_STATUS_EQUALS(1001002003, "账号已经是该状态"),
USER_MOBILE_EQUALS(1001002004, "账号已经是该手机号"),
// ========== 手机验证码模块 ==========
MOBILE_CODE_NOT_FOUND(1001003000, "验证码不存在"),
MOBILE_CODE_EXPIRED(1001003001, "验证码已过期"),
MOBILE_CODE_USED(1001003002, "验证码已使用"),
MOBILE_CODE_NOT_CORRECT(1001003003, "验证码不正确"),
MOBILE_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY(1001003004, "超过每日短信发送数量"),
MOBILE_CODE_SEND_TOO_FAST(1001003005, "短信发送过于频率"),
// ========== 用户地址 ==========
USER_ADDRESS_NOT_EXISTENT(1001004000, "用户地址不存在!"),

View File

@@ -1,25 +0,0 @@
package cn.iocoder.mall.user.biz.dao;
import cn.iocoder.mall.user.biz.dataobject.MobileCodeDO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.springframework.stereotype.Repository;
@Repository // 实际不加也没问entity就是不想 IDEA 那看到有个报错
public interface MobileCodeMapper extends BaseMapper<MobileCodeDO> {
/**
* 获得手机号的最后一个手机验证码
*
* @param mobile 手机号
* @return 手机验证码
*/
default MobileCodeDO selectLast1ByMobile(String mobile) {
QueryWrapper<MobileCodeDO> query = new QueryWrapper<MobileCodeDO>()
.eq("mobile", mobile)
.orderByDesc("id")
.last("limit 1");
return selectOne(query);
}
}

View File

@@ -49,7 +49,6 @@ public class MobileCodeServiceImpl implements MobileCodeService {
* @return 手机验证码信息
*/
public MobileCodeDO validLastMobileCode(String mobile, String code) {
// TODO: 2019-04-09 Sin 暂时先忽略掉验证码校验
// return new MobileCodeDO().setCode(code).setCreateTime(new Date()).setId(1);
MobileCodeDO mobileCodePO = mobileCodeMapper.selectLast1ByMobile(mobile);
if (mobileCodePO == null) { // 若验证码不存在,抛出异常
@@ -78,31 +77,4 @@ public class MobileCodeServiceImpl implements MobileCodeService {
mobileCodeMapper.updateById(update);
}
// TODO 芋艿,后面要返回有效时间
public void send(String mobile) {
if (!ValidationUtil.isMobile(mobile)) {
throw ServiceExceptionUtil.exception(SysErrorCodeEnum.VALIDATION_REQUEST_PARAM_ERROR.getCode(), "手机格式不正确"); // TODO 有点搓
}
// 校验是否可以发送验证码
MobileCodeDO lastMobileCodePO = mobileCodeMapper.selectLast1ByMobile(mobile);
if (lastMobileCodePO != null) {
if (lastMobileCodePO.getTodayIndex() >= sendMaximumQuantityPerDay) { // 超过当天发送的上限。
throw ServiceExceptionUtil.exception(UserErrorCodeEnum.MOBILE_CODE_EXCEED_SEND_MAXIMUM_QUANTITY_PER_DAY.getCode());
}
if (System.currentTimeMillis() - lastMobileCodePO.getCreateTime().getTime() < sendFrequency) { // 发送过于频繁
throw ServiceExceptionUtil.exception(UserErrorCodeEnum.MOBILE_CODE_SEND_TOO_FAST.getCode());
}
// TODO 提升,每个 IP 每天可发送数量
// TODO 提升,每个 IP 每小时可发送数量
}
// 创建验证码记录
MobileCodeDO newMobileCodePO = new MobileCodeDO().setMobile(mobile)
.setCode("9999") // TODO 芋艿,随机 4 位验证码 or 6 位验证码
.setTodayIndex(lastMobileCodePO != null ? lastMobileCodePO.getTodayIndex() : 1)
.setUsed(false);
newMobileCodePO.setCreateTime(new Date());
mobileCodeMapper.insert(newMobileCodePO);
// TODO 发送验证码短信
}
}

View File

@@ -1,5 +1 @@
##################### 业务模块 #####################
## MobileCodeService
modules.mobile-code-service.code-expire-time-millis = 600000
modules.mobile-code-service.send-maximum-quantity-per-day = 10
modules.mobile-code-service.send-frequency = 60000