3 changed files with 159 additions and 141 deletions
@ -0,0 +1,147 @@ |
|||||
|
<?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 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 = Session::get('login_admin_user_data'); |
||||
|
|
||||
|
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 = Session::get('login_admin_user_data'); |
||||
|
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,10010,$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()); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue