优化数据归属问题
This commit is contained in:
@@ -4,6 +4,7 @@ namespace app\admin\controller;
|
||||
|
||||
use Throwable;
|
||||
use app\common\controller\Backend;
|
||||
use app\common\service\ChannelSettlementService;
|
||||
use support\think\Db;
|
||||
use support\Response;
|
||||
use Webman\Http\Request as WebmanRequest;
|
||||
@@ -16,7 +17,7 @@ class Channel extends Backend
|
||||
/**
|
||||
* 预览接口与手动结算共用「手动结算」按钮权限(避免额外菜单节点)
|
||||
*/
|
||||
protected array $noNeedPermission = ['manualSettlePreview', 'channelAdminShareList', 'saveChannelAdminShare'];
|
||||
protected array $noNeedPermission = ['manualSettlePreview', 'channelAdminShareList', 'saveChannelAdminShare', 'batchSettlePending', 'settleStats'];
|
||||
|
||||
/**
|
||||
* Channel模型对象
|
||||
@@ -314,7 +315,7 @@ class Channel extends Backend
|
||||
return $this->error(__('You have no permission'));
|
||||
}
|
||||
|
||||
$payload = $this->buildManualSettlePayload($row->toArray());
|
||||
$payload = ChannelSettlementService::buildSettlePayload($row->toArray());
|
||||
if (is_string($payload)) {
|
||||
return $this->error($payload);
|
||||
}
|
||||
@@ -611,63 +612,80 @@ class Channel extends Backend
|
||||
return $this->error(__('You have no permission'));
|
||||
}
|
||||
|
||||
$remark = (string) $request->post('remark', '');
|
||||
$remark = trim((string) $request->post('remark', ''));
|
||||
|
||||
$payload = $this->buildManualSettlePayload($row->toArray());
|
||||
if (is_string($payload)) {
|
||||
return $this->error($payload);
|
||||
}
|
||||
|
||||
$settlementNo = $payload['settlement_no'];
|
||||
if (Db::name('agent_settlement_period')->where('settlement_no', $settlementNo)->value('id')) {
|
||||
return $this->error('结算单号已存在,请稍后重试');
|
||||
}
|
||||
|
||||
$shareRows = $this->resolveCommissionSharesForChannel((int) $row['id']);
|
||||
if ($shareRows === []) {
|
||||
return $this->error('渠道下无可用管理员分配比例,无法生成佣金记录');
|
||||
}
|
||||
|
||||
$now = time();
|
||||
Db::startTrans();
|
||||
try {
|
||||
$periodId = (int) Db::name('agent_settlement_period')->insertGetId([
|
||||
'settlement_no' => $settlementNo,
|
||||
'period_start_at' => $payload['period_start_ts'],
|
||||
'period_end_at' => $payload['period_end_ts'],
|
||||
'total_bet_amount' => $payload['total_bet_amount'],
|
||||
'total_payout_amount' => $payload['total_payout_amount'],
|
||||
'platform_profit_amount' => $payload['platform_profit_amount'],
|
||||
'status' => 2,
|
||||
'remark' => trim($remark) !== '' ? $remark : ('手动结算-渠道#' . $row['id'] . '-' . $row['name']),
|
||||
'create_time' => $now,
|
||||
'update_time' => $now,
|
||||
]);
|
||||
|
||||
$commissionRows = $this->buildCommissionRowsForSplit(
|
||||
$shareRows,
|
||||
(int) $row['id'],
|
||||
$periodId,
|
||||
(string) $payload['calc_base_amount'],
|
||||
(string) $payload['commission_amount'],
|
||||
trim($remark) !== '' ? $remark : ('手动结算佣金-CH' . $row['id']),
|
||||
$now
|
||||
);
|
||||
if ($commissionRows === []) {
|
||||
throw new \RuntimeException('分配比例拆分失败,未生成佣金记录');
|
||||
if ($this->auth->isSuperAdmin()) {
|
||||
$res = ChannelSettlementService::settleBySuperAdmin((int) $row['id'], intval($this->auth->id), $remark, false);
|
||||
if (($res['ok'] ?? false) !== true) {
|
||||
return $this->error((string) ($res['msg'] ?? '结算失败'));
|
||||
}
|
||||
Db::name('agent_commission_record')->insertAll($commissionRows);
|
||||
|
||||
Db::name('channel')->where('id', $row['id'])->update([
|
||||
'update_time' => $now,
|
||||
]);
|
||||
Db::commit();
|
||||
} catch (Throwable $e) {
|
||||
Db::rollback();
|
||||
return $this->error($e->getMessage());
|
||||
return $this->success('超管结算完成,渠道分红余额已入账');
|
||||
}
|
||||
$res = ChannelSettlementService::settleDividendByChannelAdmin((int) $row['id'], intval($this->auth->id), $remark);
|
||||
if (($res['ok'] ?? false) !== true) {
|
||||
return $this->error((string) ($res['msg'] ?? '结算失败'));
|
||||
}
|
||||
return $this->success('渠道分红已结算完成');
|
||||
}
|
||||
|
||||
return $this->success('手动结算已完成,已生成结算周期与佣金记录');
|
||||
/**
|
||||
* 超管批量结算全部待结算渠道(可作为“提前结算”入口)
|
||||
*/
|
||||
public function batchSettlePending(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
if (!$this->auth->isSuperAdmin()) {
|
||||
return $this->error(__('You have no permission'));
|
||||
}
|
||||
$res = ChannelSettlementService::settleAllDueChannels(intval($this->auth->id));
|
||||
return $this->success('批量结算完成', $res);
|
||||
}
|
||||
|
||||
/**
|
||||
* 渠道结算统计卡片
|
||||
*/
|
||||
public function settleStats(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
$query = Db::name('channel');
|
||||
if (!$this->auth->isSuperAdmin()) {
|
||||
$query->where('id', 'in', $this->currentChannelIds ?: [0]);
|
||||
}
|
||||
$rows = $query->field(['id', 'status', 'carryover_balance'])->select()->toArray();
|
||||
$total = count($rows);
|
||||
$enabled = 0;
|
||||
$disabled = 0;
|
||||
$carryoverPositiveCount = 0;
|
||||
$carryoverTotal = '0.00';
|
||||
$carryoverPositiveTotal = '0.00';
|
||||
foreach ($rows as $row) {
|
||||
$status = intval($row['status'] ?? 0);
|
||||
if ($status === 1) {
|
||||
$enabled++;
|
||||
} else {
|
||||
$disabled++;
|
||||
}
|
||||
$carry = bcadd(strval($row['carryover_balance'] ?? '0'), '0', 2);
|
||||
$carryoverTotal = bcadd($carryoverTotal, $carry, 2);
|
||||
if (bccomp($carry, '0', 2) > 0) {
|
||||
$carryoverPositiveCount++;
|
||||
$carryoverPositiveTotal = bcadd($carryoverPositiveTotal, $carry, 2);
|
||||
}
|
||||
}
|
||||
return $this->success('', [
|
||||
'channel_total' => $total,
|
||||
'enabled_count' => $enabled,
|
||||
'disabled_count' => $disabled,
|
||||
'carryover_positive_count' => $carryoverPositiveCount,
|
||||
'carryover_total' => $carryoverTotal,
|
||||
'carryover_positive_total' => $carryoverPositiveTotal,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -251,9 +251,27 @@ class Rule extends Backend
|
||||
->select()
|
||||
->toArray();
|
||||
|
||||
foreach ($rules as $idx => $rule) {
|
||||
$title = $rule['title'] ?? '';
|
||||
if (is_string($title) && $title !== '') {
|
||||
$rules[$idx]['title'] = $this->menuTitleToZh($title);
|
||||
}
|
||||
}
|
||||
|
||||
return $this->assembleTree ? $this->tree->assembleChild($rules) : $rules;
|
||||
}
|
||||
|
||||
private function menuTitleToZh(string $title): string
|
||||
{
|
||||
static $zhMap = null;
|
||||
if (!is_array($zhMap)) {
|
||||
$mapFile = app_path() . '/common/lang/zh-cn/admin_rule_title.php';
|
||||
$loaded = is_file($mapFile) ? include $mapFile : [];
|
||||
$zhMap = is_array($loaded) ? $loaded : [];
|
||||
}
|
||||
return isset($zhMap[$title]) && is_string($zhMap[$title]) ? $zhMap[$title] : $title;
|
||||
}
|
||||
|
||||
private function autoAssignPermission(int $id, int $pid): void
|
||||
{
|
||||
$groups = AdminGroup::where('rules', '<>', '*')->select();
|
||||
|
||||
@@ -43,7 +43,7 @@ class UserNoticeRead extends Backend
|
||||
$table = strtolower($this->model->getTable());
|
||||
$mainShort = $alias[$table] ?? '';
|
||||
if ($mainShort !== '' && $this->auth && !$this->auth->isSuperAdmin()) {
|
||||
$where[] = ['user.admin_id', '=', intval(strval($this->auth->id))];
|
||||
$where[] = ['user.admin_id', 'in', $this->scopedAdminIds()];
|
||||
}
|
||||
|
||||
$res = $this->model
|
||||
@@ -60,4 +60,25 @@ class UserNoticeRead extends Backend
|
||||
'remark' => get_route_remark(),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前管理员可见的管理员ID集合(本人 + 下级角色组内管理员)
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
private function scopedAdminIds(): array
|
||||
{
|
||||
if (!$this->auth) {
|
||||
return [0];
|
||||
}
|
||||
if ($this->auth->isSuperAdmin()) {
|
||||
return [];
|
||||
}
|
||||
$groupIds = $this->auth->getAdminChildGroups();
|
||||
$adminIds = $groupIds ? $this->auth->getGroupAdmins($groupIds) : [];
|
||||
$adminIds[] = $this->auth->id;
|
||||
$adminIds = array_map(static fn($id) => intval(strval($id)), $adminIds);
|
||||
$adminIds = array_values(array_unique(array_filter($adminIds, static fn($id) => $id > 0)));
|
||||
return $adminIds === [] ? [0] : $adminIds;
|
||||
}
|
||||
}
|
||||
|
||||
268
app/admin/controller/order/AdminWithdrawOrder.php
Normal file
268
app/admin/controller/order/AdminWithdrawOrder.php
Normal file
@@ -0,0 +1,268 @@
|
||||
<?php
|
||||
|
||||
namespace app\admin\controller\order;
|
||||
|
||||
use app\common\controller\Backend;
|
||||
use app\common\service\AdminWalletService;
|
||||
use support\think\Db;
|
||||
use support\Response;
|
||||
use Throwable;
|
||||
use Webman\Http\Request as WebmanRequest;
|
||||
|
||||
/**
|
||||
* 管理员提现记录(审核)
|
||||
*/
|
||||
class AdminWithdrawOrder extends Backend
|
||||
{
|
||||
protected array $noNeedPermission = ['stats'];
|
||||
|
||||
protected ?object $model = null;
|
||||
|
||||
protected bool $modelValidate = false;
|
||||
|
||||
protected string|array $quickSearchField = ['id', 'order_no', 'receive_account', 'remark'];
|
||||
|
||||
protected string|array $defaultSortField = ['id' => 'desc'];
|
||||
|
||||
protected string|array $orderGuarantee = ['id' => 'desc'];
|
||||
|
||||
protected array $withJoinTable = ['admin', 'channel', 'reviewAdmin'];
|
||||
|
||||
protected function initController(WebmanRequest $request): ?Response
|
||||
{
|
||||
$this->model = new \app\common\model\AdminWithdrawOrder();
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function _index(): Response
|
||||
{
|
||||
if ($this->request && $this->request->get('select')) {
|
||||
return $this->select($this->request);
|
||||
}
|
||||
list($where, $alias, $limit, $order) = $this->queryBuilder();
|
||||
$table = strtolower($this->model->getTable());
|
||||
$mainShort = $alias[$table] ?? '';
|
||||
if ($mainShort !== '' && $this->auth && !$this->auth->isSuperAdmin()) {
|
||||
$where[] = [$mainShort . '.channel_id', 'in', $this->getCurrentAdminTopChannelIds()];
|
||||
}
|
||||
$res = $this->model
|
||||
->withJoin($this->withJoinTable, $this->withJoinType)
|
||||
->with($this->withJoinTable)
|
||||
->visible([
|
||||
'admin' => ['username'],
|
||||
'channel' => ['name'],
|
||||
'reviewAdmin' => ['username'],
|
||||
])
|
||||
->alias($alias)
|
||||
->where($where)
|
||||
->order($order)
|
||||
->paginate($limit);
|
||||
|
||||
return $this->success('', [
|
||||
'list' => $res->items(),
|
||||
'total' => $res->total(),
|
||||
'remark' => get_route_remark(),
|
||||
]);
|
||||
}
|
||||
|
||||
protected function _edit(): Response
|
||||
{
|
||||
$pk = $this->model->getPk();
|
||||
$id = $this->request ? ($this->request->post($pk) ?? $this->request->get($pk)) : null;
|
||||
if ($id === null || $id === '') {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
if ($this->request && $this->request->method() === 'POST') {
|
||||
return $this->error('请使用通过/拒绝按钮审核');
|
||||
}
|
||||
$row = $this->loadWithRelations(intval(strval($id)));
|
||||
if (!$row) {
|
||||
return $this->error(__('Record not found'));
|
||||
}
|
||||
if (!$this->canReviewOrder($row)) {
|
||||
return $this->error(__('You have no permission'));
|
||||
}
|
||||
return $this->success('', ['row' => $row]);
|
||||
}
|
||||
|
||||
public function approve(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
if ($request->method() !== 'POST') {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
$id = intval(strval($request->post('id', 0)));
|
||||
if ($id <= 0) {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
$order = Db::name('admin_withdraw_order')->where('id', $id)->find();
|
||||
if (!is_array($order)) {
|
||||
return $this->error(__('Record not found'));
|
||||
}
|
||||
if (!$this->canReviewOrder($order)) {
|
||||
return $this->error(__('You have no permission'));
|
||||
}
|
||||
if (intval($order['status'] ?? 0) !== 0) {
|
||||
return $this->error('该提现订单已审核');
|
||||
}
|
||||
$remark = trim((string) $request->post('remark', ''));
|
||||
Db::startTrans();
|
||||
try {
|
||||
AdminWalletService::approveWithdraw($order, intval($this->auth->id), $remark);
|
||||
Db::commit();
|
||||
} catch (Throwable $e) {
|
||||
Db::rollback();
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
return $this->success('审核通过');
|
||||
}
|
||||
|
||||
public function reject(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
if ($request->method() !== 'POST') {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
$id = intval(strval($request->post('id', 0)));
|
||||
if ($id <= 0) {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
$remark = trim((string) $request->post('remark', ''));
|
||||
if ($remark === '') {
|
||||
return $this->error('请填写拒绝原因');
|
||||
}
|
||||
$order = Db::name('admin_withdraw_order')->where('id', $id)->find();
|
||||
if (!is_array($order)) {
|
||||
return $this->error(__('Record not found'));
|
||||
}
|
||||
if (!$this->canReviewOrder($order)) {
|
||||
return $this->error(__('You have no permission'));
|
||||
}
|
||||
if (intval($order['status'] ?? 0) !== 0) {
|
||||
return $this->error('该提现订单已审核');
|
||||
}
|
||||
Db::startTrans();
|
||||
try {
|
||||
AdminWalletService::rejectWithdraw($order, intval($this->auth->id), $remark);
|
||||
Db::commit();
|
||||
} catch (Throwable $e) {
|
||||
Db::rollback();
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
return $this->success('审核拒绝完成');
|
||||
}
|
||||
|
||||
public function stats(WebmanRequest $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
$query = Db::name('admin_withdraw_order');
|
||||
if ($this->auth && !$this->auth->isSuperAdmin()) {
|
||||
$query->where('channel_id', 'in', $this->getCurrentAdminTopChannelIds());
|
||||
}
|
||||
$rows = $query->field(['status', 'amount', 'actual_amount'])->select()->toArray();
|
||||
$total = count($rows);
|
||||
$pending = 0;
|
||||
$approved = 0;
|
||||
$rejected = 0;
|
||||
$totalAmount = '0.00';
|
||||
$pendingAmount = '0.00';
|
||||
$approvedAmount = '0.00';
|
||||
foreach ($rows as $row) {
|
||||
$status = intval($row['status'] ?? 0);
|
||||
$amount = bcadd(strval($row['amount'] ?? '0'), '0', 2);
|
||||
$actual = bcadd(strval($row['actual_amount'] ?? '0'), '0', 2);
|
||||
$totalAmount = bcadd($totalAmount, $amount, 2);
|
||||
if ($status === 0) {
|
||||
$pending++;
|
||||
$pendingAmount = bcadd($pendingAmount, $amount, 2);
|
||||
} elseif ($status === 1) {
|
||||
$approved++;
|
||||
$approvedAmount = bcadd($approvedAmount, $actual, 2);
|
||||
} elseif ($status === 2) {
|
||||
$rejected++;
|
||||
}
|
||||
}
|
||||
return $this->success('', [
|
||||
'total_count' => $total,
|
||||
'pending_count' => $pending,
|
||||
'approved_count' => $approved,
|
||||
'rejected_count' => $rejected,
|
||||
'total_amount' => $totalAmount,
|
||||
'pending_amount' => $pendingAmount,
|
||||
'approved_amount' => $approvedAmount,
|
||||
]);
|
||||
}
|
||||
|
||||
private function loadWithRelations(int $id): ?array
|
||||
{
|
||||
$row = $this->model
|
||||
->withJoin($this->withJoinTable, $this->withJoinType)
|
||||
->with($this->withJoinTable)
|
||||
->visible([
|
||||
'admin' => ['username'],
|
||||
'channel' => ['name'],
|
||||
'reviewAdmin' => ['username'],
|
||||
])
|
||||
->where($this->model->getTable() . '.id', $id)
|
||||
->find();
|
||||
return $row ? $row->toArray() : null;
|
||||
}
|
||||
|
||||
private function canReviewOrder(array $order): bool
|
||||
{
|
||||
if (!$this->auth) {
|
||||
return false;
|
||||
}
|
||||
if ($this->auth->isSuperAdmin()) {
|
||||
return true;
|
||||
}
|
||||
$channelId = intval($order['channel_id'] ?? 0);
|
||||
if ($channelId <= 0) {
|
||||
return false;
|
||||
}
|
||||
$allowed = $this->getCurrentAdminTopChannelIds();
|
||||
return in_array($channelId, $allowed, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前管理员可审核的“顶级角色组(pid=0)”所属渠道
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
private function getCurrentAdminTopChannelIds(): array
|
||||
{
|
||||
$uid = intval($this->auth->id ?? 0);
|
||||
if ($uid <= 0) {
|
||||
return [0];
|
||||
}
|
||||
$groupIds = Db::name('admin_group_access')->where('uid', $uid)->column('group_id');
|
||||
if ($groupIds === []) {
|
||||
return [0];
|
||||
}
|
||||
$rows = Db::name('admin_group')
|
||||
->field(['id', 'pid', 'channel_id'])
|
||||
->where('id', 'in', $groupIds)
|
||||
->where('pid', 0)
|
||||
->whereNotNull('channel_id')
|
||||
->select()
|
||||
->toArray();
|
||||
$channelIds = [];
|
||||
foreach ($rows as $row) {
|
||||
$cid = intval($row['channel_id'] ?? 0);
|
||||
if ($cid > 0) {
|
||||
$channelIds[] = $cid;
|
||||
}
|
||||
}
|
||||
return $channelIds === [] ? [0] : array_values(array_unique($channelIds));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ class BetOrder extends Backend
|
||||
$table = strtolower($this->model->getTable());
|
||||
$mainShort = $alias[$table] ?? '';
|
||||
if ($mainShort !== '' && $this->auth && !$this->auth->isSuperAdmin()) {
|
||||
$where[] = ['user.admin_id', '=', intval(strval($this->auth->id))];
|
||||
$where[] = ['user.admin_id', 'in', $this->scopedAdminIds()];
|
||||
}
|
||||
|
||||
$res = $this->model
|
||||
@@ -101,4 +101,25 @@ class BetOrder extends Backend
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前管理员可见的管理员ID集合(本人 + 下级角色组内管理员)
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
private function scopedAdminIds(): array
|
||||
{
|
||||
if (!$this->auth) {
|
||||
return [0];
|
||||
}
|
||||
if ($this->auth->isSuperAdmin()) {
|
||||
return [];
|
||||
}
|
||||
$groupIds = $this->auth->getAdminChildGroups();
|
||||
$adminIds = $groupIds ? $this->auth->getGroupAdmins($groupIds) : [];
|
||||
$adminIds[] = $this->auth->id;
|
||||
$adminIds = array_map(static fn($id) => intval(strval($id)), $adminIds);
|
||||
$adminIds = array_values(array_unique(array_filter($adminIds, static fn($id) => $id > 0)));
|
||||
return $adminIds === [] ? [0] : $adminIds;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ class DepositOrder extends Backend
|
||||
$table = strtolower($this->model->getTable());
|
||||
$mainShort = $alias[$table] ?? '';
|
||||
if ($mainShort !== '' && $this->auth && !$this->auth->isSuperAdmin()) {
|
||||
$where[] = ['user.admin_id', '=', intval(strval($this->auth->id))];
|
||||
$where[] = ['user.admin_id', 'in', $this->scopedAdminIds()];
|
||||
}
|
||||
$this->appendDepositOrderIndexWhere($where, $mainShort);
|
||||
|
||||
@@ -140,7 +140,28 @@ class DepositOrder extends Backend
|
||||
if (!is_numeric(strval($adminIdRaw))) {
|
||||
return false;
|
||||
}
|
||||
return intval(strval($adminIdRaw)) === intval(strval($this->auth->id));
|
||||
return in_array(intval(strval($adminIdRaw)), $this->scopedAdminIds(), true);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前管理员可见的管理员ID集合(本人 + 下级角色组内管理员)
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
private function scopedAdminIds(): array
|
||||
{
|
||||
if (!$this->auth) {
|
||||
return [0];
|
||||
}
|
||||
if ($this->auth->isSuperAdmin()) {
|
||||
return [];
|
||||
}
|
||||
$groupIds = $this->auth->getAdminChildGroups();
|
||||
$adminIds = $groupIds ? $this->auth->getGroupAdmins($groupIds) : [];
|
||||
$adminIds[] = $this->auth->id;
|
||||
$adminIds = array_map(static fn($id) => intval(strval($id)), $adminIds);
|
||||
$adminIds = array_values(array_unique(array_filter($adminIds, static fn($id) => $id > 0)));
|
||||
return $adminIds === [] ? [0] : $adminIds;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -48,7 +48,7 @@ class WithdrawOrder extends Backend
|
||||
$table = strtolower($this->model->getTable());
|
||||
$mainShort = $alias[$table] ?? '';
|
||||
if ($mainShort !== '' && $this->auth && !$this->auth->isSuperAdmin()) {
|
||||
$where[] = ['user.admin_id', '=', intval(strval($this->auth->id))];
|
||||
$where[] = ['user.admin_id', 'in', $this->scopedAdminIds()];
|
||||
}
|
||||
|
||||
$res = $this->model
|
||||
@@ -395,7 +395,7 @@ class WithdrawOrder extends Backend
|
||||
return false;
|
||||
}
|
||||
$ownerAdminId = $this->intParam($user['admin_id'] ?? 0);
|
||||
return $ownerAdminId > 0 && $ownerAdminId === $this->intParam($this->auth->id ?? 0);
|
||||
return $ownerAdminId > 0 && in_array($ownerAdminId, $this->scopedAdminIds(), true);
|
||||
}
|
||||
|
||||
private function intParam($raw): int
|
||||
@@ -430,6 +430,27 @@ class WithdrawOrder extends Backend
|
||||
return '#' . strval($id);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前管理员可见的管理员ID集合(本人 + 下级角色组内管理员)
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
private function scopedAdminIds(): array
|
||||
{
|
||||
if (!$this->auth) {
|
||||
return [0];
|
||||
}
|
||||
if ($this->auth->isSuperAdmin()) {
|
||||
return [];
|
||||
}
|
||||
$groupIds = $this->auth->getAdminChildGroups();
|
||||
$adminIds = $groupIds ? $this->auth->getGroupAdmins($groupIds) : [];
|
||||
$adminIds[] = $this->auth->id;
|
||||
$adminIds = array_map(fn($id) => $this->intParam($id), $adminIds);
|
||||
$adminIds = array_values(array_unique(array_filter($adminIds, fn($id) => $id > 0)));
|
||||
return $adminIds === [] ? [0] : $adminIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* 把 2 位小数金额压缩成最多 2 位小数用于展示(不影响落库精度)
|
||||
*/
|
||||
|
||||
@@ -5,12 +5,17 @@ declare(strict_types=1);
|
||||
namespace app\admin\controller\routine;
|
||||
|
||||
use app\admin\model\Admin;
|
||||
use app\common\service\AdminWalletService;
|
||||
use app\common\controller\Backend;
|
||||
use support\think\Db;
|
||||
use Webman\Http\Request;
|
||||
use support\Response;
|
||||
use Throwable;
|
||||
|
||||
class AdminInfo extends Backend
|
||||
{
|
||||
protected array $noNeedPermission = ['walletSummary', 'walletRecords', 'withdrawApply'];
|
||||
|
||||
protected ?object $model = null;
|
||||
|
||||
protected array|string $preExcludeFields = ['username', 'last_login_time', 'password', 'salt', 'status'];
|
||||
@@ -88,4 +93,109 @@ class AdminInfo extends Backend
|
||||
|
||||
return $this->success('', ['row' => $row]);
|
||||
}
|
||||
|
||||
public function walletSummary(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
$adminId = intval($this->auth->id ?? 0);
|
||||
if ($adminId <= 0) {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
$wallet = AdminWalletService::ensureWallet($adminId);
|
||||
return $this->success('', [
|
||||
'wallet' => [
|
||||
'balance' => strval($wallet['balance'] ?? '0.00'),
|
||||
'frozen_balance' => strval($wallet['frozen_balance'] ?? '0.00'),
|
||||
'total_income' => strval($wallet['total_income'] ?? '0.00'),
|
||||
'total_withdraw' => strval($wallet['total_withdraw'] ?? '0.00'),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function walletRecords(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
$adminId = intval($this->auth->id ?? 0);
|
||||
if ($adminId <= 0) {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
$limit = intval((string) $request->get('limit', 10));
|
||||
if ($limit <= 0) {
|
||||
$limit = 10;
|
||||
}
|
||||
$res = Db::name('admin_wallet_record')->alias('awr')
|
||||
->leftJoin('channel c', 'awr.channel_id = c.id')
|
||||
->leftJoin('admin oa', 'awr.operator_admin_id = oa.id')
|
||||
->field([
|
||||
'awr.id', 'awr.biz_type', 'awr.direction', 'awr.amount', 'awr.balance_before', 'awr.balance_after',
|
||||
'awr.ref_type', 'awr.ref_id', 'awr.remark', 'awr.create_time', 'c.name as channel_name', 'oa.username as operator_admin_username',
|
||||
])
|
||||
->where('awr.admin_id', $adminId)
|
||||
->order('awr.id', 'desc')
|
||||
->paginate($limit);
|
||||
return $this->success('', [
|
||||
'list' => $res->items(),
|
||||
'total' => $res->total(),
|
||||
]);
|
||||
}
|
||||
|
||||
public function withdrawApply(Request $request): Response
|
||||
{
|
||||
$response = $this->initializeBackend($request);
|
||||
if ($response !== null) {
|
||||
return $response;
|
||||
}
|
||||
if ($request->method() !== 'POST') {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
$adminId = intval($this->auth->id ?? 0);
|
||||
if ($adminId <= 0) {
|
||||
return $this->error(__('Parameter error'));
|
||||
}
|
||||
$withdrawCoinRaw = $request->post('withdraw_coin', '');
|
||||
$withdrawCoin = is_string($withdrawCoinRaw) ? trim($withdrawCoinRaw) : (is_numeric($withdrawCoinRaw) ? strval($withdrawCoinRaw) : '');
|
||||
$receiveAccount = trim(is_string($request->post('receive_account', '')) ? $request->post('receive_account', '') : '');
|
||||
$receiveType = trim(is_string($request->post('receive_type', '')) ? $request->post('receive_type', '') : '');
|
||||
$idempotencyKey = trim(is_string($request->post('idempotency_key', '')) ? $request->post('idempotency_key', '') : '');
|
||||
if ($withdrawCoin === '' || $receiveAccount === '' || $receiveType === '' || $idempotencyKey === '') {
|
||||
return $this->error('参数缺失');
|
||||
}
|
||||
if (mb_strlen($idempotencyKey) > 64) {
|
||||
return $this->error('幂等键过长');
|
||||
}
|
||||
if (!is_numeric($withdrawCoin) || bccomp($withdrawCoin, '0', 2) <= 0) {
|
||||
return $this->error('提现金额必须大于0');
|
||||
}
|
||||
$withdrawCoin = bcadd($withdrawCoin, '0', 2);
|
||||
$allowedReceiveTypes = ['bank', 'ewallet', 'crypto'];
|
||||
if (!in_array($receiveType, $allowedReceiveTypes, true)) {
|
||||
return $this->error('收款类型不合法,仅支持 bank/ewallet/crypto');
|
||||
}
|
||||
$remark = trim((string) $request->post('remark', ''));
|
||||
$admin = Db::name('admin')->field(['id', 'channel_id'])->where('id', $adminId)->find();
|
||||
$channelId = is_array($admin) ? intval($admin['channel_id'] ?? 0) : 0;
|
||||
Db::startTrans();
|
||||
try {
|
||||
$res = AdminWalletService::applyWithdraw($adminId, $channelId, $withdrawCoin, $receiveType, $receiveAccount, $idempotencyKey, $remark);
|
||||
if (($res['ok'] ?? false) !== true) {
|
||||
Db::rollback();
|
||||
return $this->error(strval($res['msg'] ?? '提现申请失败'));
|
||||
}
|
||||
Db::commit();
|
||||
} catch (Throwable $e) {
|
||||
Db::rollback();
|
||||
return $this->error($e->getMessage());
|
||||
}
|
||||
return $this->success('提现申请已提交,待渠道超管审核', [
|
||||
'order_id' => intval($res['order_id'] ?? 0),
|
||||
'order_no' => strval($res['order_no'] ?? ''),
|
||||
'idempotent_hit' => !empty($res['idempotent_hit']),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ class UserWalletRecord extends Backend
|
||||
$table = strtolower($this->model->getTable());
|
||||
$mainShort = $alias[$table] ?? '';
|
||||
if ($mainShort !== '' && $this->auth && !$this->auth->isSuperAdmin()) {
|
||||
$where[] = ['user.admin_id', '=', intval(strval($this->auth->id))];
|
||||
$where[] = ['user.admin_id', 'in', $this->scopedAdminIds()];
|
||||
}
|
||||
|
||||
$res = $this->model
|
||||
@@ -101,4 +101,25 @@ class UserWalletRecord extends Backend
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当前管理员可见的管理员ID集合(本人 + 下级角色组内管理员)
|
||||
*
|
||||
* @return int[]
|
||||
*/
|
||||
private function scopedAdminIds(): array
|
||||
{
|
||||
if (!$this->auth) {
|
||||
return [0];
|
||||
}
|
||||
if ($this->auth->isSuperAdmin()) {
|
||||
return [];
|
||||
}
|
||||
$groupIds = $this->auth->getAdminChildGroups();
|
||||
$adminIds = $groupIds ? $this->auth->getGroupAdmins($groupIds) : [];
|
||||
$adminIds[] = $this->auth->id;
|
||||
$adminIds = array_map(static fn($id) => intval(strval($id)), $adminIds);
|
||||
$adminIds = array_values(array_unique(array_filter($adminIds, static fn($id) => $id > 0)));
|
||||
return $adminIds === [] ? [0] : $adminIds;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user