如果你对该文章中的内容有疑问/不解,可以点击此处链接提问
要注明问题和此文章链接地址 点击此处跳转
网站实现使用公众号信息登录,不使用开放平台
如下效果
做下记录,后续使用直接复制即可
首先在开放平台绑定域名
然后就是代码部分
使用类
use EasyWeChat\Factory;
use EasyWeChat\Kernel\Messages\Article;
use EasyWeChat\Kernel\Messages\News;
use EasyWeChat\Kernel\Messages\NewsItem;
use EasyWeChat\Kernel\Messages\Media;
login public function login(){ $user = session('home'); $this->assign('user',$user); return view(); } //公众号登录 public function login_gzh(){ //生成token $token = md5(mt_rand(1,999999)); session('gzh_login_token',$token); $url = "http://mctool.wangmingchang.com/api/tool/create_qr_code?msg=http://fund.test.90ckm.com/index/index/login_gzh_mobile?token=".$token; //生成二维码 $this->assign('url',$url); return view(); // echo ' <img src="https://mctool.wangmingchang.com/api/tool/create_qr_code?msg='.$url.'">'; } //微信授权登录 public function login_gzh_mobile(){ $token = input('token',''); session('gzh_login_m_token',$token); $this->conf(); $oauth = $this->app->oauth; return $oauth->redirect(); } public function login_gzh_mobile_end(){ $token = session('gzh_login_m_token'); $code = input('code',''); $this->conf(); $user = $this->app->oauth->user()->toArray(); $user_data['openid'] = $user['id']; $user_data['name'] = $user['name']; $user_data['pic'] = $user['avatar']; $user_data['ctime'] = time(); $user_data['ip'] = request()->ip(); $is = Db::name('user')->where(['openid'=>$user_data['openid']])->order('id desc')->find(); if(!$is){ $user_data['token'] = $token; $res = Db::name('user')->insert($user_data); }else{ $is = Db::name('user')->where(['openid'=>$user_data['openid']])->update(['token'=>$token]); } $is = Db::name('user')->where(['openid'=>$user_data['openid']])->order('id desc')->find(); echo "登录成功"; // curls_post } public function gzh_login_is(){ $act_code = session("gzh_login_token"); $res = Db::name('user')->where("token='".$act_code."'")->find(); if($res){ session('home',$res); return jssuccess("登录成功"); }else{ return jserror("error"); } } public function gzh_login_callback(){ $user = input('post.'); // dd($user); $user_data['openid'] = $user['id']; $user_data['name'] = $user['name']; $user_data['pic'] = $user['avatar']; $user_data['ctime'] = time(); $user_data['ip'] = request()->ip(); $is = Db::name('user')->where(['openid'=>$user_data['openid']])->order('id desc')->find(); if(!$is){ $user_data['token'] = $token; $res = Db::name('user')->insert($user_data); }else{ $is = Db::name('user')->where(['openid'=>$user_data['openid']])->update(['token'=>$token]); } $is = Db::name('user')->where(['openid'=>$user_data['openid']])->order('id desc')->find(); echo "登录成功"; } public function logout(){ session('home',null); $this->redirect('/index/index/login'); } private function conf(){ $options = [ 'app_id' => 'wx**********4d4', 'secret' => '293d2*******38b08bfecace5b', 'oauth' => [ 'scopes' => ['snsapi_userinfo'], 'callback' => '/index/index/login_gzh_mobile_end', ], ]; $this->app = Factory::officialAccount($options); }
html
判断是否登录 layui.use('layer', function(){ var layer = layui.layer; function reqs() { $.post("{:url('index/gzh_login_is')}",function(res){ if(res.result==1){ layer.msg(res.msg); setTimeout(function(){ top.location.reload() },2000); } },'json'); } reqs(); setInterval(reqs, 3000); });
实现原理:
其实很简单
就是登录的时候带一个token,然后手机扫码登录的时候,把token记录到当前的用户信息,然后轮询查找用户表是否存在这个token,如果搜索到此token,就找到了当前的登录用户