LogsServices.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. namespace addons\Agent\common\services;
  3. use addons\Agent\common\enums\AgentBehaviorLogsEnum;
  4. use addons\Agent\common\models\AgentBehaviorLogs;
  5. use backend\modules\mall\services\BaseService;
  6. use common\enums\StatusEnum;
  7. use common\models\backend\Admin;
  8. use common\models\user\User;
  9. class LogsServices extends BaseService
  10. {
  11. /**
  12. * 获取用户日志
  13. *
  14. * @param array $params
  15. *
  16. * @return array
  17. */
  18. public function getAgentLogs(array $params): array
  19. {
  20. $operatorUserType = $params['operator_user_type'] ?? 0;
  21. $operatorUserKeyword = $params['operator_user_keyword'] ?? 0;
  22. $operatorTargetUserKeyword = $params['operator_target_user_keyword'] ?? '';
  23. $behavior = $params['behavior'] ?? 0;
  24. $where = [['mall_id' => $this->mall_id]];
  25. if ($operatorUserType) {
  26. $where[] = ['operator_user_type' => $operatorUserType];
  27. }
  28. if ($operatorUserKeyword) {
  29. if ($operatorUserType == AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_ADMIN) {
  30. $adminFind = Admin::find();
  31. Admin::paramPack([
  32. 'where' => [
  33. ['mall_id' => [0, $this->mall_id]],
  34. [
  35. 'or',
  36. ['like', 'username', $operatorUserKeyword],
  37. ['like', 'account', $operatorUserKeyword]
  38. ]
  39. ],
  40. 'select' => 'id'
  41. ], $adminFind);
  42. $adminIdsBy = $adminFind->column();
  43. $where[] = ['operator_user_id' => $adminIdsBy];
  44. } elseif ($operatorUserType == AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_USER) {
  45. $userIdsByMobile = User::find()
  46. ->where(['like', 'mobile', $operatorUserKeyword])
  47. ->andWhere(['mall_id' => $this->mall_id])
  48. ->select('id')
  49. ->column();
  50. $where[] = ['operator_user_id' => array_merge([$operatorUserKeyword], $userIdsByMobile)];
  51. }
  52. }
  53. if ($operatorTargetUserKeyword) {
  54. $userIdsByOperatorTargetUserKeyword = User::find()
  55. ->where(['like', 'mobile', $operatorTargetUserKeyword])
  56. ->andWhere(['mall_id' => $this->mall_id])
  57. ->select('id')
  58. ->column();
  59. $where[] = [
  60. 'or',
  61. ['=', 'operator_target_user_id', $operatorTargetUserKeyword],
  62. ['in', 'operator_target_user_id', $userIdsByOperatorTargetUserKeyword]
  63. ];
  64. }
  65. if ($behavior) {
  66. $where[] = ['=', 'behavior', $behavior];
  67. }
  68. $userSelect = 'id, mobile, nickname, avatar_url';
  69. $with = [
  70. 'user' => function ($query) use ($userSelect) {
  71. $query->select($userSelect);
  72. }
  73. ];
  74. $order = 'id desc';
  75. $list = AgentBehaviorLogs::listPage(compact('where', 'with', 'order'));
  76. $operatorUserIds = [];
  77. array_walk($list['list'], function ($item) use (&$operatorUserIds) {
  78. $operatorUserIds[$item['operator_user_type']][] = $item['operator_user_id'];
  79. });
  80. // 查询用户
  81. $userList = [];
  82. if (!empty($operatorUserIds[AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_USER])) {
  83. $userList = User::getUserList(
  84. [
  85. ['in', 'id', $operatorUserIds[AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_USER]]
  86. ],
  87. $userSelect,
  88. [],
  89. true
  90. );
  91. }
  92. // 查询后台管理员
  93. $adminList = [];
  94. if (!empty($operatorUserIds[AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_ADMIN])) {
  95. $adminList = Admin::lists([
  96. 'where' => [
  97. [
  98. 'id' => $operatorUserIds[AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_ADMIN],
  99. 'status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]
  100. ]
  101. ],
  102. 'select' => "id, account mobile, username nickname"
  103. ]);
  104. }
  105. $userList = array_column($userList, null, 'id');
  106. $adminList = array_column($adminList, null, 'id');
  107. array_walk($list['list'], function (&$item) use ($adminList, $userList) {
  108. $item['extra_info'] = json_decode($item['extra_info'], true);
  109. $item['behavior_text'] = AgentBehaviorLogsEnum::getMapValueByKey(0, $item['behavior']);
  110. $item['operator_user_type_text'] = AgentBehaviorLogsEnum::getMapValueByKey(1, $item['operator_user_type']);
  111. $item['created_at_text'] = date('Y-m-d H:i:s', $item['created_at']);
  112. $item['ip_text'] = $item['ip'] ? long2ip($item['ip']) : '-';
  113. $item['operator_user'] = AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_USER == $item['operator_user_type'] ?
  114. ($userList[$item['operator_user_id']] ?? []) :
  115. ($adminList[$item['operator_user_id']] ?? []);
  116. });
  117. return $list;
  118. }
  119. }