审查视图

application/admin/controller/Index.php 3.1 KB
Karson authored
1 2 3 4 5 6 7 8 9 10 11 12 13 14
<?php

namespace app\admin\controller;

use app\common\controller\Backend;
use think\Validate;

/**
 * 后台首页
 * @internal
 */
class Index extends Backend
{
15 16
    protected $noNeedLogin = ['login'];
    protected $noNeedRight = ['index', 'logout'];
Karson authored
17 18 19 20 21 22 23 24 25 26 27 28 29
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 后台首页
     */
    public function index()
    {
        //
Karson authored
30
        $menulist = $this->auth->getSidebar([
Karson authored
31
            'dashboard'  => 'hot',
Karson authored
32 33
            'addon'       => ['new', 'red', 'badge'],
            'auth/rule'  => 'side',
Karson authored
34
            'general'    => ['18', 'purple'],
35
                ], $this->view->site['fixedpage']);
Karson authored
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
        $this->view->assign('menulist', $menulist);
        $this->view->assign('title', __('Home'));
        return $this->view->fetch();
    }

    /**
     * 管理员登录
     */
    public function login()
    {
        $url = $this->request->get('url', 'index/index');
        if ($this->auth->isLogin())
        {
            $this->error(__("You've logged in, do not login again"), $url);
        }
        if ($this->request->isPost())
        {
            $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',
                '__token__' => 'token',
            ];
            $data = [
                'username'  => $username,
                'password'  => $password,
                '__token__' => $token,
            ];
            $validate = new Validate($rule);
            $result = $validate->check($data);
            if (!$result)
            {
71
                $this->error($validate->getError(), $url, ['token' => $this->request->token()]);
Karson authored
72
            }
73
            \app\admin\model\AdminLog::setTitle(__('Login'));
Karson authored
74 75 76
            $result = $this->auth->login($username, $password, $keeplogin ? 86400 : 0);
            if ($result === true)
            {
77
                $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $this->auth->id, 'username' => $username, 'avatar' => $this->auth->avatar]);
Karson authored
78 79 80
            }
            else
            {
81
                $this->error(__('Username or password is incorrect'), $url, ['token' => $this->request->token()]);
Karson authored
82 83 84 85 86 87 88 89
            }
        }

        // 根据客户端的cookie,判断是否可以自动登录
        if ($this->auth->autologin())
        {
            $this->redirect($url);
        }
Karson authored
90 91 92
        $background = cdnurl("/assets/img/loginbg.jpg");
        $this->view->assign('background', $background);
        \think\Hook::listen("login_init", $this->request);
Karson authored
93 94 95 96 97 98 99 100 101
        return $this->view->fetch();
    }

    /**
     * 注销登录
     */
    public function logout()
    {
        $this->auth->logout();
102
        $this->success(__('Logout successful'), 'index/login');
Karson authored
103 104 105
    }

}