<?php
declare (strict_types = 1);

namespace app\index;

use app\index\exception\ApiException;
use app\common\until\Token;
use think\App;
use think\exception\ValidateException;
use think\facade\Session;
use think\Validate;

/**
 * 控制器基础类
 */
abstract class BaseController
{
    /**
     * Request实例
     * @var \think\Request
     */
    protected $request;

    /**
     * 应用实例
     * @var \think\App
     */
    protected $app;

    /**
     * 是否批量验证
     * @var bool
     */
    protected $batchValidate = false;

    /**
     * 控制器中间件
     * @var array
     */
    protected $middleware = [];

    public $userId = 0;
    
    /**
     * 构造方法
     * @access public
     * @param  App  $app  应用对象
     */
    public function __construct(App $app)
    {
        $this->app     = $app;
        $this->request = $this->app->request;
//        $this->request['Accept'] = 'application/json';
        $route = [
            '/index/Token/getToken',
            '/index/index',
            '/index/user/login',
            '/index/admin/login',
            '/index/setting/read',
            ''
        ];
        if (!in_array($this->request->baseUrl(),$route)){
            if (empty($this->request->header('token'))) {
                throw new ApiException('token不为空');
            }else{
                $token = new Token();
                $decodeToken = $token->decodeToken();
                $this->userId = $decodeToken['userId'];
            }
//            $token = new Token();
//            $decodeToken = $token->decodeToken();
//            $this->userId = $decodeToken['userId'];
        }
        // 控制器初始化
        $this->initialize();
    }

    // 初始化
    protected function initialize()
    {}

    protected function isAdmin() {
        if (request()->header('flag') === 'admin'){
            return true;
        }
        return false;
    }
}
