php微信网页开发实现自动登录注册功能实例

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hj960511/article/details/83821081

功能:自动登录注册功能
描述:php实现微信网页自动登录注册功能
范围:适用于所有php版本

thinkphp5.0实例

$token = cookie('token');
if($token){
	//这里写登录后的逻辑
 }else{
            $code = isset($_REQUEST['code'])?$_REQUEST['code']:'';
            if($code){
            //自动登录
            $appId = sysconf('wechat_appid');
            $appSecret = sysconf('wechat_appsecret');
            $code =input('get.code');
            $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appId&secret=$appSecret&code={$code}&grant_type=authorization_code";
            $a = file_get_contents($url);
            $json= json_decode($a,true);

                if(isset($json['openid'])){
                    $openid = $json['openid'];
                    $url ="https://api.weixin.qq.com/sns/userinfo?access_token=".$json['access_token']."&openid=".$json['openid'];
                    $a = file_get_contents($url);
                    $json = (array)json_decode($a);
                    $nickname = $json['nickname'];
                    $headimgurl = $json['headimgurl'];
                    // 追加用户信息(如:openid、昵称、头像等)地址参数跳回前端页面
                    if($openid){
                        $data = [
                            'name'=>removeEmoji($nickname),
                            'openid'=>$openid,
                            'head'=>$headimgurl,
                            'token'=>echo_token(removeEmoji($nickname),'',time()),
                            'login_at'=>date('Y-m-d H:i:s'),
                            'status'=>'1',
                            'password'=>'',
                            'money'=>0.00,
                            'cid'=>4
                        ];
                        $token = db('web_user')->where(['openid'=>$data['openid']])->value('token');
                        if(!$token){
                            db('web_user')->insert($data);
                        }else{
                            db('web_user')->where(['openid'=>$data['openid']])->update(['token'=>$data['token'],'login_at'=>date('Y-m-d H:i:s'),'login_num'=>['exp','login_num+1']]);
                        }

                        cookie('token',$data['token']);
                        cookie('openid',$openid);
                        cookie('appid',$appId);
                    }
                }
            }else{

                $redirect_uri="http://" . $_SERVER['HTTP_HOST'] . "?u=".$u."&stu=1";  //http://www.youqiong.net/ " . $_SERVER['SERVER_NAME'] . "  " . $_SERVER['HTTP_HOST'] . "
                $redirect_uri=urlencode($redirect_uri);
                $url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$appId."&redirect_uri=".$redirect_uri."&response_type=code&scope=snsapi_userinfo&state=''#wechat_redirect";
                header("location:".$url."");
                die;
            }


        }

注:原生开发替换掉sql和thinkphp 相关的自带助手函数,用上原生的方式即可。

猜你喜欢

转载自blog.csdn.net/hj960511/article/details/83821081