Controller.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 app\common\exception\BaseException;
  15. use app\admin\service\admin\User as AdminUserService;
  16. /**
  17. * 超管后台控制器基类
  18. * Class Controller
  19. * @package app\admin\controller
  20. */
  21. class Controller extends BaseController
  22. {
  23. // 商家登录信息
  24. protected $admin;
  25. // 当前控制器名称
  26. protected $controller = '';
  27. // 当前方法名称
  28. protected $action = '';
  29. // 当前路由uri
  30. protected $routeUri = '';
  31. // 当前路由:分组名称
  32. protected $group = '';
  33. // 登录验证白名单
  34. protected $allowAllAction = [
  35. // 登录页面
  36. 'passport/login',
  37. ];
  38. // 无需全局layout
  39. protected $notLayoutAction = [];
  40. /**
  41. * 强制验证当前访问的控制器方法method
  42. * 例: [ 'login' => 'POST' ]
  43. * @var array
  44. */
  45. protected $methodRules = [];
  46. /**
  47. * 后台初始化
  48. * @throws \Exception
  49. */
  50. public function initialize()
  51. {
  52. // 设置管理员登录信息
  53. $this->setAdminInfo();
  54. // 当前路由信息
  55. $this->getRouteInfo();
  56. // 验证登录
  57. $this->checkLogin();
  58. // 强制验证当前访问的控制器方法method
  59. $this->checkMethodRules();
  60. }
  61. /**
  62. * 设置管理员登录信息
  63. */
  64. private function setAdminInfo()
  65. {
  66. $this->admin = AdminUserService::getLoginInfo();
  67. }
  68. /**
  69. * 解析当前路由参数 (分组名称、控制器名称、方法名)
  70. */
  71. protected function getRouteInfo()
  72. {
  73. // 控制器名称
  74. $this->controller = uncamelize($this->request->controller());
  75. // 方法名称
  76. $this->action = $this->request->action();
  77. // 控制器分组 (用于定义所属模块)
  78. $group = strstr($this->controller, '.', true);
  79. $this->group = $group !== false ? $group : $this->controller;
  80. // 当前uri
  81. $this->routeUri = "{$this->controller}/$this->action";
  82. }
  83. /**
  84. * 后台菜单配置
  85. * @return array
  86. */
  87. private function menus()
  88. {
  89. // 获取后台菜单内容 [app/admin/config/menus.php]
  90. $menus = \think\facade\Config::instance()->get('menus');
  91. foreach ($menus as $group => &$first) {
  92. $first['active'] = $group === $this->group;
  93. // 遍历:二级菜单
  94. if (isset($first['submenu'])) {
  95. foreach ($first['submenu'] as $secondKey => &$second) {
  96. // 二级菜单所有uri
  97. $secondUris = isset($second['uris']) ? $second['uris'] : [$second['index']];
  98. // 二级菜单:active
  99. !isset($second['active']) && $second['active'] = in_array($this->routeUri, $secondUris);
  100. }
  101. }
  102. }
  103. return $menus;
  104. }
  105. /**
  106. * 验证登录状态
  107. * @return bool
  108. * @throws BaseException
  109. */
  110. private function checkLogin()
  111. {
  112. // 验证当前请求是否在白名单
  113. if (in_array($this->routeUri, $this->allowAllAction)) {
  114. return true;
  115. }
  116. // 验证登录状态
  117. if (empty($this->admin) || (int)$this->admin['is_login'] !== 1) {
  118. throwError('请先登录后再访问', config('status.not_logged'));
  119. }
  120. return true;
  121. }
  122. /**
  123. * 返回封装后的 API 数据到客户端
  124. * @param int|null $status 状态码
  125. * @param string $message
  126. * @param array $data
  127. * @return array
  128. */
  129. protected function renderJson(int $status = null, string $message = '', array $data = [])
  130. {
  131. return json(compact('status', 'message', 'data'));
  132. }
  133. /**
  134. * 返回操作成功json
  135. * @param array|string $data
  136. * @param string $message
  137. * @return array
  138. */
  139. protected function renderSuccess($data = [], string $message = 'success')
  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 array
  152. */
  153. protected function renderError(string $message = 'error', array $data = [])
  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 $key
  169. * @return mixed
  170. */
  171. protected function postForm($key = 'form')
  172. {
  173. return $this->postData($key);
  174. }
  175. /**
  176. * 强制验证当前访问的控制器方法method
  177. * @throws BaseException
  178. */
  179. private function checkMethodRules()
  180. {
  181. if (!isset($this->methodRules[$this->action])) {
  182. return true;
  183. }
  184. $methodRule = $this->methodRules[$this->action];
  185. $currentMethod = $this->request->method();
  186. if (empty($methodRule)) {
  187. return true;
  188. }
  189. if (is_array($methodRule) && in_array($currentMethod, $methodRule)) {
  190. return true;
  191. }
  192. if (is_string($methodRule) && $methodRule == $currentMethod) {
  193. return true;
  194. }
  195. throwError('illegal request method');
  196. }
  197. }