Controller.php 4.0 KB

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