Controller.php 6.2 KB

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