Controller.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 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. * 返回封装后的 API 数据到客户端
  124. * @param int|null $status 状态码
  125. * @param string $message
  126. * @param array $data
  127. * @return Json
  128. */
  129. protected function renderJson(int $status = null, string $message = '', array $data = []): Json
  130. {
  131. return json(compact('status', 'message', 'data'));
  132. }
  133. /**
  134. * 返回操作成功json
  135. * @param array|string $data
  136. * @param string $message
  137. * @return Json
  138. */
  139. protected function renderSuccess($data = [], string $message = 'success'): Json
  140. {
  141. if (is_string($data)) {
  142. $message = $data;
  143. $data = [];
  144. }
  145. return $this->renderJson(config('status.success'), $message, $data);
  146. }
  147. /**
  148. * 返回操作失败json
  149. * @param string $message
  150. * @param array $data
  151. * @return Json
  152. */
  153. protected function renderError(string $message = 'error', array $data = []): Json
  154. {
  155. return $this->renderJson(config('status.error'), $message, $data);
  156. }
  157. /**
  158. * 获取post数据 (数组)
  159. * @param $key
  160. * @return mixed
  161. */
  162. protected function postData($key = null)
  163. {
  164. return $this->request->post(empty($key) ? '' : "{$key}/a");
  165. }
  166. /**
  167. * 获取post数据 (数组)
  168. * @param string $key
  169. * @return mixed
  170. */
  171. protected function postForm(string $key = 'form')
  172. {
  173. return $this->postData($key);
  174. }
  175. /**
  176. * 强制验证当前访问的控制器方法method
  177. * @throws BaseException
  178. */
  179. private function checkMethodRules(): void
  180. {
  181. if (!isset($this->methodRules[$this->action])) {
  182. return;
  183. }
  184. $methodRule = $this->methodRules[$this->action];
  185. $currentMethod = $this->request->method();
  186. if (empty($methodRule)) {
  187. return;
  188. }
  189. if (is_array($methodRule) && in_array($currentMethod, $methodRule)) {
  190. return;
  191. }
  192. if (is_string($methodRule) && $methodRule == $currentMethod) {
  193. return;
  194. }
  195. throwError('illegal request method');
  196. }
  197. }