✅P216_商城业务-认证服务-异常机制

gong_yz大约 2 分钟谷粒商城

校验验证码

cfmall-auth-server/src/main/java/com/gyz/cfmall/controller/LoginController.java

    @PostMapping("/register")
    @ResponseBody
    public String register(@Valid UserRegisterVo registerVo, BindingResult bindingResult, RedirectAttributes attributes) {
        if (bindingResult.hasErrors()) {
            Map<String, Object> errors = bindingResult.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            attributes.addFlashAttribute("errors", errors);
            return "redirect:http://auth.cfmall.com/reg.html";
        }

        //校验验证码
        String code = registerVo.getCode();
        //获取存入Redis里的验证码
        String redisCode = stringRedisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + registerVo.getPhone());
        if (!StringUtils.isEmpty(redisCode)) {
            //截取字符串
            if (code.equals(redisCode.split("_")[0])) {
                //删除验证码;令牌机制
                stringRedisTemplate.delete(AuthServerConstant.SMS_CODE_CACHE_PREFIX + registerVo.getPhone());
                //验证码通过,真正注册,调用远程服务进行注册

            } else {
                //效验出错回到注册页面
                Map<String, String> errors = new HashMap<>();
                errors.put("code", "验证码错误");
                attributes.addFlashAttribute("errors", errors);
                return "redirect:http://auth.cfmall.com/reg.html";
            }
        } else {
            //效验出错回到注册页面
            Map<String, String> errors = new HashMap<>();
            errors.put("code", "验证码错误");
            attributes.addFlashAttribute("errors", errors);
            return "redirect:http://auth.cfmall.com/reg.html";
        }
        return "redirect:/login.html";
    }

会员服务

编写注册VO

cfmall-member/src/main/java/com/gyz/cfmall/member/vo/MemberUserRegisterVo.java

package com.gyz.cfmall.member.vo;

import lombok.Data;

@Data
public class MemberUserRegisterVo {
    private String userName;

    private String password;

    private String phone;
}

会员用户注册接口

cfmall-member/src/main/java/com/gyz/cfmall/member/controller/MemberController.java

    @Autowired
    private MemberService memberService;

	@PostMapping(value = "/register")
 	public R register(@RequestBody MemberUserRegisterVo vo) {
     	memberService.register(vo);
     	return R.ok();
 	}

查询会员默认等级

cfmall-member/src/main/java/com/gyz/cfmall/member/service/impl/MemberServiceImpl.java

	@Override
    public void register(MemberUserRegisterVo vo) {
        MemberEntity memberEntity = new MemberEntity();
        MemberLevelEntity memberLevel = memberLevelDao.selectMemberLevel();
        memberEntity.setLevelId(memberLevel.getId());
    }

cfmall-member/src/main/resources/mapper/member/MemberLevelDao.xml

<select id="selectMemberLevel" resultType="com.gyz.cfmall.member.entity.MemberLevelEntity">
    SELECT * FROM ums_member_level WHERE default_status = 1
</select>

检查用户名和手机号是否唯一

这里采用异常机制处理,如果查出用户名或密码不唯一则向上抛出异常

cfmall-member/src/main/java/com/gyz/cfmall/member/exception

public class PhoneException extends RuntimeException {

    public PhoneException() {
        super("存在相同的手机号");
    }
}
public class UserNameException extends RuntimeException {

    public UserNameException() {
        super("存在相同的用户名");
    }
}

方法编写

cfmall-member/src/main/java/com/gyz/cfmall/member/service/impl/MemberServiceImpl.java

	@Override
    public void checkPhoneUnique(String phone) throws PhoneException {
        Integer phoneCount = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", phone));

        if (phoneCount > 0) {
            throw new PhoneException();
        }
    }

    @Override
    public void checkUserNameUnique(String userName) throws UserNameException {
        Integer usernameCount = this.baseMapper.selectCount(new QueryWrapper<MemberEntity>().eq("username", userName));

        if (usernameCount > 0) {
            throw new UserNameException();
        }
    }
    @Override
    public void register(MemberUserRegisterVo vo) {
        MemberEntity memberEntity = new MemberEntity();
        MemberLevelEntity memberLevel = memberLevelDao.selectMemberLevel();
        memberEntity.setLevelId(memberLevel.getId());

        //检查用户名和手机号是否唯一
        checkUserNameUnique(vo.getUserName());
        checkPhoneUnique(vo.getPhone());
        memberEntity.setUsername(vo.getUserName());
        memberEntity.setMobile(vo.getPhone());
    }

如果抛出异常,则进行捕获

cfmall-member/src/main/java/com/gyz/cfmall/member/controller/MemberController.java

    @PostMapping(value = "/register")
    public R register(@RequestBody MemberUserRegisterVo vo) {
        try {
            memberService.register(vo);

        } catch (Exception e) {
            //TODO
        }
        return R.ok();
    }

密码的设置,前端传来的密码是明文,存储到数据库中需要进行加密,后面会说到