优化数据归属问题

This commit is contained in:
2026-04-23 15:08:37 +08:00
parent 378be9909d
commit 0373234750
29 changed files with 1993 additions and 75 deletions

View File

@@ -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']),
]);
}
}