微信公众号开发框架weixin-java-tools不同版本api发生变化

github地址:https://github.com/wechat-group/weixin-java-tools

例如授权回调:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb268f3ac33d62442&redirect_uri=http%3A%2F%2Fpcr5p6.natappfree.cc%2Fmobile%2FweixinCallback&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect

其中的redirect_uri=http%3A%2F%2Fpcr5p6.natappfree.cc%2Fmobile%2FweixinCallback是我外网映射后本地的回调地址。

微信公众号上授权:

1.获取code

2.根据code获取accessToken

3.根据accessToken获取openId

然后将openId和自己的用户库字段openId字段关联,将openId字段的值设置为获取的openId。

2.8.0版本的api如下代码:

@RequestMapping("/weixinCallback")
    public String weixinCallback(String code,HttpServletResponse response, HttpSession httpSession) throws WxErrorException {
        // 步骤
        // 使用code换取token
        WxMpOAuth2AccessToken oauth2getAccessToken = wxService.oauth2getAccessToken(code);
        // 使用token换取用户信息
        WxMpUser oauth2getUserInfo = wxService.oauth2getUserInfo(oauth2getAccessToken, "zh_CN");
        // 判断是否已经授权过,如果没有授权
        String weixinOpenId = oauth2getUserInfo.getOpenId();
        Map<String, Object> reusltWeiXinOpenId= userFeign.findWeiXinOpenId(weixinOpenId);
        // 拿到用户信息,进行关联
        Integer resultCode = (Integer) reusltWeiXinOpenId.get(BaseApiConstants.HTTP_CODE_NAME);
        // 判断是否绑定会员
        if (resultCode.equals(BaseApiConstants.HTTP_200_CODE)) {
            // 已经授权过,自动登录
            String token = (String) reusltWeiXinOpenId.get("data");
            CookieUtil.addCookie(response, Constants.USER_TOKEN, token, Constants.WEBUSER_COOKIE_TOKEN_TERMVALIDITY);
            return INDEX;
        }
        httpSession.setAttribute("weixinOpenId", weixinOpenId);
        httpSession.setAttribute("source", "weixin");
        return ASSOCIATEDACCOUNT;
    }

4.1.0版本的api如下代码:

@RequestMapping("/weixinCallback")
    public String weixinCallback(String code,HttpServletResponse response, HttpSession httpSession) throws WxErrorException {
        // 步骤
        // 使用code换取token
        try {
            WxOAuth2AccessToken oauth2getAccessToken = wxService.getOAuth2Service().getAccessToken(code);
            WxOAuth2UserInfo oauth2getUserInfo = wxService.getOAuth2Service().getUserInfo(oauth2getAccessToken, "zh_CN");
            // 判断是否已经授权过,如果没有授权
            String weixinOpenId = oauth2getUserInfo.getOpenid();
            Map<String, Object> resulttWeiXinOpenId= userFeign.findWeiXinOpenId(weixinOpenId);
            // 拿到用户信息,进行关联
            Integer resultCode = (Integer) resulttWeiXinOpenId.get(BaseApiConstants.HTTP_RES_CODE_NAME);
            // 判断是否绑定会员
            if (resultCode.equals(BaseApiConstants.HTTP_RES_CODE_200)) {
                // 已经授权过,自动登录
                String token = (String) resulttWeiXinOpenId.get("data");
                CookieUtil.addCookie(response, Constants.USER_TOKEN, token, Constants.WEBUSER_COOKIE_TOKEN_TERMVALIDITY);
                return INDEX;
            }
            httpSession.setAttribute("weixinOpenId", weixinOpenId);
            httpSession.setAttribute("source", "weixin");
        } catch (WxErrorException e) {
            e.printStackTrace();
        }
        return ASSOCIATEDACCOUNT;
    }

版本变化,api有所不同。调用时需要注意。

 

posted @ 2021-08-09 19:58  super超人  阅读(234)  评论(0编辑  收藏  举报