第三段完善 代理商绑定 激活等 正式上线的版本
This commit is contained in:
@@ -82,18 +82,48 @@ class Account extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
// 检查设备是否在冷却期
|
||||
$cooldownKey = "device_cooldown_{$machineId}";
|
||||
$isInCooldown = \think\facade\Cache::get($cooldownKey);
|
||||
// 优先检查缓存中是否有该设备最近使用的账号
|
||||
$cacheKey = "device_account_{$machineId}";
|
||||
$cachedAccount = \think\facade\Cache::get($cacheKey);
|
||||
|
||||
if ($cachedAccount) {
|
||||
// 检查账号是否仍然可用
|
||||
$account = Db::name('cursor_accounts')
|
||||
->where('id', $cachedAccount['id'])
|
||||
->find();
|
||||
|
||||
if ($account) {
|
||||
// 更新缓存时间
|
||||
\think\facade\Cache::set($cacheKey, $account, 600); // 10分钟缓存
|
||||
|
||||
// 查询设备激活状态用于计算过期时间
|
||||
$activations = Db::name('cursor_activation_codes')
|
||||
->where('used_by', '=', $machineId)
|
||||
->where('is_used', '=', 1)
|
||||
->order('used_at desc')
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
if ($isInCooldown) {
|
||||
return json([
|
||||
'code' => 429,
|
||||
'msg' => '请求过于频繁,请稍后再试',
|
||||
'data' => [
|
||||
'cooldown_expires' => date('Y-m-d H:i:s', $isInCooldown)
|
||||
]
|
||||
]);
|
||||
if (!empty($activations)) {
|
||||
$totalDays = array_sum(array_column($activations, 'days'));
|
||||
$firstActivationTime = strtotime($activations[count($activations)-1]['used_at']);
|
||||
$expireTime = $firstActivationTime + ($totalDays * 24 * 3600);
|
||||
|
||||
// 返回账号信息
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'email' => $account['email'],
|
||||
'password' => $account['password'],
|
||||
'access_token' => $account['access_token'],
|
||||
'refresh_token' => $account['refresh_token'],
|
||||
'expire_time' => date('Y-m-d H:i:s', $expireTime),
|
||||
'days_left' => ceil(($expireTime - time()) / 86400)
|
||||
]
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 查询设备激活状态
|
||||
@@ -124,39 +154,18 @@ class Account extends Controller
|
||||
]);
|
||||
}
|
||||
|
||||
// 检查缓存中是否有该设备最近使用的账号
|
||||
$cacheKey = "device_account_{$machineId}";
|
||||
$cachedAccount = \think\facade\Cache::get($cacheKey);
|
||||
|
||||
if ($cachedAccount) {
|
||||
// 检查账号是否仍然可用
|
||||
$account = Db::name('cursor_accounts')
|
||||
->where('id', $cachedAccount['id'])
|
||||
->find();
|
||||
|
||||
if ($account) {
|
||||
// 更新缓存时间
|
||||
\think\facade\Cache::set($cacheKey, $account, 600); // 10分钟缓存
|
||||
|
||||
// 返回账号信息
|
||||
return json([
|
||||
'code' => 200,
|
||||
'msg' => '获取成功',
|
||||
'data' => [
|
||||
'email' => $account['email'],
|
||||
'password' => $account['password'],
|
||||
'access_token' => $account['access_token'],
|
||||
'refresh_token' => $account['refresh_token'],
|
||||
'expire_time' => date('Y-m-d H:i:s', $expireTime),
|
||||
'days_left' => ceil(($expireTime - time()) / 86400)
|
||||
]
|
||||
]);
|
||||
}
|
||||
// 在分配新账号前检查冷却期
|
||||
$cooldownKey = "device_cooldown_{$machineId}";
|
||||
$isInCooldown = \think\facade\Cache::get($cooldownKey);
|
||||
if ($isInCooldown) {
|
||||
return json([
|
||||
'code' => 429,
|
||||
'msg' => '请求过于频繁,请稍后再试',
|
||||
'data' => [
|
||||
'cooldown_expires' => date('Y-m-d H:i:s', intval($isInCooldown))
|
||||
]
|
||||
]);
|
||||
}
|
||||
|
||||
// 记录冷却期
|
||||
$cooldownExpires = time() + 1800; // 30分钟冷却期
|
||||
\think\facade\Cache::set($cooldownKey, $cooldownExpires, 1800);
|
||||
|
||||
// 开启事务
|
||||
Db::startTrans();
|
||||
@@ -195,6 +204,10 @@ class Account extends Controller
|
||||
'used_by' => $machineId
|
||||
]);
|
||||
|
||||
// 设置冷却期(仅在成功分配新账号时)
|
||||
$cooldownExpires = time() + 1800; // 30分钟冷却期
|
||||
\think\facade\Cache::set($cooldownKey, $cooldownExpires, 1800);
|
||||
|
||||
Db::commit();
|
||||
|
||||
// 缓存新账号信息
|
||||
|
||||
Reference in New Issue
Block a user