<?php

namespace app\admin\controller;

use app\BaseController;
use app\common\model\AdminModel;
use think\facade\View;

class Login extends BaseController
{
    public function index()
    {
        return view('login/login', [
            'domain' => request()->domain(),
        ]);
    }


    public function login()
    {
        if (!captcha_check(input('vercode'))) {
            ajax_return(1, '验证码错误。');
        }
        $admin = AdminModel::where(['admin_name' => input('admin_name')])->findOrEmpty()->toArray();
        if (empty($admin) || $admin['password'] != md5(input('password'))) {
            ajax_return(1, '账号密码不正确。');
        }
        if ($admin['status'] == 2) {
            ajax_return(1, '该管理员账号已被禁言，如有疑问请联系超级管理员。');
        }
        AdminModel::update(['last_date' => time(), 'last_ip' => $_SERVER['SERVER_ADDR']], ['id' => $admin['id']]);
        session('access_admin', $admin);

        ajax_return();
    }


    public function logout()
    {
        session(null);
        return redirect(url('login/index'));
    }

}
