1.新增充值档位配置

2.新增充值/提现配置
This commit is contained in:
2026-04-21 18:31:43 +08:00
parent aad00e10f8
commit 0f28c0fd2a
29 changed files with 3647 additions and 278 deletions

View File

@@ -0,0 +1,421 @@
<?php
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.deposit_channel
*/
class DepositChannel 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;
}
/**
* 列表baTablelist / total / remark+ registry + tier_options弹窗用
*/
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'));
}
$tierOptions = $this->buildTierOptions();
$registryOut = $this->buildRegistryOut();
$parsed = DepositChannelLib::parseStoredOverridesFromDb();
$items = DepositChannelLib::expandRowsForAdmin($parsed);
$quickSearch = $request->get('quickSearch', '');
if (is_string($quickSearch) && trim($quickSearch) !== '') {
$q = mb_strtolower(trim($quickSearch));
$items = array_values(array_filter($items, static function (array $it) use ($q, $registryOut): bool {
$code = isset($it['code']) && is_string($it['code']) ? mb_strtolower($it['code']) : '';
$name = '';
if (isset($it['code']) && is_string($it['code']) && isset($registryOut[$it['code']])) {
$meta = $registryOut[$it['code']];
$name = isset($meta['name']) && is_string($meta['name']) ? mb_strtolower($meta['name']) : '';
}
return $q === '' || str_contains($code, $q) || str_contains($name, $q);
}));
}
$orderRaw = $request->get('order', '');
if (is_string($orderRaw) && str_contains($orderRaw, ',')) {
$parts = explode(',', $orderRaw, 2);
$field = trim($parts[0]);
$dir = strtolower(trim($parts[1] ?? 'asc'));
if ($field === 'sort') {
usort($items, static function (array $a, array $b) use ($dir): int {
$sa = isset($a['sort']) && is_numeric($a['sort']) ? intval($a['sort']) : 0;
$sb = isset($b['sort']) && is_numeric($b['sort']) ? intval($b['sort']) : 0;
if ($sa !== $sb) {
return $dir === 'desc' ? ($sb <=> $sa) : ($sa <=> $sb);
}
$ca = isset($a['code']) && is_string($a['code']) ? $a['code'] : '';
$cb = isset($b['code']) && is_string($b['code']) ? $b['code'] : '';
return strcmp($ca, $cb);
});
}
}
$total = count($items);
$pageRaw = $request->get('page', '1');
$limitRaw = $request->get('limit', '20');
$page = is_numeric($pageRaw) ? max(1, intval($pageRaw)) : 1;
$limit = is_numeric($limitRaw) ? max(1, min(500, intval($limitRaw))) : 20;
$offset = ($page - 1) * $limit;
$pageRows = array_slice($items, $offset, $limit);
foreach ($pageRows as &$pr) {
if (!is_array($pr)) {
continue;
}
$code = isset($pr['code']) && is_string($pr['code']) ? $pr['code'] : '';
$meta = $code !== '' && isset($registryOut[$code]) ? $registryOut[$code] : null;
$pr['display_name'] = is_array($meta) && isset($meta['name']) && is_string($meta['name']) ? $meta['name'] : $code;
$pr['name_en'] = is_array($meta) && isset($meta['name_en']) && is_string($meta['name_en']) ? $meta['name_en'] : '';
}
unset($pr);
return $this->success('', [
'list' => $pageRows,
'total' => $total,
'remark' => '',
'registry' => $registryOut,
'tier_options' => $tierOptions,
'items' => $pageRows,
]);
}
/**
* 单条读取 / 单条更新(弹窗)
*/
public function edit(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
if (!$this->hasNodePermission($request, 'edit')) {
return $this->error(__('You have no permission'), [], 401);
}
if ($request->method() === 'GET') {
$code = $request->get('code', '');
if (!is_string($code) || trim($code) === '') {
$code = $request->get('id', '');
}
if (!is_string($code) || trim($code) === '') {
return $this->error(__('Parameter error'));
}
$code = strtolower(trim($code));
$parsed = DepositChannelLib::parseStoredOverridesFromDb();
$items = DepositChannelLib::expandRowsForAdmin($parsed);
$found = null;
foreach ($items as $it) {
if (!is_array($it)) {
continue;
}
$c = isset($it['code']) && is_string($it['code']) ? strtolower(trim($it['code'])) : '';
if ($c === $code) {
$found = $it;
break;
}
}
if ($found === null) {
return $this->error(__('Record not found'));
}
return $this->success('', ['row' => $found]);
}
if ($request->method() === 'POST') {
$payload = $request->post();
if (!is_array($payload)) {
return $this->error(__('Parameter %s can not be empty', ['']));
}
$code = isset($payload['code']) && is_string($payload['code']) ? strtolower(trim($payload['code'])) : '';
if ($code === '') {
return $this->error(__('Parameter error'));
}
$parsed = DepositChannelLib::parseStoredOverridesFromDb();
$items = DepositChannelLib::expandRowsForAdmin($parsed);
$foundIdx = -1;
$foundRow = null;
foreach ($items as $k => $it) {
if (!is_array($it)) {
continue;
}
$c = isset($it['code']) && is_string($it['code']) ? strtolower(trim($it['code'])) : '';
if ($c === $code) {
$foundIdx = $k;
$foundRow = $it;
break;
}
}
if ($foundIdx < 0 || $foundRow === null) {
return $this->error(__('Record not found'));
}
$items[$foundIdx] = $this->mergeDepositChannelEditPayload($foundRow, $payload, $code);
return $this->persistChannelList($items);
}
return $this->error(__('Parameter error'));
}
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', ['']));
}
$items = $payload['items'] ?? null;
if (!is_array($items)) {
return $this->error('items 必须为数组');
}
return $this->persistChannelList($items);
}
/**
* @param list<array<string, mixed>> $items
*/
private function persistChannelList(array $items): Response
{
try {
$registry = DepositChannelLib::codeRegistry();
$clean = DepositChannelLib::prepareOverridesForSave(array_values($items));
$expectedCodes = array_keys($registry);
sort($expectedCodes);
$got = array_column($clean, 'code');
sort($got);
if ($expectedCodes !== $got) {
return $this->error('请保存全部已注册渠道行(不可缺行)');
}
$json = DepositChannelLib::encodeForDb($clean);
} catch (InvalidArgumentException $e) {
return $this->error($e->getMessage());
}
$now = time();
$resourceKey = GameHotDataLock::safeResourceKeyForConfig(DepositChannelLib::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', DepositChannelLib::CONFIG_KEY)->find();
if ($exists) {
Db::name('game_config')->where('config_key', DepositChannelLib::CONFIG_KEY)->update([
'config_value' => $json,
'value_type' => 'json',
'update_time' => $now,
]);
} else {
Db::name('game_config')->insert([
'config_key' => DepositChannelLib::CONFIG_KEY,
'config_value' => $json,
'value_type' => 'json',
'remark' => '充值支付渠道 JSON与 finance_cashier.channels 同步)',
'create_time' => $now,
'update_time' => $now,
]);
}
$fcExists = Db::name('game_config')->where('config_key', FinanceCashierConfigLib::CONFIG_KEY)->find();
if ($fcExists) {
$fcPayload = FinanceCashierConfigLib::parseFromConfigValue($fcExists['config_value'] ?? null);
$fcPayload['channels'] = $clean;
$fcJson = FinanceCashierConfigLib::encodeForDb($fcPayload);
Db::name('game_config')->where('config_key', FinanceCashierConfigLib::CONFIG_KEY)->update([
'config_value' => $fcJson,
'value_type' => 'json',
'update_time' => $now,
]);
GameHotDataCoordinator::afterGameConfigKeyCommitted(FinanceCashierConfigLib::CONFIG_KEY);
}
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
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($tierRow['config_value'] ?? 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;
}
/**
* 弹窗整表提交与列表内开关均走此合并:未出现在 payload 中的字段沿用 $current。
*
* @param array<string, mixed> $current
* @param array<string, mixed> $payload
*
* @return array{code: string, sort: int, status: int, tier_ids: list<string>}
*/
private function mergeDepositChannelEditPayload(array $current, array $payload, string $code): array
{
$form = [];
if (array_key_exists('sort', $payload)) {
$form['sort'] = $payload['sort'];
} else {
$form['sort'] = $current['sort'] ?? 0;
}
if (array_key_exists('status', $payload)) {
$form['status'] = $payload['status'];
} else {
$form['status'] = $current['status'] ?? 0;
}
if (array_key_exists('tier_ids', $payload) && is_array($payload['tier_ids'])) {
$form['tier_ids'] = $payload['tier_ids'];
} else {
$form['tier_ids'] = isset($current['tier_ids']) && is_array($current['tier_ids']) ? $current['tier_ids'] : [];
}
return $this->normalizeChannelFormRow($form, $code);
}
/**
* @param array<string, mixed> $payload
*
* @return array{code: string, sort: int, status: int, tier_ids: list<string>}
*/
private function normalizeChannelFormRow(array $payload, string $code): array
{
$sort = isset($payload['sort']) && is_numeric($payload['sort']) ? intval($payload['sort']) : 0;
$status = 0;
$st = $payload['status'] ?? 1;
if ($st === true || $st === 1 || $st === '1') {
$status = 1;
}
$tierIds = [];
if (isset($payload['tier_ids']) && is_array($payload['tier_ids'])) {
foreach ($payload['tier_ids'] as $tid) {
if (is_string($tid)) {
$t = trim($tid);
if ($t !== '' && preg_match('/^[a-zA-Z0-9_\-]{1,32}$/', $t)) {
$tierIds[] = $t;
}
}
}
$tierIds = array_values(array_unique($tierIds));
}
return [
'code' => $code,
'sort' => $sort,
'status' => $status,
'tier_ids' => $tierIds,
];
}
}

View File

@@ -44,6 +44,7 @@ class DepositTier extends Backend
return true;
}
}
return false;
}
@@ -53,7 +54,7 @@ class DepositTier extends Backend
}
/**
* 读取 game_config.deposit_tier 的档位列表
* 列表baTablelist / total / remark支持 quickSearch、分页
*/
public function index(WebmanRequest $request): Response
{
@@ -69,13 +70,219 @@ class DepositTier extends Backend
}
$row = Db::name('game_config')->where('config_key', DepositTierLib::CONFIG_KEY)->find();
$items = DepositTierLib::parseFromConfigValue($row['config_value'] ?? null);
$quickSearch = $request->get('quickSearch', '');
if (is_string($quickSearch) && trim($quickSearch) !== '') {
$q = mb_strtolower(trim($quickSearch));
$items = array_values(array_filter($items, static function (array $it) use ($q): bool {
$id = isset($it['id']) && is_string($it['id']) ? mb_strtolower($it['id']) : '';
$t = isset($it['title']) && is_string($it['title']) ? mb_strtolower($it['title']) : '';
$te = isset($it['title_en']) && is_string($it['title_en']) ? mb_strtolower($it['title_en']) : '';
return $q === '' || str_contains($id, $q) || str_contains($t, $q) || str_contains($te, $q);
}));
}
$orderRaw = $request->get('order', '');
if (is_string($orderRaw) && str_contains($orderRaw, ',')) {
$parts = explode(',', $orderRaw, 2);
$field = trim($parts[0]);
$dir = strtolower(trim($parts[1] ?? 'asc'));
if ($field === 'sort') {
usort($items, static function (array $a, array $b) use ($dir): int {
$sa = isset($a['sort']) && is_numeric($a['sort']) ? intval($a['sort']) : 0;
$sb = isset($b['sort']) && is_numeric($b['sort']) ? intval($b['sort']) : 0;
if ($sa !== $sb) {
return $dir === 'desc' ? ($sb <=> $sa) : ($sa <=> $sb);
}
$ida = isset($a['id']) && is_string($a['id']) ? $a['id'] : '';
$idb = isset($b['id']) && is_string($b['id']) ? $b['id'] : '';
return strcmp($ida, $idb);
});
}
}
$total = count($items);
$pageRaw = $request->get('page', '1');
$limitRaw = $request->get('limit', '20');
$page = is_numeric($pageRaw) ? max(1, intval($pageRaw)) : 1;
$limit = is_numeric($limitRaw) ? max(1, min(500, intval($limitRaw))) : 20;
$offset = ($page - 1) * $limit;
$pageRows = array_slice($items, $offset, $limit);
return $this->success('', [
'items' => $items,
'list' => $pageRows,
'total' => $total,
'remark' => '',
'items' => $pageRows,
]);
}
/**
* 保存 JSON 数组value_type=json
* 单条读取(弹窗编辑
*/
public function edit(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
if (!$this->hasNodePermission($request, 'edit')) {
return $this->error(__('You have no permission'), [], 401);
}
if ($request->method() === 'GET') {
$id = $request->get('id', '');
if (!is_string($id) || trim($id) === '') {
return $this->error(__('Parameter error'));
}
$row = Db::name('game_config')->where('config_key', DepositTierLib::CONFIG_KEY)->find();
$items = DepositTierLib::parseFromConfigValue($row['config_value'] ?? null);
$found = null;
foreach ($items as $it) {
if (!is_array($it)) {
continue;
}
$rid = $it['id'] ?? '';
if (is_string($rid) && $rid === $id) {
$found = $it;
break;
}
}
if ($found === null) {
return $this->error(__('Record not found'));
}
return $this->success('', ['row' => $found]);
}
if ($request->method() === 'POST') {
if (!$this->hasNodePermission($request, 'edit')) {
return $this->error(__('You have no permission'), [], 401);
}
$payload = $request->post();
if (!is_array($payload)) {
return $this->error(__('Parameter %s can not be empty', ['']));
}
$id = isset($payload['id']) && is_string($payload['id']) ? trim($payload['id']) : '';
if ($id === '') {
return $this->error(__('Parameter error'));
}
$cfgRow = Db::name('game_config')->where('config_key', DepositTierLib::CONFIG_KEY)->find();
$items = DepositTierLib::parseFromConfigValue($cfgRow['config_value'] ?? null);
$foundIdx = -1;
$foundRow = null;
foreach ($items as $k => $it) {
if (!is_array($it)) {
continue;
}
$rid = $it['id'] ?? '';
if (is_string($rid) && $rid === $id) {
$foundIdx = $k;
$foundRow = $it;
break;
}
}
if ($foundIdx < 0 || $foundRow === null) {
return $this->error(__('Record not found'));
}
$items[$foundIdx] = $this->mergeDepositTierEditPayload($foundRow, $payload, $id);
return $this->persistTierList($items);
}
return $this->error(__('Parameter error'));
}
/**
* 新增一条档位
*/
public function add(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
if (!$this->hasNodePermission($request, 'add')) {
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', ['']));
}
$cfgRow = Db::name('game_config')->where('config_key', DepositTierLib::CONFIG_KEY)->find();
$items = DepositTierLib::parseFromConfigValue($cfgRow['config_value'] ?? null);
$newId = isset($payload['id']) && is_string($payload['id']) ? trim($payload['id']) : '';
if ($newId === '') {
$newId = DepositTierLib::generateId();
}
foreach ($items as $it) {
if (!is_array($it)) {
continue;
}
$rid = $it['id'] ?? '';
if (is_string($rid) && $rid === $newId) {
return $this->error('档位 ID 已存在');
}
}
$items[] = $this->normalizeFormRow($payload, $newId);
return $this->persistTierList($items);
}
/**
* 删除(支持批量 ids
*/
public function del(WebmanRequest $request): Response
{
$response = $this->initializeBackend($request);
if ($response !== null) {
return $response;
}
if (!$this->hasNodePermission($request, 'del')) {
return $this->error(__('You have no permission'), [], 401);
}
if ($request->method() !== 'DELETE') {
return $this->error(__('Parameter error'));
}
$ids = $request->get('ids', []);
if (!is_array($ids)) {
if (is_string($ids) && $ids !== '') {
$ids = [$ids];
} else {
$ids = [];
}
}
$idSet = [];
foreach ($ids as $id) {
if (is_string($id) && trim($id) !== '') {
$idSet[trim($id)] = true;
}
}
if ($idSet === []) {
return $this->error(__('Parameter error'));
}
$cfgRow = Db::name('game_config')->where('config_key', DepositTierLib::CONFIG_KEY)->find();
$items = DepositTierLib::parseFromConfigValue($cfgRow['config_value'] ?? null);
$filtered = [];
foreach ($items as $it) {
if (!is_array($it)) {
continue;
}
$rid = isset($it['id']) && is_string($it['id']) ? $it['id'] : '';
if ($rid !== '' && isset($idSet[$rid])) {
continue;
}
$filtered[] = $it;
}
return $this->persistTierList($filtered);
}
/**
* 整表保存 JSON 数组(兼容旧版批量提交)
*/
public function save(WebmanRequest $request): Response
{
@@ -97,6 +304,15 @@ class DepositTier extends Backend
if (!is_array($items)) {
return $this->error('items 必须为数组');
}
return $this->persistTierList($items);
}
/**
* @param list<array<string, mixed>> $items
*/
private function persistTierList(array $items): Response
{
try {
$clean = DepositTierLib::prepareItemsForSave(array_values($items));
$json = DepositTierLib::encodeForDb($clean);
@@ -132,7 +348,6 @@ class DepositTier extends Backend
} catch (Throwable $e) {
return $this->error($e->getMessage());
}
GameHotDataCoordinator::afterGameConfigKeyCommitted(DepositTierLib::CONFIG_KEY);
return $this->success(__('Saved successfully'));
@@ -140,4 +355,67 @@ class DepositTier extends Backend
GameHotDataLock::release(GameHotDataLock::TYPE_GAME_CONFIG, $resourceKey, $lock['token'], $lock['redis_lock']);
}
}
/**
* 弹窗整表提交与列表内开关:未出现在 payload 中的字段沿用 $current。
*
* @param array<string, mixed> $current
* @param array<string, mixed> $payload
*
* @return array<string, mixed>
*/
private function mergeDepositTierEditPayload(array $current, array $payload, string $id): array
{
$merged = [];
$merged['id'] = $id;
if (array_key_exists('sort', $payload)) {
$merged['sort'] = $payload['sort'];
} else {
$merged['sort'] = $current['sort'] ?? 0;
}
if (array_key_exists('status', $payload)) {
$merged['status'] = $payload['status'];
} else {
$merged['status'] = $current['status'] ?? 0;
}
$stringKeys = ['title', 'title_en', 'currency', 'pay_amount', 'amount', 'bonus_amount', 'desc', 'desc_en'];
foreach ($stringKeys as $key) {
if (array_key_exists($key, $payload)) {
$merged[$key] = $payload[$key];
} else {
$merged[$key] = $current[$key] ?? ($key === 'bonus_amount' ? '0' : '');
}
}
return $this->normalizeFormRow($merged, $id);
}
/**
* @param array<string, mixed> $payload
*
* @return array<string, mixed>
*/
private function normalizeFormRow(array $payload, string $id): array
{
$sort = isset($payload['sort']) && is_numeric($payload['sort']) ? intval($payload['sort']) : 0;
$status = 0;
$st = $payload['status'] ?? 1;
if ($st === true || $st === 1 || $st === '1') {
$status = 1;
}
return [
'id' => $id,
'title' => isset($payload['title']) && is_string($payload['title']) ? $payload['title'] : '',
'title_en' => isset($payload['title_en']) && is_string($payload['title_en']) ? $payload['title_en'] : '',
'currency' => isset($payload['currency']) && is_string($payload['currency']) ? $payload['currency'] : '',
'pay_amount' => $payload['pay_amount'] ?? '',
'amount' => $payload['amount'] ?? '',
'bonus_amount' => $payload['bonus_amount'] ?? '0',
'desc' => isset($payload['desc']) && is_string($payload['desc']) ? $payload['desc'] : '',
'desc_en' => isset($payload['desc_en']) && is_string($payload['desc_en']) ? $payload['desc_en'] : '',
'sort' => $sort,
'status' => $status,
];
}
}

