Controller.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace app\shop\controller;
  3. use app\common\exception\BaseException;
  4. use app\common\model\settings\Setting;
  5. use app\common\model\shop\OptLog as OptLogModel;
  6. use app\JjjController;
  7. use app\shop\service\AuthService;
  8. /**
  9. * 商户后台控制器基类
  10. */
  11. class Controller extends JjjController
  12. {
  13. /** @var array $store 商家登录信息 */
  14. protected $store;
  15. /** @var string $route 当前控制器名称 */
  16. protected $controller = '';
  17. /** @var string $route 当前方法名称 */
  18. protected $action = '';
  19. /** @var string $route 当前路由uri */
  20. protected $routeUri = '';
  21. /** @var string $route 当前路由:分组名称 */
  22. protected $group = '';
  23. /** @var string $route 当前路由:分组名称 */
  24. protected $menu = '';
  25. /** @var array $allowAllAction 登录验证白名单 */
  26. protected $allowAllAction = [
  27. // 登录页面
  28. '/passport/login',
  29. /*登录信息*/
  30. '/index/base'
  31. ];
  32. /**
  33. * 后台初始化
  34. */
  35. public function initialize()
  36. {
  37. // 商家登录信息
  38. $this->store = session('jjjshop_store');
  39. // 当前路由信息
  40. $this->getRouteinfo();
  41. // 验证登录状态
  42. $this->checkLogin();
  43. // 写入操作日志
  44. $this->saveOptLog();
  45. // 验证当前页面权限
  46. $this->checkPrivilege();
  47. }
  48. /**
  49. * 验证当前页面权限
  50. */
  51. private function checkPrivilege()
  52. {
  53. if($this->store == null){
  54. return false;
  55. }
  56. if (!AuthService::getInstance()->checkPrivilege($this->routeUri)) {
  57. throw new BaseException(['msg' => '很抱歉,没有访问权限']);
  58. }
  59. return true;
  60. }
  61. /**
  62. * 解析当前路由参数 (分组名称、控制器名称、方法名)
  63. */
  64. protected function getRouteinfo()
  65. {
  66. // 控制器名称
  67. $this->controller = strtolower($this->request->controller());
  68. $this->controller = str_replace(".","/",$this->controller);
  69. // 方法名称
  70. $this->action = Request()->action();
  71. // 控制器分组 (用于定义所属模块)
  72. $groupstr = strstr($this->controller, '.', true);
  73. $this->group = $groupstr !== false ? $groupstr : $this->controller;
  74. // 当前uri
  75. $this->routeUri = '/' . $this->controller . '/' . $this->action;
  76. }
  77. /**
  78. * 验证登录状态
  79. */
  80. private function checkLogin()
  81. {
  82. // 验证当前请求是否在白名单
  83. if (in_array($this->routeUri, $this->allowAllAction)) {
  84. return true;
  85. }
  86. // 验证登录状态
  87. if ($this->store != null && $this->store['is_login'] == 1) {
  88. return true;
  89. }
  90. throw new BaseException(['code' => -1, 'msg' => 'not_login']);
  91. }
  92. /**
  93. * 操作日志
  94. */
  95. private function saveOptLog(){
  96. if($this->store == null){
  97. return;
  98. }
  99. $shop_user_id = $this->store['user']['shop_user_id'];
  100. if(!$shop_user_id){
  101. return;
  102. }
  103. // 如果不记录查询日志
  104. $config = Setting::getItem('store');
  105. if(!$config || !$config['is_get_log']){
  106. return;
  107. }
  108. $model = new OptLogModel();
  109. $model->save([
  110. 'shop_user_id' => $shop_user_id,
  111. 'ip' => \request()->ip(),
  112. 'request_type' => $this->request->isGet()?'Get':'Post',
  113. 'url' => $this->routeUri,
  114. 'content' => json_encode($this->request->param(), JSON_UNESCAPED_UNICODE),
  115. 'browser' => get_client_browser(),
  116. 'agent' => $_SERVER['HTTP_USER_AGENT'],
  117. 'title' => AuthService::getAccessNameByPath($this->routeUri, $this->store['app']['app_id']),
  118. 'app_id' => $this->store['app']['app_id']
  119. ]);
  120. }
  121. }