网站应用微信开放平台二维码扫描登录配置流程步骤

2023-06-01 00:00:00 扫描 步骤 流程

微信公众号跟微信开放平台是两个东西,各位老铁要搞清楚

微信公众号扫码是跳公众号内页

微信开放平台只要是网站都可以跳


开放平台需要以下东西

1.开发者认证(300rmb)

2.创建网站应用(需要公司盖章,个人劝退)

  生成appid,appsecret

3.审核通过到,就配置你的授权回调域了,无限制


简单测试代码 (我用的是laravel)


//生成微信登录二维码

Route::get('/wxcode', function () {
     $appid = 'xx';
     $appsecret = 'xx';
     $return_url = "https://www.zongscan.com/wxcallback";
     
     //pc二维码
     $url = "https://open.weixin.qq.com/connect/qrconnect?appid=".$appid."&redirect_uri=".urlencode($return_url)."&response_type=code&scope=snsapi_login&state=123#wechat_redirect";
     return redirect($url);
});


//回调地址


Route::get('/wxcallback', function () {
        $appid = 'xx';
        $appsecret = 'xx';
            //获取code换access_token
            $code = $_GET['code'];
            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
            $res_one = json_decode(file_get_contents($url),true);
            $access_token = $res_one['access_token'];
            $openid = $res_one['openid'];
            
            //获取详细信息
            $url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$access_token."&openid=".$openid."&lang=zh_CN";
            $res = json_decode(file_get_contents($url),true);
            var_dump($res);
           
            /*array(10) {
                ["openid"]=> string(28) "ho-XfE5qIHB3velLGGBzCIsPCuUpM"
                ["nickname"]=> string(3) "宗"
                ["sex"]=> int(1)
                ["language"]=> string(5) "zh_CN"
                ["city"]=> string(6) "广州"
                ["province"]=> string(6) "广东"
                ["country"]=> string(6) "中国"
                ["headimgurl"]=> string(135) "https://thirdwx.qlogo.cn/mmopen/vi_32/t2xumPLOLI62HiacX77rSBiaMiaGbJ8FIGbrzCgq4QOGmClSYZACBU3kiay3r2O2PDEiac72qb9Aeia30PibXe6LMC2CXA/132"
                ["privilege"]=> array(0) { }
                ["unionid"]=> string(28) "zoqFKzw0L9oQq-JSxG-RTGsTzsH1o" }
            */
           
        $user = new App\Models\User;
        $user = $user->where('weixin_unionid','zoqFKzw0L9oQq-JSxG-RTGsTzsH1o')->first();
       
        dd($user);
       
        判断新用户还是老用户
           入库
           写session
           ...

           return redirect('xx');
});



相关文章