108 lines
3.5 KiB
PHP
108 lines
3.5 KiB
PHP
<?php
|
|
|
|
namespace app\agent\controller;
|
|
|
|
use think\admin\Controller;
|
|
use think\facade\Db;
|
|
|
|
/**
|
|
* 代理商后台
|
|
*/
|
|
class Index extends Controller
|
|
{
|
|
/**
|
|
* 生成激活码
|
|
*/
|
|
public function generate()
|
|
{
|
|
if ($this->request->isPost()) {
|
|
$count = intval(input('count', 1));
|
|
$count = max(1, min($count, 100)); // 限制一次最多生成100个
|
|
|
|
// 获取当前代理商信息
|
|
$agentId = session('agent.id');
|
|
$agent = Db::name('cursor_agents')->where('id', $agentId)->find();
|
|
if (empty($agent)) {
|
|
$this->error('代理商信息不存在!');
|
|
}
|
|
|
|
// 开启事务
|
|
Db::startTrans();
|
|
try {
|
|
$codes = [];
|
|
$agentCodes = [];
|
|
$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' => 30, // 固定30天
|
|
'created_at' => $now,
|
|
'is_used' => 0
|
|
];
|
|
}
|
|
|
|
// 批量插入激活码
|
|
$codeIds = Db::name('cursor_activation_codes')->insertAll($codes);
|
|
|
|
// 计算佣金
|
|
$price = 100; // 固定价格100元
|
|
$commission = $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 = $price * ($parent['commission_rate'] / 100);
|
|
}
|
|
}
|
|
|
|
// 准备代理商激活码数据
|
|
foreach ($codes as $index => $code) {
|
|
$agentCodes[] = [
|
|
'agent_id' => $agentId,
|
|
'code_id' => $codeIds[$index],
|
|
'price' => $price,
|
|
'commission' => $commission,
|
|
'parent_commission' => $parentCommission,
|
|
'status' => 0,
|
|
'created_at' => $now,
|
|
'updated_at' => $now
|
|
];
|
|
}
|
|
|
|
// 批量插入代理商激活码关联
|
|
Db::name('cursor_agent_codes')->insertAll($agentCodes);
|
|
|
|
Db::commit();
|
|
$this->success('生成成功!');
|
|
} catch (\Exception $e) {
|
|
Db::rollback();
|
|
$this->error('生成失败:' . $e->getMessage());
|
|
}
|
|
} else {
|
|
$this->fetch();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 我的激活码列表
|
|
*/
|
|
public function codes()
|
|
{
|
|
$this->title = '我的激活码';
|
|
$agentId = session('agent.id');
|
|
|
|
$query = $this->_query('cursor_agent_codes')->alias('ac')
|
|
->join('cursor_activation_codes c', 'c.id = ac.code_id')
|
|
->where('ac.agent_id', $agentId)
|
|
->field('ac.*, c.code, c.days, c.is_used, c.used_at, c.used_by');
|
|
|
|
// 数据列表处理
|
|
$query->equal('c.is_used')->equal('ac.status');
|
|
// 列表排序并显示
|
|
$query->order('ac.id desc')->page();
|
|
}
|
|
}
|