package com.alatus.common.exception;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public enum BizCodeEnum {
UNKNOW_EXCEPTION(10000,"系统未知异常"),
VALID_EXCEPTION(10001,"参数校验失败"),
SMS_CODE_EXCEPTION(10002,"短信验证码获取频率太高,请稍后再试"),
PRODUCT_UP_EXCEPTION(11000,"商品上架异常");
private int code;
private String msg;
}
package com.alatus.common.exception;
import lombok.AllArgsConstructor;
import lombok.Getter;
@AllArgsConstructor
@Getter
public enum BizCodeEnum {
UNKNOW_EXCEPTION(10000,"系统未知异常"),
VALID_EXCEPTION(10001,"参数校验失败"),
SMS_CODE_EXCEPTION(10002,"短信验证码获取频率太高,请稍后再试"),
PRODUCT_UP_EXCEPTION(11000,"商品上架异常");
private int code;
private String msg;
}
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 org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@RestController
public class LoginController {
@Autowired
private ThirdPartFeignService thirdPartFeignService;
@Autowired
private StringRedisTemplate redisTemplate;
@GetMapping("/sms/sendCode")
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();
}
}
}
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 org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
@RestController
public class LoginController {
@Autowired
private ThirdPartFeignService thirdPartFeignService;
@Autowired
private StringRedisTemplate redisTemplate;
@GetMapping("/sms/sendCode")
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();
}
}
}
spring.application.name=Mall-auth-server
spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848
server.port=20000
spring.thymeleaf.cache=false
spring.redis.host=192.168.56.10
spring.redis.port=6379