Controller.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2023 https://www.yiovo.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: 萤火科技 <admin@yiovo.com>
  10. // +----------------------------------------------------------------------
  11. declare (strict_types=1);
  12. namespace app\admin\controller;
  13. use cores\BaseController;
  14. use cores\exception\BaseException;
  15. use app\admin\service\admin\User as AdminUserService;
  16. use think\response\Json;
  17. /**
  18. * 超管后台控制器基类
  19. * Class Controller
  20. * @package app\admin\controller
  21. */
  22. class Controller extends BaseController
  23. {
  24. // 商家登录信息
  25. protected $admin;
  26. // 当前控制器名称
  27. protected $controller = '';
  28. // 当前方法名称
  29. protected $action = '';
  30. // 当前路由uri
  31. protected $routeUri = '';
  32. // 当前路由:分组名称
  33. protected $group = '';
  34. // 登录验证白名单
  35. protected $allowAllAction = [
  36. // 登录页面
  37. 'passport/login',
  38. ];
  39. // 无需全局layout
  40. protected $notLayoutAction = [];
  41. /**
  42. * 强制验证当前访问的控制器方法method
  43. * 例: [ 'login' => 'POST' ]
  44. * @var array
  45. */
  46. protected $methodRules = [];
  47. /**
  48. * 后台初始化
  49. * @throws \Exception
  50. */
  51. public function initialize()
  52. {
  53. // 设置管理员登录信息
  54. $this->setAdminInfo();
  55. // 当前路由信息
  56. $this->getRouteInfo();
  57. // 验证登录
  58. $this->checkLogin();
  59. // 强制验证当前访问的控制器方法method
  60. $this->checkMethodRules();
  61. }
  62. /**
  63. * 设置管理员登录信息
  64. */
  65. private function setAdminInfo()
  66. {
  67. $this->admin = AdminUserService::getLoginInfo();
  68. }
  69. /**
  70. * 解析当前路由参数 (分组名称、控制器名称、方法名)
  71. */
  72. protected function getRouteInfo()
  73. {
  74. // 控制器名称
  75. $this->controller = uncamelize($this->request->controller());
  76. // 方法名称
  77. $this->action = $this->request->action();
  78. // 控制器分组 (用于定义所属模块)
  79. $group = strstr($this->controller, '.', true);
  80. $this->group = $group !== false ? $group : $this->controller;
  81. // 当前uri
  82. $this->routeUri = "{$this->controller}/$this->action";
  83. }
  84. /**
  85. * 后台菜单配置
  86. * @return array
  87. */
  88. private function menus(): array
  89. {
  90. // 获取后台菜单内容 [app/admin/config/menus.php]
  91. $menus = \think\facade\Config::get('menus');
  92. foreach ($menus as $group => &$first) {
  93. $first['active'] = $group === $this->group;
  94. // 遍历:二级菜单
  95. if (isset($first['submenu'])) {
  96. foreach ($first['submenu'] as $secondKey => &$second) {
  97. // 二级菜单所有uri
  98. $secondUris = $second['uris'] ?? [$second['index']];
  99. // 二级菜单:active
  100. !isset($second['active']) && $second['active'] = in_array($this->routeUri, $secondUris);
  101. }
  102. }
  103. }
  104. return $menus;
  105. }
  106. /**
  107. * 验证登录状态
  108. * @return void
  109. * @throws BaseException
  110. */
  111. private function checkLogin(): void
  112. {
  113. // 验证当前请求是否在白名单
  114. if (in_array($this->routeUri, $this->allowAllAction)) {
  115. return;
  116. }
  117. // 验证登录状态
  118. if (empty($this->admin) || (int)$this->admin['is_login'] !== 1) {
  119. throwError('请先登录后再访问', config('status.not_logged'));
  120. }
  121. }
  122. /**
  123. * 强制验证当前访问的控制器方法method
  124. * @throws BaseException
  125. */
  126. private function checkMethodRules(): void
  127. {
  128. if (!isset($this->methodRules[$this->action])) {
  129. return;
  130. }
  131. $methodRule = $this->methodRules[$this->action];
  132. $currentMethod = $this->request->method();
  133. if (empty($methodRule)) {
  134. return;
  135. }
  136. if (is_array($methodRule) && in_array($currentMethod, $methodRule)) {
  137. return;
  138. }
  139. if (is_string($methodRule) && $methodRule == $currentMethod) {
  140. return;
  141. }
  142. throwError('illegal request method');
  143. }
  144. }