Controller.php 6.3 KB

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