package com.alatus.mall.auth.app;
import com.alatus.common.constant.AuthServerConstant;
import com.alatus.common.exception.BizCodeEnum;
import com.alatus.common.utils.R;
import com.alatus.mall.auth.feign.ThirdPartFeignService;
import com.alatus.mall.auth.vo.UserRegisterVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Controller
public class LoginController {
@Autowired
private ThirdPartFeignService thirdPartFeignService;
@Autowired
private StringRedisTemplate redisTemplate;
@GetMapping("/sms/sendCode")
@ResponseBody
public R sendCode(@RequestParam("phone") String phone){
// TODO 接口防刷
// 防止反复刷验证码
String value = redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + phone);
if(value!=null&&!StringUtils.isEmpty(value)){
long time = Long.parseLong(value.split("_")[1]);
if(System.currentTimeMillis() - time < 60000){
// 60秒内不能再发
return R.error(BizCodeEnum.SMS_CODE_EXCEPTION.getCode(),BizCodeEnum.SMS_CODE_EXCEPTION.getMsg());
}
}
String code = UUID.randomUUID().toString().substring(0, 5);
R sendCode = thirdPartFeignService.sendCode(phone, code);
// 缓存验证码
redisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_PREFIX+phone,code+"_"+System.currentTimeMillis(),10, TimeUnit.MINUTES);
if(sendCode.getCode()==0){
return R.ok();
}
else{
return R.error();
}
}
@PostMapping("/register")
public String register(@Valid UserRegisterVo vo, BindingResult result, Model model){
if(result.hasErrors()){
// 收集错误信息返回
Map<String, String> collect = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField,FieldError::getDefaultMessage));
model.addAttribute("errors",collect);
// 原请求是post请求,当我们失败以后,在这里转发,转发是会保持请求方式不变的,但是路径访问默认是get
// 就会出现错误
// return "forward:/reg.html";
return "reg";
}
// 注册成功返回首页登录页
else{
return "login";
}
}
}
package com.alatus.mall.auth.app;
import com.alatus.common.constant.AuthServerConstant;
import com.alatus.common.exception.BizCodeEnum;
import com.alatus.common.utils.R;
import com.alatus.mall.auth.feign.ThirdPartFeignService;
import com.alatus.mall.auth.vo.UserRegisterVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.Valid;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
@Controller
public class LoginController {
@Autowired
private ThirdPartFeignService thirdPartFeignService;
@Autowired
private StringRedisTemplate redisTemplate;
@GetMapping("/sms/sendCode")
@ResponseBody
public R sendCode(@RequestParam("phone") String phone){
// TODO 接口防刷
// 防止反复刷验证码
String value = redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + phone);
if(value!=null&&!StringUtils.isEmpty(value)){
long time = Long.parseLong(value.split("_")[1]);
if(System.currentTimeMillis() - time < 60000){
// 60秒内不能再发
return R.error(BizCodeEnum.SMS_CODE_EXCEPTION.getCode(),BizCodeEnum.SMS_CODE_EXCEPTION.getMsg());
}
}
String code = UUID.randomUUID().toString().substring(0, 5);
R sendCode = thirdPartFeignService.sendCode(phone, code);
// 缓存验证码
redisTemplate.opsForValue().set(AuthServerConstant.SMS_CODE_CACHE_PREFIX+phone,code+"_"+System.currentTimeMillis(),10, TimeUnit.MINUTES);
if(sendCode.getCode()==0){
return R.ok();
}
else{
return R.error();
}
}
@PostMapping("/register")
public String register(@Valid UserRegisterVo vo, BindingResult result, Model model){
if(result.hasErrors()){
// 收集错误信息返回
Map<String, String> collect = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField,FieldError::getDefaultMessage));
model.addAttribute("errors",collect);
// 原请求是post请求,当我们失败以后,在这里转发,转发是会保持请求方式不变的,但是路径访问默认是get
// 就会出现错误
// return "forward:/reg.html";
return "reg";
}
// 注册成功返回首页登录页
else{
return "login";
}
}
}
package com.alatus.mall.auth.vo;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
@Data
public class UserRegisterVo {
@NotEmpty(message = "用户名不能为空")
@Length(min = 6,max = 18,message = "用户名必须是6-18位字符")
private String username;
@NotEmpty(message = "密码不能为空")
@Length(min = 6,max = 18,message = "用户名必须是6-18位字符")
private String password;
@NotEmpty(message = "手机号不能为空")
@Pattern(regexp = "^[1]([3-9])[0-9]{9}$",message = "手机号格式不正确")
private String phone;
@NotEmpty(message = "验证码不能为空")
private String code;
}
package com.alatus.mall.auth.vo;
import lombok.Data;
import org.hibernate.validator.constraints.Length;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.Pattern;
@Data
public class UserRegisterVo {
@NotEmpty(message = "用户名不能为空")
@Length(min = 6,max = 18,message = "用户名必须是6-18位字符")
private String username;
@NotEmpty(message = "密码不能为空")
@Length(min = 6,max = 18,message = "用户名必须是6-18位字符")
private String password;
@NotEmpty(message = "手机号不能为空")
@Pattern(regexp = "^[1]([3-9])[0-9]{9}$",message = "手机号格式不正确")
private String phone;
@NotEmpty(message = "验证码不能为空")
private String code;
}