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