Controller.php 6.2 KB

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