Controller.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. namespace app\admin\controller;
  3. use think\Config;
  4. use think\Request;
  5. use think\Session;
  6. // use app\store\model\Setting;
  7. /**
  8. * 超管后台控制器基类
  9. * Class Controller
  10. * @package app\admin\controller
  11. */
  12. class Controller extends \think\Controller
  13. {
  14. /* @var array $admin 商家登录信息 */
  15. protected $admin;
  16. /* @var string $route 当前控制器名称 */
  17. protected $controller = '';
  18. /* @var string $route 当前方法名称 */
  19. protected $action = '';
  20. /* @var string $route 当前路由uri */
  21. protected $routeUri = '';
  22. /* @var string $route 当前路由:分组名称 */
  23. protected $group = '';
  24. /* @var array $allowAllAction 登录验证白名单 */
  25. protected $allowAllAction = [
  26. // 登录页面
  27. 'passport/login',
  28. ];
  29. /* @var array $notLayoutAction 无需全局layout */
  30. protected $notLayoutAction = [
  31. // 登录页面
  32. 'passport/login',
  33. ];
  34. /**
  35. * 后台初始化
  36. * @throws \Exception
  37. */
  38. public function _initialize()
  39. {
  40. // 商家登录信息
  41. $this->admin = Session::get('yoshop_admin');
  42. // 当前路由信息
  43. $this->getRouteinfo();
  44. // 验证登录
  45. $this->checkLogin();
  46. // 全局layout
  47. $this->layout();
  48. }
  49. /**
  50. * 全局layout模板输出
  51. * @throws \Exception
  52. */
  53. private function layout()
  54. {
  55. // 验证当前请求是否在白名单
  56. if (!in_array($this->routeUri, $this->notLayoutAction)) {
  57. // 输出到view
  58. $this->assign([
  59. 'base_url' => base_url(), // 当前域名
  60. 'admin_url' => url('/admin'), // 后台模块url
  61. 'group' => $this->group,
  62. 'menus' => $this->menus(), // 后台菜单
  63. 'admin' => $this->admin, // 商家登录信息
  64. 'version' => get_version(), // 当前系统版本号
  65. 'request' => Request::instance() // Request对象
  66. ]);
  67. }
  68. }
  69. /**
  70. * 解析当前路由参数 (分组名称、控制器名称、方法名)
  71. */
  72. protected function getRouteinfo()
  73. {
  74. // 控制器名称
  75. $this->controller = toUnderScore($this->request->controller());
  76. // 方法名称
  77. $this->action = $this->request->action();
  78. // 控制器分组 (用于定义所属模块)
  79. $groupstr = strstr($this->controller, '.', true);
  80. $this->group = $groupstr !== false ? $groupstr : $this->controller;
  81. // 当前uri
  82. $this->routeUri = $this->controller . '/' . $this->action;
  83. }
  84. /**
  85. * 后台菜单配置
  86. * @return array
  87. */
  88. private function menus()
  89. {
  90. foreach ($data = Config::get('menus') as $group => $first) {
  91. $data[$group]['active'] = $group === $this->group;
  92. // 遍历:二级菜单
  93. if (isset($first['submenu'])) {
  94. foreach ($first['submenu'] as $secondKey => $second) {
  95. // 二级菜单所有uri
  96. $secondUris = isset($second['uris']) ? $second['uris'] : [$second['index']];
  97. // 二级菜单:active
  98. !isset($data[$group]['submenu'][$secondKey]['active'])
  99. && $data[$group]['submenu'][$secondKey]['active'] = in_array($this->routeUri, $secondUris);
  100. }
  101. }
  102. }
  103. return $data;
  104. }
  105. /**
  106. * 验证登录状态
  107. * @return bool
  108. */
  109. private function checkLogin()
  110. {
  111. // 验证当前请求是否在白名单
  112. if (in_array($this->routeUri, $this->allowAllAction)) {
  113. return true;
  114. }
  115. // 验证登录状态
  116. if (empty($this->admin)
  117. || (int)$this->admin['is_login'] !== 1
  118. ) {
  119. $this->redirect('passport/login');
  120. return false;
  121. }
  122. return true;
  123. }
  124. /**
  125. * 返回封装后的 API 数据到客户端
  126. * @param int $code
  127. * @param string $msg
  128. * @param string $url
  129. * @param array $data
  130. * @return array
  131. */
  132. protected function renderJson($code = 1, $msg = '', $url = '', $data = [])
  133. {
  134. return compact('code', 'msg', 'url', 'data');
  135. }
  136. /**
  137. * 返回操作成功json
  138. * @param string $msg
  139. * @param string $url
  140. * @param array $data
  141. * @return array
  142. */
  143. protected function renderSuccess($msg = 'success', $url = '', $data = [])
  144. {
  145. return $this->renderJson(1, $msg, $url, $data);
  146. }
  147. /**
  148. * 返回操作失败json
  149. * @param string $msg
  150. * @param string $url
  151. * @param array $data
  152. * @return array
  153. */
  154. protected function renderError($msg = 'error', $url = '', $data = [])
  155. {
  156. return $this->renderJson(0, $msg, $url, $data);
  157. }
  158. /**
  159. * 获取post数据 (数组)
  160. * @param $key
  161. * @return mixed
  162. */
  163. protected function postData($key)
  164. {
  165. return $this->request->post($key . '/a');
  166. }
  167. }