微信授权回调获取用户信息

    @GetMapping("/userInfo")
    @ApiOperation(value = "微信授权回调", httpMethod = "GET", produces = "application/json;charset=UTF-8")
    public ServerResponse weixinCallBack(String code) throws IOException {
    
    
        if (StrUtil.isBlank(code)) {
    
    
            return ServerResponse.createServerResponseByFail("code为空!");
        }
        return wxService.WxLogin(code);
    }

public ServerResponse WxLogin(String code) throws IOException {
    
    

        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=";
        StringBuffer stringBuffer = new StringBuffer(url);
        stringBuffer.append(appidgzh);
        stringBuffer.append("&secret=");
        stringBuffer.append(secretgzh);
        stringBuffer.append("&code=");
        stringBuffer.append(code);
        stringBuffer.append("&grant_type=authorization_code");

        JSONObject jsonObject = AuthUtil.doGetJson(stringBuffer.toString());
        //存在错误
        if(jsonObject.has("errcode")){
    
    
            Integer errcode = jsonObject.getInt("errcode");
            if(null != errcode){
    
    
                String errmsg = jsonObject.getString("errmsg");
                return ServerResponse.createServerResponseByFail(errcode,errmsg);
            }
        }

        //1.获取微信用户的openid
        String openid = jsonObject.getString("openid");
        //2.获取获取access_token
        String access_token = jsonObject.getString("access_token");
        //info Url
        String infoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=";
        StringBuffer infoStringBuffer = new StringBuffer(infoUrl);
        infoStringBuffer.append(access_token);
        infoStringBuffer.append("&openid=");
        infoStringBuffer.append(openid);
        infoStringBuffer.append("&lang=zh_CN");

        //3.获取微信用户信息
        JSONObject userInfo = AuthUtil.doGetJson(infoStringBuffer.toString());
        //unionid
        String unionid = jsonObject.getString("unionid");
        //去数据库查询此微信是否注册过
        SUser sUser = sUserMapper.selectUserByOpenId(unionid);
        Map map = new HashMap();
        //没注册过的新用户
        if (null == sUser) {
    
    
            //添加新用户
            SUser insetUser = new SUser();
            insetUser.setUnionid(unionid);
            insetUser.setUuid(UUID.randomUUID().toString());
            insetUser.setSystemName(userInfo.getString("nickname"));
            //用户表添加
            try {
    
    
                sUserMapper.insertIntoUser(insetUser);
            }catch (Exception e){
    
    
                StringBuffer nickNameBuffer = new StringBuffer("表情符:");
                for (int i = 1; i < 3; i++) {
    
    
                    nickNameBuffer.append(getRandomChar());
                }

                log.info("用户名涉及表情符,将自动生成汉字;"+"生成的nickName为"+nickNameBuffer.toString());

                insetUser.setSystemName(nickNameBuffer.toString());
                sUserMapper.insertIntoUser(insetUser);
            }

            //根据服务号id查平台uuid
            String platformUuid = sPlatformMapper.selectUuidByAppid(appidgzh);
            int count = sUserPlatformMapper.selectIsExistUser1(openid,platformUuid);
            if (count < 1) {
    
    
                sUserPlatformMapper.insert(insetUser,platformUuid,openid);
            }
            //向用户、平台表中添加数据
            //生成token
            String userToken = authorizationService.createAccessIdToken(insetUser.getUuid());
            //返回前端map
            map.put("token",userToken);
            return ServerResponse.createServerResponseByFail("新用户请绑定手机号",map);

        }else {
    
    
            //生成token
            String userToken = authorizationService.createAccessIdToken(sUser.getUuid());
            map.put("token",userToken);
            if(null == sUser.getPhone() || "".equals(sUser.getPhone())){
    
    
                return ServerResponse.createServerResponseByFail("请绑定手机号",map);
            }else {
    
    
                return ServerResponse.createServerResponseBySuccess("ok",map);
            }
        }
    }

随机生成汉字


    //随机生成汉字
    private static char getRandomChar() {
    
    
        String str = "";
        int hightPos; //
        int lowPos;

        Random random = new Random();

        hightPos = (176 + Math.abs(random.nextInt(39)));
        lowPos = (161 + Math.abs(random.nextInt(93)));

        byte[] b = new byte[2];
        b[0] = (Integer.valueOf(hightPos)).byteValue();
        b[1] = (Integer.valueOf(lowPos)).byteValue();

        try {
    
    
            str = new String(b, "GBK");
        } catch (UnsupportedEncodingException e) {
    
    
            e.printStackTrace();
            System.out.println("错误");
        }

        return str.charAt(0);
    }

猜你喜欢

转载自blog.csdn.net/KuKu_Nao/article/details/121414557