You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
3.7 KiB
95 lines
3.7 KiB
<?php
|
|
namespace app\admin\controller;
|
|
use think\Controller;
|
|
use app\admin\model\Admin;
|
|
use think\captcha\Captcha;
|
|
class Login extends Controller
|
|
{
|
|
private $cache_model,$system;
|
|
public function initialize(){
|
|
if (session('aid')) {
|
|
$this->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 = request()->param("code");
|
|
$wechat= request()->param("wechat");
|
|
$weixinInfo = config('weixin_info');
|
|
$appid = $weixinInfo['appid'];
|
|
$appsecret = $weixinInfo['appSecret'];
|
|
$wxInfo = [];
|
|
$wxid = 0;
|
|
$openid = "";
|
|
|
|
$admin = new Admin();
|
|
if($wechat && !$code){
|
|
$redirect_uri = rawurlencode( 'http://jm.iiixo.com/admin/login/index/wechat/'.$wechat.".html" );//将字符串以 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 报头。
|
|
return ;
|
|
}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,true);
|
|
$openid = $wxInfo['openid'];
|
|
$wxid = $admin->saveWxUserInfo($wxInfo);
|
|
}
|
|
|
|
|
|
if(request()->isPost()) {
|
|
$data = input('post.');
|
|
$return = $admin->login($data,$this->system['code']);
|
|
return ['code' => $return['code'], 'msg' => $return['msg']];
|
|
}else{
|
|
$this->assign("wxid",$wxid);
|
|
$this->assign("openid",$openid);
|
|
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();
|
|
}
|
|
}
|