Controller.php 6.7 KB

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