From 08c340a2bf037f2848c7c4c80a24c43a967f2be8 Mon Sep 17 00:00:00 2001 From: augushong Date: Sat, 23 Sep 2023 14:50:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0response=E7=9A=84=E4=BA=8B?= =?UTF-8?q?=E4=BB=B6=E6=9C=BA=E5=88=B6=EF=BC=9B=E4=B8=BAlogin=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E4=BA=8B=E4=BB=B6=EF=BC=9B=E5=8F=91=E5=B8=83=E6=96=B0?= =?UTF-8?q?=E7=89=88=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/admin/controller/Login.php | 4 +++ app/common.php | 23 ++++++++++++++-- app/common/command/admin/Version.php | 6 ++-- app/common/controller/AdminController.php | 24 ++++++++-------- app/common/event/AdminLoginIndex/Index.php | 17 ++++++++++++ app/common/provider/View.php | 32 ++++++++++++++++++++++ 6 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 app/common/event/AdminLoginIndex/Index.php diff --git a/app/admin/controller/Login.php b/app/admin/controller/Login.php index e2a26c3..b7ae349 100644 --- a/app/admin/controller/Login.php +++ b/app/admin/controller/Login.php @@ -36,6 +36,10 @@ class Login extends AdminController */ public function index() { + event_response('AdminLoginIndex', [ + 'controller' => $this, + ]); + $captcha = Env::get('adminsystem.captcha', 1); if ($this->request->isPost()) { $post = $this->request->post(); diff --git a/app/common.php b/app/common.php index 4f772b5..d0a71b3 100644 --- a/app/common.php +++ b/app/common.php @@ -4,10 +4,12 @@ use app\commno\exception\EventException; use app\common\service\AuthService; +use think\exception\HttpResponseException; use think\facade\Cache; use think\facade\Env; use think\facade\Event; use think\facade\Filesystem; +use think\response\View; use think\route\Url; include_once __DIR__ . '/common/app/functions.php'; @@ -230,9 +232,9 @@ function build_upload_url($url, $upload_type = null) return Filesystem::disk($upload_type)->url($url); } -function event_handle_result($name, $key, $type = 'all') : array +function event_handle_result($name, $key, $type = 'all', $params = []) : array { - $list_result = Event::trigger($name); + $list_result = Event::trigger($name, $params); $result = []; @@ -287,3 +289,20 @@ function event_view_replace_js($name) return ""; } + +function event_response($name, $params = []) +{ + $list_result = event_handle_result($name, 'response', 'last', $params); + + if (empty($list_result)) { + return; + } + + $response = $list_result[0]; + + if (is_string($response)) { + $response = View::create($response); + } + + throw new HttpResponseException($response); +} diff --git a/app/common/command/admin/Version.php b/app/common/command/admin/Version.php index 3c81bd9..db78f1c 100644 --- a/app/common/command/admin/Version.php +++ b/app/common/command/admin/Version.php @@ -14,13 +14,13 @@ use think\facade\App; class Version extends Command { - public const VERSION = 'v2.0.38'; + public const VERSION = 'v2.0.3'; public const LAYUI_VERSION = '2.8.16'; public const COMMENT = [ - '增加js的事件处理', - '增加忘记密码的js替换事件', + '增加response的事件机制', + '为login增加事件', '发布新版本', ]; diff --git a/app/common/controller/AdminController.php b/app/common/controller/AdminController.php index 0eccaaa..076fd13 100644 --- a/app/common/controller/AdminController.php +++ b/app/common/controller/AdminController.php @@ -141,20 +141,22 @@ class AdminController extends BaseController return $this->app->view->assign($name, $value); } - protected function fetchJS() + public function fetchJS($template = '') { $content_js = ''; - try { - $content_js .= View::layout(false) - ->config([ - 'view_suffix' => 'js', - ])->fetch('_common'); + $common_template = '_common'; - $content_js .= View::layout(false) - ->config([ - 'view_suffix' => 'js', - ])->fetch(); + if (!empty($template)) { + $template_arr = explode('/', $template); + unset($template_arr[count($template_arr) - 1]); + $template_arr[] = '_common'; + $common_template = implode('/', $template_arr); + } + + try { + $content_js .= View::layout(false)->fetchJS($common_template); + $content_js .= View::layout(false)->fetchJS($template); } catch (TemplateNotFoundException $th) { if (Env::get('adminsystem.strict_view_js', true)) { throw $th; @@ -174,7 +176,7 @@ class AdminController extends BaseController { $this->assign('data_brage', json_encode($this->dataBrage)); - $vars['content_js'] = $this->fetchJS(); + $vars['content_js'] = $this->fetchJS($template); $content_main = View::layout($this->layout) ->config([ diff --git a/app/common/event/AdminLoginIndex/Index.php b/app/common/event/AdminLoginIndex/Index.php new file mode 100644 index 0000000..2950f23 --- /dev/null +++ b/app/common/event/AdminLoginIndex/Index.php @@ -0,0 +1,17 @@ +assign('captcha',1); + + return [ + 'response' => $controller->fetch('login/ext/index'), + ]; + } +} diff --git a/app/common/provider/View.php b/app/common/provider/View.php index cbcd3f3..7e618be 100644 --- a/app/common/provider/View.php +++ b/app/common/provider/View.php @@ -48,4 +48,36 @@ class View extends ThinkView return $this; } + + /** + * 解析和获取模板内容 用于输出. + * @param string $template 模板文件名或者内容 + * @param array $vars 模板变量 + * @return string + * @throws \Exception + */ + public function fetch(string $template = '', array $vars = []): string + { + return $this->config([ + 'view_suffix' => 'html', + ])->getContent(function () use ($vars, $template) { + $this->engine()->fetch($template, array_merge($this->data, $vars)); + }); + } + + /** + * 解析和获取模板内容 用于输出. + * @param string $template 模板文件名或者内容 + * @param array $vars 模板变量 + * @return string + * @throws \Exception + */ + public function fetchJS(string $template = '', array $vars = []): string + { + return $this->config([ + 'view_suffix' => 'js', + ])->getContent(function () use ($vars, $template) { + $this->engine()->fetch($template, array_merge($this->data, $vars)); + }); + } }