微信扫码登录

准备:申请微信开发者,申请认证,获取appid与appserect。

<?php

class WechatAction extends AppbaseAction {

    private $appid = "";
    private $secret = "";
    //private $scope = "snsapi_base";
    private $scope = "snsapi_login";
    
    /继承父类/
    protected function _initialize() {
        parent::_initialize();
    }

    //微信登录,同意授权,获取code
    public function oauthLogin(){
        $appid = $this->appid;
        $scope = $this->scope;
        //回调域
        $redirect_uri = urlencode('http://www.13sai.com'.U('Member/Wechat/oauthCallback'));
        $url = "https://open.weixin.qq.com/connect/qrconnect?appid=$appid&redirect_uri=$redirect_uri&response_type=code&scope=$scope&state=1#wechat_redirect";
        //echo $url;die();
        header("Location:".$url);
    }

    //OAthu2.0 回调函数
    public function oauthCallback(){
        $appid = $this->appid;
        $secret = $this->secret;
        $code = $GET["code"];
        
        //取得openid ,access_token
        $oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appid&secret=$secret&code=$code&grant_type=authorization_code";
        $oauth2 = z_get_json($oauth2Url);
          
        //根据全局access_token和openid查询用户信息  
        $access_token = $oauth2["access_token"];  
        $openid = $oauth2['openid']; 
        //echo $openid; 
        $get_user_info_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN";
  
        $userinfo = z_get_json($get_user_info_url);
        //dump($userinfo);die();
        //判断注册还是登录
        if($userinfo['openid']!=''){
            $row = M("member")->where("m_weixin_unionid='%s'",$userinfo["unionid"])->find();
            if($row){
                session('m_id',$row['m_id']);
                session('m_nick_name',$row['m_nick_name']);
                session('m_login_name',$row['m_login_name']);
                //U('Home/Home/index',null,true,true);
                echo '<script>window.close();window.opener.location.href="/";</script>';
            }else{
                // 创建新用户
                $count = M("member")->max('m_id');
                $count++;
                $rand = mt_rand(1000,9999);
                
                $data['m_sex']  = $userinfo['sex']; 
                $data['m_avatar']  = $userinfo['headimgurl']; 
                $data['m_nick_name']  = mb_substr($userinfo['nickname'],0,6,"utf-8") . $count;
                $data['m_login_name'] = 'wx
'.$rand.''.$count;
                $data['m_password'] = md5(md5(time()));
                $data['m_weixin_unionid']  = $userinfo["unionid"]; 
                $data['from'] = 'weixin';
                echo '<script>window.close();window.opener.location.href="'.U('Member/Index/oauthBind').'";</script>';
                
            }
            
        }else{
            echo $str;
        }
    }
}

其中的z_get_json方法如下:

function z_get_json($url){
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($ch);
    curl_close($ch);
    return json_decode($output, true);
}

前台:

<a href="{:U('Member/Wechat/oauthLogin')}"><img src="/webui/home/images/weixin.png"></a></p>

效果:

QQ截图20161104172914.png


微信扫码登录
https://github.13sai.com/2016/09/21/72/
作者
13sai
许可协议