公众号支付 微信支付 小程序支付 h5支付开发连载(一):获取用户openid

版权声明:本文为博主原创文章,如需转载,敬请注明转载链接 https://blog.csdn.net/guobinhui/article/details/85336962

从本期开发,笔者将会把公众号里实现网页h5支付的全流程做个连载详细教程(微信支付和小程序支付流程基本一样,同样可以参考本连载教程学习),本教程服务端是java语言,采用现在主流的springmvc框架,感兴趣的开发者可以一起参与进来与笔者共同学习。

基本思路:登陆公众号服务号(订阅号也可以)后台配置一个网页路径,用户通过公众号菜单入口打开h5网页,授权微信网页(有2种授权方式),后台获取该用户和该公众号之间的唯一标识openid ,获取到这个唯一标识后才可以进行其余的业务操作,建立用户和该公众号之间的业务数据绑定关系。

1、配置公众号菜单,接口地址:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=APPID&redirect_uri=REDIRECT_URI&response_type=code&scope=SCOPE#wechat_redirect

根据微信官方文档的这个接口,我们可以在自己的公众号配置这样一个测试菜单,地址为:

https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx12345678910&redirect_uri=http://192.168.0.109:8070/home&response_type=code&scope=snsapi_base#wechat_redirect

这里的appid替换成自己公众号的appid即可,redirect_uri这个参数的值填写实际需要返回h5页面的接口地址,比如我这里的是通过controller的路径为home的接口返回页面。

2、用户同意授权,后端接口获取code

    /**
     * 公众号网页授权,获取code和openid
     */
    @RequestMapping("/home")
    public ModelAndView register(HttpServletRequest request) throws IOException {
        request.setCharacterEncoding("utf-8");
        String code = request.getParameter("code");
        AccessToken token = WeiXinUtil.getAccessToken(code);
        String openid = token.getOpenId();//用户和该公众号之间的唯一标识
        ModelAndView mv = new ModelAndView();
        mv.setViewName("home");
        mv.addObject("openid",openid);
        return mv;
    }

这里获取到code后,根据code继续获取access_token,这里封装一个具体的工具类

这里把获取access_token的微信接口定义一个常量

private static String ACCESS_TOKEN_URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=APPSECRET&code=CODE&grant_type=authorization_code";

 根据appid,appsecret以及上面获取到的code得到网页授权access_token,注意这里的access_token和普通的access_token不一样,区别详见《关于网页授权access_token和普通access_token的区别》

    /**
     * 此处通过code获取的access_token用于公众号网页授权,获取用户openid
     */
    public static AccessToken getAccessToken(String code) throws IOException{
        AccessToken token =  new AccessToken();
        String url = ACCESS_TOKEN_URL.replace("APPID",Constants.HQAPPID).replace("APPSECRET",Constants.HQSECRET).replace("CODE",code);//
        JSONObject jsonObj = doGetStr(url);
        if(!StringUtils.isEmpty(jsonObj)){
            token.setToken(jsonObj.getString("access_token"));
            token.setExpiresIn(jsonObj.getInt("expires_in"));
            token.setOpenId(jsonObj.getString("openid"));
            token.setRefreshToken(jsonObj.getString("refresh_token"));
        }
        return token;
    }
    public static JSONObject doGetStr(String url) throws IOException{
        HttpClient httpClient = new DefaultHttpClient();
        HttpGet  httpGet = new HttpGet(url);//HttpGet使用Get方式发送请求URL
        JSONObject jsonObj = null;
        HttpResponse  res = httpClient.execute(httpGet);//使用httpClient从Client执行httpGet的请求
        HttpEntity  entity = res.getEntity();//从HttpResponse中获取结果
        if(!StringUtils.isEmpty(entity)){
          String result =   EntityUtils.toString(entity,"utf-8");
            jsonObj = JSONObject.fromObject(result);//字符串类型转换为JSON对象
        }
        return jsonObj;
    }

 至此,获取公众号的用户openid的过程就完成了,下一节分享h5支付的其他相关教程

猜你喜欢

转载自blog.csdn.net/guobinhui/article/details/85336962