第二阶段完善代理商激活码
This commit is contained in:
304
app/manager/controller/Account.php
Normal file
304
app/manager/controller/Account.php
Normal file
@@ -0,0 +1,304 @@
|
||||
<?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()}");
|
||||
}
|
||||
}
|
||||
}
|
||||
275
app/manager/controller/Agent.php
Normal file
275
app/manager/controller/Agent.php
Normal file
@@ -0,0 +1,275 @@
|
||||
<?php
|
||||
|
||||
namespace app\manager\controller;
|
||||
|
||||
use think\admin\Controller;
|
||||
use think\facade\Db;
|
||||
|
||||
/**
|
||||
* 代理商管理
|
||||
* Class Agent
|
||||
* @package app\manager\controller
|
||||
*/
|
||||
class Agent extends Controller
|
||||
{
|
||||
/**
|
||||
* 代理商列表
|
||||
* @auth true
|
||||
* @menu true
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->title = '代理商管理';
|
||||
// 创建查询对象
|
||||
$query = $this->_query('cursor_agents');
|
||||
// 数据列表处理
|
||||
$query->equal('level,status')->like('username,nickname');
|
||||
// 列表排序并显示
|
||||
$query->order('id desc')->page();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加代理商
|
||||
* @auth true
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->_vali([
|
||||
'username.require' => '用户名不能为空',
|
||||
'password.require' => '密码不能为空',
|
||||
'nickname.default' => '',
|
||||
'level.require' => '代理级别不能为空',
|
||||
'parent_id.default' => 0,
|
||||
'commission_rate.require' => '佣金比例不能为空',
|
||||
'status.default' => 1,
|
||||
'remark.default' => ''
|
||||
]);
|
||||
|
||||
// 检查用户名是否存在
|
||||
if (Db::name('cursor_agents')->where('username', $data['username'])->find()) {
|
||||
$this->error('用户名已存在!');
|
||||
}
|
||||
|
||||
// 如果是二级代理,检查上级代理
|
||||
if ($data['level'] == 2) {
|
||||
if (empty($data['parent_id'])) {
|
||||
$this->error('请选择上级代理!');
|
||||
}
|
||||
$parent = Db::name('cursor_agents')->where('id', $data['parent_id'])->find();
|
||||
if (empty($parent) || $parent['level'] != 1) {
|
||||
$this->error('上级代理不存在或不是一级代理!');
|
||||
}
|
||||
}
|
||||
|
||||
// 处理密码
|
||||
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
|
||||
$data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s');
|
||||
|
||||
if (Db::name('cursor_agents')->insert($data) !== false) {
|
||||
$this->success('添加成功!');
|
||||
} else {
|
||||
$this->error('添加失败!');
|
||||
}
|
||||
} else {
|
||||
// 获取一级代理列表(用于选择上级)
|
||||
$this->agents = Db::name('cursor_agents')
|
||||
->where('level', 1)
|
||||
->where('status', 1)
|
||||
->select();
|
||||
$this->fetch();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑代理商
|
||||
* @auth true
|
||||
*/
|
||||
public function edit()
|
||||
{
|
||||
$this->_applyFormToken();
|
||||
$this->_form('cursor_agents', 'form');
|
||||
}
|
||||
|
||||
/**
|
||||
* 修改代理商状态
|
||||
* @auth true
|
||||
*/
|
||||
public function state()
|
||||
{
|
||||
$this->_applyFormToken();
|
||||
$this->_save('cursor_agents', ['status' => input('status')]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除代理商
|
||||
* @auth true
|
||||
*/
|
||||
public function remove()
|
||||
{
|
||||
$this->_applyFormToken();
|
||||
$this->_delete('cursor_agents');
|
||||
}
|
||||
|
||||
/**
|
||||
* 分配激活码
|
||||
* @auth true
|
||||
*/
|
||||
public function assignCodes()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->_vali([
|
||||
'agent_id.require' => '代理商不能为空',
|
||||
'code_ids.require' => '请选择要分配的激活码',
|
||||
'price.require' => '请设置销售价格'
|
||||
]);
|
||||
|
||||
// 开启事务
|
||||
Db::startTrans();
|
||||
try {
|
||||
$agent = Db::name('cursor_agents')->where('id', $data['agent_id'])->find();
|
||||
if (empty($agent)) {
|
||||
$this->error('代理商不存在!');
|
||||
}
|
||||
|
||||
$codeIds = explode(',', $data['code_ids']);
|
||||
$insertData = [];
|
||||
$now = date('Y-m-d H:i:s');
|
||||
|
||||
foreach ($codeIds as $codeId) {
|
||||
// 检查激活码是否已分配
|
||||
$exists = Db::name('cursor_agent_codes')->where('code_id', $codeId)->find();
|
||||
if ($exists) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// 计算佣金
|
||||
$commission = $data['price'] * ($agent['commission_rate'] / 100);
|
||||
|
||||
// 如果是二级代理,计算上级佣金
|
||||
$parentCommission = 0;
|
||||
if ($agent['level'] == 2 && $agent['parent_id'] > 0) {
|
||||
$parent = Db::name('cursor_agents')->where('id', $agent['parent_id'])->find();
|
||||
if ($parent) {
|
||||
$parentCommission = $data['price'] * ($parent['commission_rate'] / 100);
|
||||
}
|
||||
}
|
||||
|
||||
$insertData[] = [
|
||||
'agent_id' => $data['agent_id'],
|
||||
'code_id' => $codeId,
|
||||
'price' => $data['price'],
|
||||
'commission' => $commission,
|
||||
'parent_commission' => $parentCommission,
|
||||
'status' => 0,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now
|
||||
];
|
||||
}
|
||||
|
||||
// 批量插入数据
|
||||
if (!empty($insertData)) {
|
||||
Db::name('cursor_agent_codes')->insertAll($insertData);
|
||||
}
|
||||
|
||||
Db::commit();
|
||||
$this->success('分配成功!');
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error('分配失败:' . $e->getMessage());
|
||||
}
|
||||
} else {
|
||||
// 获取未分配的激活码
|
||||
$this->codes = Db::name('cursor_activation_codes')
|
||||
->where('is_used', 0)
|
||||
->whereNotExists(function($query) {
|
||||
$query->table('cursor_agent_codes')->whereRaw('cursor_agent_codes.code_id = cursor_activation_codes.id');
|
||||
})
|
||||
->select();
|
||||
|
||||
// 获取代理商列表
|
||||
$this->agents = Db::name('cursor_agents')
|
||||
->where('status', 1)
|
||||
->select();
|
||||
|
||||
$this->fetch();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看代理商激活码
|
||||
* @auth true
|
||||
*/
|
||||
public function codes()
|
||||
{
|
||||
$this->title = '代理商激活码';
|
||||
$query = $this->_query('cursor_agent_codes')->alias('ac')
|
||||
->join('cursor_agents a', 'a.id = ac.agent_id')
|
||||
->join('cursor_activation_codes c', 'c.id = ac.code_id')
|
||||
->field('ac.*, a.username as agent_name, c.code, c.days, c.is_used, c.used_at, c.used_by');
|
||||
|
||||
// 数据列表处理
|
||||
$query->equal('ac.agent_id')->equal('c.is_used')->equal('ac.status');
|
||||
// 列表排序并显示
|
||||
$query->order('ac.id desc')->page();
|
||||
}
|
||||
|
||||
/**
|
||||
* 结算佣金
|
||||
* @auth true
|
||||
*/
|
||||
public function settle()
|
||||
{
|
||||
$id = input('id');
|
||||
if (empty($id)) {
|
||||
$this->error('参数错误!');
|
||||
}
|
||||
|
||||
$agentCode = Db::name('cursor_agent_codes')->where('id', $id)->find();
|
||||
if (empty($agentCode)) {
|
||||
$this->error('记录不存在!');
|
||||
}
|
||||
|
||||
if ($agentCode['status'] == 1) {
|
||||
$this->error('该记录已结算!');
|
||||
}
|
||||
|
||||
// 开启事务
|
||||
Db::startTrans();
|
||||
try {
|
||||
// 更新代理商余额
|
||||
Db::name('cursor_agents')
|
||||
->where('id', $agentCode['agent_id'])
|
||||
->inc('balance', $agentCode['commission'])
|
||||
->inc('total_income', $agentCode['commission'])
|
||||
->update();
|
||||
|
||||
// 如果有上级佣金,更新上级代理商余额
|
||||
if ($agentCode['parent_commission'] > 0) {
|
||||
$agent = Db::name('cursor_agents')->where('id', $agentCode['agent_id'])->find();
|
||||
if ($agent && $agent['parent_id'] > 0) {
|
||||
Db::name('cursor_agents')
|
||||
->where('id', $agent['parent_id'])
|
||||
->inc('balance', $agentCode['parent_commission'])
|
||||
->inc('total_income', $agentCode['parent_commission'])
|
||||
->update();
|
||||
}
|
||||
}
|
||||
|
||||
// 更新结算状态
|
||||
Db::name('cursor_agent_codes')
|
||||
->where('id', $id)
|
||||
->update([
|
||||
'status' => 1,
|
||||
'settle_time' => date('Y-m-d H:i:s'),
|
||||
'updated_at' => date('Y-m-d H:i:s')
|
||||
]);
|
||||
|
||||
Db::commit();
|
||||
$this->success('结算成功!');
|
||||
} catch (\Exception $e) {
|
||||
Db::rollback();
|
||||
$this->error('结算失败:' . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user