刮刮后端接口
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.

223 lines
6.5 KiB

<?php
declare (strict_types = 1);
namespace app\controller;
use app\BaseController;
use app\model\AdminDownScoresRecords;
use app\model\AdminUpScoresRecords;
use app\model\AgentRechargeRecords;
use app\model\AgentUser;
use app\validate\Admin;
use app\validate\Agent;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Request;
use think\facade\Session;
class AdminAgentTeam extends BaseController
{
/**
* 代理列表
* @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 = [];
$UserModel = new AgentUser();
# 用户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,phone,avatar,balance,withdrawal_balance';
$userRes = $UserModel->field($field)->where($where)->order('id desc')->paginate($limit);
$list = $userRes->items();
$total = $userRes->total();
foreach ($list as &$item) {
#$item['phone'] = format_phone_number($item['phone']);
$item['avatar'] = get_image_url($item['avatar']);
$item['status'] = $item['status'] == 1 ? '正常' : '停用';
}
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 = AgentUser::incrBalance($aid,$quota);
# 代理上分记录
AdminUpScoresRecords::createRecords($aid,$adminUser['id'],$quota,$agentBalance,2);
# 代理充值记录
AgentRechargeRecords::createRecords($aid,$quota,$agentBalance,1);
$connection->commit();
return $this->renderSuccess('上分成功');
} catch (ValidateException $e) {
return $this->renderError($e->getMessage());
} catch (\Exception $exception) {
$connection->rollback();
return $this->renderError($exception->getMessage());
}
}
/**
* 代理下分
* @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 = AgentUser::inspectWithdrawalBalance($aid,$quota);
if (!$inspectRes['status']) throw new ValidateException($inspectRes['msg']);
# 开启事务
$connection->startTrans();
# 扣减代理可提余额
$agentBalance = AgentUser::decrWithdrawalBalance($aid,$quota);
# 代理下分记录
AdminDownScoresRecords::createRecords($aid,$adminUser['id'],$quota,$agentBalance,2);
$connection->commit();
return $this->renderSuccess('下分成功');
} catch (ValidateException $e) {
return $this->renderError($e->getMessage());
} catch (\Exception $exception) {
$connection->rollback();
return $this->renderError($exception->getMessage());
}
}
/**
* 编辑代理信息
*/
public function editAgent()
{
$param = Request::param();
try {
validate(Agent::class)->scene('edit')->check($param);
$UserModel = new AgentUser();
$result = $UserModel->retrieve($param);
if (!$result['status']) throw new ValidateException($result['msg']);
return $this->renderSuccess($result['msg']);
} catch (ValidateException $validateException) {
return $this->renderError($validateException->getMessage());
}
}
/**
* 删除代理
* @return array
*/
public function deleteAgent()
{
$param = Request::param();
try {
validate(Agent::class)->scene('del')->check($param);
$result = AgentUser::destroy($param['aid']);
if (!$result) throw new ValidateException('删除失败');
return $this->renderSuccess('已删除');
} catch (ValidateException $validateException) {
return $this->renderError($validateException->getMessage());
} catch (\Exception $e) {
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();
try {
validate(Agent::class)->scene('register')->check($param);
$AgentUser = new AgentUser();
$result = $AgentUser->register($param);
if (!$result) throw new ValidateException('代理已存在');
return $this->renderSuccess('添加成功');
} catch (ValidateException $validateException) {
return $this->renderError($validateException->getMessage());
} catch (\Exception $e) {
return $this->renderError('操作失败');
}
}
}