Controller.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\store\controller;
  3. use app\store\service\Auth;
  4. use app\store\service\Menus;
  5. use app\store\model\Setting;
  6. use app\common\exception\BaseException;
  7. use think\Request;
  8. use think\Session;
  9. /**
  10. * 商户后台控制器基类
  11. * Class BaseController
  12. * @package app\store\controller
  13. */
  14. class Controller extends \think\Controller
  15. {
  16. /** @var array $store 商家登录信息 */
  17. protected $store;
  18. /** @var string $route 当前控制器名称 */
  19. protected $controller = '';
  20. /** @var string $route 当前方法名称 */
  21. protected $action = '';
  22. /** @var string $route 当前路由uri */
  23. protected $routeUri = '';
  24. /** @var string $route 当前路由:分组名称 */
  25. protected $group = '';
  26. /** @var array $allowAllAction 登录验证白名单 */
  27. protected $allowAllAction = [
  28. // 登录页面
  29. 'passport/login',
  30. ];
  31. /* @var array $notLayoutAction 无需全局layout */
  32. protected $notLayoutAction = [
  33. // 登录页面
  34. 'passport/login',
  35. ];
  36. /**
  37. * 后台初始化
  38. * @throws BaseException
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\ModelNotFoundException
  41. * @throws \think\exception\DbException
  42. */
  43. public function _initialize()
  44. {
  45. // 商家登录信息
  46. $this->store = Session::get('yoshop_store');
  47. $this->store = $this->store? $this->store : [];
  48. var_dump($this->store);
  49. // 当前路由信息
  50. $this->getRouteinfo();
  51. var_dump(2);
  52. // 验证登录状态
  53. $this->checkLogin();
  54. var_dump(3);
  55. // 验证当前页面权限
  56. $this->checkPrivilege();
  57. var_dump(4);
  58. // 全局layout
  59. $this->layout();
  60. }
  61. /**
  62. * 验证当前页面权限
  63. * @throws BaseException
  64. * @throws \think\db\exception\DataNotFoundException
  65. * @throws \think\db\exception\ModelNotFoundException
  66. * @throws \think\exception\DbException
  67. */
  68. private function checkPrivilege()
  69. {
  70. var_dump(5);
  71. var_dump($this->routeUri);
  72. if ($this->routeUri === 'index/index') {
  73. return true;
  74. }
  75. var_dump(6);
  76. if (!Auth::getInstance()->checkPrivilege($this->routeUri)) {
  77. throw new BaseException(['msg' => '很抱歉,没有访问权限']);
  78. }
  79. return true;
  80. }
  81. /**
  82. * 全局layout模板输出
  83. * @throws \think\exception\DbException
  84. * @throws \Exception
  85. */
  86. private function layout()
  87. {
  88. // 验证当前请求是否在白名单
  89. if (!in_array($this->routeUri, $this->notLayoutAction)) {
  90. // 输出到view
  91. $this->assign([
  92. 'base_url' => base_url(), // 当前域名
  93. 'store_url' => url('/store'), // 后台模块url
  94. 'group' => $this->group, // 当前控制器分组
  95. 'menus' => $this->menus(), // 后台菜单
  96. 'store' => $this->store, // 商家登录信息
  97. 'setting' => Setting::getAll() ?: null, // 当前商城设置
  98. 'request' => Request::instance(), // Request对象
  99. 'version' => get_version(), // 系统版本号
  100. ]);
  101. }
  102. }
  103. /**
  104. * 解析当前路由参数 (分组名称、控制器名称、方法名)
  105. */
  106. protected function getRouteinfo()
  107. {
  108. // 控制器名称
  109. $this->controller = toUnderScore($this->request->controller());
  110. // 方法名称
  111. $this->action = $this->request->action();
  112. // 控制器分组 (用于定义所属模块)
  113. $groupstr = strstr($this->controller, '.', true);
  114. $this->group = $groupstr !== false ? $groupstr : $this->controller;
  115. // 当前uri
  116. $this->routeUri = $this->controller . '/' . $this->action;
  117. }
  118. /**
  119. * 后台菜单配置
  120. * @return mixed
  121. * @throws \think\exception\DbException
  122. */
  123. protected function menus()
  124. {
  125. static $menus = [];
  126. if (empty($menus)) {
  127. $menus = Menus::getInstance()->getMenus($this->routeUri, $this->group);
  128. }
  129. return $menus;
  130. }
  131. /**
  132. * 验证登录状态
  133. * @return bool
  134. */
  135. private function checkLogin()
  136. {
  137. // 验证当前请求是否在白名单
  138. if (in_array($this->routeUri, $this->allowAllAction)) {
  139. return true;
  140. }
  141. // 验证登录状态
  142. if (empty($this->store)
  143. || (int)$this->store['is_login'] !== 1
  144. || !isset($this->store['wxapp'])
  145. || empty($this->store['wxapp'])
  146. ) {
  147. $this->redirect('passport/login');
  148. return false;
  149. }
  150. return true;
  151. }
  152. /**
  153. * 获取当前wxapp_id
  154. */
  155. protected function getWxappId()
  156. {
  157. return $this->store['wxapp']['wxapp_id'];
  158. }
  159. /**
  160. * 返回封装后的 API 数据到客户端
  161. * @param int $code
  162. * @param string $msg
  163. * @param string $url
  164. * @param array $data
  165. * @return array
  166. */
  167. protected function renderJson($code = 1, $msg = '', $url = '', $data = [])
  168. {
  169. return compact('code', 'msg', 'url', 'data');
  170. }
  171. /**
  172. * 返回操作成功json
  173. * @param string $msg
  174. * @param string $url
  175. * @param array $data
  176. * @return array
  177. */
  178. protected function renderSuccess($msg = 'success', $url = '', $data = [])
  179. {
  180. return $this->renderJson(1, $msg, $url, $data);
  181. }
  182. /**
  183. * 返回操作失败json
  184. * @param string $msg
  185. * @param string $url
  186. * @param array $data
  187. * @return array|bool
  188. */
  189. protected function renderError($msg = 'error', $url = '', $data = [])
  190. {
  191. if ($this->request->isAjax()) {
  192. return $this->renderJson(0, $msg, $url, $data);
  193. }
  194. $this->error($msg);
  195. return false;
  196. }
  197. /**
  198. * 获取post数据 (数组)
  199. * @param $key
  200. * @return mixed
  201. */
  202. protected function postData($key = null)
  203. {
  204. return $this->request->post(is_null($key) ? '' : $key . '/a');
  205. }
  206. /**
  207. * 获取post数据 (数组)
  208. * @param $key
  209. * @return mixed
  210. */
  211. protected function getData($key = null)
  212. {
  213. return $this->request->get(is_null($key) ? '' : $key);
  214. }
  215. }