mirror of
https://gitee.com/log4j/pig.git
synced 2025-01-03 23:42:22 +08:00
♻️ Refactoring code. 发送验证码组件需要判断手机号是否存在用户列表中 #749
This commit is contained in:
parent
112375dd84
commit
23b18c43e0
@ -0,0 +1,27 @@
|
||||
package com.pig4cloud.pig.admin.api.dto;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
import javax.validation.constraints.NotBlank;
|
||||
|
||||
/**
|
||||
* 客户端请求验证码
|
||||
*
|
||||
* @author lengleng
|
||||
* @date 2022/10/13
|
||||
*/
|
||||
@Data
|
||||
public class AppSmsDTO {
|
||||
|
||||
/**
|
||||
* 手机号
|
||||
*/
|
||||
@NotBlank(message = "手机号不能为空")
|
||||
private String phone;
|
||||
|
||||
/**
|
||||
* 手机号是否存在数据库
|
||||
*/
|
||||
private Boolean exist;
|
||||
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
package com.pig4cloud.pig.admin.controller;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.pig4cloud.pig.admin.api.dto.AppSmsDTO;
|
||||
import com.pig4cloud.pig.admin.api.dto.UserInfo;
|
||||
import com.pig4cloud.pig.admin.api.entity.SysUser;
|
||||
import com.pig4cloud.pig.admin.service.AppService;
|
||||
@ -13,10 +14,9 @@ import io.swagger.v3.oas.annotations.security.SecurityRequirement;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import lombok.AllArgsConstructor;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import javax.validation.Valid;
|
||||
|
||||
/**
|
||||
* @author lengleng
|
||||
@ -33,10 +33,15 @@ public class AppController {
|
||||
|
||||
private final SysUserService userService;
|
||||
|
||||
/**
|
||||
* 发送手机验证码
|
||||
* @param sms 请求手机对象
|
||||
* @return code
|
||||
*/
|
||||
@Inner(value = false)
|
||||
@GetMapping("/{mobile}")
|
||||
public R<Boolean> sendSmsCode(@PathVariable String mobile) {
|
||||
return appService.sendSmsCode(mobile);
|
||||
@PostMapping("/sms")
|
||||
public R<Boolean> sendSmsCode(@Valid @RequestBody AppSmsDTO sms) {
|
||||
return appService.sendSmsCode(sms);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -17,6 +17,7 @@
|
||||
|
||||
package com.pig4cloud.pig.admin.service;
|
||||
|
||||
import com.pig4cloud.pig.admin.api.dto.AppSmsDTO;
|
||||
import com.pig4cloud.pig.common.core.util.R;
|
||||
|
||||
/**
|
||||
@ -27,10 +28,10 @@ public interface AppService {
|
||||
|
||||
/**
|
||||
* 发送手机验证码
|
||||
* @param phone phone
|
||||
* @param sms phone
|
||||
* @return code
|
||||
*/
|
||||
R<Boolean> sendSmsCode(String phone);
|
||||
R<Boolean> sendSmsCode(AppSmsDTO sms);
|
||||
|
||||
/**
|
||||
* 校验验证码
|
||||
|
@ -18,6 +18,9 @@
|
||||
package com.pig4cloud.pig.admin.service.impl;
|
||||
|
||||
import cn.hutool.core.util.RandomUtil;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||
import com.pig4cloud.pig.admin.api.dto.AppSmsDTO;
|
||||
import com.pig4cloud.pig.admin.api.entity.SysUser;
|
||||
import com.pig4cloud.pig.admin.mapper.SysUserMapper;
|
||||
import com.pig4cloud.pig.admin.service.AppService;
|
||||
import com.pig4cloud.pig.common.core.constant.CacheConstants;
|
||||
@ -53,25 +56,31 @@ public class AppServiceImpl implements AppService {
|
||||
|
||||
/**
|
||||
* 发送手机验证码 TODO: 调用短信网关发送验证码,测试返回前端
|
||||
* @param phone 手机号
|
||||
* @param sms 手机号
|
||||
* @return code
|
||||
*/
|
||||
@Override
|
||||
public R<Boolean> sendSmsCode(String phone) {
|
||||
Object codeObj = redisTemplate.opsForValue().get(CacheConstants.DEFAULT_CODE_KEY + phone);
|
||||
public R<Boolean> sendSmsCode(AppSmsDTO sms) {
|
||||
Object codeObj = redisTemplate.opsForValue().get(CacheConstants.DEFAULT_CODE_KEY + sms.getPhone());
|
||||
|
||||
if (codeObj != null) {
|
||||
log.info("手机号验证码未过期:{},{}", phone, codeObj);
|
||||
log.info("手机号验证码未过期:{},{}", sms.getPhone(), codeObj);
|
||||
return R.ok(Boolean.FALSE, MsgUtils.getMessage(ErrorCodes.SYS_APP_SMS_OFTEN));
|
||||
}
|
||||
|
||||
// 校验手机号是否存在 sys_user 表
|
||||
if (sms.getExist()
|
||||
&& !userMapper.exists(Wrappers.<SysUser>lambdaQuery().eq(SysUser::getPhone, sms.getPhone()))) {
|
||||
return R.ok(Boolean.FALSE, MsgUtils.getMessage(ErrorCodes.SYS_APP_PHONE_UNREGISTERED, sms.getPhone()));
|
||||
}
|
||||
|
||||
String code = RandomUtil.randomNumbers(Integer.parseInt(SecurityConstants.CODE_SIZE));
|
||||
log.info("手机号生成验证码成功:{},{}", phone, code);
|
||||
redisTemplate.opsForValue().set(CacheConstants.DEFAULT_CODE_KEY + phone, code, SecurityConstants.CODE_TIME,
|
||||
TimeUnit.SECONDS);
|
||||
log.info("手机号生成验证码成功:{},{}", sms.getPhone(), code);
|
||||
redisTemplate.opsForValue().set(CacheConstants.DEFAULT_CODE_KEY + sms.getPhone(), code,
|
||||
SecurityConstants.CODE_TIME, TimeUnit.SECONDS);
|
||||
|
||||
// 调用短信通道发送
|
||||
this.smsClient.sendCode(code, phone);
|
||||
this.smsClient.sendCode(code, sms.getPhone());
|
||||
return R.ok(Boolean.TRUE, code);
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user