You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
293 lines
9.4 KiB
293 lines
9.4 KiB
<?php
|
|
declare (strict_types = 1);
|
|
|
|
namespace app\controller;
|
|
|
|
use app\BaseController;
|
|
use app\event\AgentPasswordChange;
|
|
use app\middleware\CheckAdmin;
|
|
use app\model\AdminDownScoresRecords;
|
|
use app\model\AdminUpScoresRecords;
|
|
use app\model\AgentInfo;
|
|
use app\model\RechargeRecords;
|
|
use app\model\User;
|
|
use app\model\WithdrawalRecords;
|
|
use app\validate\Admin;
|
|
use app\validate\Agent;
|
|
use think\exception\ValidateException;
|
|
use think\facade\Db;
|
|
use think\facade\Event;
|
|
use think\facade\Request;
|
|
|
|
class AdminAgentTeam extends BaseController
|
|
{
|
|
|
|
protected $middleware = [CheckAdmin::class];
|
|
|
|
/**
|
|
* 代理列表
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function agentList()
|
|
{
|
|
$request = Request::param();
|
|
|
|
$limit = $request['limit'] ?? 10;
|
|
|
|
$where = ['identity' => 2];
|
|
|
|
$UserModel = new User();
|
|
|
|
# 用户id / 手机号码查询
|
|
if (isset($request['title']) && !empty($request['title'])) {
|
|
$queryUser = $UserModel
|
|
->whereOr(['phone' => $request['title'], 'id' => $request['title']])
|
|
->field('id')
|
|
->select()
|
|
->toArray();
|
|
if ($queryUser) {
|
|
foreach ($queryUser as $queryUserRow) {
|
|
$where['id'][] = $queryUserRow['id'];
|
|
}
|
|
} else {
|
|
$where['id'] = 0;
|
|
}
|
|
}
|
|
|
|
# 查询用户列表
|
|
$field = 'id,aid,phone,avatar,balance,withdrawal_balance,status';
|
|
$userRes = $UserModel->field($field)->where($where)->order('id desc')->paginate($limit);
|
|
|
|
$list = $userRes->items();
|
|
$total = $userRes->total();
|
|
|
|
$AgentInfo = new AgentInfo();
|
|
foreach ($list as &$item) {
|
|
|
|
$item['avatar'] = get_image_url($item['avatar']);
|
|
$item['status'] = $item['status'] == 1 ? '正常' : '停用';
|
|
# 获取返点占比
|
|
$rebate_ratio = $AgentInfo->where('aid',$item['id'])->value('rebate_ratio');
|
|
$item['rebate_ratio'] = $rebate_ratio ? round($rebate_ratio * 100,2) : '0';
|
|
$item['grade'] = empty($item['aid']) ? '1级' : 'id:' . $item['aid'];
|
|
unset($item['aid']);
|
|
}
|
|
|
|
return $this->renderSuccess('数据返回成功', ['list' => $list, 'total' => $total]);
|
|
}
|
|
|
|
/**
|
|
* 代理上分
|
|
* @return array
|
|
*/
|
|
public function agentUpScores()
|
|
{
|
|
$request = Request::param();
|
|
|
|
$connection = Db::connect();
|
|
try {
|
|
$adminUser = $this->request->userInfo;
|
|
|
|
validate(Admin::class)->scene('scores')->check($request);
|
|
|
|
$aid = $request['aid']; # 代理id
|
|
$quota = $request['quota']; # 额度
|
|
|
|
# 开启事务
|
|
$connection->startTrans();
|
|
|
|
# 增加代理余额
|
|
$agentBalance = User::incrBalance($aid,$quota);
|
|
# 代理上分记录
|
|
AdminUpScoresRecords::createRecords($aid,$adminUser['id'],$quota,$agentBalance,2);
|
|
# 代理充值记录
|
|
RechargeRecords::createRecords($aid,$quota,$agentBalance,1,2,$adminUser['id']);
|
|
|
|
$connection->commit();
|
|
return $this->renderSuccess('上分成功');
|
|
} catch (ValidateException $e) {
|
|
return $this->renderError($e->getMessage());
|
|
} catch (\Exception $exception) {
|
|
$connection->rollback();
|
|
return $this->renderError('操作失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 代理下分
|
|
* @return array|void
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function agentDownScores()
|
|
{
|
|
$request = Request::param();
|
|
|
|
$connection = Db::connect();
|
|
try {
|
|
$adminUser = $this->request->userInfo;
|
|
validate(Admin::class)->scene('scores')->check($request);
|
|
|
|
$aid = $request['aid']; # 用户id
|
|
$quota = $request['quota']; # 额度
|
|
|
|
# 检测代理可提余额是否足够
|
|
$inspectRes = User::inspectUserBalance($aid,$quota);
|
|
if (!$inspectRes['status']) throw new ValidateException($inspectRes['msg']);
|
|
|
|
# 开启事务
|
|
$connection->startTrans();
|
|
|
|
# 扣减代理可提余额
|
|
$agentBalance = User::decrWithdrawalBalance($aid,$quota);
|
|
|
|
# 代理下分记录
|
|
AdminDownScoresRecords::createRecords($aid,$adminUser['id'],$quota,$agentBalance,2);
|
|
|
|
# 用户提现记录-下分
|
|
WithdrawalRecords::createRecords($aid,$quota,$agentBalance,1,2,$adminUser['id']);
|
|
|
|
$connection->commit();
|
|
return $this->renderSuccess('下分成功');
|
|
} catch (ValidateException $e) {
|
|
return $this->renderError($e->getMessage());
|
|
} catch (\Exception $exception) {
|
|
$connection->rollback();
|
|
return $this->renderError('操作失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 编辑代理信息
|
|
*/
|
|
public function editAgent()
|
|
{
|
|
$param = Request::param();
|
|
|
|
$connection = Db::connect();
|
|
try {
|
|
|
|
validate()->rule([
|
|
'aid|代理id' => 'require|number',
|
|
'password|密码' => 'min:6|max:20',
|
|
'rebate_ratio|返点占比' => 'float|between:0.01,100',
|
|
])->check($param);
|
|
$password = $param['password'] ?? '';
|
|
$rebate_ratio = (string) $param['rebate_ratio'] ?? '';
|
|
if (empty($password) && strlen($rebate_ratio) <= 0) {
|
|
throw new ValidateException('请填写修改的密码或返点占比');
|
|
}
|
|
|
|
# 开启事务
|
|
$connection->startTrans();
|
|
|
|
# 保存密码
|
|
if (!empty($password)) {
|
|
|
|
$param['user_id'] = $param['aid'];
|
|
|
|
$UserModel = new User();
|
|
$result = $UserModel->retrieve($param);
|
|
|
|
if (!$result['status']) throw new ValidateException($result['msg']);
|
|
# 清除登录记录
|
|
$event = new AgentPasswordChange($param['aid']);
|
|
Event::trigger(AgentPasswordChange::class,$event);
|
|
}
|
|
# 保存返点占比
|
|
if (strlen($rebate_ratio) > 0) {
|
|
$rebate_ratio = $param['rebate_ratio'] ?? 0;
|
|
$validateAgentRebateRatio = validate(Agent::class)->validateAgentRebateRatio($param['aid'],$rebate_ratio);
|
|
if ($validateAgentRebateRatio !== true) throw new ValidateException($validateAgentRebateRatio);
|
|
AgentInfo::updateRebateRatio($param['aid'],$rebate_ratio);
|
|
}
|
|
|
|
$connection->commit();
|
|
return $this->renderSuccess('成功');
|
|
} catch (ValidateException $validateException) {
|
|
$connection->rollback();
|
|
return $this->renderError($validateException->getMessage());
|
|
} catch (\Exception $exception) {
|
|
$connection->rollback();
|
|
return $this->renderError('操作失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 删除代理
|
|
* @return array
|
|
*/
|
|
public function deleteAgent()
|
|
{
|
|
$param = Request::param();
|
|
|
|
$connection = Db::connect();
|
|
try {
|
|
|
|
validate(Agent::class)->scene('del')->check($param);
|
|
|
|
$connection->startTrans();
|
|
|
|
$result = User::destroy($param['aid']);
|
|
|
|
if (!$result) throw new ValidateException('删除失败');
|
|
|
|
$agent_info_id = AgentInfo::where('aid',$param['aid'])->value('id');
|
|
$resInfo = AgentInfo::destroy($agent_info_id);
|
|
if (!$resInfo) throw new ValidateException('删除失败');
|
|
|
|
AgentInfo::syncUpdateAid($param['aid']);
|
|
|
|
$connection->commit();
|
|
return $this->renderSuccess('已删除');
|
|
} catch (ValidateException $validateException) {
|
|
return $this->renderError($validateException->getMessage());
|
|
} catch (\Exception $e) {
|
|
$connection->rollback();
|
|
return $this->renderError('操作失败');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 新增代理
|
|
* @return array
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\DbException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
*/
|
|
public function addAgent()
|
|
{
|
|
$param = Request::param();
|
|
|
|
$connection = Db::connect();
|
|
try {
|
|
|
|
validate(Agent::class)->scene('register')->check($param);
|
|
|
|
# 开启事务
|
|
$connection->startTrans();
|
|
|
|
$AgentUser = new User();
|
|
$param['aid'] = 0;
|
|
$aid = $AgentUser->register($param,2);
|
|
|
|
if (!$aid) throw new ValidateException('代理已存在');
|
|
|
|
if (isset($param['rebate_ratio'])) {
|
|
AgentInfo::createInfo($aid,$param['rebate_ratio']);
|
|
}
|
|
|
|
$connection->commit();
|
|
return $this->renderSuccess('添加成功');
|
|
} catch (ValidateException $validateException) {
|
|
return $this->renderError($validateException->getMessage());
|
|
} catch (\Exception $e) {
|
|
$connection->rollback();
|
|
return $this->renderError('操作失败');
|
|
}
|
|
}
|
|
}
|
|
|