Controller.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. /**
  3. *
  4. * @copyright ©2020 点小铺
  5. * @author hanj
  6. * @link: https://dyuit.com
  7. * Created by VSCode
  8. */
  9. namespace app\store\controller;
  10. use app\store\service\Auth;
  11. use app\store\service\Menus;
  12. use app\store\model\Setting;
  13. use app\common\exception\BaseException;
  14. use think\Request;
  15. use think\Session;
  16. /**
  17. * 商户后台控制器基类
  18. * Class BaseController
  19. * @package app\store\controller
  20. */
  21. class Controller extends \think\Controller
  22. {
  23. /** @var array $store 商家登录信息 */
  24. protected $store;
  25. /** @var string $route 当前控制器名称 */
  26. protected $controller = '';
  27. /** @var string $route 当前方法名称 */
  28. protected $action = '';
  29. /** @var string $route 当前路由uri */
  30. protected $routeUri = '';
  31. /** @var string $route 当前路由:分组名称 */
  32. protected $group = '';
  33. /** @var array $allowAllAction 登录验证白名单 */
  34. protected $allowAllAction = [
  35. // 登录页面
  36. 'passport/login',
  37. ];
  38. /* @var array $notLayoutAction 无需全局layout */
  39. protected $notLayoutAction = [
  40. // 登录页面
  41. 'passport/login',
  42. ];
  43. /**
  44. * 后台初始化
  45. * @throws BaseException
  46. * @throws \think\db\exception\DataNotFoundException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. * @throws \think\exception\DbException
  49. */
  50. public function _initialize()
  51. {
  52. // 商家登录信息
  53. $this->store = Session::get('dyu_store');
  54. // 当前路由信息
  55. $this->getRouteinfo();
  56. // 验证登录状态
  57. $this->checkLogin();
  58. // 验证当前页面权限
  59. $this->checkPrivilege();
  60. // 全局layout
  61. $this->layout();
  62. }
  63. /**
  64. * 验证当前页面权限
  65. * @throws BaseException
  66. * @throws \think\db\exception\DataNotFoundException
  67. * @throws \think\db\exception\ModelNotFoundException
  68. * @throws \think\exception\DbException
  69. */
  70. private function checkPrivilege()
  71. {
  72. if ($this->routeUri === 'index/index') {
  73. return true;
  74. }
  75. if (!Auth::getInstance()->checkPrivilege($this->routeUri)) {
  76. throw new BaseException(['msg' => '很抱歉,没有访问权限']);
  77. }
  78. return true;
  79. }
  80. /**
  81. * 全局layout模板输出
  82. * @throws \think\exception\DbException
  83. * @throws \Exception
  84. */
  85. private function layout()
  86. {
  87. // 验证当前请求是否在白名单
  88. if (!in_array($this->routeUri, $this->notLayoutAction)) {
  89. // 输出到view
  90. $this->assign([
  91. 'base_url' => base_url(), // 当前域名
  92. 'store_url' => url('/store'), // 后台模块url
  93. 'group' => $this->group, // 当前控制器分组
  94. 'menus' => $this->menus(), // 后台菜单
  95. 'store' => $this->store, // 商家登录信息
  96. 'setting' => Setting::getAll() ?: null, // 当前商城设置
  97. 'request' => Request::instance(), // Request对象
  98. 'version' => get_version(), // 系统版本号
  99. ]);
  100. }
  101. }
  102. /**
  103. * 解析当前路由参数 (分组名称、控制器名称、方法名)
  104. */
  105. protected function getRouteinfo()
  106. {
  107. // 控制器名称
  108. $this->controller = toUnderScore($this->request->controller());
  109. // 方法名称
  110. $this->action = $this->request->action();
  111. // 控制器分组 (用于定义所属模块)
  112. $groupstr = strstr($this->controller, '.', true);
  113. $this->group = $groupstr !== false ? $groupstr : $this->controller;
  114. // 当前uri
  115. $this->routeUri = $this->controller . '/' . $this->action;
  116. }
  117. /**
  118. * 后台菜单配置
  119. * @return mixed
  120. * @throws \think\exception\DbException
  121. */
  122. protected function menus()
  123. {
  124. static $menus = [];
  125. if (empty($menus)) {
  126. $menus = Menus::getInstance()->getMenus($this->routeUri, $this->group);
  127. }
  128. return $menus;
  129. }
  130. /**
  131. * 验证登录状态
  132. * @return bool
  133. */
  134. private function checkLogin()
  135. {
  136. // 验证当前请求是否在白名单
  137. if (in_array($this->routeUri, $this->allowAllAction)) {
  138. return true;
  139. }
  140. // 验证登录状态
  141. if (empty($this->store)
  142. || (int)$this->store['is_login'] !== 1
  143. || !isset($this->store['wxapp'])
  144. || empty($this->store['wxapp'])
  145. ) {
  146. $this->redirect('passport/login');
  147. return false;
  148. }
  149. return true;
  150. }
  151. /**
  152. * 获取当前wxapp_id
  153. */
  154. protected function getWxappId()
  155. {
  156. return $this->store['wxapp']['wxapp_id'];
  157. }
  158. /**
  159. * 返回封装后的 API 数据到客户端
  160. * @param int $code
  161. * @param string $msg
  162. * @param string $url
  163. * @param array $data
  164. * @return array
  165. */
  166. protected function renderJson($code = 1, $msg = '', $url = '', $data = [])
  167. {
  168. return compact('code', 'msg', 'url', 'data');
  169. }
  170. /**
  171. * 返回操作成功json
  172. * @param string $msg
  173. * @param string $url
  174. * @param array $data
  175. * @return array
  176. */
  177. protected function renderSuccess($msg = 'success', $url = '', $data = [])
  178. {
  179. return $this->renderJson(1, $msg, $url, $data);
  180. }
  181. /**
  182. * 返回操作失败json
  183. * @param string $msg
  184. * @param string $url
  185. * @param array $data
  186. * @return array|bool
  187. */
  188. protected function renderError($msg = 'error', $url = '', $data = [])
  189. {
  190. if ($this->request->isAjax()) {
  191. return $this->renderJson(0, $msg, $url, $data);
  192. }
  193. $this->error($msg);
  194. return false;
  195. }
  196. /**
  197. * 获取post数据 (数组)
  198. * @param $key
  199. * @return mixed
  200. */
  201. protected function postData($key = null)
  202. {
  203. return $this->request->post(is_null($key) ? '' : $key . '/a');
  204. }
  205. /**
  206. * 获取post数据 (数组)
  207. * @param $key
  208. * @return mixed
  209. */
  210. protected function getData($key = null)
  211. {
  212. return $this->request->get(is_null($key) ? '' : $key);
  213. }
  214. }