304 lines
9.3 KiB
PHP
304 lines
9.3 KiB
PHP
<?php
|
|
|
|
namespace app\manager\controller;
|
|
|
|
use think\admin\Controller;
|
|
|
|
/**
|
|
* Cursor账号管理
|
|
* Class Account
|
|
* @package app\manager\controller
|
|
*/
|
|
class Account extends Controller
|
|
{
|
|
/**
|
|
* Cursor账号管理
|
|
* @auth true
|
|
* @menu true
|
|
*/
|
|
public function index()
|
|
{
|
|
$this->title = 'Cursor账号管理';
|
|
// 创建查询对象
|
|
$query = $this->_query('cursor_accounts');
|
|
// 数据列表处理
|
|
$query->equal('is_used')->like('email');
|
|
// 列表排序并显示
|
|
$query->order('id desc')->page();
|
|
}
|
|
|
|
/**
|
|
* 生成激活码
|
|
* @auth true
|
|
*/
|
|
public function generate()
|
|
{
|
|
if ($this->request->isPost()) {
|
|
// 获取参数
|
|
$count = intval(input('count', 1));
|
|
$days = intval(input('days', 30));
|
|
$agentId = input('agent_id', '');
|
|
$price = floatval(input('price', 0));
|
|
|
|
// 参数验证
|
|
if ($count < 1 || $count > 100) {
|
|
$this->error('生成数量必须在1-100之间');
|
|
}
|
|
if ($days < 1) {
|
|
$this->error('有效天数必须大于0');
|
|
}
|
|
|
|
// 开启事务
|
|
\think\facade\Db::startTrans();
|
|
|
|
$codes = [];
|
|
$now = date('Y-m-d H:i:s');
|
|
|
|
// 生成激活码
|
|
for ($i = 0; $i < $count; $i++) {
|
|
$codes[] = [
|
|
'code' => strtoupper(substr(md5(uniqid() . mt_rand()), 0, 16)),
|
|
'days' => $days,
|
|
'created_at' => $now,
|
|
'is_used' => 0
|
|
];
|
|
}
|
|
|
|
// 批量插入激活码
|
|
$result = \think\facade\Db::name('cursor_activation_codes')->insertAll($codes);
|
|
if (!$result) {
|
|
\think\facade\Db::rollback();
|
|
$this->error('激活码生成失败');
|
|
}
|
|
|
|
// 获取插入的ID范围
|
|
$firstId = \think\facade\Db::name('cursor_activation_codes')->getLastInsID();
|
|
$lastId = $firstId + count($codes) - 1;
|
|
|
|
// 如果指定了代理商,则自动分配
|
|
if (!empty($agentId) && $price > 0) {
|
|
$agent = \think\facade\Db::name('cursor_agents')
|
|
->where('id', $agentId)
|
|
->find();
|
|
|
|
if ($agent) {
|
|
$agentCodes = [];
|
|
$commission = $price * ($agent['commission_rate'] / 100);
|
|
|
|
// 如果是二级代理,计算上级佣金
|
|
$parentCommission = 0;
|
|
if ($agent['level'] == 2 && $agent['parent_id'] > 0) {
|
|
$parent = \think\facade\Db::name('cursor_agents')
|
|
->where('id', $agent['parent_id'])
|
|
->find();
|
|
if ($parent) {
|
|
$parentCommission = $price * ($parent['commission_rate'] / 100);
|
|
}
|
|
}
|
|
|
|
// 准备代理商激活码数据
|
|
for ($id = $firstId; $id <= $lastId; $id++) {
|
|
$agentCodes[] = [
|
|
'agent_id' => $agent['id'],
|
|
'code_id' => $id,
|
|
'price' => $price,
|
|
'commission' => $commission,
|
|
'parent_commission' => $parentCommission,
|
|
'status' => 0,
|
|
'created_at' => $now,
|
|
'updated_at' => $now
|
|
];
|
|
}
|
|
|
|
// 批量插入代理商激活码关联
|
|
if (!empty($agentCodes)) {
|
|
$result = \think\facade\Db::name('cursor_agent_codes')->insertAll($agentCodes);
|
|
if (!$result) {
|
|
\think\facade\Db::rollback();
|
|
$this->error('代理商激活码分配失败');
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
\think\facade\Db::commit();
|
|
$this->success('生成成功!');
|
|
} else {
|
|
// 获取代理商列表
|
|
$this->agents = \think\facade\Db::name('cursor_agents')
|
|
->where('status', 1)
|
|
->select()
|
|
->toArray();
|
|
$this->fetch();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 激活码管理
|
|
* @auth true
|
|
* @menu true
|
|
*/
|
|
public function codes()
|
|
{
|
|
$this->title = '激活码管理';
|
|
|
|
// 获取代理商列表用于筛选
|
|
$this->agents = \think\facade\Db::name('cursor_agents')
|
|
->where('status', 1)
|
|
->select()
|
|
->toArray();
|
|
|
|
// 创建查询对象
|
|
$query = $this->_query('cursor_activation_codes')->alias('c')
|
|
->leftJoin('cursor_agent_codes ac', 'c.id = ac.code_id')
|
|
->leftJoin('cursor_agents a', 'ac.agent_id = a.id')
|
|
->field([
|
|
'c.*',
|
|
'a.id as agent_id',
|
|
'a.username as agent_name',
|
|
'a.level as agent_level',
|
|
'ac.price',
|
|
'ac.commission',
|
|
'ac.parent_commission',
|
|
'ac.status as settle_status'
|
|
]);
|
|
|
|
// 数据列表处理
|
|
$query->where(function($query) {
|
|
$agentId = input('agent_id', '');
|
|
if ($agentId !== '') {
|
|
$query->where('a.id', '=', $agentId);
|
|
}
|
|
|
|
$isUsed = input('is_used', '');
|
|
if ($isUsed !== '') {
|
|
$query->where('c.is_used', '=', $isUsed);
|
|
}
|
|
|
|
$code = input('code', '');
|
|
if ($code) {
|
|
$query->whereLike('c.code', "%{$code}%");
|
|
}
|
|
|
|
$usedBy = input('used_by', '');
|
|
if ($usedBy) {
|
|
$query->whereLike('c.used_by', "%{$usedBy}%");
|
|
}
|
|
});
|
|
|
|
// 列表排序并显示
|
|
$query->order('c.id desc')->page();
|
|
}
|
|
|
|
/**
|
|
* 修改账号状态
|
|
* @auth true
|
|
*/
|
|
public function state()
|
|
{
|
|
$this->_applyFormToken();
|
|
$this->_save('cursor_accounts', ['is_used' => input('state')]);
|
|
}
|
|
|
|
/**
|
|
* 删除账号
|
|
* @auth true
|
|
*/
|
|
public function remove()
|
|
{
|
|
$this->_applyFormToken();
|
|
$this->_delete('cursor_accounts');
|
|
}
|
|
|
|
/**
|
|
* 删除激活码
|
|
* @auth true
|
|
*/
|
|
public function removeCode()
|
|
{
|
|
$this->_applyFormToken();
|
|
$this->_delete('cursor_activation_codes');
|
|
}
|
|
|
|
/**
|
|
* 设备账号管理
|
|
* @auth true
|
|
* @menu true
|
|
*/
|
|
public function devices()
|
|
{
|
|
$this->title = '设备账号管理';
|
|
// 创建查询对象
|
|
$query = $this->_query('cursor_accounts')
|
|
->where('used_by', '<>', '')
|
|
->where('is_used', 1);
|
|
|
|
// 数据列表处理
|
|
$query->like('email,used_by');
|
|
// 列表排序并显示
|
|
$query->order('used_at desc')->page();
|
|
|
|
// 获取列表数据
|
|
$list = $this->app->db->getList();
|
|
|
|
// 检查每个设备的缓存状态
|
|
foreach ($list as &$item) {
|
|
if (!empty($item['used_by'])) {
|
|
// 检查账号缓存
|
|
$cacheKey = "device_account_{$item['used_by']}";
|
|
$cachedAccount = \think\facade\Cache::get($cacheKey);
|
|
$item['has_cache'] = !empty($cachedAccount);
|
|
|
|
// 检查冷却期
|
|
$cooldownKey = "device_cooldown_{$item['used_by']}";
|
|
$cooldownTime = \think\facade\Cache::get($cooldownKey);
|
|
$item['in_cooldown'] = $cooldownTime > time();
|
|
$item['cooldown_time'] = $cooldownTime ? date('Y-m-d H:i:s', $cooldownTime) : '';
|
|
|
|
// 检查缓存账号是否与数据库一致
|
|
$item['cache_match'] = false;
|
|
if ($cachedAccount) {
|
|
$item['cache_match'] = ($cachedAccount['id'] == $item['id']);
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->list = $list;
|
|
}
|
|
|
|
/**
|
|
* 重置设备账号
|
|
* @auth true
|
|
*/
|
|
public function resetDevice()
|
|
{
|
|
$this->_applyFormToken();
|
|
try {
|
|
$machineId = input('machine_id', '');
|
|
if (empty($machineId)) {
|
|
$this->error('设备ID不能为空');
|
|
}
|
|
|
|
// 清除设备的账号缓存
|
|
$cacheKey = "device_account_{$machineId}";
|
|
\think\facade\Cache::delete($cacheKey);
|
|
|
|
// 清除冷却期
|
|
$cooldownKey = "device_cooldown_{$machineId}";
|
|
\think\facade\Cache::delete($cooldownKey);
|
|
|
|
// 将该设备使用的账号标记为未使用
|
|
\think\facade\Db::name('cursor_accounts')
|
|
->where('used_by', $machineId)
|
|
->update([
|
|
'is_used' => 0,
|
|
'used_at' => null,
|
|
'used_by' => ''
|
|
]);
|
|
|
|
$this->success('重置成功!');
|
|
} catch (\Exception $e) {
|
|
$this->error("重置失败:{$e->getMessage()}");
|
|
}
|
|
}
|
|
}
|