Controller.php 5.0 KB

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