| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace app\index\controller;
- use think\Db;
- /**
- * 后台微信登录
- * Created by PhpStorm.
- * User: Titan 名字就是密码
- * Date: 2019/5/5
- * Time: 17:21
- */
- class AdminWeixinLogin extends \think\Controller
- {
- private $appid =APPID;
- private $appsecret=APPSECRET;
- public function index()
- {
- if(isset($_GET['code'])){
-
- $code = $_GET['code'];
- $state = $_GET['state'];
- if(time()-$_GET['time']>120){//判断二维码失效
- $this->assign('isauth','hasexpire');
- return view();
- }
- //获取openid
- $url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$this->appid."&secret=$this->appsecret&code=$code&grant_type=authorization_code";
- $info = json_decode($this->https_request($url), true);
- $openid = $info['openid'];
- if(!$openid){
- $this->assign('isauth','err');
- return view();
-
-
- }
- //查看是否绑定管理员
- $admin = \think\Db::name('admin')->where(['openid'=>$info["openid"],'status'=>0])->find();
-
- if(!$admin){
- $this->assign('isauth','nobind');
- return view();
- }
- //记录状态用于后台登录轮询
- $login_id = Db::name('login')->where(['openid'=>$openid,'uid'=>$admin['id']])->value('id');
-
- if(!$login_id){
- $res = Db::name('login')->insert(['openid'=>$openid,'uid'=>$admin['id'],'code'=>$state,'create_time'=>time()]);
-
- }else{
- $res = Db::name('login')->where('id',$login_id)->setField('code',$state);
- }
-
- if($res){
- $this->assign('isauth','suss');
- return view();
- }
- }else{
- $this->assign('isauth','err');
- return view();
- }
- }
- public function https_request($url, $data = null)
- {
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, $url);
- curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
- curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
- if (!empty($data)) {
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
- }
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- $output = curl_exec($curl);
- curl_close($curl);
- return $output;
- }
- }
|