php做微信扫码登录,验证手机号

//微信登录
public function wx()
{
    $AppID = 'wxa2c7aa8411eb5';
    //        $AppSecret = 'af3b468aef0010fb6e2064bbb028';
    $callback  =  'https://www.zhixiaoya.com/open/callback'; //回调地址
    //微信登录 login.php
    session_start();
    //-------生成唯一随机串防CSRF攻击
    $state  = md5(uniqid(rand(), TRUE));
    $_SESSION["wx_state"]    =   $state; //存到SESSION
    $callback = urlencode($callback);
    //        $wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid={$AppID}&redirect_uri={$callback}&scope=snsapi_login&response_type=code&state={$state}#wechat_redirect";
    $wxurl = "https://open.weixin.qq.com/connect/qrconnect?appid={$AppID}&redirect_uri={$callback}&response_type=code&scope=snsapi_login&state={$state}#wechat_redirect ";
    //        var_dump($wxurl);die;
    header("Location: $wxurl");
    exit;

    //        $redirect_uri="https://test.wd.idianliang.cn/open/callback";
    //        $redirect_uri=urlencode($redirect_uri);//该回调需要url编码
    //        $appID="wx739403d990202";
    //        $scope="snsapi_login";//写死,微信暂时只支持这个值
    准备向微信发请求
    //        $url = "https://open.weixin.qq.com/connect/qrconnect?appid=" . $appID."&redirect_uri=".$redirect_uri
    //            ."&response_type=code&scope=".$scope."&state=STATE#wechat_redirect";
    请求返回的结果(实际上是个html的字符串)
    //        header("Location: $url");
    //        exit;

}
//微信登陆回调函数
public function callback()
{
    if ($_GET['state'] != $_SESSION["wx_state"]) {
        exit("5001");
    }
    $AppID = 'wxa2c7aa8411eb5';
    $AppSecret = 'dd19ec2e977651b1527c81bf3801';
    $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid=' . $AppID . '&secret=' . $AppSecret . '&code=' . $_GET['code'] . '&grant_type=authorization_code';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    $json =  curl_exec($ch);
    curl_close($ch);
    $arr = json_decode($json, 1);
    //得到 access_token 与 openid


    //        Array
    //        (
    //            [access_token] => 39_yxIUUrXFjohWyzkimVz4rm0Km9zQ1F5WduoKK7RXGan_XHQJO9xWGelFb82fV7o774JTVZn8z7WnbRIqquKUFADBqE7Eg
    //        [expires_in] => 7200
    //    [refresh_token] => 39_teS66hlyG9JlDXWOI6kI-5zH4nh_PRMhPck_Pi0LjiTX62G3Fv0LHLNlSGh2nl1QZiLfP0JgNnP_HeLMJP1Qv06KxnrTlWv-H3U0
    //        [openid] => oUHxb59CjjCexpbTiteYfPpo
    //        [scope] => snsapi_login
    //        [unionid] => oFl2d6m-IPDXkRoneuAE3o
    //)

    //        print_r($arr);
    $url = 'https://api.weixin.qq.com/sns/userinfo?access_token=' . $arr['access_token'] . '&openid=' . $arr['openid'] . '&lang=zh_CN';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_URL, $url);
    $json =  curl_exec($ch);
    curl_close($ch);
    $arr = json_decode($json, 1);
    //        得到 用户资料
    //print_r($arr);

    //        Array
    //        (
    //            [openid] => oUHxb59CjjrpteYfPpo
    //            [nickname] => 哈哈😏
    //        [sex] => 1
    //    [language] => zh_CN
    //        [city] => 昌平
    //        [province] => 北京
    //        [country] => 中国
    //        [headimgurl] => https://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eqD1fqTTJQvdFYW0P8NPAYpVoCgX4BkDaGheBNicFBdlPWPr9tk15mtsjJWgXs05jCo3AA5WjqnUNQ/132
    //    [privilege] => Array
    //        (
    //        )
    //
    //        [unionid] => oFl2d6m-IfgmSwgeuAE3o
    //)

    //根据openid去查询数据库存是否注册了
    $opendId = $arr['openid'];
    cookie($opendId, $arr['headimgurl']);
    //        var_dump($opendId);die;
    $memberInfo = M('bussiness')->where(array('open_id' => $opendId))->find();
    if ($memberInfo) {
        //查到用户信息,直接跳转到个人中心
        if ($memberInfo['is_delete'] == 1 || $memberInfo['status'] == 0 || $memberInfo['status'] == 2) {
            $this->error('该帐号被禁用,请联系客服人员!', U('open/mlogin'));
        } else {

            if ($memberInfo['account_type'] != 2) {
                $this->error("非普通用户无法用微信登录!", U('open/mlogin'));
            } else {
                //跳转个人中心
                $res = D('Bussiness')->wxlogin($memberInfo['mobile'], 1);
                if ($res['status'] == 1) {
                    $this->error('登录成功!', U('Personal/normalcenter'));
                } else {
                    $this->error("{$res['status']},请联系客服人员!", U('open/mlogin'));
                }
            }
        }
    } else {
        //未查询到需要绑定手机号注册

        //            $this->error('微信登录请先绑定手机号,稍后跳转手机号绑定页面!', U('open/bind'));
        $this->redirect('open/bind', array('openid' => $opendId), 2, '微信登录请先绑定手机号,稍后跳转手机号绑定页面!');
    }
}
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页