微信授权登录

“`
public function weiXinCallback( code=0, state = 0) {
if(code != 0){  
            // 获取域名.
config = \MClient\Text::inst(‘api’)->setClass(‘SiteConfig’)->getSiteConfig(“siteurl”, [“id” => 1]);
domain=!empty( config[‘siteurl’]) ? $config[‘siteurl’] : ‘http://demo.meilibo.net‘;

        if (count($arr = explode("|", $state))) {
            $state = $arr[0]; // 操作类型.
            $curRoomNum = $arr[1];
            // 下面数据与代理有关.
            $o_id   = $arr[2]?$arr[2]:1;
            $a_id   = $arr[3]?$arr[3]:1;
            $b_id   = $arr[4]?$arr[4]:1;
            $c_id   = $arr[5]?$arr[5]:1;
            $mobile = $arr[6]?$arr[6]:0;
        }else{
            $curRoomNum = "hall"; // 默认进大厅.
        }
        switch ($state) {
            case "ShareLogin":
                $appId     = static::$MP_WEIXIN['APPID'];
                $AppSecret = static::$MP_WEIXIN['APPSECRET'];
                $data = array(
                    "appid"      => $appId,
                    "secret"     => $AppSecret,
                    "code"       => $code,
                    "grant_type" => "authorization_code"
                );
                $url = "https://api.weixin.qq.com/sns/oauth2/access_token";
                $json = $this->curlRequest($url, false, $data);
                $json = json_decode($json);
                file_put_contents(".a.log", 'AAAA'.PHP_EOL, FILE_APPEND);
                file_put_contents(".a.log", $json.PHP_EOL, FILE_APPEND);
                $info_data = array(
                    "access_token" => $json->access_token,
                    "openid"       => $json->openid,
                    "lang"         => "zh_CN"
                );

                $info_url = "https://api.weixin.qq.com/sns/userinfo";
                $info = $this->curlRequest($info_url, false, $info_data);
                $info = json_decode($info);
                $info->web = 1;//之後區分是分享頁登錄還是APP微信登錄
                if(isset($_COOKIE['referee_id'])) {
                    $info->referee_id = $_COOKIE['referee_id'];
                }
                $info = json_encode($info);
                if(!empty($c_id)&&$c_id !=1){
                    $login_data = array(
                        "openid"  => $json->openid,
                        "type"    => "wechat",
                        "payload" => $info,
                        "param"   => json_encode(array("o_id"=>$o_id,"a_id"=>$a_id,"b_id"=>$b_id,"c_id"=>$c_id,"mobile"=>$mobile))
                    );
                }else{
                    $login_data = array(
                        "openid"  => $json->openid,
                        "type"    => "wechat",
                        "payload" => $info
                    );
                }
                $login_url = $domain."/OpenAPI/V1/Auth/login";
                $userInfo = $this->curlRequest($login_url, true, $login_data);
                $userInfo = json_decode($userInfo, true);
                file_put_contents(".a.log", 'BBBB'.PHP_EOL, FILE_APPEND);
                file_put_contents(".a.log", $userInfo.PHP_EOL, FILE_APPEND);
                $_SESSION['uid'] = $userInfo['data']['id'];
                $_SESSION['token'] = $userInfo['data']['token'];
                //微信大厅登录
                if ($curRoomNum == 'hall') {
                    $back_url = $domain."/app/wx_index?uid=".$userInfo['data']['id'];
                    echo "<script>window.location.href='".$back_url."'</script>";
                    break;
                }
                $back_url = $domain."/app/share";
                echo "<script>window.location.href='".$back_url."?current_room=".$curRoomNum."'</script>";
                break;

            case "login": //如果为绑定公众号登录
                file_put_contents("/tmp/wx.log","code:".$code);
                $appid = static::$MP_WEIXIN['APPID'];
                $AppSecret = static::$MP_WEIXIN['APPSECRET'];
                $data = array(
                    "appid" => $appid,
                    "secret" => $AppSecret,
                    "code" => $code,
                    "grant_type" => "authorization_code"
                );
                $url = "https://api.weixin.qq.com/sns/oauth2/access_token";
                $json = $this->curlRequest($url, false, $data);
                file_put_contents("/tmp/wx.log","access_token:".$json.PHP_EOL,FILE_APPEND);
                $json = json_decode($json);
                $info_data = array(
                    "access_token" => $json->access_token,
                    "openid" => $json->openid,
                    "lang" => "zh_CN"
                );

                $info_url = "https://api.weixin.qq.com/sns/userinfo";
                $info = $this->curlRequest($info_url, false, $info_data);
                file_put_contents("/tmp/wx.log","info:".$info.PHP_EOL,FILE_APPEND);
                $info = json_decode($info);
                $unionid = $info->unionid;
                $userWeiXinExists =  D('Member')->where(array('wxunionid' => $unionid))->field("id, beanbalance,agentuid")->find();
                $userQQExists =  D('Member')->where(array('wxunionid' => $unionid."_qq"))->field("id, beanbalance,agentuid")->find();
                $userMobileExists =  D('Member')->where(array('wxunionid' => $unionid."_mobile"))->field("id, beanbalance,agentuid")->find();
                // 如果没有任何一种登录方式绑定过微信.
                if (empty($userWeiXinExists) && empty($userQQExists) && empty($userMobileExists)) {
                    echo "您的微信还未绑定APP直播账号,打开APP直播的app-我的主页-收益-进行微信授权绑定";exit();
                }
                //如果已经绑定过微信,有wxunionid,绑定openid
                $info = json_decode($info);
                $openid = $json->openid;
                $uidArr = array();
                $typeArr = array();
                //如果为手机号注册的用户
                if (!empty($userMobileExists)) {
                    $unionid = $unionid."_mobile";
                    $uidArr[] = $userMobileExists["id"];
                    if ($userMobileExists["agentuid"] !=0){
                        $typeArr[] = "agent";
                    } else {
                        $typeArr[] = "normal";
                    }
                    D('Member')->where(array('wxunionid' => $unionid))->save(array("wxopenid"=>$openid));
                }
                //QQ
                if (!empty($userQQExists)) {
                    $unionid = $unionid."_qq";
                    $uidArr[] = $userQQExists["id"];
                    if ($userQQExists["agentuid"] !=0){
                        $typeArr[] = "agent";
                    } else {
                        $typeArr[] = "normal";
                    }
                    D('Member')->where(array('wxunionid' => $unionid))->save(array("wxopenid"=>$openid));
                }
                // 微信
                if (!empty($userWeiXinExists)) {
                    $uidArr[] = $userWeiXinExists["id"];
                    if ($userWeiXinExists["agentuid"] !=0){
                        $typeArr[] = "agent";
                    } else {
                        $typeArr[] = "normal";
                    }
                    D('Member')->where(array('wxunionid' => $unionid))->save(array("wxopenid"=>$openid));
                }
                $_SESSION['uidArr'] = $uidArr;
                $_SESSION['check_time'] = time();
                $_SESSION["typeArr"] = $typeArr;
                $url = $domain."/app/personInfo";
                // 绑定成功以后. 跳转到个人余额页面.
                echo "<script>window.location.href='".$url."'</script>";
        }
    } else {
        $this->responseError(L('_DATA_TYPE_INVALID_'));
    }

}```
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值