Controller.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. if ($this->routeUri === 'index/index') {
  71. return true;
  72. }
  73. if (!Auth::getInstance()->checkPrivilege($this->routeUri)) {
  74. throw new BaseException(['msg' => '很抱歉,没有访问权限']);
  75. }
  76. return true;
  77. }
  78. /**
  79. * 全局layout模板输出
  80. * @throws \think\exception\DbException
  81. * @throws \Exception
  82. */
  83. private function layout()
  84. {
  85. // 验证当前请求是否在白名单
  86. if (!in_array($this->routeUri, $this->notLayoutAction)) {
  87. // 输出到view
  88. $this->assign([
  89. 'base_url' => base_url(), // 当前域名
  90. 'store_url' => url('/store'), // 后台模块url
  91. 'group' => $this->group, // 当前控制器分组
  92. 'menus' => $this->menus(), // 后台菜单
  93. 'store' => $this->store, // 商家登录信息
  94. 'setting' => Setting::getAll() ?: null, // 当前商城设置
  95. 'request' => Request::instance(), // Request对象
  96. 'version' => get_version(), // 系统版本号
  97. ]);
  98. }
  99. }
  100. /**
  101. * 解析当前路由参数 (分组名称、控制器名称、方法名)
  102. */
  103. protected function getRouteinfo()
  104. {
  105. // 控制器名称
  106. $this->controller = toUnderScore($this->request->controller());
  107. // 方法名称
  108. $this->action = $this->request->action();
  109. // 控制器分组 (用于定义所属模块)
  110. $groupstr = strstr($this->controller, '.', true);
  111. $this->group = $groupstr !== false ? $groupstr : $this->controller;
  112. // 当前uri
  113. $this->routeUri = $this->controller . '/' . $this->action;
  114. }
  115. /**
  116. * 后台菜单配置
  117. * @return mixed
  118. * @throws \think\exception\DbException
  119. */
  120. protected function menus()
  121. {
  122. static $menus = [];
  123. if (empty($menus)) {
  124. $menus = Menus::getInstance()->getMenus($this->routeUri, $this->group);
  125. }
  126. return $menus;
  127. }
  128. /**
  129. * 验证登录状态
  130. * @return bool
  131. */
  132. private function checkLogin()
  133. {
  134. // 验证当前请求是否在白名单
  135. if (in_array($this->routeUri, $this->allowAllAction)) {
  136. return true;
  137. }
  138. // 验证登录状态
  139. if (empty($this->store)
  140. || (int)$this->store['is_login'] !== 1
  141. || !isset($this->store['wxapp'])
  142. || empty($this->store['wxapp'])
  143. ) {
  144. $this->redirect('passport/login');
  145. return false;
  146. }
  147. return true;
  148. }
  149. /**
  150. * 获取当前wxapp_id
  151. */
  152. protected function getWxappId()
  153. {
  154. return $this->store['wxapp']['wxapp_id'];
  155. }
  156. /**
  157. * 返回封装后的 API 数据到客户端
  158. * @param int $code
  159. * @param string $msg
  160. * @param string $url
  161. * @param array $data
  162. * @return array
  163. */
  164. protected function renderJson($code = 1, $msg = '', $url = '', $data = [])
  165. {
  166. return compact('code', 'msg', 'url', 'data');
  167. }
  168. /**
  169. * 返回操作成功json
  170. * @param string $msg
  171. * @param string $url
  172. * @param array $data
  173. * @return array
  174. */
  175. protected function renderSuccess($msg = 'success', $url = '', $data = [])
  176. {
  177. return $this->renderJson(1, $msg, $url, $data);
  178. }
  179. /**
  180. * 返回操作失败json
  181. * @param string $msg
  182. * @param string $url
  183. * @param array $data
  184. * @return array|bool
  185. */
  186. protected function renderError($msg = 'error', $url = '', $data = [])
  187. {
  188. if ($this->request->isAjax()) {
  189. return $this->renderJson(0, $msg, $url, $data);
  190. }
  191. $this->error($msg);
  192. return false;
  193. }
  194. /**
  195. * 获取post数据 (数组)
  196. * @param $key
  197. * @return mixed
  198. */
  199. protected function postData($key = null)
  200. {
  201. return $this->request->post(is_null($key) ? '' : $key . '/a');
  202. }
  203. /**
  204. * 获取post数据 (数组)
  205. * @param $key
  206. * @return mixed
  207. */
  208. protected function getData($key = null)
  209. {
  210. return $this->request->get(is_null($key) ? '' : $key);
  211. }
  212. }