SystemUser.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | saiadmin [ saiadmin快速开发框架 ]
  4. // +----------------------------------------------------------------------
  5. // | Author: sai <1430792918@qq.com>
  6. // +----------------------------------------------------------------------
  7. namespace plugin\saiadmin\app\event;
  8. use plugin\saiadmin\app\cache\ReflectionCache;
  9. use plugin\saiadmin\app\model\system\SystemLoginLog;
  10. use plugin\saiadmin\app\model\system\SystemOperLog;
  11. class SystemUser
  12. {
  13. /**
  14. * 登录日志
  15. * @param $item
  16. */
  17. public function login($item)
  18. {
  19. $request = request();
  20. $ip = $request ? $request->getRealIp() : '127.0.0.1';
  21. $http_user_agent = $request ? $request->header('user-agent') : '';
  22. $data['username'] = $item['username'];
  23. $data['ip'] = $ip;
  24. $data['ip_location'] = self::getIpLocation($ip);
  25. $data['os'] = self::getOs($http_user_agent);
  26. $data['browser'] = self::getBrowser($http_user_agent);
  27. $data['status'] = $item['status'];
  28. $data['message'] = $item['message'];
  29. $data['login_time'] = date('Y-m-d H:i:s');
  30. if (isset($item['admin_id'])) {
  31. $data['created_by'] = $item['admin_id'];
  32. $data['updated_by'] = $item['admin_id'];
  33. }
  34. SystemLoginLog::create($data);
  35. }
  36. /**
  37. * 记录操作日志
  38. */
  39. public function operateLog(): bool
  40. {
  41. $request = request();
  42. if (!$request) {
  43. return false;
  44. }
  45. if ($request->method() === 'GET') {
  46. return false;
  47. }
  48. $info = getCurrentInfo();
  49. $ip = $request->getRealIp();
  50. $module = $request->plugin;
  51. $rule = trim($request->uri());
  52. $data['username'] = $info['username'];
  53. $data['method'] = $request->method();
  54. $data['router'] = $rule;
  55. $data['service_name'] = self::getServiceName();
  56. $data['app'] = $module;
  57. $data['ip'] = $ip;
  58. $data['ip_location'] = self::getIpLocation($ip);
  59. $data['request_data'] = $this->filterParams($request->all());
  60. SystemOperLog::create($data);
  61. return true;
  62. }
  63. /**
  64. * 获取业务名称
  65. */
  66. protected function getServiceName(): string
  67. {
  68. $request = request();
  69. if (!$request) {
  70. return '未命名业务';
  71. }
  72. $permissions = ReflectionCache::getPermissionAttributes($request->controller, $request->action);
  73. if (!empty($permissions)) {
  74. return $permissions['title'] ?? '未命名业务';
  75. } else {
  76. return '未命名业务';
  77. }
  78. }
  79. /**
  80. * 过滤字段
  81. */
  82. protected function filterParams($params): string
  83. {
  84. $blackList = ['password', 'oldPassword', 'newPassword', 'confirmPassword'];
  85. foreach ($params as $key => $value) {
  86. if (in_array($key, $blackList)) {
  87. $params[$key] = '******';
  88. }
  89. }
  90. return json_encode($params, JSON_UNESCAPED_UNICODE);
  91. }
  92. /**
  93. * 获取IP地理位置
  94. */
  95. protected function getIpLocation($ip): string
  96. {
  97. $ip2region = new \Ip2Region();
  98. try {
  99. $region = $ip2region->memorySearch($ip);
  100. } catch (\Exception $e) {
  101. return '未知';
  102. }
  103. list($country, $province, $city, $network) = explode('|', $region['region']);
  104. if ($network === '内网IP') {
  105. return $network;
  106. }
  107. if ($country == '中国') {
  108. return $province . '-' . $city . ':' . $network;
  109. } else if ($country == '0') {
  110. return '未知';
  111. } else {
  112. return $country;
  113. }
  114. }
  115. /**
  116. * 获取浏览器信息
  117. */
  118. protected function getBrowser($user_agent): string
  119. {
  120. $br = 'Unknown';
  121. if (preg_match('/MSIE/i', $user_agent)) {
  122. $br = 'MSIE';
  123. } elseif (preg_match('/Firefox/i', $user_agent)) {
  124. $br = 'Firefox';
  125. } elseif (preg_match('/Chrome/i', $user_agent)) {
  126. $br = 'Chrome';
  127. } elseif (preg_match('/Safari/i', $user_agent)) {
  128. $br = 'Safari';
  129. } elseif (preg_match('/Opera/i', $user_agent)) {
  130. $br = 'Opera';
  131. } else {
  132. $br = 'Other';
  133. }
  134. return $br;
  135. }
  136. /**
  137. * 获取操作系统信息
  138. */
  139. protected function getOs($user_agent): string
  140. {
  141. $os = 'Unknown';
  142. if (preg_match('/win/i', $user_agent)) {
  143. $os = 'Win';
  144. } elseif (preg_match('/mac/i', $user_agent)) {
  145. $os = 'Mac';
  146. } elseif (preg_match('/linux/i', $user_agent)) {
  147. $os = 'Linux';
  148. } else {
  149. $os = 'Other';
  150. }
  151. return $os;
  152. }
  153. }