<?php


namespace app\admin\controller;

use app\BaseController;
use Firebase\JWT\JWT;
use think\App;
use think\facade\Cache;
use think\facade\Db;
use think\facade\Request;
use think\facade\Validate;

class Login extends BaseController
{
    protected $middleware = ['app\middleware\CheckValidata::class'];
    public function login(){

        if(Request::isPost()){
            $account = Request::param('account');
            $pass_word = Request::param('pass_word');
            $where['account'] = $account;
            $where['pass_word'] = md5($pass_word);
            //数据库验证数据
            $is_find = Db::name('admin')->where($where)->find();
            if($is_find) {
                $to['admin_id'] = $is_find['admin_id'];
                $to['expire_time'] = time() + 43200; //设置12小时过期
                $jwt = JWT::encode(json_encode($to),config('app.jwt_key'));  //根据参数生成了 token
                $res['token'] = $jwt;
                $res['user_name'] = $is_find['user_name'];
                return $this->returnJson($res,'登入成功');
            } else {
                return $this->returnJson([],'账号或密码错误!','400');
            }
        }else {
            return $this->returnJson([],'请求错误!','400');
        }
    }


}