| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136 |
- <?php
- namespace addons\Agent\common\services;
- use addons\Agent\common\enums\AgentBehaviorLogsEnum;
- use addons\Agent\common\models\AgentBehaviorLogs;
- use backend\modules\mall\services\BaseService;
- use common\enums\StatusEnum;
- use common\models\backend\Admin;
- use common\models\user\User;
- class LogsServices extends BaseService
- {
- /**
- * 获取用户日志
- *
- * @param array $params
- *
- * @return array
- */
- public function getAgentLogs(array $params): array
- {
- $operatorUserType = $params['operator_user_type'] ?? 0;
- $operatorUserKeyword = $params['operator_user_keyword'] ?? 0;
- $operatorTargetUserKeyword = $params['operator_target_user_keyword'] ?? '';
- $behavior = $params['behavior'] ?? 0;
- $where = [['mall_id' => $this->mall_id]];
- if ($operatorUserType) {
- $where[] = ['operator_user_type' => $operatorUserType];
- }
- if ($operatorUserKeyword) {
- if ($operatorUserType == AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_ADMIN) {
- $adminFind = Admin::find();
- Admin::paramPack([
- 'where' => [
- ['mall_id' => [0, $this->mall_id]],
- [
- 'or',
- ['like', 'username', $operatorUserKeyword],
- ['like', 'account', $operatorUserKeyword]
- ]
- ],
- 'select' => 'id'
- ], $adminFind);
- $adminIdsBy = $adminFind->column();
- $where[] = ['operator_user_id' => $adminIdsBy];
- } elseif ($operatorUserType == AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_USER) {
- $userIdsByMobile = User::find()
- ->where(['like', 'mobile', $operatorUserKeyword])
- ->andWhere(['mall_id' => $this->mall_id])
- ->select('id')
- ->column();
- $where[] = ['operator_user_id' => array_merge([$operatorUserKeyword], $userIdsByMobile)];
- }
- }
- if ($operatorTargetUserKeyword) {
- $userIdsByOperatorTargetUserKeyword = User::find()
- ->where(['like', 'mobile', $operatorTargetUserKeyword])
- ->andWhere(['mall_id' => $this->mall_id])
- ->select('id')
- ->column();
- $where[] = [
- 'or',
- ['=', 'operator_target_user_id', $operatorTargetUserKeyword],
- ['in', 'operator_target_user_id', $userIdsByOperatorTargetUserKeyword]
- ];
- }
- if ($behavior) {
- $where[] = ['=', 'behavior', $behavior];
- }
- $userSelect = 'id, mobile, nickname, avatar_url';
- $with = [
- 'user' => function ($query) use ($userSelect) {
- $query->select($userSelect);
- }
- ];
- $order = 'id desc';
- $list = AgentBehaviorLogs::listPage(compact('where', 'with', 'order'));
- $operatorUserIds = [];
- array_walk($list['list'], function ($item) use (&$operatorUserIds) {
- $operatorUserIds[$item['operator_user_type']][] = $item['operator_user_id'];
- });
- // 查询用户
- $userList = [];
- if (!empty($operatorUserIds[AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_USER])) {
- $userList = User::getUserList(
- [
- ['in', 'id', $operatorUserIds[AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_USER]]
- ],
- $userSelect,
- [],
- true
- );
- }
- // 查询后台管理员
- $adminList = [];
- if (!empty($operatorUserIds[AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_ADMIN])) {
- $adminList = Admin::lists([
- 'where' => [
- [
- 'id' => $operatorUserIds[AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_ADMIN],
- 'status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]
- ]
- ],
- 'select' => "id, account mobile, username nickname"
- ]);
- }
- $userList = array_column($userList, null, 'id');
- $adminList = array_column($adminList, null, 'id');
- array_walk($list['list'], function (&$item) use ($adminList, $userList) {
- $item['extra_info'] = json_decode($item['extra_info'], true);
- $item['behavior_text'] = AgentBehaviorLogsEnum::getMapValueByKey(0, $item['behavior']);
- $item['operator_user_type_text'] = AgentBehaviorLogsEnum::getMapValueByKey(1, $item['operator_user_type']);
- $item['created_at_text'] = date('Y-m-d H:i:s', $item['created_at']);
- $item['ip_text'] = $item['ip'] ? long2ip($item['ip']) : '-';
- $item['operator_user'] = AgentBehaviorLogsEnum::OPERATOR_USER_TYPE_USER == $item['operator_user_type'] ?
- ($userList[$item['operator_user_id']] ?? []) :
- ($adminList[$item['operator_user_id']] ?? []);
- });
- return $list;
- }
- }
|