审查视图

application/admin/controller/Index.php 4.2 KB
Karson authored
1 2 3 4
<?php

namespace app\admin\controller;
5
use app\admin\model\AdminLog;
Karson authored
6
use app\common\controller\Backend;
7 8
use think\Config;
use think\Hook;
Karson authored
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
use think\Validate;

/**
 * 后台首页
 * @internal
 */
class Index extends Backend
{

    protected $noNeedLogin = ['login'];
    protected $noNeedRight = ['index', 'logout'];
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
25 26
        //移除HTML标签
        $this->request->filter('trim,strip_tags,htmlspecialchars');
Karson authored
27 28 29 30 31 32 33
    }

    /**
     * 后台首页
     */
    public function index()
    {
34
        //左侧菜单
35
        list($menulist, $navlist, $fixedmenu, $referermenu) = $this->auth->getSidebar([
Karson authored
36 37
            'dashboard' => 'hot',
            'addon'     => ['new', 'red', 'badge'],
38
            'auth/rule' => __('Menu'),
39
            'general'   => ['new', 'purple'],
40
        ], $this->view->site['fixedpage']);
41
        $action = $this->request->request('action');
42 43 44
        if ($this->request->isPost()) {
            if ($action == 'refreshmenu') {
                $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
45 46
            }
        }
Karson authored
47
        $this->view->assign('menulist', $menulist);
48
        $this->view->assign('navlist', $navlist);
49 50
        $this->view->assign('fixedmenu', $fixedmenu);
        $this->view->assign('referermenu', $referermenu);
Karson authored
51 52 53 54 55 56 57 58 59 60
        $this->view->assign('title', __('Home'));
        return $this->view->fetch();
    }

    /**
     * 管理员登录
     */
    public function login()
    {
        $url = $this->request->get('url', 'index/index');
61
        if ($this->auth->isLogin()) {
62
            $this->success(__("You've logged in, do not login again"), $url);
Karson authored
63
        }
64
        if ($this->request->isPost()) {
Karson authored
65 66 67 68 69 70 71
            $username = $this->request->post('username');
            $password = $this->request->post('password');
            $keeplogin = $this->request->post('keeplogin');
            $token = $this->request->post('__token__');
            $rule = [
                'username'  => 'require|length:3,30',
                'password'  => 'require|length:3,30',
72
                '__token__' => 'require|token',
Karson authored
73 74 75 76 77 78
            ];
            $data = [
                'username'  => $username,
                'password'  => $password,
                '__token__' => $token,
            ];
79
            if (Config::get('fastadmin.login_captcha')) {
80 81 82 83
                $rule['captcha'] = 'require|captcha';
                $data['captcha'] = $this->request->post('captcha');
            }
            $validate = new Validate($rule, [], ['username' => __('Username'), 'password' => __('Password'), 'captcha' => __('Captcha')]);
Karson authored
84
            $result = $validate->check($data);
85
            if (!$result) {
Karson authored
86 87
                $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
            }
88
            AdminLog::setTitle(__('Login'));
Karson authored
89
            $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
90
            if ($result === true) {
91
                Hook::listen("admin_login_after", $this->request);
Karson authored
92
                $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
93
            } else {
94 95 96
                $msg = $this->auth->getError();
                $msg = $msg ? $msg : __('Username or password is incorrect');
                $this->error($msg, $url, ['token' => $this->request->token()]);
Karson authored
97 98 99 100
            }
        }

        // 根据客户端的cookie,判断是否可以自动登录
101
        if ($this->auth->autologin()) {
Karson authored
102 103
            $this->redirect($url);
        }
104
        $background = Config::get('fastadmin.login_background');
Karson authored
105
        $background = $background ? (stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background) : '';
106
        $this->view->assign('background', $background);
Karson authored
107
        $this->view->assign('title', __('Login'));
108
        Hook::listen("admin_login_init", $this->request);
Karson authored
109 110 111 112
        return $this->view->fetch();
    }

    /**
Karson authored
113
     * 退出登录
Karson authored
114 115 116 117
     */
    public function logout()
    {
        $this->auth->logout();
118
        Hook::listen("admin_logout_after", $this->request);
Karson authored
119 120 121 122
        $this->success(__('Logout successful'), 'index/login');
    }

}