redirect('admin/index/index'); } $this->cache_model=array('Module','AuthRule','Category','Posid','Field','System'); $this->system = cache('System'); $this->assign('system',$this->system); if(empty($this->system)){ foreach($this->cache_model as $r){ savecache($r); } } } public function index(){ $code = $_GET["code"]; $wechat= $_GET["wechat"]; $weixinInfo = config('weixin_info'); $appid = $weixinInfo['appid']; $appsecret = $weixinInfo['appSecret']; $wxInfo = []; if($wechat && !$code){ $redirect_uri = urlencode ( 'http://jm.ilixo.com/admin/login/index?wechat='.$wechat );//将字符串以 URL 编码。 $url ="https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$redirect_uri& response_type=code&scope=snsapi_userinfo&state=1#wechat_redirect"; header("Location:".$url);//header() 函数向客户端发送原始的 HTTP 报头。 }else if($wechat && $code){ //Get access_token $access_token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$appid}&secret={$appsecret}&code={$code}&grant_type=authorization_code"; $access_token_json = $this->https_request($access_token_url);//自定义函数 $access_token_array = json_decode($access_token_json,true);//对 JSON 格式的字符串进行解码,转换为 PHP 变量,自带函数 //获取access_token $access_token = $access_token_array['access_token'];//获取access_token对应的值 //获取openid $openid = $access_token_array['openid'];//获取openid对应的值 //Get user info $userinfo_url = "https://api.weixin.qq.com/sns/userinfo?access_token=$access_token&openid=$openid&lang=zh_CN"; $userinfo_json = $this->https_request($userinfo_url); $wxInfo = json_decode($userinfo_json,ture); } if(request()->isPost()) { $data = input('post.'); $admin = new Admin(); $return = $admin->login($data,$this->system['code'],$wxInfo); return ['code' => $return['code'], 'msg' => $return['msg']]; }else{ return $this->fetch(); } } public function https_request($url)//自定义函数,访问url返回结果 { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $data = curl_exec($curl); if (curl_errno($curl)){ return 'ERROR'.curl_error($curl); } curl_close($curl); return $data; } public function verify(){ $config = [ // 验证码字体大小 'fontSize' => 25, // 验证码位数 'length' => 4, // 关闭验证码杂点 'useNoise' => false, 'bg' => [255,255,255], ]; $captcha = new Captcha($config); return $captcha->entry(); } }