SystemUser.php 4.4 KB

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