View File

@@ -0,0 +1,225 @@
<?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;
}
}

View File

@@ -3,7 +3,9 @@
namespace app\admin\controller\config;
use app\common\controller\Backend;
use app\common\library\game\DepositChannel;
use app\common\library\game\DepositTier;
use app\common\library\game\FinanceCashierConfig;
use app\common\library\game\StreakWinReward;
use app\common\library\game\ZiHuaDictionary as ZiHuaDictionaryLib;
use support\Response;
@@ -42,6 +44,8 @@ class GameConfig extends Backend
return [
ZiHuaDictionaryLib::CONFIG_KEY,
DepositTier::CONFIG_KEY,
DepositChannel::CONFIG_KEY,
FinanceCashierConfig::CONFIG_KEY,
StreakWinReward::CONFIG_KEY,
];
}

View File

@@ -0,0 +1,29 @@
<?php
namespace app\admin\controller\order;
use app\common\library\game\DepositChannel;
/**
* 渠道充值订单仅列出已注册且启用的支付渠道pay_channel产生的充值单
*/
class DepositChannelOrder extends DepositOrder
{
/**
* @param list<array<mixed>> $where
*/
protected function appendDepositOrderIndexWhere(array &$where, string $mainShort): void
{
if ($mainShort === '') {
return;
}
$effective = DepositChannel::effectiveRowsFromDb();
$codes = DepositChannel::enabledPayChannelCodes($effective);
if ($codes === []) {
$where[] = [$mainShort . '.pay_channel', '=', '__no_pay_channel__'];
return;
}
$where[] = [$mainShort . '.pay_channel', 'in', $codes];
}
}

View File

@@ -52,6 +52,7 @@ class DepositOrder extends Backend
$channelIds = $this->getScopedChannelIdsForFilter();
$where[] = [$mainShort . '.channel_id', 'in', $channelIds !== [] ? $channelIds : [0]];
}
$this->appendDepositOrderIndexWhere($where, $mainShort);
$res = $this->model
->withJoin($this->withJoinTable, $this->withJoinType)
@@ -72,6 +73,15 @@ class DepositOrder extends Backend
]);
}
/**
* 子类可追加列表过滤条件(例如仅展示已注册充值渠道的订单)
*
* @param list<array<mixed>> $where
*/
protected function appendDepositOrderIndexWhere(array &$where, string $mainShort): void
{
}
/**
* GET 时返回关联信息,便于前端详情弹窗直接渲染 user.username / channel.name
* POST 一律拒绝,保证充值订单的金额/状态只能由结算服务变更。