Controller.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. /**
  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()
  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()
  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()
  129. {
  130. // app/store/common.php
  131. return getStoreId();
  132. }
  133. /**
  134. * 返回封装后的 API 数据到客户端
  135. * @param int|null $status 状态码
  136. * @param string $message
  137. * @param array $data
  138. * @return array
  139. */
  140. protected function renderData(int $status = null, string $message = '', array $data = [])
  141. {
  142. is_null($status) && $status = config('status.success');
  143. return json(compact('status', 'message', 'data'));
  144. }
  145. /**
  146. * 返回操作成功json
  147. * @param array|string $data
  148. * @param string $message
  149. * @return array
  150. */
  151. protected function renderSuccess($data = [], string $message = 'success')
  152. {
  153. if (is_string($data)) {
  154. $message = $data;
  155. $data = [];
  156. }
  157. return $this->renderData(config('status.success'), $message, $data);
  158. }
  159. /**
  160. * 返回操作失败json
  161. * @param string $message
  162. * @param array $data
  163. * @return array
  164. */
  165. protected function renderError(string $message = 'error', array $data = [])
  166. {
  167. return $this->renderData(config('status.error'), $message, $data);
  168. }
  169. /**
  170. * 获取post数据 (数组)
  171. * @param $key
  172. * @return mixed
  173. */
  174. protected function postData($key = null)
  175. {
  176. return $this->request->post(empty($key) ? '' : "{$key}/a");
  177. }
  178. /**
  179. * 获取post数据 (数组)
  180. * @param $key
  181. * @return mixed
  182. */
  183. protected function postForm($key = 'form')
  184. {
  185. return $this->postData($key);
  186. }
  187. /**
  188. * 获取post数据 (数组)
  189. * @param $key
  190. * @return mixed
  191. */
  192. protected function getData($key = null)
  193. {
  194. return $this->request->get(is_null($key) ? '' : $key);
  195. }
  196. /**
  197. * 强制验证当前访问的控制器方法method
  198. * @return bool
  199. * @throws BaseException
  200. */
  201. private function checkMethodRules()
  202. {
  203. if (!isset($this->methodRules[$this->action])) {
  204. return true;
  205. }
  206. $methodRule = $this->methodRules[$this->action];
  207. $currentMethod = $this->request->method();
  208. if (empty($methodRule)) {
  209. return true;
  210. }
  211. if (is_array($methodRule) && in_array($currentMethod, $methodRule)) {
  212. return true;
  213. }
  214. if (is_string($methodRule) && $methodRule == $currentMethod) {
  215. return true;
  216. }
  217. throwError('illegal request method');
  218. return false;
  219. }
  220. }