Files
amb_wechatapp/application/admin/controller/Index.php
T

132 lines
4.1 KiB
PHP

<?php
namespace app\admin\controller;
use app\admin\model\AdminLog;
use app\common\controller\Backend;
use think\Config;
use think\Hook;
use think\Session;
use think\Validate;
/**
* 后台首页
* @internal
*/
class Index extends Backend
{
protected $noNeedLogin = ['login','index'];
protected $noNeedRight = ['index', 'logout'];
protected $layout = '';
public function _initialize()
{
parent::_initialize();
//移除HTML标签
$this->request->filter('trim,strip_tags,htmlspecialchars');
}
/**
* 后台首页
*/
public function index()
{
global $_W,$_GPC;
$footer = [
'firend'=>'',
'copyright'=>''
];
if(isset($_W['setting']['copyright']) && isset($_W['setting']['copyright']['footerright'])){
$footer['firend'] = $_W['setting']['copyright']['footerright'];
}
if(isset($_W['setting']['footerleft']) && isset($_W['setting']['copyright']['footerleft'])){
$footer['copyright'] = $_W['setting']['copyright']['footerleft'];
}
$footer['firend'] = str_replace("\n",'',$footer['firend']);
$footer['copyright'] = str_replace("\n",'',$footer['copyright']);
$SCHEME = 'http';
if(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on'){
$SCHEME = 'https';
}
if(isset($SCHEME['REQUEST_SCHEME'])){
$SCHEME = $SCHEME['REQUEST_SCHEME'];
}
$url = $SCHEME."://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$this->view->assign('url', $url);
$this->view->assign('gpc', $_GPC);
$this->view->assign('footer', $footer);
$this->view->assign('w', $_W);
return $this->view->fetch('index_vue');
}
/**
* 管理员登录
*/
public function login()
{
if ($this->auth->isLogin()) {
$this->success(__("You've logged in, do not login again"));
}
$username = $this->request->post('username');
$password = $this->request->post('password');
$keeplogin = $this->request->post('keeplogin');
$rule = [
'username' => 'require|length:3,30',
'password' => 'require|length:3,30'
];
$data = [
'username' => $username,
'password' => $password
];
// if (Config::get('fastadmin.login_captcha')) {
// $rule['captcha'] = 'require|captcha';
// $data['captcha'] = $this->request->post('captcha');
// }
$validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
$result = $validate->check($data);
if (!$result) {
$this->error($validate->getError(), '', ['token' => $this->request->token()]);
}
AdminLog::setTitle(__('Login'));
$result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
if ($result === true) {
Hook::listen("admin_login_after", $this->request);
$rules = $this->auth->getRuleIds($this->auth->id);
$this->success(__('Login successful'), ['url' => '', 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar, 'role' => $rules, 'is_lecturer' => $this->auth->isLecturer()]);
} else {
$msg = $this->auth->getError();
$msg = $msg ? $msg : __('Username or password is incorrect');
$this->error($msg, '', ['token' => $this->request->token()]);
}
}
/**
* 退出登录
*/
public function logout()
{
if ($this->request->isPost()) {
$this->auth->logout();
Hook::listen("admin_logout_after", $this->request);
$this->success(__('Logout successful'), 'index/login');
}
$html = "<form id='logout_submit' name='logout_submit' action='' method='post'>" . token() . "<input type='submit' value='ok' style='display:none;'></form>";
$html .= "<script>document.forms['logout_submit'].submit();</script>";
return $html;
}
}