Controller
@ApiOperation(value = "手机或邮箱注册(LB)", notes = "若注册成功,返回token")
@ApiImplicitParam(name = "param", value = "注册参数", required = true, dataType = "RegisterParamV2", paramType = "body")
@PostMapping("register/v3")
@ApiResponses({
@ApiResponse(code = 1014, message = "该手机号已存在"),
@ApiResponse(code = 1015, message = "该邮箱已存在")
})
public ResponseData registerV3(@RequestBody @Valid RegisterParamV2 param, BindingResult bindingResult,HttpServletRequest request) {
if (bindingResult.hasErrors()) {
return errorMsg(bindingResult);
}
User user = userService.register(MyUtil.getIpAddr(request),param);
String accessToken = UUID.randomUUID().toString().replace("-", "");
CurrentInfo currentInfo = new CurrentInfo(user.getId(), user.getUsername(), user.getNickname(), accessToken);
redisTemplate.opsForValue().set(RedisKeyPrefix.LOGIN_TOKEN_KEY_PREFIX + accessToken, currentInfo);
redisTemplate.expire(RedisKeyPrefix.LOGIN_TOKEN_KEY_PREFIX + accessToken, 7, TimeUnit.DAYS);
LoginVO loginVO = new LoginVO();
loginVO.setAccessToken(accessToken);
loginVO.setStatus(user.getStatus());
return ResponseData.ok(loginVO);
}
Service
@Override
@Transactional(rollbackFor = Exception.class)
public User register(String ip,RegisterParamV2 param) {
//注册之前核对验证码
ApiResult apiResult = commonService.checkCode(SendCodeType.REGISTER.ordinal(), param.getUsername(), param.getCode());
if (apiResult.equals(ApiResult.SUCCESS)){
boolean isEmail = false;
boolean isPhone = false;
if (ReUtil.isMatch(Constant.PHONE_REGEXP, param.getUsername())) {
isPhone = true;
}
if (ReUtil.isMatch(Constant.EMAIL_REGEXP, param.getUsername())) {
isEmail = true;
}
if (!isEmail && !isPhone) {
throw new BusinessException(ApiResult.PARAM_IS_ILLEGAL);
}
Example example = new Example(User.class);
//加入手机号还是邮箱判断逻辑
if(isPhone){
example.createCriteria().andEqualTo("username", param.getUsername());
}
if(isEmail){
example.createCriteria().andEqualTo("email", param.getUsername());
}
if (userMapper.selectCountByExample(example) > 0) {
throw new BusinessException(ApiResult.USERNAME_IS_REGISTERED);
}
User user = new User();
user.setCreateDate(new Date());
user.setModifyDate(user.getCreateDate());
user.setDeleted(false);
user.setRegisteredTime(user.getModifyDate());
user.setPassword(DigestUtils.md5Hex(param.getPassword()));
user.setStatus(true);
user.setType(param.getType());
user.setUsername(param.getUsername());
//如果是邮箱注册 把是邮箱的用户名加入到用户email字段中
if(isEmail){
user.setEmail(param.getUsername());
}
user.setAreaId(areaService.getAreaIdByIp(ip));
userMapper.insert(user);
return user;
}
return null;
}
ConmmonService:
@Override
public ApiResult checkCode(Integer type, String phoneOrEmail, String code) {
String prefix;
if (SendCodeType.REGISTER.ordinal() == type) {
prefix = RedisKeyPrefix.USER_REGISTER_PREFIX + phoneOrEmail;
} else if (SendCodeType.FORGET_PWD.ordinal() == type) {
prefix = RedisKeyPrefix.USER_FORGET_PWD_PREFIX + phoneOrEmail;
} else if (type.equals(SendCodeType.BIND.ordinal())) {
prefix = RedisKeyPrefix.USER_BIND_PHONE_PREFIX + phoneOrEmail;
} else {
prefix = RedisKeyPrefix.USER_UPDATE_PREFIX + phoneOrEmail;
}
if (!code.equals(redisTemplate.opsForHash().get(prefix, "code"))) {
throw new BusinessException(ApiResult.UN_CORRECT_VERIFY_CODE);
}
return ApiResult.SUCCESS;
}
public class RedisKeyPrefix {
/**
* 用户登录token
*/
public static final String LOGIN_TOKEN_KEY_PREFIX = "user:login:";
/**
* 注册验证码
*/
public static final String USER_REGISTER_PREFIX = "user:register:code:";
/**
* 忘记密码验证码
*/
public static final String USER_FORGET_PWD_PREFIX = "user:forgetPwd:code:";
/**
* 修改密码
*/
public static final String USER_UPDATE_PREFIX = "user:updatePwd:code:";
/**
* 绑定手机号
*/
public static final String USER_BIND_PHONE_PREFIX = "user:bindPhone:code:";
/**
* 提醒发货次数
*/
public static final String ORDER_REMIND_SHIP_COUNT_PREFIX = "sw:orderRemindShipCount:";
public static final String SHARE_COOKBOOK_CODE = "sw:share:code:";
}