Controller.php 5.2 KB

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