<?php

namespace app\admin\controller;

use app\common\model\Admin as AdminModel;
use think\facade\Session;

class Login
{

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


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


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

}
