Controller.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | 商城系统 [ 致力于通过产品和服务,帮助商家高效化开拓市场 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2017~2021 https://www.thinkphp.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed 这不是一个自由软件,不允许对程序代码以任何形式任何目的的再发行
  8. // +----------------------------------------------------------------------
  9. // | Author: thinkphp <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 app\common\exception\BaseException;
  17. use think\response\Json;
  18. /**
  19. * 商户后台控制器基类
  20. * Class Controller
  21. * @package app\store\controller
  22. */
  23. class Controller extends BaseController
  24. {
  25. // 商家登录信息
  26. protected $store;
  27. // 当前商城ID
  28. protected $storeId;
  29. // 管理绑定的学校ID
  30. protected $schoolId;
  31. // 当前控制器名称
  32. protected $controller = '';
  33. // 当前方法名称
  34. protected $action = '';
  35. // 当前路由uri
  36. protected $routeUri = '';
  37. // 当前路由:分组名称
  38. protected $group = '';
  39. // 登录验证白名单
  40. protected $allowAllAction = [
  41. 'passport/login',
  42. 'passport/logout',
  43. ];
  44. /**
  45. * 强制验证当前访问的控制器方法method
  46. * 例: [ 'login' => 'POST' ]
  47. * @var array
  48. */
  49. protected $methodRules = [];
  50. /**
  51. * 后台初始化
  52. * @throws BaseException
  53. * @throws \think\db\exception\DataNotFoundException
  54. * @throws \think\db\exception\DbException
  55. * @throws \think\db\exception\ModelNotFoundException
  56. */
  57. public function initialize()
  58. {
  59. // 当前商城id
  60. $this->storeId = $this->getStoreId();
  61. // 设置管理员登录信息
  62. $this->setStoreInfo();
  63. // 当前路由信息
  64. $this->getRouteInfo();
  65. // 验证登录状态
  66. $this->checkLogin();
  67. // 验证当前API权限
  68. $this->checkPrivilege();
  69. // 强制验证当前访问的控制器方法method
  70. $this->checkMethodRules();
  71. }
  72. /**
  73. * 设置管理员登录信息
  74. */
  75. private function setStoreInfo()
  76. {
  77. $this->store = StoreUserService::getLoginInfo();
  78. $this->schoolId = isset($this->store['school_id'])? intval($this->store['school_id']) : 0;
  79. }
  80. /**
  81. * 验证当前路由权限
  82. * @return bool
  83. * @throws BaseException
  84. * @throws \think\db\exception\DataNotFoundException
  85. * @throws \think\db\exception\DbException
  86. * @throws \think\db\exception\ModelNotFoundException
  87. */
  88. private function checkPrivilege(): bool
  89. {
  90. // 在开发模式下, 建议把此处代码暂时屏蔽, 开发完成后在超管后台新增菜单和api
  91. if (!AuthService::getInstance()->checkPrivilege('/' . $this->routeUri)) {
  92. throwError('很抱歉,没有当前api的访问权限 ' . $this->routeUri);
  93. }
  94. return true;
  95. }
  96. /**
  97. * 解析当前路由参数 (分组名称、控制器名称、方法名)
  98. */
  99. protected function getRouteInfo()
  100. {
  101. // 控制器名称
  102. $this->controller = uncamelize($this->request->controller());
  103. // 方法名称
  104. $this->action = $this->request->action();
  105. // 控制器分组 (用于定义所属模块)
  106. $group = strstr($this->controller, '.', true);
  107. $this->group = $group !== false ? $group : $this->controller;
  108. // 当前uri
  109. $this->routeUri = "{$this->controller}/$this->action";
  110. }
  111. /**
  112. * 验证登录状态
  113. * @return bool
  114. * @throws BaseException
  115. */
  116. private function checkLogin(): bool
  117. {
  118. // 验证当前请求是否在白名单
  119. if (in_array($this->routeUri, $this->allowAllAction)) {
  120. return true;
  121. }
  122. // 验证登录状态
  123. if (empty($this->store) || (int)$this->store['is_login'] !== 1) {
  124. throwError('请先登录后再访问', config('status.not_logged'));
  125. }
  126. return true;
  127. }
  128. /**
  129. * 获取当前store_id
  130. * @return int|null
  131. */
  132. protected function getStoreId(): ?int
  133. {
  134. // app/store/common.php
  135. return getStoreId();
  136. }
  137. /**
  138. * 返回封装后的 API 数据到客户端
  139. * @param int|null $status 状态码
  140. * @param string $message
  141. * @param array $data
  142. * @return Json
  143. */
  144. protected function renderData(int $status = null, string $message = '', array $data = []): Json
  145. {
  146. is_null($status) && $status = config('status.success');
  147. return json(compact('status', 'message', 'data'));
  148. }
  149. /**
  150. * 返回操作成功json
  151. * @param array|string $data
  152. * @param string $message
  153. * @return Json
  154. */
  155. protected function renderSuccess($data = [], string $message = 'success'): Json
  156. {
  157. if (is_string($data)) {
  158. $message = $data;
  159. $data = [];
  160. }
  161. return $this->renderData(config('status.success'), $message, $data);
  162. }
  163. /**
  164. * 返回操作失败json
  165. * @param string $message
  166. * @param array $data
  167. * @return Json
  168. */
  169. protected function renderError(string $message = 'error', array $data = []): Json
  170. {
  171. return $this->renderData(config('status.error'), $message, $data);
  172. }
  173. /**
  174. * 获取post数据 (数组)
  175. * @param $key
  176. * @return mixed
  177. */
  178. protected function postData($key = null)
  179. {
  180. return $this->request->post(empty($key) ? '' : "{$key}/a");
  181. }
  182. /**
  183. * 获取post数据 (数组)
  184. * @param string $key
  185. * @return mixed
  186. */
  187. protected function postForm(string $key = 'form')
  188. {
  189. return $this->postData($key);
  190. }
  191. /**
  192. * 获取post数据 (数组)
  193. * @param $key
  194. * @return mixed
  195. */
  196. protected function getData($key = null)
  197. {
  198. return $this->request->get(is_null($key) ? '' : $key);
  199. }
  200. /**
  201. * 强制验证当前访问的控制器方法method
  202. * @return bool
  203. * @throws BaseException
  204. */
  205. private function checkMethodRules(): bool
  206. {
  207. if (!isset($this->methodRules[$this->action])) {
  208. return true;
  209. }
  210. $methodRule = $this->methodRules[$this->action];
  211. $currentMethod = $this->request->method();
  212. if (empty($methodRule)) {
  213. return true;
  214. }
  215. if (is_array($methodRule) && in_array($currentMethod, $methodRule)) {
  216. return true;
  217. }
  218. if (is_string($methodRule) && $methodRule == $currentMethod) {
  219. return true;
  220. }
  221. throwError('illegal request method');
  222. return false;
  223. }
  224. }