fastadmin继承api auth和controller fetch
<?php namespace app\api\controller; use app\common\library\Auth; use think\Config; use think\Controller; use think\Hook; use think\Lang; use think\Loader; class Base extends Controller { /** * 无需登录的方法,同时也就不需要鉴权了 * @var array */ protected $noNeedLogin = []; /** * 无需鉴权的方法,但需要登录 * @var array */ protected $noNeedRight = []; /** * 初始化操作 * @access protected */ protected function _initialize() { //跨域请求检测 check_cors_request(); // 检测IP是否允许 check_ip_allowed(); //移除HTML标签 $this->request->filter('trim,strip_tags,htmlspecialchars'); $this->auth = Auth::instance(); $modulename = $this->request->module(); $controllername = Loader::parseName($this->request->controller()); $actionname = strtolower($this->request->action()); // token $token = $this->request->server('HTTP_TOKEN', $this->request->request('token', \think\Cookie::get('token'))); $path = str_replace('.', '/', $controllername) . '/' . $actionname; // 设置当前请求的URI $this->auth->setRequestUri($path); // 检测是否需要验证登录 if (!$this->auth->match($this->noNeedLogin)) { //初始化 $this->auth->init($token); //检测是否登录 if (!$this->auth->isLogin()) { $this->error(__('Please login first'), null, 401); } // 判断是否需要验证权限 if (!$this->auth->match($this->noNeedRight)) { // 判断控制器和方法判断是否有对应权限 if (!$this->auth->check($path)) { $this->error(__('You have no permission'), null, 403); } } } else { // 如果有传递token才验证是否登录状态 if ($token) { $this->auth->init($token); } } $upload = \app\common\model\Config::upload(); // 上传信息配置后 Hook::listen("upload_config_init", $upload); Config::set('upload', array_merge(Config::get('upload'), $upload)); // 加载当前控制器语言包 $this->loadlang($controllername); } /** * 加载语言文件 * @param string $name */ protected function loadlang($name) { $name = Loader::parseName($name); $name = preg_match("/^([a-zA-Z0-9_\.\/]+)\$/i", $name) ? $name : 'index'; $lang = $this->request->langset(); $lang = preg_match("/^([a-zA-Z\-_]{2,10})\$/i", $lang) ? $lang : 'zh-cn'; Lang::load(APP_PATH . $this->request->module() . '/lang/' . $lang . '/' . str_replace('.', '/', $name) . '.php'); } }
新建控制器继承base