Index.php 4.7 KB
<?php

namespace app\teacher\controller;

use app\common\controller\TeacherBase;
use think\Config;
use think\Hook;
use think\Validate;

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

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

    public function _initialize()
    {
        parent::_initialize();
        //移除HTML标签
        $this->request->filter('trim,strip_tags,htmlspecialchars');
    }

    /**
     * 后台首页
     */
    public function index()
    {

        $menulist = [];
        $navlist = [];
        $action = $this->request->request('action');
        if ($this->request->isPost()) {
            if ($action == 'refreshmenu') {
                $this->success('', null, ['menulist' => $menulist, 'navlist' => $navlist]);
            }
        }
        $fixedmenu= [
        "id" => 1,
        "type" =>  "file",
        "level_type" =>  "1",
        "pid" =>  0,
        "name" =>   "dashboard",
        "title" =>   "控制台",
        "icon" =>  "fa fa-dashboard fa-fw",
        "condition" =>  "",
        "remark" =>  "Dashboard tips",
        "ismenu" =>  1,
        "createtime" =>  1602726835,
        "updatetime" =>  1602726835,
        "weigh" =>  0,
        "status" =>   "normal",
        "url" =>  "/teacher/dashboard",
        "badge" =>   "",
        "py" => "kzt",
        "pinyin" => "kongzhitai"
        ];
        $referermenu = [];
        $this->view->assign('fixedmenu', $fixedmenu);
        $this->view->assign('referermenu', $referermenu);
        $this->view->assign('title', __('Home'));
        return $this->view->fetch();
    }

    /**
     * 管理员登录
     */
    public function login()
    {
        $url = $this->request->get('url', 'index/index');
        if ($this->isLogin()) {
            $this->success(__("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__' => 'require|token',
            ];
            $data = [
                'username'  => $username,
                'password'  => $password,
               // '__token__' => $token,
            ];
//            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(), $url, ['token' => $this->request->token()]);
//            }

            $result = $this->login_base($username, $password, $keeplogin ? 86400 : 0);
            if (is_array($result)) {
                Hook::listen("admin_login_after", $this->request);
                $this->success(__('Login successful'), $url, ['url' => $url, 'id' => $result['id'], 'username' => $username, 'avatar' => '/assets/img/avatar.png']);
            } else {
//                $msg = $this->auth->getError();
                $msg = $result ? $result : __('Username or password is incorrect');
                $this->error($msg);
            }
        }

        // 根据客户端的cookie,判断是否可以自动登录
//        if ($this->autologin()) {
//            $this->redirect($url);
//        }
        $background = Config::get('fastadmin.login_background');
        $background = stripos($background, 'http') === 0 ? $background : config('site.cdnurl') . $background;
        $this->view->assign('background', $background);
        $this->view->assign('title', __('Login'));
        Hook::listen("admin_login_init", $this->request);

        return $this->view->fetch();
    }

    /**
     * 注销登录
     */
    public function logout()
    {
        $this->logout_base();
        Hook::listen("teacher_logout_after", $this->request);
        $this->success(__('Logout successful'), 'index/login');
    }

    /**
     * 页面跳转
    */
    public function transfer($url = NULL) {
        $this->assignconfig('target_url', $url);

        return $this->view->fetch();
    }

    public function checkUserLogin(){
        $teacher = session("teacher");
        if(empty($teacher)){
            $this->error("请登录");
        }else{
            $this->success("success");
        }
    }

}