ActionLogService.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services\Common;
  12. use App\Models\ActionLogModel;
  13. use App\Services\Api\MemberService;
  14. use App\Services\BaseService;
  15. use App\Services\RedisService;
  16. /**
  17. * 行为日志-服务类
  18. * @author laravel开发员
  19. * @since 2020/11/12
  20. * Class ActionLogService
  21. * @package App\Services\Common
  22. */
  23. class ActionLogService extends BaseService
  24. {
  25. /**
  26. * 构造函数
  27. * @author laravel开发员
  28. * @since 2020/11/12
  29. * ActionLogService constructor.
  30. */
  31. public function __construct()
  32. {
  33. $this->model = new ActionLogModel();
  34. }
  35. /**
  36. * 静态入口
  37. * @return static|null
  38. */
  39. public static function make()
  40. {
  41. if (!self::$instance) {
  42. self::$instance = (new static());
  43. }
  44. return self::$instance;
  45. }
  46. /**
  47. * 常用设备
  48. * @param $username
  49. * @return array|false|mixed
  50. */
  51. public function getDevices($username)
  52. {
  53. $cacheKey = "caches:devices:".md5($username);
  54. if($datas = RedisService::get($cacheKey)){
  55. return $datas;
  56. }
  57. $datas = $this->model->where(['username'=> $username,'mark'=>1])
  58. ->distinct()
  59. ->select(['user_agent','ip','username','create_time'])
  60. ->orderBy('create_time','desc')
  61. ->take(3)
  62. ->get();
  63. $datas = $datas? $datas->toArray() : [];
  64. if($datas){
  65. foreach ($datas as &$item){
  66. $br = $item['user_agent'];
  67. if($br){
  68. if (preg_match('/MSIE\/[0-9\.]+/i', $br, $result)) {
  69. $br = isset($result[0])? trim($result[0]) : '';
  70. } elseif (preg_match('/Firefox\/[0-9\.]+/i', $br, $result)) {
  71. $br = isset($result[0])? trim($result[0]) : '';
  72. } elseif (preg_match('/Chrome\/[0-9\.]+/i', $br, $result)) {
  73. $br = isset($result[0])? trim($result[0]) : '';
  74. } elseif (preg_match('/Safari\/[0-9\.]+/i', $br, $result)) {
  75. $br = isset($result[0])? trim($result[0]) : '';
  76. } elseif (preg_match('/Opera\/[0-9\.]+/i', $br, $result)) {
  77. $br = isset($result[0])? trim($result[0]) : '';
  78. } else {
  79. $br = 'Other';
  80. }
  81. }
  82. $item['device'] = $br;
  83. $item['time_text'] = $item['create_time']? datetime($item['create_time'], 'm-d H:i'):'';
  84. }
  85. RedisService::set($cacheKey, $datas, rand(10, 30));
  86. }
  87. return $datas;
  88. }
  89. }