Controller.php 6.2 KB

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