第三段完善 代理商绑定 激活等 正式上线的版本

This commit is contained in:
huangzhenpc
2025-02-13 15:25:19 +08:00
parent abfa783907
commit d1b0a26dd2
31 changed files with 2729 additions and 641 deletions

View File

@@ -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();
// 缓存新账号信息

View File

@@ -45,6 +45,17 @@ class Mail extends Controller
'BROWSER_USER_AGENT' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36',
'MAIL_SERVER' => 'https://tempmail.plus'
]
] ,
[
'name' => '备用配置3',
'config' => [
'DOMAIN' => 'jxyweb.site',
'TEMP_MAIL' => 'exvet',
'TEMP_MAIL_EXT' => '@mailto.plus',
'BROWSER_USER_AGENT' => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.6723.92 Safari/537.36',
'MAIL_SERVER' => 'https://tempmail.plus',
'TEMP_MAIL_EPIN'=>'889944'
]
]
];

View File

@@ -205,16 +205,31 @@ class Member extends Controller
return json(['code' => 400, 'msg' => '设备ID不能为空']);
}
try {
// 获取Redis实例
$redis = cache('redis');
$cacheKey = "device_status:{$machineId}";
// 尝试获取缓存
if ($redis && $statusInfo = $redis->get($cacheKey)) {
return json(json_decode($statusInfo, true));
}
} catch (\Exception $e) {
// Redis异常时记录日志但继续执行
trace("Redis异常: " . $e->getMessage(), 'error');
}
// 获取该设备所有的激活记录
$activations = Db::name('cursor_activation_codes')
->where('used_by', '=', $machineId)
->where('is_used', '=', 1)
->field(['days', 'used_at', 'code', 'device_info'])
->order('used_at desc')
->select()
->toArray();
if (empty($activations)) {
return json([
$result = [
'code' => 401,
'msg' => '未激活',
'data' => [
@@ -224,7 +239,16 @@ class Member extends Controller
'days_left' => 0,
'activation_records' => []
]
]);
];
// 未激活状态缓存1分钟
try {
if ($redis) {
$redis->set($cacheKey, json_encode($result), 60);
}
} catch (\Exception $e) {
trace("Redis缓存写入异常: " . $e->getMessage(), 'error');
}
return json($result);
}
// 计算总天数
@@ -232,13 +256,13 @@ class Member extends Controller
// 计算最终到期时间
$now = time();
$firstActivationTime = strtotime($activations[count($activations)-1]['used_at']); // 第一次激活时间
$expireTime = $firstActivationTime + ($totalDays * 24 * 3600); // 总天数转换为秒数
$firstActivationTime = strtotime($activations[count($activations)-1]['used_at']);
$expireTime = $firstActivationTime + ($totalDays * 24 * 3600);
$daysLeft = ceil(($expireTime - $now) / 86400);
// 判断是否过期
if ($daysLeft <= 0) {
return json([
$result = [
'code' => 401,
'msg' => '已过期',
'data' => [
@@ -256,11 +280,20 @@ class Member extends Controller
];
}, $activations)
]
]);
];
// 过期状态缓存1分钟
try {
if ($redis) {
$redis->set($cacheKey, json_encode($result), 60);
}
} catch (\Exception $e) {
trace("Redis缓存写入异常: " . $e->getMessage(), 'error');
}
return json($result);
}
// 返回正常状态
return json([
// 正常状态返回
$result = [
'code' => 200,
'msg' => '正常',
'data' => [
@@ -278,12 +311,21 @@ class Member extends Controller
];
}, $activations)
]
]);
];
// 正常状态缓存5分钟
try {
if ($redis) {
$redis->set($cacheKey, json_encode($result), 300);
}
} catch (\Exception $e) {
trace("Redis缓存写入异常: " . $e->getMessage(), 'error');
}
return json($result);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => $e->getMessage()
'msg' => '系统异常,请稍后重试'
]);
}
}

View File

@@ -0,0 +1,137 @@
<?php
declare (strict_types = 1);
namespace app\admin\controller\api;
use app\admin\model\Version as VersionModel;
use think\admin\Controller;
use think\facade\Request;
/**
* 版本管理接口
* @auth true
*
* 接口说明:
* ====================================================
* 接口域名:{:sysconf('site_domain')}
*
* 公共返回参数:
* - code: 错误码0表示成功非0表示失败
* - msg: 提示信息
* - data: 返回的数据,请求失败时可能为空
*
* 错误码说明:
* - 0: 成功
* - 1: 一般性错误(具体错误信息见msg)
* - 401: 未授权或授权失败
* - 404: 请求的资源不存在
* - 500: 服务器内部错误
*
* 版本号格式x.x.x (例如: 3.4.1)
* 平台类型:
* - all: 全平台
* - windows: Windows平台
* - mac: Mac平台
* - linux: Linux平台
* ====================================================
*
* 1. 获取最新版本 [GET] /admin/api.version/latest
* 请求参数:
* - platform: 平台类型(all|windows|mac|linux), 默认为all
* 返回数据:
* {
* "code": 0,
* "msg": "获取成功",
* "data": {
* "id": "1",
* "version_no": "3.4.1.4",
* "version_name": "听泉cursor助手",
* "download_url": "http://domain/upload/xxx.exe",
* "is_force": 1, // 是否强制更新(1是,0否)
* "min_version": "3.4.0.0", // 最低要求版本
* "platform": "all", // 平台类型
* "description": "版本描述", // 版本描述
* "status": 1, // 状态(1启用,0禁用)
* "create_time": "2024-03-20 10:00:00"
* }
* }
*
* 2. 检查版本更新 [GET] /admin/api.version/check
* 请求参数:
* - version: 当前版本号(必填)
* - platform: 平台类型(all|windows|mac|linux), 默认为all
* 返回数据:
* {
* "code": 0,
* "msg": "检查完成",
* "data": {
* "has_update": true, // 是否有更新
* "is_force": 1, // 是否强制更新
* "version_info": { // 新版本信息(has_update为true时返回)
* // 同上面的版本信息
* }
* }
* }
*
* 错误返回示例:
* {
* "code": 1,
* "msg": "请提供当前版本号",
* "data": null
* }
*/
class Version extends Controller
{
/**
* 获取最新版本
* @return void
*/
public function latest()
{
$platform = $this->request->param('platform', 'all');
$version = VersionModel::mk()->getLatestVersion($platform);
if (!$version) {
$this->error('暂无版本信息');
}
// 处理下载地址
$version['download_url'] = $this->getFullUrl($version['download_url']);
$this->success('获取成功', $version);
}
/**
* 检查更新
* @return void
*/
public function check()
{
$currentVersion = $this->request->param('version');
$platform = $this->request->param('platform', 'all');
if (empty($currentVersion)) {
$this->error('请提供当前版本号');
}
$result = VersionModel::mk()->checkUpdate($currentVersion, $platform);
if (isset($result['error'])) {
$this->error($result['error']);
}
// 如果有更新,处理下载地址
if ($result['has_update'] && isset($result['version_info'])) {
$result['version_info']['download_url'] = $this->getFullUrl($result['version_info']['download_url']);
}
$this->success('检查完成', $result);
}
/**
* 获取完整的下载地址
* @param string $path 相对路径
* @return string
*/
protected function getFullUrl($path)
{
return sysconf('site_domain') . $path;
}
}