需求: 1、用户可以用微信登录,也可以用手机号码登录,不过第一次用微信的登录的时候,需要绑定手机号码。
    网页授权有时候获取不到openid 的坑网页授权有时候获取不到openid 的坑

 

 


    如果用户点击微信登录并且绑定手机号码,校验通过则登录。
    2、如果退出登录,再次进来选择登录方式,只需点微信登录,便可以登录
方法:
    
    
 1 if($config['bw'] == "weixin" && !isset($_SESSION['openid']) )
 2 {
 3     /**
 4      * 成功调起支付第一步骤:
 5      * 步骤1:网页授权获取用户openid
 6      */
 7     include_once("./pay/module/payment/lib/WxPayPubHelper/WxPayPubHelper.php");
 8     //使用jsapi接口
 9     $jsApi = new JsApi_pub();
10     //通过code获得openid
11     if (!isset($_GET['code']) && !isset($_SESSION['openid']))
12     {
13         //触发微信返回code码
14         
15         $url = $jsApi->createOauthUrlForCode(WxPayConf_pub::JS_API_CALL_URL);
16         Header("Location: $url");
17 
18 
19     }
20     else if(isset($_GET['code']))
21     {
22         //获取code码,以获取openid
23         $code = $_GET['code'];
24         $jsApi->setCode($code);
25         $openid = $jsApi->getOpenId();
26         $_SESSION['openid'] = $openid;
27     }
28 }
首页触发微信返回code码
    
 1 //-----------------
 2     $wx_return = urlencode($config['weburl'] . '/login.php?type=wx&forward='.urlencode($_GET['forward']));//申请用的是taodida.com,注意不能加www
 3     $wx_state = randomkeys(32);
 4     if ($config['bw'] == "weixin") {    //微信客户端
 5 
 6         $wx_mobile_appid = $config['wx_mobile_app_id'];
 7         $wx_mobile_secret = $config['wx_mobile_key'];
 8         //$wx_mobile_appid = $config['wx_app_id'];
 9         //$wx_mobile_secret = $config['wx_key'];
10         $wx_scope = 'snsapi_userinfo';//snsapi_base不弹出授权页面,直接跳转,只能获取用户openid
11         //$scope='snsapi_userinfo';//需要授权snsapi_userinfo (弹出授权页面,可通过openid拿到昵称、性别、所在地。并且,即使在未关注的情况下,只要用户授权,也能获取其信息)
12         $wx_url = "https://open.weixin.qq.com/connect/oauth2/authorize?"
13             . "appid=$wx_mobile_appid"
14             . "&redirect_uri=$wx_return"
15             . "&response_type=code"
16             . "&scope=$wx_scope"
17             . "&state=$wx_state"
18             . "#wechat_redirect";
19         $takenid = @get_url_contents($wx_url);
20         //$takenid = addslashes($takenid);
21 
22         if ($_GET['type'] == 'wx' && isset($_REQUEST['code'])) {
23 
24             //----------------
25             $wx_code = $_GET["code"];
26             $get_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?"
27                 . "appid=$wx_mobile_appid"
28                 . "&secret=$wx_mobile_secret"
29                 . "&code=$wx_code"
30                 . "&grant_type=authorization_code";
31             $con = get_url_contents($get_token_url);
32             $ar2 = json_decode($con, true);
33             //----------------
34             //获取用户信息,不需要关注公众号
35             if(empty($ar2['openid'])){
36                 header("location:index.php");exit;
37             }
38             $access_token = $ar2['access_token'];
39             $openid = $ar2['openid'];
40             $get_user_info_url =  'https://api.weixin.qq.com/sns/userinfo?access_token=' . $access_token . '&openid=' . $openid . '&lang=zh_CN';
41             $con = get_url_contents($get_user_info_url);
42             $ar = json_decode($con, true);//获取微信名,头像,性别
网页授权代码

相关文章: