小程序+公众号统一账号unionid,实现pc+公众号+小程序统一身份

一、微信开放平台  注册开发者账号、绑定公众号、小程序

二、小程序端获取unionid

1获取code

    wx.login({
      success: res => {
        console.log("getCode", res.code)
        this.getOpenId(res.code)
      }
    })

2通过code调用后台方法获取openid,unionid

小程序端 

  getOpenId: function (code) {

    let url = config.apiUrl + "/wx/user/getOpenId";
    var param = {
      "code": code
    };
    util.httpPost(url, param).then((res) => {
      console.log(res);
    })
  }, 

java端 

    @RequestMapping(value = "/getOpenId", method = RequestMethod.POST)
    public Result<?> callBack(@RequestBody WeixinModel model) throws ServletException, IOException {

        System.out.println("start");
        String code = model.code;
        //第二步:通过code换取网页授权access_token
        String url = "https://api.weixin.qq.com/sns/jscode2session?appid="+ appid1
                + "&secret="+ secret1
                + "&js_code="+code
                + "&grant_type=authorization_code";

        System.out.println("url:"+url);
        JSONObject jsonObject = WeixinHelper.doGetJson(url);
        return Result.ok(jsonObject);
    }

效果

三、公众号通过openid获取unionid

1获取accss_token

public static String getNewAccessToken() {
        String access_token = "";
        String grant_type = "client_credential";//获取access_token填写client_credential
        String url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=" + grant_type + "&appid=" + appId + "&secret=" + secret;
        try {
            URL urlGet = new URL(url);
            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
            http.setRequestMethod("GET"); // 必须是get方式请求
            http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
            http.connect();
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String message = new String(jsonBytes, "UTF-8");
            JSONObject demoJson = JSONObject.parseObject(message);
            System.out.println("JSON字符串:" + demoJson);
            access_token = demoJson.getString("access_token");
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return access_token;
    }

2通过openid获取unionid

private static String getUnionId(String openId) {
        String result = null;
        String url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" + accessToken + "&openid=" + openId +  "&lang=zh_CN";//这个url链接和参数不能变
        try {
            URL urlGet = new URL(url);
            HttpURLConnection http = (HttpURLConnection) urlGet.openConnection();
            http.setRequestMethod("GET"); // 必须是get方式请求
            http.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
            http.setDoOutput(true);
            http.setDoInput(true);
            System.setProperty("sun.net.client.defaultConnectTimeout", "30000");// 连接超时30秒
            System.setProperty("sun.net.client.defaultReadTimeout", "30000"); // 读取超时30秒
            http.connect();
            InputStream is = http.getInputStream();
            int size = is.available();
            byte[] jsonBytes = new byte[size];
            is.read(jsonBytes);
            String message = new String(jsonBytes, "UTF-8");
            JSONObject demoJson = JSONObject.parseObject(message);
            System.out.println("JSON字符串:" + demoJson);
            result = demoJson.getString("unionid");
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

测试

    public static void main(String[] args) throws ClientException, InterruptedException {

        WeixinHelper.setAppId("wx88e7974f9***8be");
        WeixinHelper.setSecret("0b73d127be64****416cd6eb105c20dc");

        String accessToken = getAccessToken();
        System.out.println(accessToken);

        String unioinId =  getUnionId("ots5M6wVAgsx5B****eJrfjajiE");


    }

效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我是唐赢

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值