第二阶段完善代理商激活码

This commit is contained in:
huangzhenpc
2025-02-12 10:45:49 +08:00
parent 46b14149c9
commit abfa783907
23 changed files with 2541 additions and 0 deletions

View File

@@ -0,0 +1,272 @@
<?php
declare (strict_types = 1);
namespace app\admin\controller\api;
use think\admin\Controller;
use think\facade\Db;
/**
* Cursor账号管理接口
*/
class Account extends Controller
{
/**
* 添加账号
*/
public function add()
{
try {
$data = $this->_vali([
'email.require' => '邮箱不能为空',
'password.require' => '密码不能为空',
'first_name.require' => '名字不能为空',
'last_name.require' => '姓氏不能为空',
'access_token.require' => 'access_token不能为空',
'refresh_token.require' => 'refresh_token不能为空',
'registration_time.require' => '注册时间不能为空'
]);
// 检查邮箱是否已存在
if (Db::name('cursor_accounts')->where('email', $data['email'])->find()) {
return json([
'code' => 400,
'msg' => '该邮箱已存在'
]);
}
// 准备插入数据
$insertData = [
'email' => $data['email'],
'password' => $data['password'],
'first_name' => $data['first_name'],
'last_name' => $data['last_name'],
'access_token' => $data['access_token'],
'refresh_token' => $data['refresh_token'],
'machine_id' => $data['machine_id'] ?? '',
'user_agent' => $data['user_agent'] ?? '',
'registration_time' => $data['registration_time'],
'created_at' => date('Y-m-d H:i:s'),
'is_used' => 0
];
// 插入数据
$id = Db::name('cursor_accounts')->insertGetId($insertData);
return json([
'code' => 200,
'msg' => '添加成功',
'data' => ['id' => $id]
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => $e->getMessage()
]);
}
}
/**
* 获取未使用的账号
*/
public function getUnused()
{
try {
// 获取machine_id
$machineId = input('machine_id', '');
if (empty($machineId)) {
return json([
'code' => 401,
'msg' => '设备ID不能为空'
]);
}
// 检查设备是否在冷却期
$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', $isInCooldown)
]
]);
}
// 查询设备激活状态
$activations = Db::name('cursor_activation_codes')
->where('used_by', '=', $machineId)
->where('is_used', '=', 1)
->order('used_at desc')
->select()
->toArray();
if (empty($activations)) {
return json([
'code' => 401,
'msg' => '设备未激活'
]);
}
// 计算总天数和到期时间
$totalDays = array_sum(array_column($activations, 'days'));
$firstActivationTime = strtotime($activations[count($activations)-1]['used_at']);
$expireTime = $firstActivationTime + ($totalDays * 24 * 3600);
// 检查是否过期
if (time() > $expireTime) {
return json([
'code' => 401,
'msg' => '设备授权已过期'
]);
}
// 检查缓存中是否有该设备最近使用的账号
$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)
]
]);
}
}
// 记录冷却期
$cooldownExpires = time() + 1800; // 30分钟冷却期
\think\facade\Cache::set($cooldownKey, $cooldownExpires, 1800);
// 开启事务
Db::startTrans();
try {
// 获取一个未使用的账号
$account = Db::name('cursor_accounts')
->where('is_used', 0)
->lock(true)
->find();
if (empty($account)) {
Db::rollback();
return json([
'code' => 404,
'msg' => '没有可用的账号'
]);
}
// 如果有旧账号,将其标记为未使用
if ($cachedAccount) {
Db::name('cursor_accounts')
->where('id', $cachedAccount['id'])
->update([
'is_used' => 0,
'used_at' => null,
'used_by' => ''
]);
}
// 更新新账号状态
Db::name('cursor_accounts')
->where('id', $account['id'])
->update([
'is_used' => 1,
'used_at' => date('Y-m-d H:i:s'),
'used_by' => $machineId
]);
Db::commit();
// 缓存新账号信息
\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)
]
]);
} catch (\Exception $e) {
Db::rollback();
throw $e;
}
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => $e->getMessage()
]);
}
}
/**
* 后台强制重置设备账号
* @auth true
*/
public function resetDeviceAccount()
{
try {
$data = $this->_vali([
'machine_id.require' => '设备ID不能为空'
]);
$machineId = $data['machine_id'];
// 清除设备的账号缓存
$cacheKey = "device_account_{$machineId}";
\think\facade\Cache::delete($cacheKey);
// 清除冷却期
$cooldownKey = "device_cooldown_{$machineId}";
\think\facade\Cache::delete($cooldownKey);
// 将该设备使用的账号标记为未使用
Db::name('cursor_accounts')
->where('used_by', $machineId)
->update([
'is_used' => 0,
'used_at' => null,
'used_by' => ''
]);
return json([
'code' => 200,
'msg' => '重置成功'
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => $e->getMessage()
]);
}
}
}

