php微信公众号登录及注册
php微信公众号登录及注册
1,h5端访问后端登录接口:
后端代码:
public function wxLogin(){ //appId $refer_id = input('refer_id'); $rank = input('rank'); $appId = Db::name('wxsystem')->where('id',1)->value('appid'); // $appId = 'wx5ea802d92c543852'; // 回调的url $redirect_uri = urlencode('http://'.$_SERVER['HTTP_HOST'].'/api/Login/register/refer_id/'.$refer_id.'/rank/'.$rank); //跳转微信回调到redirect_uri获取code $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appId&redirect_uri=$redirect_uri&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect"; $this->redirect($url); }
2,重定向到register方法:
public function register(){ if(request()->post()){ if(input('post.real_name') == '' || input('post.openid') == '' || input('post.password') == '' || input('post.mobile_phone') == ''){ $this->error('请填写完整信息'); }else{ $user = Db::name('member')->where('openid',input('post.openid'))->find(); if(!empty($user)){ $this->error('该微信号已注册','api/login/login'); } if(input('post.rank' == '')){ $rank= Db::name('rank')->where('sort',4)->value('id'); }else{ $rank = input('post.rank'); } $data = array( 'real_name'=>input('post.real_name'), 'username'=>'', 'password'=>md5(md5(input('post.password'))), 'mobile_phone'=>input('post.mobile_phone'), 'weixin'=>input('post.weixin'), 'register_time'=>date('Y-m-d H:i:s'), 'openid'=>input('post.openid'), 'refer_id'=>input('post.refer_id'), 'is_xr'=>1, 'shouquan_num'=>date("YHis"), 'rank'=>$rank, 'status'=>0, 'avatar'=>input('avatar'), ); $result = Db::name('member')->insertGetId($data); $openid = input('post.openid'); $appid = Db::name('wxsystem')->where('id',1)->value('appid'); $appscrect = Db::name('wxsystem')->where('id',1)->value('appscrect'); $template_id = Db::name('template')->where('id',1)->value('zc_template_id'); $this->SendWechatMessage($openid,$appid,$appscrect,$data1,$template_id); $template_id2 = Db::name('template')->where('id',1)->value('zc_template_id'); $openid2 = Db::name('member')->where('member_id',input('post.refer_id'))->value('openid'); $this->SendWechatMessage($openid2,$appid,$appscrect,$data1,$template_id2); if($result){ $this->redirect('login/wxLogin1'); // $this->wxLogin1(); }else{ $this->error('注册失败'); } } }else{ //获取code $code = $_GET["code"]; // appId与appSecret $appId = Db::name('wxsystem')->where('id',1)->value('appid'); // $appSecret = '1dcda2ca8542cf796c602a9fe81baf09'; $appSecret = Db::name('wxsystem')->where('id',1)->value('appscrect'); $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=$appId&secret=$appSecret&code=$code&grant_type=authorization_code"; $res = $this->sendRequest($url); $access_token = $res["access_token"]; $openId = $res['openid']; $getUserInfo = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openId&lang=zh_CN"; //得到用户信息 $user_info = $this->sendRequest($getUserInfo); $openid = $user_info['openid']; $refer_id = input('refer_id'); $avatar = $user_info['headimgurl']; $this->assign('avatar',$avatar); $rank = input('rank'); $this->assign('rank',$rank); $this->assign('openid',$openid); $this->assign('refer_id',$refer_id); return $this->fetch(); } }