request->user ?? null; if (!$auth) { return error('未登录', 401); } if (!in_array($auth['role'] ?? '', ['admin', 'enterprise_admin'])) { return error('无权限访问', 403); } $userId = (int) Request::param('userId', 0); if ($userId <= 0) { return error('userId 不能为空', 400); } $enterpriseId = $auth['enterpriseId'] ?? null; if (!$enterpriseId) { $adminRow = Db::name('users')->where('id', $auth['userId'] ?? 0)->find(); $enterpriseId = $adminRow['enterpriseId'] ?? null; } if ($enterpriseId) { $has = Db::name('user_profile') ->where('userId', $userId) ->where('enterpriseId', (int) $enterpriseId) ->find(); if (!$has) { return error('无权限查看该用户', 403); } } $days = min(180, max(1, (int) Request::param('days', 30))); $since = date('Y-m-d H:i:s', time() - $days * 86400); $page = max(1, (int) Request::param('page', 1)); $pageSize = min(100, max(1, (int) Request::param('pageSize', 20))); $offset = ($page - 1) * $pageSize; try { $baseQ = Db::name('analytics_events') ->where('userId', $userId) ->where('createdAt', '>=', $since); $total = (int) (clone $baseQ)->count(); $rows = (clone $baseQ) ->order('id', 'desc') ->limit($offset, $pageSize) ->select() ->toArray(); foreach ($rows as &$r) { if (!empty($r['propsJson'])) { $decoded = json_decode($r['propsJson'], true); $r['props'] = is_array($decoded) ? $decoded : null; } else { $r['props'] = null; } unset($r['propsJson']); } unset($r); $rows = AnalyticsEventLabels::withCn($rows); return success([ 'userId' => $userId, 'days' => $days, 'list' => $rows, 'total' => $total, 'page' => $page, 'pageSize' => $pageSize, ]); } catch (\Throwable $e) { return success([ 'userId' => $userId, 'days' => $days, 'list' => [], 'total' => 0, 'page' => $page, 'pageSize' => $pageSize, 'tableMissing' => true, ]); } } }