BaseController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app;
  4. use think\App;
  5. use think\exception\ValidateException;
  6. use think\Validate;
  7. use app\model\UserBehaviorLogModel;
  8. use think\facade\Request;
  9. /**
  10. * 控制器基础类
  11. */
  12. abstract class BaseController
  13. {
  14. /**
  15. * Request实例
  16. * @var \think\Request
  17. */
  18. protected $request;
  19. /**
  20. * 应用实例
  21. * @var \think\App
  22. */
  23. protected $app;
  24. /**
  25. * 是否批量验证
  26. * @var bool
  27. */
  28. protected $batchValidate = false;
  29. protected array $userInfo = [];
  30. protected int $userId = 0;
  31. /**
  32. * 控制器中间件
  33. * @var array
  34. */
  35. protected $middleware = [];
  36. /**
  37. * 构造方法
  38. * @access public
  39. * @param App $app 应用对象
  40. */
  41. public function __construct(App $app)
  42. {
  43. $this->app = $app;
  44. $this->request = $this->app->request;
  45. // 控制器初始化
  46. $this->initialize();
  47. }
  48. // 初始化
  49. protected function initialize()
  50. {
  51. // 从中间件注入的用户信息
  52. if (isset($this->request->userInfo)) {
  53. $this->userInfo = $this->request->userInfo;
  54. $this->userId = $this->request->userId;
  55. }
  56. }
  57. /**
  58. * 获取当前用户ID
  59. */
  60. protected function getUserId(): int
  61. {
  62. return $this->userId;
  63. }
  64. /**
  65. * 获取当前用户信息
  66. */
  67. protected function getUserInfo(): array
  68. {
  69. return $this->userInfo;
  70. }
  71. /**
  72. * 记录操作日志
  73. * @param int $status 操作状态
  74. * @param array $filterParams 需要过滤的参数键名(如密码等敏感信息)
  75. * @return bool
  76. */
  77. protected function recordBehavior(int $status = UserBehaviorLogModel::STATUS_SUCCESS, array $filterParams = ['password']): bool
  78. {
  79. try {
  80. // 获取用户信息
  81. $userInfo = $this->getUserInfo();
  82. if (empty($userInfo)) {
  83. return false;
  84. }
  85. // 获取控制器和方法名
  86. $controller = Request::controller();
  87. $action = Request::action();
  88. $behavior = $controller . '/' . $action;
  89. // 获取权限配置中的行为描述
  90. $permissions = config('permission.permissions');
  91. $behaviorText = '';
  92. if (isset($permissions[$controller]['actions'][$action])) {
  93. $behaviorText = $permissions[$controller]['module'] . '-' . $permissions[$controller]['actions'][$action];
  94. } else {
  95. $behaviorText = $behavior;
  96. }
  97. // 获取请求参数并过滤敏感信息
  98. $params = Request::param();
  99. foreach ($filterParams as $key) {
  100. if (isset($params[$key])) {
  101. unset($params[$key]);
  102. }
  103. }
  104. // 构建日志数据
  105. $data = [
  106. 'merchant_id' => $userInfo['merchant_id'] ?? 0,
  107. 'user_id' => $userInfo['user_id'] ?? 0,
  108. 'behavior' => $behaviorText,
  109. 'behavior_desc' => json_encode($params, JSON_UNESCAPED_UNICODE),
  110. 'behavior_ip' => getClientIp(),
  111. 'behavior_url' => Request::url(true),
  112. 'behavior_status' => $status
  113. ];
  114. // 记录日志
  115. return UserBehaviorLogModel::recordBehavior($data);
  116. } catch (\Exception $e) {
  117. // 记录日志失败不影响业务
  118. return false;
  119. }
  120. }
  121. /**
  122. * 验证数据
  123. * @access protected
  124. * @param array $data 数据
  125. * @param string|array $validate 验证器名或者验证规则数组
  126. * @param array $message 提示信息
  127. * @param bool $batch 是否批量验证
  128. * @return array|string|true
  129. * @throws ValidateException
  130. */
  131. protected function validate(array $data, string|array $validate, array $message = [], bool $batch = false)
  132. {
  133. if (is_array($validate)) {
  134. $v = new Validate();
  135. $v->rule($validate);
  136. } else {
  137. if (strpos($validate, '.')) {
  138. // 支持场景
  139. [$validate, $scene] = explode('.', $validate);
  140. }
  141. $class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
  142. $v = new $class();
  143. if (!empty($scene)) {
  144. $v->scene($scene);
  145. }
  146. }
  147. $v->message($message);
  148. // 是否批量验证
  149. if ($batch || $this->batchValidate) {
  150. $v->batch(true);
  151. }
  152. return $v->failException(true)->check($data);
  153. }
  154. }