// +---------------------------------------------------------------------- namespace app\http\middleware; use think\facade\Session; use think\facade\View; /** * @title 后台中间件 */ class Admin { protected $data = []; protected $outAuth = ['admin/index/login', 'admin/index/logout', 'admin/index/verify']; public function handle($request, \Closure $next) { $user = Session::get('user'); $request->url = str_replace(".", "/", strtolower($request->controller())) . '/' . $request->action(); if (!Session::has('user') && !$user['uid'] && !in_array($request->url, $this->outAuth)) { return redirect('admin/index/login'); } $request->isAdmin = 1; $request->user = $user; $request->pageConfig = array( 'list_rows' => $request->param('limit', 20), 'page' => $request->param('page', 1), ); $response = $next($request); if (is_array($response->getData())) { $this->data = array_merge($this->data, $response->getData()); } else { $this->data = $response->getData(); } if ($request->isAjax()) { if (isset($this->data['config'])) { unset($this->data['config']); } return json($this->data); } else { if (\is_string($this->data) && $this->data != '') { return $response; } else { return $response->data($this->fetch()); } } } /** * @title 显示类 */ protected function fetch($template = '') { // 使用内置PHP模板引擎渲染模板输出 $config = array( 'tpl_replace_string' => array( '__static__' => '/static', '__img__' => '/static/admin/images', '__css__' => '/static/admin/css', '__js__' => '/static/admin/js', '__public__' => '/static/admin', ), ); $template = (isset($this->data['template']) && $this->data['template']) ? $this->data['template'] : $template; View::config($config); View::assign('sent_version', sent_version); View::assign('config', (isset($this->data['config']) ? $this->data['config'] : [])); View::assign((isset($this->data['data']) ? $this->data['data'] : [])); return View::fetch($template); } }