Controller.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 萤火商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2024 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\store\controller;
  13. use cores\BaseController;
  14. use app\store\service\Auth as AuthService;
  15. use app\common\service\store\User as StoreUserService;
  16. use cores\exception\BaseException;
  17. /**
  18. * 商户后台控制器基类
  19. * Class BaseController
  20. * @package app\store\controller
  21. */
  22. class Controller extends BaseController
  23. {
  24. // 商家登录信息
  25. protected array $store;
  26. // 当前商城ID
  27. protected int $storeId;
  28. // 当前控制器名称
  29. protected string $controller = '';
  30. // 当前方法名称
  31. protected string $action = '';
  32. // 当前路由uri
  33. protected string $routeUri = '';
  34. // 当前路由:分组名称
  35. protected string $group = '';
  36. // 登录验证白名单
  37. protected array $allowAllAction = [
  38. 'passport/login',
  39. 'passport/logout',
  40. 'map/transfer',
  41. ];
  42. /**
  43. * 强制验证当前访问的控制器方法method
  44. * 例: [ 'login' => 'POST' ]
  45. * @var array
  46. */
  47. protected array $methodRules = [];
  48. /**
  49. * 后台初始化
  50. * @throws BaseException
  51. * @throws \think\db\exception\DataNotFoundException
  52. * @throws \think\db\exception\DbException
  53. * @throws \think\db\exception\ModelNotFoundException
  54. */
  55. public function initialize()
  56. {
  57. // 获取当前登录的商城ID
  58. $this->getStoreId();
  59. // 设置管理员登录信息
  60. $this->setStoreInfo();
  61. // 当前路由信息
  62. $this->getRouteinfo();
  63. // 验证登录状态
  64. $this->checkLogin();
  65. // 验证当前API权限
  66. $this->checkPrivilege();
  67. // 强制验证当前访问的控制器方法method
  68. $this->checkMethodRules();
  69. }
  70. /**
  71. * 设置管理员登录信息
  72. */
  73. private function setStoreInfo()
  74. {
  75. $this->store = StoreUserService::getLoginInfo();
  76. }
  77. /**
  78. * 验证当前路由权限
  79. * @return void
  80. * @throws BaseException
  81. * @throws \think\db\exception\DataNotFoundException
  82. * @throws \think\db\exception\DbException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. */
  85. private function checkPrivilege(): void
  86. {
  87. // 在开发模式下, 建议把此处代码暂时屏蔽, 开发完成后在超管后台新增菜单和api
  88. if (!AuthService::getInstance()->checkPrivilege('/' . $this->routeUri)) {
  89. throwError('很抱歉,没有当前api的访问权限 ' . $this->routeUri);
  90. }
  91. }
  92. /**
  93. * 解析当前路由参数 (分组名称、控制器名称、方法名)
  94. */
  95. protected function getRouteinfo()
  96. {
  97. // 控制器名称
  98. $this->controller = uncamelize($this->request->controller());
  99. // 方法名称
  100. $this->action = $this->request->action();
  101. // 控制器分组 (用于定义所属模块)
  102. $groupstr = strstr($this->controller, '.', true);
  103. $this->group = $groupstr !== false ? $groupstr : $this->controller;
  104. // 当前uri
  105. $this->routeUri = "{$this->controller}/$this->action";
  106. }
  107. /**
  108. * 验证登录状态
  109. * @return void
  110. * @throws BaseException
  111. */
  112. private function checkLogin(): void
  113. {
  114. // 验证当前请求是否在白名单
  115. if (in_array($this->routeUri, $this->allowAllAction)) {
  116. return;
  117. }
  118. // 验证登录状态
  119. if (empty($this->store) || (int)$this->store['is_login'] !== 1) {
  120. throwError('请先登录后再访问', config('status.not_logged'));
  121. }
  122. }
  123. /**
  124. * 获取当前登录的商城ID
  125. */
  126. protected function getStoreId()
  127. {
  128. $this->storeId = \getStoreId();
  129. }
  130. /**
  131. * 强制验证当前访问的控制器方法method
  132. * @return void
  133. * @throws BaseException
  134. */
  135. private function checkMethodRules(): void
  136. {
  137. if (!isset($this->methodRules[$this->action])) {
  138. return;
  139. }
  140. $methodRule = $this->methodRules[$this->action];
  141. $currentMethod = $this->request->method();
  142. if (empty($methodRule)) {
  143. return;
  144. }
  145. if (is_array($methodRule) && in_array($currentMethod, $methodRule)) {
  146. return;
  147. }
  148. if (is_string($methodRule) && $methodRule == $currentMethod) {
  149. return;
  150. }
  151. throwError('illegal request method');
  152. }
  153. }