微信三方登录与注册逻辑处理

微信,作为三方接口的处理逻辑。以下是个人项目处理的逻辑,欢迎大家吐槽!
微信三方登录与注册逻辑处理_第1张图片
这边展示项目部分代码

 /**
     * 微信 登录与注册
     * @param request  请求
     * @param code     要解析的code,或 已解析的 openid
     * @param nickName 昵称
     * @return
     */
    public Map wxLogin(HttpServletRequest request,String code,String nickName){
        Map map=new HashMap<>();
        //判断是否存在请求头
        String key=CookieUtil.getKeyFromTokenCookie(request);
        User user=null;
        if (key ==""){ //请求头无值
            JsCodeDto jsCodeDto= WeChatUtil.getCode(code);//解析 code
            ThrowException.illegal(jsCodeDto.getOpenid()==null,"code解析失败");
            //判断openid是否存在(是否注册过)
            Third third = thirdMapper.queryThirdByOpenId(jsCodeDto.getOpenid());
            if (third==null){
                //不存在,做注册注册操作
                user=new User();
                user.setOrg(0);
                user.setIsValid(0);
                //时间处理
                Date date=new Date();
                Calendar rightNow = Calendar.getInstance();
                rightNow.setTime(date);
                rightNow.add(Calendar.DAY_OF_YEAR,30);//日期加30天
                Date addDate=rightNow.getTime();
                //写入时间
                user.setExpireDate(addDate);
                user.setCreateDate(date);
                user.setUpdateDate(date);
                ThrowException.illegal(userMapper.insertSelective(user)<1,"添加用户表失败!");
                //写入third
                third=new Third();
                third.setUserId(user.getId());
                third.setNickname(nickName);
                third.setOpenid(jsCodeDto.getOpenid());
                third.setSessionKey(jsCodeDto.getSession_key());
                int row= thirdMapper.insertSelective(third);
                if (row<1){
                    userMapper.deleteByPrimaryKey(user.getId());
                    new ParamsException("添加三方表失败");
                }

                map.put("uuid",setRedis(user,nickName));//写入redis
                map.put("openid",jsCodeDto.getOpenid());

            }else{
                //存在,做查询操作
                user=userMapper.selectByPrimaryKey(third.getUserId());
                map.put("uuid",setRedis(user,nickName));
                map.put("openid",third.getOpenid());
            }
        }else{//有请求头,表明之前注册过
            Long expire=redisUtil.getExpire(key);//判断是发存在redis
            if (expire<=0){//时间已经过期
                Third third = thirdMapper.queryThirdByOpenId(code);//code就是openid
                user=userMapper.selectByPrimaryKey(third.getUserId());
                map.put("uuid",setRedis(user,nickName));
                map.put("openid",code);
            }else{//存在
                map.put("uuid",key);
                map.put("openid",code);
            }

        }
        return map;
    }

你可能感兴趣的:(小程序,小程序)