第三段完善 代理商绑定 激活等 正式上线的版本
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace app\manager\controller;
|
||||
|
||||
use think\admin\Controller;
|
||||
use think\admin\service\AdminService;
|
||||
|
||||
/**
|
||||
* Cursor账号管理
|
||||
@@ -41,7 +42,7 @@ class Account extends Controller
|
||||
$price = floatval(input('price', 0));
|
||||
|
||||
// 参数验证
|
||||
if ($count < 1 || $count > 100) {
|
||||
if ($count < 1 || $count > 500) {
|
||||
$this->error('生成数量必须在1-100之间');
|
||||
}
|
||||
if ($days < 1) {
|
||||
@@ -60,7 +61,8 @@ class Account extends Controller
|
||||
'code' => strtoupper(substr(md5(uniqid() . mt_rand()), 0, 16)),
|
||||
'days' => $days,
|
||||
'created_at' => $now,
|
||||
'is_used' => 0
|
||||
'is_used' => 0,
|
||||
'status' => 1 // 设置默认状态为正常
|
||||
];
|
||||
}
|
||||
|
||||
@@ -153,7 +155,7 @@ class Account extends Controller
|
||||
->leftJoin('cursor_agent_codes ac', 'c.id = ac.code_id')
|
||||
->leftJoin('cursor_agents a', 'ac.agent_id = a.id')
|
||||
->field([
|
||||
'c.*',
|
||||
'c.*', // 包含所有激活码字段,包括 status
|
||||
'a.id as agent_id',
|
||||
'a.username as agent_name',
|
||||
'a.level as agent_level',
|
||||
@@ -184,6 +186,18 @@ class Account extends Controller
|
||||
if ($usedBy) {
|
||||
$query->whereLike('c.used_by', "%{$usedBy}%");
|
||||
}
|
||||
|
||||
// 添加天数筛选
|
||||
$days = input('days', '');
|
||||
if ($days !== '') {
|
||||
$query->where('c.days', '=', intval($days));
|
||||
}
|
||||
|
||||
// 添加状态筛选
|
||||
$status = input('status', '');
|
||||
if ($status !== '') {
|
||||
$query->where('c.status', '=', $status);
|
||||
}
|
||||
});
|
||||
|
||||
// 列表排序并显示
|
||||
@@ -220,6 +234,36 @@ class Account extends Controller
|
||||
$this->_delete('cursor_activation_codes');
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量删除激活码
|
||||
* @auth true
|
||||
*/
|
||||
public function batchRemoveCodes()
|
||||
{
|
||||
$this->_applyFormToken();
|
||||
$ids = $this->request->post('ids', '');
|
||||
// 如果是字符串,转换为数组
|
||||
if (is_string($ids)) {
|
||||
$ids = explode(',', $ids);
|
||||
}
|
||||
// 确保ids是数组且不为空
|
||||
if (empty($ids) || !is_array($ids)) {
|
||||
$this->error('请选择要删除的激活码');
|
||||
}
|
||||
|
||||
// 删除激活码
|
||||
$deleteResult1 = \think\facade\Db::name('cursor_activation_codes')->whereIn('id', $ids)->delete();
|
||||
// 删除关联的代理商激活码记录
|
||||
$deleteResult2 = \think\facade\Db::name('cursor_agent_codes')->whereIn('code_id', $ids)->delete();
|
||||
|
||||
// 只要有一个删除操作成功就认为是成功的
|
||||
if ($deleteResult1 || $deleteResult2) {
|
||||
$this->success('批量删除成功!');
|
||||
} else {
|
||||
$this->success('没有需要删除的数据!');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 设备账号管理
|
||||
* @auth true
|
||||
@@ -228,42 +272,77 @@ class Account extends Controller
|
||||
public function devices()
|
||||
{
|
||||
$this->title = '设备账号管理';
|
||||
|
||||
// 创建查询对象
|
||||
$query = $this->_query('cursor_accounts')
|
||||
->where('used_by', '<>', '')
|
||||
->where('is_used', 1);
|
||||
$query = $this->_query('cursor_activation_codes')->alias('c')
|
||||
->field([
|
||||
'c.used_by',
|
||||
'MIN(c.used_at) as first_activation',
|
||||
'COUNT(c.id) as total_codes',
|
||||
'SUM(c.days) as total_days',
|
||||
'GROUP_CONCAT(DISTINCT a.username) as agent_names'
|
||||
])
|
||||
->leftJoin('cursor_agent_codes ac', 'c.id = ac.code_id')
|
||||
->leftJoin('cursor_agents a', 'ac.agent_id = a.id')
|
||||
->where('c.used_by', '<>', '')
|
||||
->where('c.is_used', 1)
|
||||
->group('c.used_by');
|
||||
|
||||
// 数据列表处理
|
||||
$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']);
|
||||
}
|
||||
$query->where(function($query) {
|
||||
$usedBy = input('used_by', '');
|
||||
if ($usedBy) {
|
||||
$query->whereLike('c.used_by', "%{$usedBy}%");
|
||||
}
|
||||
});
|
||||
|
||||
// 获取数据
|
||||
$result = $query->order('first_activation desc')->page();
|
||||
|
||||
// 处理列表数据
|
||||
$list = [];
|
||||
foreach ($result['list'] as $item) {
|
||||
// 获取当前使用的账号
|
||||
$account = \think\facade\Db::name('cursor_accounts')
|
||||
->where('used_by', $item['used_by'])
|
||||
->where('is_used', 1)
|
||||
->find();
|
||||
|
||||
// 计算到期时间
|
||||
$firstActivationTime = strtotime($item['first_activation']);
|
||||
$expireTime = $firstActivationTime + ($item['total_days'] * 24 * 3600);
|
||||
|
||||
// 检查缓存状态
|
||||
$cacheKey = "device_account_{$item['used_by']}";
|
||||
$cooldownKey = "device_cooldown_{$item['used_by']}";
|
||||
|
||||
$cachedAccount = \think\facade\Cache::get($cacheKey);
|
||||
$cooldownTime = \think\facade\Cache::get($cooldownKey);
|
||||
|
||||
// 构建数据
|
||||
$list[] = [
|
||||
'used_by' => $item['used_by'],
|
||||
'email' => $account ? $account['email'] : '--',
|
||||
'agent_names' => $item['agent_names'] ?: '--',
|
||||
'total_codes' => $item['total_codes'],
|
||||
'total_days' => $item['total_days'],
|
||||
'first_activation' => $item['first_activation'],
|
||||
'expire_time' => date('Y-m-d H:i:s', $expireTime),
|
||||
'days_left' => ceil(($expireTime - time()) / 86400),
|
||||
'has_cache' => !empty($cachedAccount),
|
||||
'in_cooldown' => $cooldownTime > time(),
|
||||
'cooldown_time' => $cooldownTime ? date('Y-m-d H:i:s', $cooldownTime) : ''
|
||||
];
|
||||
}
|
||||
|
||||
$this->list = $list;
|
||||
// 传递数据到模板
|
||||
$this->assign([
|
||||
'list' => $list,
|
||||
'page' => $result['page']
|
||||
]);
|
||||
|
||||
// 渲染模板
|
||||
$this->fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -301,4 +380,355 @@ class Account extends Controller
|
||||
$this->error("重置失败:{$e->getMessage()}");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量切换代理商
|
||||
* @auth true
|
||||
*/
|
||||
public function batchChangeAgent()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->_vali([
|
||||
'agent_id.require' => '代理商不能为空',
|
||||
'code_ids.require' => '请选择要切换的激活码',
|
||||
'price.require' => '销售价格不能为空',
|
||||
'price.float' => '销售价格必须是数字'
|
||||
]);
|
||||
|
||||
// 检查代理商是否存在
|
||||
$agent = \think\facade\Db::name('cursor_agents')
|
||||
->where('id', $data['agent_id'])
|
||||
->where('status', 1)
|
||||
->find();
|
||||
if (empty($agent)) {
|
||||
$this->error('代理商不存在或已被禁用!');
|
||||
}
|
||||
|
||||
// 解析激活码ID
|
||||
$codeIds = explode(',', $data['code_ids']);
|
||||
if (empty($codeIds)) {
|
||||
$this->error('请选择要切换的激活码!');
|
||||
}
|
||||
|
||||
// 开启事务
|
||||
\think\facade\Db::startTrans();
|
||||
|
||||
$now = date('Y-m-d H:i:s');
|
||||
|
||||
// 计算佣金
|
||||
$commission = $data['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 = $data['price'] * ($parent['commission_rate'] / 100);
|
||||
}
|
||||
}
|
||||
|
||||
// 删除旧的代理商关联
|
||||
$deleteResult = \think\facade\Db::name('cursor_agent_codes')
|
||||
->whereIn('code_id', $codeIds)
|
||||
->delete();
|
||||
|
||||
// 批量插入新的关联
|
||||
$insertData = [];
|
||||
foreach ($codeIds as $codeId) {
|
||||
$insertData[] = [
|
||||
'agent_id' => $agent['id'],
|
||||
'code_id' => $codeId,
|
||||
'price' => $data['price'],
|
||||
'commission' => $commission,
|
||||
'parent_commission' => $parentCommission,
|
||||
'status' => 0,
|
||||
'created_at' => $now,
|
||||
'updated_at' => $now
|
||||
];
|
||||
}
|
||||
|
||||
// 插入新数据
|
||||
if (!empty($insertData)) {
|
||||
$insertResult = \think\facade\Db::name('cursor_agent_codes')->insertAll($insertData);
|
||||
if ($insertResult === false) {
|
||||
\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 agentCodes()
|
||||
{
|
||||
$this->title = '代理商激活码';
|
||||
|
||||
// 获取当前管理员ID和角色
|
||||
$adminId = session('user.id');
|
||||
$isSuper = AdminService::isSuper();
|
||||
|
||||
if (!$isSuper) {
|
||||
// 普通管理员,获取关联的代理商信息
|
||||
$agent = \think\facade\Db::name('cursor_agents')
|
||||
->where('admin_id', $adminId)
|
||||
->where('status', 1)
|
||||
->find();
|
||||
|
||||
if (empty($agent)) {
|
||||
$this->error('未找到关联的代理商账号');
|
||||
}
|
||||
}
|
||||
|
||||
// 创建查询对象
|
||||
$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.*', // 包含所有激活码字段,包括 status
|
||||
'ac.price',
|
||||
'ac.commission',
|
||||
'ac.status as settle_status', // 重命名代理商激活码的状态字段
|
||||
'a.username as agent_name',
|
||||
'a.level as agent_level'
|
||||
]);
|
||||
|
||||
// 如果不是超级管理员,只显示自己的激活码
|
||||
if (!$isSuper) {
|
||||
$query->where('ac.agent_id', $agent['id']);
|
||||
}
|
||||
|
||||
// 数据列表处理
|
||||
$query->where(function($query) use ($isSuper) {
|
||||
$code = input('code', '');
|
||||
if ($code) {
|
||||
$query->whereLike('c.code', "%{$code}%");
|
||||
}
|
||||
|
||||
$isUsed = input('is_used', '');
|
||||
if ($isUsed !== '') {
|
||||
$query->where('c.is_used', '=', $isUsed);
|
||||
}
|
||||
|
||||
$status = input('status', '');
|
||||
if ($status !== '') {
|
||||
$query->where('c.status', '=', $status);
|
||||
}
|
||||
|
||||
// 添加天数筛选
|
||||
$days = input('days', '');
|
||||
if ($days !== '') {
|
||||
$query->where('c.days', '=', intval($days));
|
||||
}
|
||||
|
||||
// 超级管理员可以按代理商筛选
|
||||
if ($isSuper) {
|
||||
$agentId = input('agent_id', '');
|
||||
if ($agentId !== '') {
|
||||
$query->where('ac.agent_id', '=', $agentId);
|
||||
}
|
||||
}
|
||||
|
||||
// 添加时间范围筛选
|
||||
$createTime = input('create_time', '');
|
||||
if ($createTime) {
|
||||
$createTimeArr = explode(' - ', $createTime);
|
||||
if (count($createTimeArr) == 2) {
|
||||
$query->whereBetween('c.created_at', [
|
||||
"{$createTimeArr[0]} 00:00:00",
|
||||
"{$createTimeArr[1]} 23:59:59"
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
// 使用时间范围筛选
|
||||
$useTime = input('use_time', '');
|
||||
if ($useTime) {
|
||||
$useTimeArr = explode(' - ', $useTime);
|
||||
if (count($useTimeArr) == 2) {
|
||||
$query->whereBetween('c.used_at', [
|
||||
"{$useTimeArr[0]} 00:00:00",
|
||||
"{$useTimeArr[1]} 23:59:59"
|
||||
]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 获取代理商列表(仅超级管理员可见)
|
||||
if ($isSuper) {
|
||||
$this->agents = \think\facade\Db::name('cursor_agents')
|
||||
->where('status', 1)
|
||||
->select()
|
||||
->toArray();
|
||||
}
|
||||
|
||||
// 列表排序并显示
|
||||
$query->order('c.id desc')->page();
|
||||
}
|
||||
|
||||
/**
|
||||
* 禁用激活码
|
||||
* @auth true
|
||||
*/
|
||||
public function disableCode()
|
||||
{
|
||||
$this->_applyFormToken();
|
||||
$id = input('id');
|
||||
|
||||
// 更新激活码状态为禁用 (0)
|
||||
$result = \think\facade\Db::name('cursor_activation_codes')
|
||||
->where('id', $id)
|
||||
->update(['status' => 0]);
|
||||
|
||||
if ($result !== false) {
|
||||
$this->success('禁用成功!');
|
||||
} else {
|
||||
$this->error('禁用失败!');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 批量禁用激活码
|
||||
* @auth true
|
||||
*/
|
||||
public function batchDisableCodes()
|
||||
{
|
||||
$this->_applyFormToken();
|
||||
$ids = $this->request->post('ids', '');
|
||||
|
||||
if (empty($ids)) {
|
||||
$this->error('请选择要禁用的激活码');
|
||||
}
|
||||
|
||||
// 如果是字符串,转换为数组
|
||||
if (is_string($ids)) {
|
||||
$ids = explode(',', $ids);
|
||||
}
|
||||
|
||||
// 批量更新状态为禁用 (0)
|
||||
$result = \think\facade\Db::name('cursor_activation_codes')
|
||||
->whereIn('id', $ids)
|
||||
->update(['status' => 0]);
|
||||
|
||||
if ($result !== false) {
|
||||
$this->success('批量禁用成功!');
|
||||
} else {
|
||||
$this->error('批量禁用失败!');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 导出激活码到txt文件
|
||||
* @auth true
|
||||
*/
|
||||
public function exportCodes()
|
||||
{
|
||||
try {
|
||||
// 获取当前管理员ID和角色
|
||||
$adminId = session('user.id');
|
||||
$isSuper = AdminService::isSuper();
|
||||
|
||||
if (!$isSuper) {
|
||||
// 普通管理员,获取关联的代理商信息
|
||||
$agent = \think\facade\Db::name('cursor_agents')
|
||||
->where('admin_id', $adminId)
|
||||
->where('status', 1)
|
||||
->find();
|
||||
|
||||
if (empty($agent)) {
|
||||
$this->error('未找到关联的代理商账号');
|
||||
}
|
||||
}
|
||||
|
||||
// 创建查询对象
|
||||
$query = \think\facade\Db::name('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.code',
|
||||
'c.days',
|
||||
'c.is_used',
|
||||
'c.status'
|
||||
]);
|
||||
|
||||
// 如果不是超级管理员,只显示自己的激活码
|
||||
if (!$isSuper) {
|
||||
$query->where('ac.agent_id', $agent['id']);
|
||||
}
|
||||
|
||||
// 应用筛选条件
|
||||
$code = input('code', '');
|
||||
if ($code) {
|
||||
$query->whereLike('c.code', "%{$code}%");
|
||||
}
|
||||
|
||||
$isUsed = input('is_used', '');
|
||||
if ($isUsed !== '') {
|
||||
$query->where('c.is_used', '=', $isUsed);
|
||||
}
|
||||
|
||||
$status = input('status', '');
|
||||
if ($status !== '') {
|
||||
$query->where('c.status', '=', $status);
|
||||
}
|
||||
|
||||
$days = input('days', '');
|
||||
if ($days !== '') {
|
||||
$query->where('c.days', '=', intval($days));
|
||||
}
|
||||
|
||||
// 超级管理员可以按代理商筛选
|
||||
if ($isSuper) {
|
||||
$agentId = input('agent_id', '');
|
||||
if ($agentId !== '') {
|
||||
$query->where('ac.agent_id', '=', $agentId);
|
||||
}
|
||||
}
|
||||
|
||||
// 获取数据
|
||||
$list = $query->order('c.id desc')->select()->toArray();
|
||||
|
||||
// 生成文件内容
|
||||
$content = '';
|
||||
foreach ($list as $item) {
|
||||
// 获取状态文本
|
||||
$statusText = $item['status'] == 1 ? '正常' : '禁用';
|
||||
$usedText = $item['is_used'] == 1 ? '已使用' : '未使用';
|
||||
|
||||
// 格式化每一行: 激活码 天数 状态
|
||||
$content .= sprintf("%s\t%d\t%s/%s\n",
|
||||
$item['code'],
|
||||
$item['days'],
|
||||
$usedText,
|
||||
$statusText
|
||||
);
|
||||
}
|
||||
|
||||
// 设置响应头
|
||||
header('Content-Type: text/plain');
|
||||
header('Content-Disposition: attachment; filename="activation_codes_' . date('YmdHis') . '.txt"');
|
||||
|
||||
// 输出内容
|
||||
echo $content;
|
||||
exit;
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->error($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,6 +43,7 @@ class Agent extends Controller
|
||||
'parent_id.default' => 0,
|
||||
'commission_rate.require' => '佣金比例不能为空',
|
||||
'status.default' => 1,
|
||||
'admin_id.default' => 0, // 绑定的管理员ID
|
||||
'remark.default' => ''
|
||||
]);
|
||||
|
||||
@@ -62,6 +63,19 @@ class Agent extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
// 如果绑定了管理员账号,检查是否存在
|
||||
if (!empty($data['admin_id'])) {
|
||||
$admin = Db::name('system_user')->where('id', $data['admin_id'])->find();
|
||||
if (empty($admin)) {
|
||||
$this->error('管理员账号不存在!');
|
||||
}
|
||||
// 检查管理员账号是否已被绑定
|
||||
$exists = Db::name('cursor_agents')->where('admin_id', $data['admin_id'])->find();
|
||||
if ($exists) {
|
||||
$this->error('该管理员账号已被其他代理商绑定!');
|
||||
}
|
||||
}
|
||||
|
||||
// 处理密码
|
||||
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
|
||||
$data['created_at'] = $data['updated_at'] = date('Y-m-d H:i:s');
|
||||
@@ -77,6 +91,13 @@ class Agent extends Controller
|
||||
->where('level', 1)
|
||||
->where('status', 1)
|
||||
->select();
|
||||
|
||||
// 获取管理员列表
|
||||
$this->admins = Db::name('system_user')
|
||||
->where('status', 1)
|
||||
->field('id, username, nickname')
|
||||
->select();
|
||||
|
||||
$this->fetch();
|
||||
}
|
||||
}
|
||||
@@ -87,8 +108,85 @@ class Agent extends Controller
|
||||
*/
|
||||
public function edit()
|
||||
{
|
||||
$this->_applyFormToken();
|
||||
$this->_form('cursor_agents', 'form');
|
||||
if ($this->request->isPost()) {
|
||||
$data = $this->_vali([
|
||||
'id.require' => '代理商ID不能为空',
|
||||
'username.require' => '用户名不能为空',
|
||||
'password.default' => '',
|
||||
'nickname.default' => '',
|
||||
'level.require' => '代理级别不能为空',
|
||||
'parent_id.default' => 0,
|
||||
'commission_rate.require' => '佣金比例不能为空',
|
||||
'status.default' => 1,
|
||||
'admin_id.default' => 0, // 绑定的管理员ID
|
||||
'remark.default' => ''
|
||||
]);
|
||||
|
||||
// 检查用户名是否存在
|
||||
$exists = Db::name('cursor_agents')
|
||||
->where('username', $data['username'])
|
||||
->where('id', '<>', $data['id'])
|
||||
->find();
|
||||
if ($exists) {
|
||||
$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('上级代理不存在或不是一级代理!');
|
||||
}
|
||||
}
|
||||
|
||||
// 如果绑定了管理员账号,检查是否存在
|
||||
if (!empty($data['admin_id'])) {
|
||||
$admin = Db::name('system_user')->where('id', $data['admin_id'])->find();
|
||||
if (empty($admin)) {
|
||||
$this->error('管理员账号不存在!');
|
||||
}
|
||||
// 检查管理员账号是否已被其他代理商绑定
|
||||
$exists = Db::name('cursor_agents')
|
||||
->where('admin_id', $data['admin_id'])
|
||||
->where('id', '<>', $data['id'])
|
||||
->find();
|
||||
if ($exists) {
|
||||
$this->error('该管理员账号已被其他代理商绑定!');
|
||||
}
|
||||
}
|
||||
|
||||
// 处理密码
|
||||
if (!empty($data['password'])) {
|
||||
$data['password'] = password_hash($data['password'], PASSWORD_DEFAULT);
|
||||
} else {
|
||||
unset($data['password']);
|
||||
}
|
||||
|
||||
$data['updated_at'] = date('Y-m-d H:i:s');
|
||||
|
||||
if (Db::name('cursor_agents')->update($data) !== false) {
|
||||
$this->success('更新成功!');
|
||||
} else {
|
||||
$this->error('更新失败!');
|
||||
}
|
||||
} else {
|
||||
// 获取一级代理列表(用于选择上级)
|
||||
$this->agents = Db::name('cursor_agents')
|
||||
->where('level', 1)
|
||||
->where('status', 1)
|
||||
->select();
|
||||
|
||||
// 获取管理员列表
|
||||
$this->admins = Db::name('system_user')
|
||||
->where('status', 1)
|
||||
->field('id, username, nickname')
|
||||
->select();
|
||||
|
||||
$this->_form('cursor_agents', 'form');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
228
app/manager/controller/Version.php
Normal file
228
app/manager/controller/Version.php
Normal file
@@ -0,0 +1,228 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\manager\controller;
|
||||
|
||||
use app\admin\model\Version as VersionModel;
|
||||
use think\admin\Controller;
|
||||
use think\admin\extend\DataExtend;
|
||||
use think\admin\service\AdminService;
|
||||
use think\admin\service\SystemService;
|
||||
use think\admin\Storage;
|
||||
|
||||
/**
|
||||
* 版本管理
|
||||
* @auth true
|
||||
* @menu true
|
||||
*/
|
||||
class Version extends Controller
|
||||
{
|
||||
/**
|
||||
* 允许上传的文件类型
|
||||
* @var array
|
||||
*/
|
||||
protected $allowExts = ['zip', 'exe', 'dmg', 'pkg', 'deb', 'rpm'];
|
||||
|
||||
/**
|
||||
* 版本列表
|
||||
* @auth true
|
||||
* @menu true
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
$this->title = '版本管理';
|
||||
// 创建查询对象
|
||||
$query = VersionModel::mQuery();
|
||||
// 数据列表处理
|
||||
$query->like('version_no,version_name')->equal('platform,status');
|
||||
// 列表排序处理
|
||||
$query->order('version_no desc')->page();
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加版本
|
||||
* @auth true
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
$this->title = '添加版本';
|
||||
VersionModel::mForm('form');
|
||||
}
|
||||
|
||||
/**
|
||||
* 编辑版本
|
||||
* @auth true
|
||||
*/
|
||||
public function edit()
|
||||
{
|
||||
$this->title = '编辑版本';
|
||||
VersionModel::mForm('form');
|
||||
}
|
||||
|
||||
/**
|
||||
* 表单数据处理
|
||||
*/
|
||||
protected function _form_filter(&$data)
|
||||
{
|
||||
if ($this->request->isGet()) {
|
||||
// 获取版本日志
|
||||
if (isset($data['id'])) {
|
||||
$data['logs'] = \think\facade\Db::name('version_log')
|
||||
->where(['version_id' => $data['id']])
|
||||
->order('id asc')
|
||||
->select()
|
||||
->toArray();
|
||||
}
|
||||
} else {
|
||||
// 检查版本号是否已存在
|
||||
if (empty($data['id'])) {
|
||||
$exists = VersionModel::mk()->where('version_no', $data['version_no'])->find();
|
||||
if ($exists) {
|
||||
$this->error('该版本号已存在!');
|
||||
}
|
||||
}
|
||||
|
||||
// 保存版本日志
|
||||
if (isset($data['logs'])) {
|
||||
$logs = [];
|
||||
foreach ($data['logs']['type'] as $key => $type) {
|
||||
if (!empty($data['logs']['content'][$key])) {
|
||||
$logs[] = [
|
||||
'version_id' => $data['id'] ?? 0,
|
||||
'type' => $type,
|
||||
'content' => $data['logs']['content'][$key],
|
||||
'create_time' => date('Y-m-d H:i:s')
|
||||
];
|
||||
}
|
||||
}
|
||||
unset($data['logs']);
|
||||
if (!empty($logs)) {
|
||||
if (isset($data['id'])) {
|
||||
\think\facade\Db::name('version_log')->where(['version_id' => $data['id']])->delete();
|
||||
}
|
||||
\think\facade\Db::name('version_log')->insertAll($logs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除版本
|
||||
* @auth true
|
||||
*/
|
||||
public function remove()
|
||||
{
|
||||
VersionModel::mDelete();
|
||||
}
|
||||
|
||||
/**
|
||||
* 版本状态
|
||||
* @auth true
|
||||
*/
|
||||
public function state()
|
||||
{
|
||||
VersionModel::mSave($this->_vali([
|
||||
'status.in:0,1' => '状态值范围异常!',
|
||||
'status.require' => '状态值不能为空!',
|
||||
]));
|
||||
}
|
||||
|
||||
/**
|
||||
* 上传更新包
|
||||
* @auth true
|
||||
*/
|
||||
public function upload()
|
||||
{
|
||||
if ($this->request->isPost()) {
|
||||
try {
|
||||
// 获取上传的文件
|
||||
$file = $this->request->file('file');
|
||||
if (empty($file)) {
|
||||
$this->error('文件上传异常,文件可能过大!');
|
||||
}
|
||||
|
||||
// 检查文件类型
|
||||
$ext = strtolower($file->getOriginalExtension());
|
||||
if (!in_array($ext, $this->allowExts)) {
|
||||
$this->error('不支持的文件类型!');
|
||||
}
|
||||
|
||||
// 获取原始文件名
|
||||
$originalName = $file->getOriginalName();
|
||||
|
||||
// 从文件名解析版本号
|
||||
$version_no = VersionModel::parseVersionFromFilename($originalName);
|
||||
|
||||
// 生成版本名称
|
||||
$version_name = VersionModel::generateVersionName($version_no, $originalName);
|
||||
|
||||
// 执行文件上传
|
||||
$type = sysconf('storage.type|raw') ?: 'local';
|
||||
if ($type === 'local') {
|
||||
$local = \think\admin\storage\LocalStorage::instance();
|
||||
$distName = dirname($local->path($originalName, false)) . '/' . $originalName;
|
||||
$file->move(dirname($distName), basename($distName));
|
||||
$info = [
|
||||
'url' => '/upload/' . $originalName,
|
||||
'key' => $originalName
|
||||
];
|
||||
|
||||
// 设置上传文件的权限为755
|
||||
@chmod($distName, 0755);
|
||||
} else {
|
||||
$bina = file_get_contents($file->getPathname());
|
||||
$info = \think\admin\Storage::instance($type)->set($originalName, $bina, false, $originalName);
|
||||
}
|
||||
|
||||
|
||||
if (isset($info['url'])) {
|
||||
$data = [
|
||||
'code' => 0,
|
||||
'msg' => '文件上传成功!',
|
||||
'data' => [
|
||||
'url' => $info['url'],
|
||||
'name' => $originalName,
|
||||
'version_no' => $version_no,
|
||||
'version_name' => $version_name
|
||||
]
|
||||
];
|
||||
return json($data);
|
||||
} else {
|
||||
$this->error('文件上传失败!');
|
||||
}
|
||||
} catch (\Exception $exception) {
|
||||
$this->error($exception->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从文件名解析版本信息
|
||||
* @param string $filename
|
||||
* @return array
|
||||
*/
|
||||
protected function parseVersionFromFilename($filename)
|
||||
{
|
||||
// 移除扩展名
|
||||
$name = pathinfo($filename, PATHINFO_FILENAME);
|
||||
|
||||
// 匹配版本号 (支持 v3.4.0 或 3.4.0 格式)
|
||||
if (preg_match('/[v]?(\d+\.\d+\.\d+)/', $name, $matches)) {
|
||||
$version_no = $matches[1];
|
||||
// 提取版本名称 (去掉版本号部分)
|
||||
$version_name = trim(str_replace($matches[0], '', $name));
|
||||
if (empty($version_name)) {
|
||||
$version_name = "版本 {$version_no}";
|
||||
}
|
||||
} else {
|
||||
// 如果没有找到版本号,使用时间戳作为版本号
|
||||
$version_no = date('y.m.d');
|
||||
$version_name = $name ?: "版本 {$version_no}";
|
||||
}
|
||||
|
||||
return [
|
||||
'version_no' => $version_no,
|
||||
'version_name' => $version_name
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user