fix: 超大 duration_days 导致卡密误标已使用

- 新增 calcExpireTime(): >36500天视为永久(null), 避免超出 MySQL DATETIME
- publicActivate 事务包裹, 失败回滚不残留 used 状态
- verify/订单发卡/自动发卡 统一使用安全过期时间
This commit is contained in:
root committed 2026-10-11 12:40:37 +08:00
1 parent f838f1a3d1
commit b317c1f1d8
4 files changed
+135 -106

No files matched your search

@@ -380,44 +380,103 @@ class AuthorizationController extends Controller
[$card['project_id'], $botQq]
);
$db->execute(
"UPDATE {$db->table('cards')} SET status = 'used', bound_at = NOW() WHERE id = ?",
[$card['id']]
);
$agentId = (int) ($card['use_user_id'] ?? 0);
$cardDays = (int) $card['duration_days'];
if ($existingAuth) {
// 续费:在原有过期时间基础上累加
$baseTime = null;
if ($existingAuth['expire_time']) {
$baseTime = max(date('Y-m-d H:i:s'), $existingAuth['expire_time']);
}
$newExpireTime = null;
if ($card['duration_days'] > 0) {
if ($baseTime) {
$newExpireTime = date('Y-m-d H:i:s', strtotime("+{$card['duration_days']} days", strtotime($baseTime)));
} else {
// 原授权为永久,新卡密有时长:从当前时间开始加
$newExpireTime = date('Y-m-d H:i:s', strtotime("+{$card['duration_days']} days"));
}
} else {
// 新卡密为永久卡,直接变为永久
$newExpireTime = null;
}
$newDuration = (int) $existingAuth['duration_days'] + (int) $card['duration_days'];
$db->beginTransaction();
try {
$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']]
"UPDATE {$db->table('cards')} SET status = 'used', bound_at = NOW() WHERE id = ?",
[$card['id']]
);
// 记录使用记录(便于追踪每次续费卡密)
$renewAuthId = $db->insert(
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)
@@ -430,14 +489,14 @@ class AuthorizationController extends Controller
$botQq,
$contactQq,
$contactName,
(int) $card['duration_days'],
$newExpireTime,
$cardDays,
$expireTime,
0,
$agentId,
]
);
// 如果需要扣减代理额度(续费场景)
// 如果需要扣减代理额度(新授权场景)
if ($needDeductQuota) {
$db->execute(
"UPDATE {$db->table('agents')} SET used_quota = used_quota + ? WHERE user_id = ?",
@@ -446,68 +505,17 @@ class AuthorizationController extends Controller
$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()]
VALUES (?, 'consume', ?, ?, ?, 'authorization', ?, '授权扣费', ?, NOW())",
[$currentUserId, $deductAmount, $quotaBefore, $quotaAfter, $authId, $this->getUserId()]
);
}
$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 = null;
if ($card['duration_days'] > 0) {
$expireTime = date('Y-m-d H:i:s', strtotime("+{$card['duration_days']} days"));
}
$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,
(int) $card['duration_days'],
$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([