fix: 超大 duration_days 导致卡密误标已使用
- 新增 calcExpireTime(): >36500天视为永久(null), 避免超出 MySQL DATETIME - publicActivate 事务包裹, 失败回滚不残留 used 状态 - verify/订单发卡/自动发卡 统一使用安全过期时间
This commit is contained in:
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([
|
||||
|
||||
@@ -263,7 +263,7 @@ class OrderController extends Controller
|
||||
$base = $existing['expire_time'] && $existing['expire_time'] > date('Y-m-d H:i:s')
|
||||
? $existing['expire_time']
|
||||
: date('Y-m-d H:i:s');
|
||||
$expireTime = date('Y-m-d H:i:s', strtotime($base . " +{$durationDays} days"));
|
||||
$expireTime = calcExpireTime($durationDays, $base);
|
||||
} else {
|
||||
// 永久卡:到期时间置为null
|
||||
$expireTime = null;
|
||||
@@ -275,10 +275,7 @@ class OrderController extends Controller
|
||||
);
|
||||
} else {
|
||||
// 新建授权
|
||||
$expireTime = null;
|
||||
if ($durationDays > 0) {
|
||||
$expireTime = date('Y-m-d H:i:s', strtotime("+{$durationDays} days"));
|
||||
}
|
||||
$expireTime = calcExpireTime($durationDays);
|
||||
|
||||
$db->execute(
|
||||
"INSERT INTO {$db->table('authorizations')}
|
||||
@@ -307,10 +304,7 @@ class OrderController extends Controller
|
||||
[$order['project_id'], $order['card_type_id']]
|
||||
);
|
||||
if ($card) {
|
||||
$expireTime = null;
|
||||
if ($durationDays > 0) {
|
||||
$expireTime = date('Y-m-d H:i:s', strtotime("+{$durationDays} days"));
|
||||
}
|
||||
$expireTime = calcExpireTime($durationDays);
|
||||
$bindInfo = json_encode([
|
||||
'machine_id' => $machineId ?: 'AUTO-' . $order['order_no'],
|
||||
'ip' => '',
|
||||
|
||||
@@ -669,10 +669,7 @@ class PublicController extends Controller
|
||||
'first_bind_at' => date('Y-m-d H:i:s'),
|
||||
];
|
||||
|
||||
$expireTime = null;
|
||||
if ($card['duration_days'] > 0) {
|
||||
$expireTime = date('Y-m-d H:i:s', strtotime("+{$card['duration_days']} days"));
|
||||
}
|
||||
$expireTime = calcExpireTime((int) $card['duration_days']);
|
||||
|
||||
$db->execute(
|
||||
"UPDATE {$db->table('cards')} SET status = 'used', bind_info = ?, bound_at = NOW(), expire_time = ? WHERE id = ?",
|
||||
|
||||
@@ -197,4 +197,34 @@ function beijingTimeFields(array $row, array $fields): array
|
||||
}
|
||||
}
|
||||
return $row;
|
||||
}
|
||||
|
||||
/**
|
||||
* 根据有效天数计算过期时间
|
||||
* - days <= 0:永久,返回 null
|
||||
* - 超大天数(超出 MySQL DATETIME 或 100 年):视为永久 null,避免插入失败导致卡被标已用但无授权
|
||||
* @return string|null
|
||||
*/
|
||||
function calcExpireTime(int $days, ?string $baseTime = null): ?string
|
||||
{
|
||||
if ($days <= 0) {
|
||||
return null;
|
||||
}
|
||||
$base = $baseTime ? strtotime($baseTime) : time();
|
||||
if ($base === false) {
|
||||
$base = time();
|
||||
}
|
||||
// 超过约 100 年视为永久(避免 strtotime 溢出 / 超出 DATETIME 9999-12-31)
|
||||
if ($days > 36500) {
|
||||
return null;
|
||||
}
|
||||
$ts = strtotime("+{$days} days", $base);
|
||||
if ($ts === false) {
|
||||
return null;
|
||||
}
|
||||
$max = strtotime('9999-12-31 23:59:59');
|
||||
if ($ts === false || $ts > $max) {
|
||||
return '9999-12-31 23:59:59';
|
||||
}
|
||||
return date('Y-m-d H:i:s', $ts);
|
||||
}
|
||||
Reference in new issue
Block a user