Controller.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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\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. // 当前控制器名称
  30. protected $controller = '';
  31. // 当前方法名称
  32. protected $action = '';
  33. // 当前路由uri
  34. protected $routeUri = '';
  35. // 当前路由:分组名称
  36. protected $group = '';
  37. // 登录验证白名单
  38. protected $allowAllAction = [
  39. 'passport/login',
  40. 'passport/logout',
  41. ];
  42. /**
  43. * 强制验证当前访问的控制器方法method
  44. * 例: [ 'login' => 'POST' ]
  45. * @var array
  46. */
  47. protected $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->storeId = $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 bool
  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(): bool
  86. {
  87. // 在开发模式下, 建议把此处代码暂时屏蔽, 开发完成后在超管后台新增菜单和api
  88. if (!AuthService::getInstance()->checkPrivilege('/' . $this->routeUri)) {
  89. throwError('很抱歉,没有当前api的访问权限 ' . $this->routeUri);
  90. }
  91. return true;
  92. }
  93. /**
  94. * 解析当前路由参数 (分组名称、控制器名称、方法名)
  95. */
  96. protected function getRouteInfo()
  97. {
  98. // 控制器名称
  99. $this->controller = uncamelize($this->request->controller());
  100. // 方法名称
  101. $this->action = $this->request->action();
  102. // 控制器分组 (用于定义所属模块)
  103. $group = strstr($this->controller, '.', true);
  104. $this->group = $group !== false ? $group : $this->controller;
  105. // 当前uri
  106. $this->routeUri = "{$this->controller}/$this->action";
  107. }
  108. /**
  109. * 验证登录状态
  110. * @return bool
  111. * @throws BaseException
  112. */
  113. private function checkLogin(): bool
  114. {
  115. // 验证当前请求是否在白名单
  116. if (in_array($this->routeUri, $this->allowAllAction)) {
  117. return true;
  118. }
  119. // 验证登录状态
  120. if (empty($this->store) || (int)$this->store['is_login'] !== 1) {
  121. throwError('请先登录后再访问', config('status.not_logged'));
  122. }
  123. return true;
  124. }
  125. /**
  126. * 获取当前store_id
  127. * @return int|null
  128. */
  129. protected function getStoreId(): ?int
  130. {
  131. // app/store/common.php
  132. return getStoreId();
  133. }
  134. /**
  135. * 返回封装后的 API 数据到客户端
  136. * @param int|null $status 状态码
  137. * @param string $message
  138. * @param array $data
  139. * @return Json
  140. */
  141. protected function renderData(int $status = null, string $message = '', array $data = []): Json
  142. {
  143. is_null($status) && $status = config('status.success');
  144. return json(compact('status', 'message', 'data'));
  145. }
  146. /**
  147. * 返回操作成功json
  148. * @param array|string $data
  149. * @param string $message
  150. * @return Json
  151. */
  152. protected function renderSuccess($data = [], string $message = 'success'): Json
  153. {
  154. if (is_string($data)) {
  155. $message = $data;
  156. $data = [];
  157. }
  158. return $this->renderData(config('status.success'), $message, $data);
  159. }
  160. /**
  161. * 返回操作失败json
  162. * @param string $message
  163. * @param array $data
  164. * @return Json
  165. */
  166. protected function renderError(string $message = 'error', array $data = []): Json
  167. {
  168. return $this->renderData(config('status.error'), $message, $data);
  169. }
  170. /**
  171. * 获取post数据 (数组)
  172. * @param $key
  173. * @return mixed
  174. */
  175. protected function postData($key = null)
  176. {
  177. return $this->request->post(empty($key) ? '' : "{$key}/a");
  178. }
  179. /**
  180. * 获取post数据 (数组)
  181. * @param string $key
  182. * @return mixed
  183. */
  184. protected function postForm(string $key = 'form')
  185. {
  186. return $this->postData($key);
  187. }
  188. /**
  189. * 获取post数据 (数组)
  190. * @param $key
  191. * @return mixed
  192. */
  193. protected function getData($key = null)
  194. {
  195. return $this->request->get(is_null($key) ? '' : $key);
  196. }
  197. /**
  198. * 强制验证当前访问的控制器方法method
  199. * @return bool
  200. * @throws BaseException
  201. */
  202. private function checkMethodRules(): bool
  203. {
  204. if (!isset($this->methodRules[$this->action])) {
  205. return true;
  206. }
  207. $methodRule = $this->methodRules[$this->action];
  208. $currentMethod = $this->request->method();
  209. if (empty($methodRule)) {
  210. return true;
  211. }
  212. if (is_array($methodRule) && in_array($currentMethod, $methodRule)) {
  213. return true;
  214. }
  215. if (is_string($methodRule) && $methodRule == $currentMethod) {
  216. return true;
  217. }
  218. throwError('illegal request method');
  219. return false;
  220. }
  221. }