其他分享
首页 > 其他分享> > 谷粒商城认证服务(三十三)

谷粒商城认证服务(三十三)

作者:互联网

211、商城业务-认证服务-环境搭建- 219、商城业务-认证服务-账号密码登录完成

感觉总体也比较简单,就是html太烦事了。

发送短信的代码

    public void sendSms(String phone,String code) {
        String method = "GET";
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        Map<String, String> querys = new HashMap<String, String>();
        querys.put("phone", phone);
        querys.put("sign", sign);
        querys.put("skin", skin);
        querys.put("param", code);
        try {
            HttpResponse response = HttpUtils.doGet(host, path, method, headers, querys);
            System.out.println(response);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

进行注册和登录的代码

  @PostMapping("/regist")
    public String regist(@Valid UserRegistVo vo, BindingResult result, RedirectAttributes redirectAttributes){
        if(result.hasErrors()){
            Map<String, String> errors = result.getFieldErrors().stream().collect(Collectors.toMap(FieldError::getField, FieldError::getDefaultMessage));
            redirectAttributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.gulimall.com/reg.html";
        }

        //1.校验验证码
       String code = vo.getCode();
        String s = redisTemplate.opsForValue().get(AuthServerConstant.SMS_CODE_CACHE_PREFIX + vo.getPhone());
        if(StringUtils.isNotBlank(s)){
            if(code.equals(s.split("_")[0])){
                redisTemplate.delete(AuthServerConstant.SMS_CODE_CACHE_PREFIX + vo.getPhone());
                R r = memberFeignService.regist(vo);
                if(r.getCode() == 0){
                    return "redirect:http://auth.gulimall.com/login.html";
                }else{
                    HashMap<String, String> errors = new HashMap<>();
                    errors.put("msg",r.getData("msg",new TypeReference<String>(){}));
                    System.out.println(errors);
                    redirectAttributes.addFlashAttribute("errors",errors);
                    return "redirect:http://auth.gulimall.com/reg.html";
                }
            }else{
                HashMap<String, String> errors = new HashMap<>();
                errors.put("code","验证码错误");
                redirectAttributes.addFlashAttribute("errors",errors);
                return "redirect:http://auth.gulimall.com/reg.html";
            }
        }else{
            HashMap<String, String> errors = new HashMap<>();
            errors.put("code","验证码错误");
            redirectAttributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.gulimall.com/reg.html";
        }
    }

    @PostMapping("/login")
    public String login(UserLoginVo vo,RedirectAttributes redirectAttributes){
        R login = memberFeignService.login(vo);
        if(login.getCode()==0){
            return "redirect:http://gulimall.com";
        }else{
            HashMap<String, String> errors = new HashMap<>();
            errors.put("msg",login.getData("msg",new TypeReference<String>(){}));
            redirectAttributes.addFlashAttribute("errors",errors);
            return "redirect:http://auth.gulimall.com/login.html";
        }
    }

注:在视频中,认证服务和会员服务进行远程调用的时候,vo类名字不一样,视频直接说可以,我处进行测试的时候,会报错,

所以改成了一样的。

/**
 * @author WGR
 * @create 2020/7/15 -- 21:16
 */
@FeignClient("gulimall-member")
public interface MemberFeignService {

    @PostMapping("/member/member/regist")
     R regist(@RequestBody UserRegistVo vo);

    @PostMapping ("/member/member/login")
     R login(@RequestBody UserLoginVo vo);
}

还有就是gulimall-common中有一个自定义异常了,视频中校验手机号和密码的时候,又定义了2个,感觉有点累赘,我就直接用一个了

@PostMapping ("/regist")
    public R regist(@RequestBody UserRegistVo vo){
        try{
            memberService.regist(vo);
        }catch (RRException e){
            System.out.println(e.getMsg());
            return R.error(e.getCode(),e.getMsg());
        }
        return R.ok();
    }

    @PostMapping ("/login")
    public R login(@RequestBody UserLoginVo vo){
            MemberEntity entity = memberService.login(vo);
            if(entity !=null){
                return R.ok();
            }else{
                return R.error(BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getCode(),BizCodeEnume.LOGINACCT_PASSWORD_INVAILD_EXCEPTION.getMessage());
            }
    }

具体实现:

 @Override
    public void regist(UserRegistVo vo) {
        MemberDao memberDao = this.baseMapper;
        MemberEntity memberEntity = new MemberEntity();
        MemberLevelEntity levelEntity = memberLevelDao.selectOne(new QueryWrapper<MemberLevelEntity>().eq("default_Status", "1"));
        memberEntity.setLevelId(levelEntity.getId());
        memberEntity.setUsername(vo.getUserName());
        memberEntity.setMobile(vo.getPhone());

        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(vo.getPassword());
        memberEntity.setPassword(encode);
        Integer count = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("mobile", vo.getPhone()));
        if(count > 0){
            throw new RRException(BizCodeEnume.PHONE_EXIST_EXCEPTION.getMessage(),BizCodeEnume.PHONE_EXIST_EXCEPTION.getCode());
        }
        Integer count2 = memberDao.selectCount(new QueryWrapper<MemberEntity>().eq("username", vo.getPhone()));
        if(count2 > 0){
            throw new RRException(BizCodeEnume.USER_EXIST_EXCEPTION.getMessage(),BizCodeEnume.USER_EXIST_EXCEPTION.getCode());
        }

        memberDao.insert(memberEntity);

    }

    @Override
    public MemberEntity login(UserLoginVo vo) {
        String loginacct = vo.getLoginacct();
        String password = vo.getPassword();

        MemberDao memberDao = this.baseMapper;
        MemberEntity entity = memberDao.selectOne(new QueryWrapper<MemberEntity>().eq("username", loginacct)
                .or().eq("mobile", loginacct));
        if(entity == null){
            return null;
        }else{
            String passwordDb = entity.getPassword();
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            boolean matches = passwordEncoder.matches(password, passwordDb);
            if(matches){
                return entity;
            }else{
                return null;
            }
        }

    }

标签:errors,三十三,String,vo,return,谷粒,new,login,认证
来源: https://www.cnblogs.com/dalianpai/p/13322452.html