Files
webman-buildadmin/app/admin/controller/config/FinanceCashierConfig.php
zhenhui 0f28c0fd2a 1.新增充值档位配置
2.新增充值/提现配置
2026-04-21 18:31:43 +08:00

226 lines
8.8 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
declare(strict_types=1);
namespace app\admin\controller\config;
use app\common\controller\Backend;
use app\common\library\game\DepositChannel as DepositChannelLib;
use app\common\library\game\DepositTier as DepositTierLib;
use app\common\library\game\FinanceCashierConfig as FinanceCashierConfigLib;
use app\common\service\GameHotDataCoordinator;
use app\common\service\GameHotDataLock;
use InvalidArgumentException;
use support\think\Db;
use support\Response;
use Throwable;
use Webman\Http\Request as WebmanRequest;
/**
* 支付/收款配置game_config.finance_cashier含充值渠道
*/
class FinanceCashierConfig extends Backend
{
protected bool $modelValidate = false;
protected array $noNeedPermission = ['index', 'save'];
private function hasNodePermission(WebmanRequest $request, string $action): bool
{
if (!$this->auth) {
return false;
}
$controllerPath = get_controller_path($request);
if (!$controllerPath) {
return false;
}
$paths = [];
$paths[] = $controllerPath . '/' . $action;
$parts = explode('/', $controllerPath);
foreach ($parts as &$part) {
if (str_contains($part, '_')) {
$part = lcfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', $part))));
}
}
$paths[] = implode('/', $parts) . '/' . $action;
foreach (array_values(array_unique($paths)) as $path) {
if ($this->auth->check($path)) {
return true;
}
}
return false;
}
protected function initController(WebmanRequest $request): ?Response
{
return null;
}
public function index(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
if (!$this->hasNodePermission($request, 'index')) {
return $this->error(__('You have no permission'), [], 401);
}
if ($request->method() !== 'GET') {
return $this->error(__('Parameter error'));
}
$row = Db::name('game_config')->where('config_key', FinanceCashierConfigLib::CONFIG_KEY)->find();
$form = FinanceCashierConfigLib::parseFromConfigValue($row['config_value'] ?? null);
$decoded = null;
if (is_array($row) && isset($row['config_value']) && is_string($row['config_value']) && trim($row['config_value']) !== '') {
$tmp = json_decode($row['config_value'], true);
if (is_array($tmp)) {
$decoded = $tmp;
}
}
$channelsKeyPresent = is_array($decoded) && array_key_exists('channels', $decoded);
if (!$channelsKeyPresent) {
$depRow = Db::name('game_config')->where('config_key', DepositChannelLib::CONFIG_KEY)->find();
$legacy = DepositChannelLib::parseOverridesFromConfigValue(is_array($depRow) ? ($depRow['config_value'] ?? null) : null);
$form['channels'] = DepositChannelLib::expandRowsForAdmin($legacy);
} else {
$form['channels'] = DepositChannelLib::expandRowsForAdmin($form['channels'] ?? []);
}
return $this->success('', [
'form' => $form,
'registry' => $this->buildRegistryOut(),
'tier_options' => $this->buildTierOptions(),
]);
}
public function save(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
if (!$this->hasNodePermission($request, 'save')) {
return $this->error(__('You have no permission'), [], 401);
}
if ($request->method() !== 'POST') {
return $this->error(__('Parameter error'));
}
$payload = $request->post();
if (!is_array($payload)) {
return $this->error(__('Parameter %s can not be empty', ['']));
}
try {
$json = FinanceCashierConfigLib::encodeForDb($payload);
} catch (InvalidArgumentException $e) {
return $this->error($e->getMessage());
}
$now = time();
$resourceKey = GameHotDataLock::safeResourceKeyForConfig(FinanceCashierConfigLib::CONFIG_KEY);
$lock = GameHotDataLock::tryAcquire(GameHotDataLock::TYPE_GAME_CONFIG, $resourceKey);
if (!$lock['acquired']) {
return $this->error('该配置正在被其他操作占用,请稍后再试');
}
try {
try {
$exists = Db::name('game_config')->where('config_key', FinanceCashierConfigLib::CONFIG_KEY)->find();
if ($exists) {
Db::name('game_config')->where('config_key', FinanceCashierConfigLib::CONFIG_KEY)->update([
'config_value' => $json,
'value_type' => 'json',
'update_time' => $now,
]);
} else {
Db::name('game_config')->insert([
'config_key' => FinanceCashierConfigLib::CONFIG_KEY,
'config_value' => $json,
'value_type' => 'json',
'remark' => '支付/收款配置(表单维护,含充值渠道)',
'create_time' => $now,
'update_time' => $now,
]);
}
$decodedSave = json_decode($json, true);
$chList = is_array($decodedSave) && isset($decodedSave['channels']) && is_array($decodedSave['channels'])
? $decodedSave['channels']
: [];
$mirrorJson = DepositChannelLib::encodeForDb($chList);
$depExists = Db::name('game_config')->where('config_key', DepositChannelLib::CONFIG_KEY)->find();
if ($depExists) {
Db::name('game_config')->where('config_key', DepositChannelLib::CONFIG_KEY)->update([
'config_value' => $mirrorJson,
'value_type' => 'json',
'update_time' => $now,
]);
} else {
Db::name('game_config')->insert([
'config_key' => DepositChannelLib::CONFIG_KEY,
'config_value' => $mirrorJson,
'value_type' => 'json',
'remark' => '充值渠道(与 finance_cashier.channels 同步)',
'create_time' => $now,
'update_time' => $now,
]);
}
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
GameHotDataCoordinator::afterGameConfigKeyCommitted(FinanceCashierConfigLib::CONFIG_KEY);
GameHotDataCoordinator::afterGameConfigKeyCommitted(DepositChannelLib::CONFIG_KEY);
return $this->success(__('Saved successfully'));
} finally {
GameHotDataLock::release(GameHotDataLock::TYPE_GAME_CONFIG, $resourceKey, $lock['token'], $lock['redis_lock']);
}
}
/**
* @return list<array{id: string, label: string}>
*/
private function buildTierOptions(): array
{
$tierRow = Db::name('game_config')->where('config_key', DepositTierLib::CONFIG_KEY)->find();
$allTiers = DepositTierLib::parseFromConfigValue(is_array($tierRow) ? ($tierRow['config_value'] ?? null) : null);
$tierOptions = [];
foreach ($allTiers as $t) {
if (!is_array($t)) {
continue;
}
$tid = isset($t['id']) && is_string($t['id']) ? $t['id'] : '';
if ($tid === '') {
continue;
}
$title = isset($t['title']) && is_string($t['title']) ? trim($t['title']) : '';
$tierOptions[] = [
'id' => $tid,
'label' => $title !== '' ? $title . ' (' . $tid . ')' : $tid,
];
}
return $tierOptions;
}
/**
* @return array<string, array{name: string, name_en: string, sort: int}>
*/
private function buildRegistryOut(): array
{
$registry = DepositChannelLib::codeRegistry();
$registryOut = [];
foreach ($registry as $code => $meta) {
if (!is_array($meta)) {
continue;
}
$registryOut[$code] = [
'name' => isset($meta['name']) && is_string($meta['name']) ? $meta['name'] : '',
'name_en' => isset($meta['name_en']) && is_string($meta['name_en']) ? $meta['name_en'] : '',
'sort' => isset($meta['sort']) && is_numeric($meta['sort']) ? intval($meta['sort']) : 10,
];
}
return $registryOut;
}
}