View File

@@ -0,0 +1,225 @@
<?php
declare (strict_types = 1);
namespace app\admin\controller\api;
use think\admin\Controller;
use think\facade\Db;
use think\facade\Cache;
/**
* 代理商接口
*/
class Agent extends Controller
{
/**
* 代理商登录
*/
public function login()
{
try {
$data = $this->_vali([
'username.require' => '用户名不能为空',
'password.require' => '密码不能为空'
]);
// 查询代理商
$agent = Db::name('cursor_agents')
->where('username', $data['username'])
->find();
if (empty($agent)) {
return json(['code' => 400, 'msg' => '用户名或密码错误']);
}
// 验证密码
if (!password_verify($data['password'], $agent['password'])) {
return json(['code' => 400, 'msg' => '用户名或密码错误']);
}
// 检查状态
if ($agent['status'] != 1) {
return json(['code' => 400, 'msg' => '账号已被禁用']);
}
// 生成token
$token = md5($agent['id'] . time() . rand(1000, 9999));
// 存储token有效期7天
Cache::set('agent_token_' . $token, $agent['id'], 7 * 24 * 3600);
// 更新登录信息
Db::name('cursor_agents')
->where('id', $agent['id'])
->update([
'last_login_time' => date('Y-m-d H:i:s'),
'last_login_ip' => $this->request->ip(),
'updated_at' => date('Y-m-d H:i:s')
]);
// 返回登录信息
return json([
'code' => 200,
'msg' => '登录成功',
'data' => [
'token' => $token,
'agent' => [
'id' => $agent['id'],
'username' => $agent['username'],
'nickname' => $agent['nickname'],
'level' => $agent['level'],
'balance' => $agent['balance'],
'commission_rate' => $agent['commission_rate']
]
]
]);
} catch (\Exception $e) {
return json(['code' => 500, 'msg' => $e->getMessage()]);
}
}
/**
* 验证token
*/
protected function checkToken()
{
$token = $this->request->header('token');
if (empty($token)) {
return json(['code' => 401, 'msg' => '请先登录']);
}
$agentId = Cache::get('agent_token_' . $token);
if (empty($agentId)) {
return json(['code' => 401, 'msg' => '登录已过期,请重新登录']);
}
return $agentId;
}
/**
* 获取代理商信息
*/
public function info()
{
try {
$agentId = $this->checkToken();
if (!is_numeric($agentId)) {
return $agentId; // 返回错误信息
}
$agent = Db::name('cursor_agents')
->where('id', $agentId)
->find();
if (empty($agent)) {
return json(['code' => 400, 'msg' => '代理商不存在']);
}
// 获取激活码统计
$codeStats = Db::name('cursor_agent_codes')
->where('agent_id', $agentId)
->field([
'COUNT(*) as total_codes',
'SUM(CASE WHEN status = 1 THEN 1 ELSE 0 END) as settled_codes',
'SUM(commission) as total_commission',
'SUM(CASE WHEN status = 1 THEN commission ELSE 0 END) as settled_commission'
])
->find();
// 获取未使用的激活码
$unusedCodes = Db::name('cursor_agent_codes')
->alias('ac')
->join('cursor_activation_codes c', 'ac.code_id = c.id')
->where('ac.agent_id', $agentId)
->where('c.is_used', 0)
->field('c.code, c.days, ac.price')
->select()
->toArray();
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'agent' => [
'id' => $agent['id'],
'username' => $agent['username'],
'nickname' => $agent['nickname'],
'level' => $agent['level'],
'balance' => $agent['balance'],
'total_income' => $agent['total_income'],
'commission_rate' => $agent['commission_rate'],
'last_login_time' => $agent['last_login_time']
],
'stats' => [
'total_codes' => $codeStats['total_codes'] ?? 0,
'settled_codes' => $codeStats['settled_codes'] ?? 0,
'total_commission' => $codeStats['total_commission'] ?? 0,
'settled_commission' => $codeStats['settled_commission'] ?? 0
],
'unused_codes' => $unusedCodes
]
]);
} catch (\Exception $e) {
return json(['code' => 500, 'msg' => $e->getMessage()]);
}
}
/**
* 获取代理商的激活码列表
*/
public function codes()
{
try {
$agentId = $this->checkToken();
if (!is_numeric($agentId)) {
return $agentId;
}
$page = input('page/d', 1);
$limit = input('limit/d', 20);
$status = input('status');
$query = Db::name('cursor_agent_codes')
->alias('ac')
->join('cursor_activation_codes c', 'ac.code_id = c.id')
->where('ac.agent_id', $agentId);
if (isset($status)) {
$query = $query->where('c.is_used', intval($status));
}
$total = $query->count();
$list = $query->field([
'c.code',
'c.days',
'c.is_used',
'c.used_at',
'c.used_by',
'ac.price',
'ac.commission',
'ac.status as settle_status',
'ac.created_at'
])
->page($page, $limit)
->order('ac.created_at desc')
->select()
->toArray();
return json([
'code' => 200,
'msg' => '获取成功',
'data' => [
'list' => $list,
'total' => $total,
'page' => $page,
'limit' => $limit
]
]);
} catch (\Exception $e) {
return json(['code' => 500, 'msg' => $e->getMessage()]);
}
}
}

