Files
cursoradminapiformjifengqiang/app/manager/controller/Agent.php

373 lines
13 KiB
PHP

<?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,
'admin_id.default' => 0, // 绑定的管理员ID
'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('上级代理不存在或不是一级代理!');
}
}
// 如果绑定了管理员账号,检查是否存在
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');
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->admins = Db::name('system_user')
->where('status', 1)
->field('id, username, nickname')
->select();
$this->fetch();
}
}
/**
* 编辑代理商
* @auth true
*/
public function edit()
{
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');
}
}
/**
* 修改代理商状态
* @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());
}
}
}