getCurrentUser()); $where .= $scope['where']; $params = array_merge($params, $scope['params']); if ($botQq) { $where .= ' AND a.bot_qq = ?'; $params[] = $botQq; } if ($contactQq) { $where .= ' AND a.contact_qq = ?'; $params[] = $contactQq; } if ($status) { $where .= ' AND a.status = ?'; $params[] = $status; } if ($projectId) { $where .= ' AND a.project_id = ?'; $params[] = $projectId; } if ($cardKey) { $where .= ' AND a.card_key LIKE ?'; $params[] = "%{$cardKey}%"; } if ($isExpired === '1') { $where .= ' AND a.status = \'active\' AND a.expire_time IS NOT NULL AND a.expire_time < NOW()'; } if ($isExpired === '0') { $where .= ' AND (a.expire_time IS NULL OR a.expire_time >= NOW() OR a.status != \'active\')'; } $total = $db->fetchColumn( "SELECT COUNT(*) FROM {$db->table('authorizations')} a {$where}", $params ); $rows = $db->fetchAll( "SELECT a.*, p.name AS project_name, ct.name AS card_type_name FROM {$db->table('authorizations')} a LEFT JOIN {$db->table('projects')} p ON a.project_id = p.id LEFT JOIN {$db->table('cards')} c ON a.card_id = c.id LEFT JOIN {$db->table('card_types')} ct ON c.card_type_id = ct.id {$where} ORDER BY a.id DESC LIMIT {$offset}, {$pageSize}", $params ); // 实时计算过期状态 $now = time(); foreach ($rows as &$row) { $row['is_expired'] = $row['status'] === 'active' && $row['expire_time'] && strtotime($row['expire_time']) < $now; } $this->success([ 'list' => $rows, 'total' => (int) $total, 'page' => $page, 'page_size' => $pageSize, ]); } /** * 授权详情 * GET /api/authorizations/{id} */ public function detail(int $id): void { $db = Database::getInstance(); $row = $db->fetch( "SELECT a.*, p.name AS project_name, ct.name AS card_type_name FROM {$db->table('authorizations')} a LEFT JOIN {$db->table('projects')} p ON a.project_id = p.id LEFT JOIN {$db->table('cards')} c ON a.card_id = c.id LEFT JOIN {$db->table('card_types')} ct ON c.card_type_id = ct.id WHERE a.id = ?", [$id] ); if (!$row) { $this->error('授权记录不存在', 404); } $row['is_expired'] = $row['status'] === 'active' && $row['expire_time'] && strtotime($row['expire_time']) < time(); $this->success($row); } /** * 新增授权(绑定卡密到机器人QQ/联系人QQ) * POST /api/authorizations * Body: { card_key, bot_qq, contact_qq, contact_name? } */ public function create(): void { $input = $this->getJsonInput(); $projectId = (int) ($input['project_id'] ?? 0); $cardTypeId = (int) ($input['card_type_id'] ?? 0); $botQq = trim($input['bot_qq'] ?? ''); $contactQq = trim($input['contact_qq'] ?? ''); $contactName = trim($input['contact_name'] ?? ''); // 参数校验 if (empty($projectId)) { $this->error('请选择项目'); } if (empty($cardTypeId)) { $this->error('请选择套餐'); } if (empty($botQq)) { $this->error('机器人QQ不能为空'); } if (empty($contactQq)) { $this->error('联系人QQ不能为空'); } if (!preg_match('/^\d{5,15}$/', $botQq)) { $this->error('机器人QQ格式不正确'); } if (!preg_match('/^\d{5,15}$/', $contactQq)) { $this->error('联系人QQ格式不正确'); } $db = Database::getInstance(); $userId = $this->getUserId() ?? 0; $currentRole = $this->getUserRole(); // 验证项目存在 $project = $db->fetch("SELECT * FROM {$db->table('projects')} WHERE id = ? AND deleted_at IS NULL", [$projectId]); if (!$project) { $this->error('项目不存在'); } // 验证套餐 $cardType = $db->fetch( "SELECT * FROM {$db->table('card_types')} WHERE id = ? AND project_id = ?", [$cardTypeId, $projectId] ); if (!$cardType) { $this->error('套餐不存在或不属于该项目'); } // 项目管理员:只能在自己负责的项目中创建授权 if ($currentRole === 'project_admin') { $currentUser = $this->getCurrentUser(); $projectIds = $currentUser['project_ids'] ?? []; if (!in_array($projectId, $projectIds)) { $this->error('无权在此项目中创建授权', 403); } } // 代理扣费 $needDeductQuota = false; $deductAmount = 0; $quotaBefore = 0; if ($currentRole === 'agent') { if ((float) $cardType['agent_cost'] <= 0) { $this->error('该套餐未配置代理价格,无法创建授权'); } $agentAccount = $db->fetch("SELECT * FROM {$db->table('agents')} WHERE user_id = ?", [$userId]); if (!$agentAccount) { $this->error('代理额度账户不存在,请联系管理员充值'); } $remainQuota = (float) ($agentAccount['total_quota'] ?? 0) - (float) ($agentAccount['used_quota'] ?? 0) - (float) ($agentAccount['frozen_quota'] ?? 0); if ($remainQuota < (float) $cardType['agent_cost']) { $this->error('可用额度不足,需要 ¥' . $cardType['agent_cost'] . ',当前剩余 ¥' . $remainQuota); } $needDeductQuota = true; $deductAmount = (float) $cardType['agent_cost']; $quotaBefore = $remainQuota; } // 计算过期时间 $expireTime = null; if ($cardType['duration_days'] > 0) { $expireTime = date('Y-m-d H:i:s', strtotime("+{$cardType['duration_days']} days")); } // 确定 agent_id $agentId = 0; if ($currentRole === 'agent') { $agentId = $userId; } // 写入授权记录 $authId = $db->insert( "INSERT INTO {$db->table('authorizations')} (card_id, card_key, project_id, project_name, bot_qq, contact_qq, contact_name, duration_days, status, expire_time, operator_id, agent_id, authorized_at, created_at) VALUES (0, '', ?, ?, ?, ?, ?, ?, 'active', ?, ?, ?, NOW(), NOW())", [ $projectId, $project['name'] ?? '', $botQq, $contactQq, $contactName, (int) $cardType['duration_days'], $expireTime, $userId, $agentId, ] ); // 如果需要扣减代理额度 if ($needDeductQuota) { $db->execute( "UPDATE {$db->table('agents')} SET used_quota = used_quota + ? WHERE user_id = ?", [$deductAmount, $userId] ); $quotaAfter = $quotaBefore - $deductAmount; // 记录额度变动 $db->insert( "INSERT INTO {$db->table('agent_quota_logs')} (agent_id, change_type, amount, balance_before, balance_after, target_type, target_id, remark, operator_id, created_at) VALUES (?, 'consume', ?, ?, ?, 'authorization', ?, '授权扣费', ?, NOW())", [$userId, $deductAmount, $quotaBefore, $quotaAfter, $authId, $this->getUserId()] ); } // 记录操作日志 $this->logAction('authorization_create', 'authorization', $authId, [ 'project_id' => $projectId, 'card_type_id'=> $cardTypeId, 'bot_qq' => $botQq, 'contact_qq' => $contactQq, ]); $this->success([ 'id' => $authId, 'bot_qq' => $botQq, 'contact_qq' => $contactQq, 'expire_time' => $expireTime, ], '授权成功'); } /** * 公开在线授权(使用卡密激活机器人授权) * POST /api/public/authorizations/activate * Body: { card_key, bot_qq, contact_qq, contact_name? } */ public function publicActivate(): void { $input = $this->getJsonInput(); $cardKey = trim($input['card_key'] ?? ''); $botQq = trim($input['bot_qq'] ?? ''); $contactQq = trim($input['contact_qq'] ?? ''); $contactName = trim($input['contact_name'] ?? ''); if (empty($cardKey)) { $this->error('卡密不能为空'); } if (empty($botQq)) { $this->error('机器人QQ不能为空'); } if (empty($contactQq)) { $this->error('联系人QQ不能为空'); } if (!preg_match('/^\d{5,15}$/', $botQq)) { $this->error('机器人QQ格式不正确'); } if (!preg_match('/^\d{5,15}$/', $contactQq)) { $this->error('联系人QQ格式不正确'); } $db = Database::getInstance(); $card = $db->fetch( "SELECT c.*, p.name AS project_name FROM {$db->table('cards')} c LEFT JOIN {$db->table('projects')} p ON c.project_id = p.id WHERE c.card_key = ?", [$cardKey] ); if (!$card) { $this->error('卡密不存在'); } if ($card['status'] === 'disabled') { $this->error('该卡密已被禁用'); } if ($card['status'] === 'used') { $this->error('该卡密已被使用'); } // 卡密归属与使用权限验证 + 代理扣费 $currentUserId = $this->getUserId() ?? 0; $currentRole = $this->getUserRole(); $cardAgentId = (int) ($card['use_user_id'] ?? 0); $needDeductQuota = false; if ($cardAgentId > 0) { // 该卡密是某个代理生成的(生成时已扣额度) // 公开激活(终端客户):直接允许,无需登录 // 仅当已登录代理使用他人卡密时拒绝 if ($currentUserId > 0 && $currentRole === 'agent' && $cardAgentId !== $currentUserId) { $this->error('此卡密不属于您,无法使用'); } // 公开用户 或 归属代理本人 → 允许 } else { // 该卡密非代理生成(管理员生成或其他渠道) // 如果当前用户是代理,使用此类卡密需按代理价格扣费 if ($currentRole === 'agent' && $currentUserId > 0) { $cardTypeForCost = $db->fetch( "SELECT agent_cost FROM {$db->table('card_types')} WHERE id = ? AND project_id = ?", [$card['card_type_id'], $card['project_id']] ); if (!$cardTypeForCost || (float) $cardTypeForCost['agent_cost'] <= 0) { $this->error('该卡密套餐未配置代理价格,无法使用'); } $agentAccount = $db->fetch("SELECT * FROM {$db->table('agents')} WHERE user_id = ?", [$currentUserId]); if (!$agentAccount) { $this->error('代理额度账户不存在,请联系管理员充值'); } $remainQuota = (float) ($agentAccount['total_quota'] ?? 0) - (float) ($agentAccount['used_quota'] ?? 0) - (float) ($agentAccount['frozen_quota'] ?? 0); if ($remainQuota < (float) $cardTypeForCost['agent_cost']) { $this->error('可用额度不足,需要 ¥' . $cardTypeForCost['agent_cost'] . ',当前剩余 ¥' . $remainQuota); } $needDeductQuota = true; $deductAmount = (float) $cardTypeForCost['agent_cost']; $quotaBefore = $remainQuota; } } // 检查当前机器人QQ在同一项目下是否已有授权(未撤销),有则续费 $existingAuth = $db->fetch( "SELECT * FROM {$db->table('authorizations')} WHERE project_id = ? AND bot_qq = ? AND status != 'revoked' ORDER BY expire_time IS NULL DESC, expire_time DESC, id DESC LIMIT 1", [$card['project_id'], $botQq] ); $agentId = (int) ($card['use_user_id'] ?? 0); $cardDays = (int) $card['duration_days']; $db->beginTransaction(); try { $db->execute( "UPDATE {$db->table('cards')} SET status = 'used', bound_at = NOW() WHERE id = ?", [$card['id']] ); if ($existingAuth) { // 续费:在原有过期时间基础上累加 $baseTime = null; if ($existingAuth['expire_time']) { $baseTime = max(date('Y-m-d H:i:s'), $existingAuth['expire_time']); } // 原永久 + 新卡有时长 → 从当前/原到期起算;超大天数视为永久 $newExpireTime = calcExpireTime($cardDays, $baseTime); if ($baseTime === null && $cardDays > 0 && $cardDays <= 36500) { // 原授权为永久,新卡有时长:从当前时间开始加 $newExpireTime = calcExpireTime($cardDays, date('Y-m-d H:i:s')); } if ($existingAuth['expire_time'] === null && $cardDays > 0 && $cardDays <= 36500 && $baseTime === null) { // 保持原逻辑:原永久 + 有时长卡 → 从现在加时长 $newExpireTime = calcExpireTime($cardDays); } if ($cardDays <= 0) { // 新卡为永久卡 $newExpireTime = null; } $newDuration = (int) $existingAuth['duration_days'] + $cardDays; $db->execute( "UPDATE {$db->table('authorizations')} SET expire_time = ?, duration_days = ?, status = 'active', contact_qq = ?, contact_name = ?, updated_at = NOW() WHERE id = ?", [$newExpireTime, $newDuration, $contactQq, $contactName, $existingAuth['id']] ); // 记录使用记录(便于追踪每次续费卡密) $renewAuthId = $db->insert( "INSERT INTO {$db->table('authorizations')} (card_id, card_key, project_id, project_name, bot_qq, contact_qq, contact_name, duration_days, status, expire_time, operator_id, agent_id, authorized_at, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'active', ?, ?, ?, NOW(), NOW())", [ $card['id'], $card['card_key'], $card['project_id'], $card['project_name'] ?? '', $botQq, $contactQq, $contactName, $cardDays, $newExpireTime, 0, $agentId, ] ); // 如果需要扣减代理额度(续费场景) if ($needDeductQuota) { $db->execute( "UPDATE {$db->table('agents')} SET used_quota = used_quota + ? WHERE user_id = ?", [$deductAmount, $currentUserId] ); $quotaAfter = $quotaBefore - $deductAmount; $db->insert( "INSERT INTO {$db->table('agent_quota_logs')} (agent_id, change_type, amount, balance_before, balance_after, target_type, target_id, remark, operator_id, created_at) VALUES (?, 'consume', ?, ?, ?, 'authorization', ?, '续费扣费', ?, NOW())", [$currentUserId, $deductAmount, $quotaBefore, $quotaAfter, $renewAuthId, $this->getUserId()] ); } $db->commit(); $this->success([ 'id' => (int) $existingAuth['id'], 'card_key' => $cardKey, 'bot_qq' => $botQq, 'contact_qq' => $contactQq, 'contact_name'=> $contactName, 'project_id' => (int) $card['project_id'], 'project_name'=> $card['project_name'] ?? '', 'expire_time' => $newExpireTime, 'is_renew' => true, ], '续费成功'); return; } $expireTime = calcExpireTime($cardDays); $authId = $db->insert( "INSERT INTO {$db->table('authorizations')} (card_id, card_key, project_id, project_name, bot_qq, contact_qq, contact_name, duration_days, status, expire_time, operator_id, agent_id, authorized_at, created_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 'active', ?, ?, ?, NOW(), NOW())", [ $card['id'], $card['card_key'], $card['project_id'], $card['project_name'] ?? '', $botQq, $contactQq, $contactName, $cardDays, $expireTime, 0, $agentId, ] ); // 如果需要扣减代理额度(新授权场景) if ($needDeductQuota) { $db->execute( "UPDATE {$db->table('agents')} SET used_quota = used_quota + ? WHERE user_id = ?", [$deductAmount, $currentUserId] ); $quotaAfter = $quotaBefore - $deductAmount; $db->insert( "INSERT INTO {$db->table('agent_quota_logs')} (agent_id, change_type, amount, balance_before, balance_after, target_type, target_id, remark, operator_id, created_at) VALUES (?, 'consume', ?, ?, ?, 'authorization', ?, '授权扣费', ?, NOW())", [$currentUserId, $deductAmount, $quotaBefore, $quotaAfter, $authId, $this->getUserId()] ); } $db->commit(); } catch (\Throwable $e) { $db->rollback(); throw $e; } $this->success([ 'id' => $authId, 'card_key' => $cardKey, 'bot_qq' => $botQq, 'contact_qq' => $contactQq, 'contact_name'=> $contactName, 'project_id' => (int) $card['project_id'], 'project_name'=> $card['project_name'] ?? '', 'expire_time' => $expireTime, 'is_renew' => false, ], '授权成功'); } /** * 更新授权信息 * PUT /api/authorizations/{id} * Body: { bot_qq?, contact_qq?, contact_name?, duration_days?, expire_time?, remark? } */ public function update(int $id): void { $db = Database::getInstance(); $auth = $db->fetch("SELECT * FROM {$db->table('authorizations')} WHERE id = ?", [$id]); if (!$auth) { $this->error('授权记录不存在', 404); } $input = $this->getJsonInput(); $updates = []; $params = []; // 可编辑字段 if (isset($input['bot_qq']) && $input['bot_qq'] !== '') { $botQq = trim($input['bot_qq']); if (!preg_match('/^\d{5,15}$/', $botQq)) { $this->error('机器人QQ格式不正确'); } $updates[] = 'bot_qq = ?'; $params[] = $botQq; } if (isset($input['contact_qq']) && $input['contact_qq'] !== '') { $contactQq = trim($input['contact_qq']); if (!preg_match('/^\d{5,15}$/', $contactQq)) { $this->error('联系人QQ格式不正确'); } $updates[] = 'contact_qq = ?'; $params[] = $contactQq; } if (isset($input['contact_name'])) { $updates[] = 'contact_name = ?'; $params[] = trim($input['contact_name']); } if (isset($input['remark'])) { $updates[] = 'remark = ?'; $params[] = trim($input['remark']); } if (isset($input['duration_days'])) { $durationDays = (int) $input['duration_days']; $updates[] = 'duration_days = ?'; $params[] = $durationDays; // 更新过期时间 if ($durationDays === 0) { $updates[] = 'expire_time = NULL'; } else { // 从 authorized_at 重新计算过期时间 $baseTime = $auth['authorized_at'] ?? date('Y-m-d H:i:s'); $updates[] = 'expire_time = ?'; $params[] = date('Y-m-d H:i:s', strtotime("+{$durationDays} days", strtotime($auth['expire_time'] ?? $baseTime))); } } if (isset($input['expire_time']) && $input['expire_time'] !== '') { $updates[] = 'expire_time = ?'; $params[] = $input['expire_time']; } if (empty($updates)) { $this->error('没有需要更新的字段'); } $updates[] = 'updated_at = NOW()'; $params[] = $id; $db->execute( "UPDATE {$db->table('authorizations')} SET " . implode(', ', $updates) . " WHERE id = ?", $params ); $this->logAction('authorization_update', 'authorization', $id, [ 'fields' => array_keys($input), ]); $this->success(null, '授权更新成功'); } /** * 撤销授权 * PUT /api/authorizations/{id}/revoke * Body: { reason? } */ public function revoke(int $id): void { $db = Database::getInstance(); $auth = $db->fetch("SELECT * FROM {$db->table('authorizations')} WHERE id = ?", [$id]); if (!$auth) { $this->error('授权记录不存在', 404); } if ($auth['status'] === 'revoked') { $this->error('授权已被撤销'); } $input = $this->getJsonInput(); $reason = $input['reason'] ?? ''; $db->execute( "UPDATE {$db->table('authorizations')} SET status = 'revoked', revoked_at = NOW(), revoke_reason = ?, updated_at = NOW() WHERE id = ?", [$reason, $id] ); $this->logAction('authorization_revoke', 'authorization', $id, [ 'card_key' => $auth['card_key'], 'bot_qq' => $auth['bot_qq'], 'contact_qq' => $auth['contact_qq'], 'reason' => $reason, ]); $this->success(null, '授权已撤销'); } /** * 删除授权记录 * DELETE /api/authorizations/{id} */ public function delete(int $id): void { $db = Database::getInstance(); $auth = $db->fetch("SELECT * FROM {$db->table('authorizations')} WHERE id = ?", [$id]); if (!$auth) { $this->error('授权记录不存在', 404); } $db->execute("DELETE FROM {$db->table('authorizations')} WHERE id = ?", [$id]); $this->logAction('authorization_delete', 'authorization', $id, [ 'card_key' => $auth['card_key'], 'bot_qq' => $auth['bot_qq'], 'contact_qq' => $auth['contact_qq'], ]); $this->success(null, '授权记录已删除'); } /** * 批量删除授权记录 * POST /api/authorizations/batch-delete * Body: { ids: [int] } */ public function batchDelete(): void { $input = $this->getJsonInput(); $ids = $input['ids'] ?? []; if (!is_array($ids) || empty($ids)) { $this->error('请选择要删除的授权记录'); } $ids = array_filter(array_map('intval', $ids)); if (empty($ids)) { $this->error('无效的授权ID'); } $db = Database::getInstance(); $placeholders = implode(',', array_fill(0, count($ids), '?')); $this->logAction('authorization_batch_delete', 'authorization', 0, ['count' => count($ids)]); $db->execute( "DELETE FROM {$db->table('authorizations')} WHERE id IN ({$placeholders})", $ids ); $this->success(['count' => count($ids)], '批量删除成功'); } /** * 导出授权记录 * GET /api/authorizations/export * Query: ids, format(csv|excel) */ public function export(): void { $ids = $_GET['ids'] ?? []; $format = strtolower($_GET['format'] ?? 'csv'); if (!in_array($format, ['csv', 'excel'])) { $format = 'csv'; } $db = Database::getInstance(); $where = 'WHERE 1=1'; $params = []; if (!empty($ids)) { $ids = is_array($ids) ? $ids : explode(',', $ids); $ids = array_filter(array_map('intval', $ids)); if (!empty($ids)) { $where .= ' AND a.id IN (' . implode(',', array_fill(0, count($ids), '?')) . ')'; $params = array_merge($params, $ids); } } $rows = $db->fetchAll( "SELECT a.*, p.name AS project_name, ct.name AS card_type_name FROM {$db->table('authorizations')} a LEFT JOIN {$db->table('projects')} p ON a.project_id = p.id LEFT JOIN {$db->table('cards')} c ON a.card_id = c.id LEFT JOIN {$db->table('card_types')} ct ON c.card_type_id = ct.id {$where} ORDER BY a.id DESC LIMIT 50000", $params ); $statusMap = ['active' => '有效', 'revoked' => '已撤销']; if ($format === 'excel') { $filename = 'authorizations_export_' . date('YmdHis') . '.xls'; header('Content-Type: application/vnd.ms-excel; charset=utf-8'); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Cache-Control: no-cache'); $html = '
| ID | 机器人QQ | 联系人QQ | 项目 | 卡密 | 有效天数 | 状态 | 过期时间 | 授权时间 |
|---|---|---|---|---|---|---|---|---|
| ' . htmlspecialchars($row['id']) . ' | '; $html .= '' . htmlspecialchars($row['bot_qq']) . ' | '; $html .= '' . htmlspecialchars($row['contact_qq']) . ' | '; $html .= '' . htmlspecialchars($row['project_name'] ?? '') . ' | '; $html .= '' . htmlspecialchars($row['card_key']) . ' | '; $html .= '' . htmlspecialchars($row['duration_days'] == 0 ? '永久' : $row['duration_days'] . '天') . ' | '; $html .= '' . htmlspecialchars($statusMap[$row['status']] ?? $row['status']) . ' | '; $html .= '' . htmlspecialchars($row['expire_time'] ?? '') . ' | '; $html .= '' . htmlspecialchars($row['authorized_at']) . ' | '; $html .= '