View File

@@ -35,6 +35,16 @@ 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' => '备用配置2',
'config' => [
'DOMAIN' => 'wuen.site',
'TEMP_MAIL' => 'actoke',
'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'
]
]
];

View File

@@ -5,6 +5,7 @@ namespace app\admin\controller\api;
use app\manager\model\Member as MemberModel;
use think\admin\Controller;
use think\facade\Db;
/**
@@ -82,4 +83,208 @@ class Member extends Controller
]
]);
}
/**
* 激活码验证并激活
* @return void
*/
public function activate()
{
try {
// 验证激活码
$code = input('code');
if (empty($code)) {
return json(['code' => 400, 'msg' => '激活码不能为空']);
}
// 获取设备信息
$machineId = input('machine_id', '');
if (empty($machineId)) {
return json(['code' => 400, 'msg' => '设备ID不能为空']);
}
// 收集设备详细信息
$deviceInfo = [
'machine_id' => $machineId,
'os' => input('os', ''), // 操作系统信息
'device_name' => input('device_name', ''), // 设备名称
'ip' => $this->request->ip(), // IP地址
'user_agent' => $this->request->header('user-agent', ''), // UA信息
'location' => input('location', '') // 地理位置(如果有)
];
// 获取激活码信息
$activation = Db::name('cursor_activation_codes')
->where('code', $code)
->where('is_used', 0)
->find();
if (empty($activation)) {
return json(['code' => 400, 'msg' => '激活码无效或已被使用']);
}
// 开启事务
Db::startTrans();
try {
// 标记新激活码为已使用
Db::name('cursor_activation_codes')
->where('id', $activation['id'])
->update([
'is_used' => 1,
'used_at' => date('Y-m-d H:i:s'),
'used_by' => $machineId,
'device_info' => json_encode($deviceInfo)
]);
Db::commit();
// 获取该设备所有的激活记录
$activations = Db::name('cursor_activation_codes')
->where('used_by', '=', $machineId)
->where('is_used', '=', 1)
->order('used_at desc')
->select()
->toArray();
// 计算总天数
$totalDays = array_sum(array_column($activations, 'days'));
// 计算最终到期时间
$now = time();
$firstActivationTime = strtotime($activations[count($activations)-1]['used_at']); // 第一次激活时间
$expireTime = $firstActivationTime + ($totalDays * 24 * 3600); // 总天数转换为秒数
$daysLeft = ceil(($expireTime - $now) / 86400);
// 返回成功信息
$result = [
'code' => 200,
'msg' => '激活成功',
'data' => [
'expire_time' => date('Y-m-d H:i:s', $expireTime),
'total_days' => $totalDays,
'added_days' => $activation['days'],
'days_left' => $daysLeft,
'machine_id' => $machineId,
'device_info' => $deviceInfo,
'activation_time' => date('Y-m-d H:i:s'),
'activation_records' => array_map(function($record) {
return [
'code' => $record['code'],
'days' => $record['days'],
'activation_time' => $record['used_at'],
'device_info' => json_decode($record['device_info'], true)
];
}, $activations)
]
];
return json($result);
} catch (\Exception $e) {
Db::rollback();
throw $e;
}
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => $e->getMessage()
]);
}
}
/**
* 获取会员状态
*/
public function status()
{
try {
// 获取设备ID
$machineId = input('machine_id', '');
if (empty($machineId)) {
return json(['code' => 400, 'msg' => '设备ID不能为空']);
}
// 获取该设备所有的激活记录
$activations = Db::name('cursor_activation_codes')
->where('used_by', '=', $machineId)
->where('is_used', '=', 1)
->order('used_at desc')
->select()
->toArray();
if (empty($activations)) {
return json([
'code' => 401,
'msg' => '未激活',
'data' => [
'status' => 'inactive',
'expire_time' => '',
'total_days' => 0,
'days_left' => 0,
'activation_records' => []
]
]);
}
// 计算总天数
$totalDays = array_sum(array_column($activations, 'days'));
// 计算最终到期时间
$now = time();
$firstActivationTime = strtotime($activations[count($activations)-1]['used_at']); // 第一次激活时间
$expireTime = $firstActivationTime + ($totalDays * 24 * 3600); // 总天数转换为秒数
$daysLeft = ceil(($expireTime - $now) / 86400);
// 判断是否过期
if ($daysLeft <= 0) {
return json([
'code' => 401,
'msg' => '已过期',
'data' => [
'status' => 'expired',
'expire_time' => date('Y-m-d H:i:s', $expireTime),
'total_days' => $totalDays,
'days_left' => 0,
'machine_id' => $machineId,
'activation_records' => array_map(function($record) {
return [
'code' => $record['code'],
'days' => $record['days'],
'activation_time' => $record['used_at'],
'device_info' => json_decode($record['device_info'], true)
];
}, $activations)
]
]);
}
// 返回正常状态
return json([
'code' => 200,
'msg' => '正常',
'data' => [
'status' => 'active',
'expire_time' => date('Y-m-d H:i:s', $expireTime),
'total_days' => $totalDays,
'days_left' => $daysLeft,
'machine_id' => $machineId,
'activation_records' => array_map(function($record) {
return [
'code' => $record['code'],
'days' => $record['days'],
'activation_time' => $record['used_at'],
'device_info' => json_decode($record['device_info'], true)
];
}, $activations)
]
]);
} catch (\Exception $e) {
return json([
'code' => 500,
'msg' => $e->getMessage()
]);
}
}
}