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,438 @@
<?php
declare(strict_types=1);
namespace app\common\library\game;
use InvalidArgumentException;
use support\think\Db;
/**
* 充值支付渠道:优先读取 game_config.finance_cashier.channels无此键时回退 game_config.deposit_channel迁移期镜像
*
* 每项code须在代码/环境注册表内、sort、status(0/1)、tier_ids空=全部启用档位)
*
* 渠道展示名以代码注册表为准;运营只配置开关、排序、可用档位。
*/
final class DepositChannel
{
public const CONFIG_KEY = 'deposit_channel';
/**
* @return array<string, array{name: string, name_en: string, sort: int}>
*/
public static function codeRegistry(): array
{
$base = [
'directpay' => ['name' => 'DirectPay', 'name_en' => 'DirectPay', 'sort' => 10],
];
$extra = self::registryFromEnv();
foreach ($extra as $code => $meta) {
if (!is_string($code) || trim($code) === '' || !is_array($meta)) {
continue;
}
$normCode = strtolower(trim($code));
if (!preg_match('/^[a-z0-9_\-]{1,24}$/', $normCode)) {
continue;
}
$name = isset($meta['name']) && is_string($meta['name']) ? trim($meta['name']) : '';
$nameEn = isset($meta['name_en']) && is_string($meta['name_en']) ? trim($meta['name_en']) : '';
$sort = isset($meta['sort']) && is_numeric($meta['sort']) ? intval($meta['sort']) : 50;
if ($name === '') {
continue;
}
$base[$normCode] = [
'name' => $name,
'name_en' => $nameEn !== '' ? $nameEn : $name,
'sort' => $sort,
];
}
return $base;
}
/**
* @return array<string, mixed>
*/
private static function registryFromEnv(): array
{
$raw = getenv('DEPOSIT_CHANNELS_REGISTRY_JSON');
if (!is_string($raw) || trim($raw) === '') {
return [];
}
$decoded = json_decode($raw, true);
if (!is_array($decoded)) {
return [];
}
$out = [];
foreach ($decoded as $item) {
if (!is_array($item)) {
continue;
}
$code = isset($item['code']) && is_string($item['code']) ? strtolower(trim($item['code'])) : '';
if ($code === '') {
continue;
}
$out[$code] = $item;
}
return $out;
}
public static function parseOverridesFromConfigValue($raw): array
{
if (!is_string($raw) || trim($raw) === '') {
return [];
}
$decoded = json_decode($raw, true);
if (!is_array($decoded)) {
return [];
}
$list = isset($decoded['channels']) && is_array($decoded['channels']) ? $decoded['channels'] : $decoded;
return self::normalizeOverrides($list);
}
/**
* 库中「运营覆盖」原始列表未与注册表合并finance_cashier 含 channels 键时用其值;否则读 deposit_channel
*
* @return list<array{code: string, sort: int, status: int, tier_ids: list<string>}>
*/
public static function parseStoredOverridesFromDb(): array
{
$fcRow = Db::name('game_config')->where('config_key', FinanceCashierConfig::CONFIG_KEY)->find();
$rawFc = is_array($fcRow) ? ($fcRow['config_value'] ?? null) : null;
$decoded = null;
if (is_string($rawFc) && trim($rawFc) !== '') {
$tmp = json_decode($rawFc, true);
if (is_array($tmp)) {
$decoded = $tmp;
}
}
$channelsKeyPresent = is_array($decoded) && array_key_exists('channels', $decoded);
if ($channelsKeyPresent) {
$list = isset($decoded['channels']) && is_array($decoded['channels']) ? $decoded['channels'] : [];
return self::normalizeOverrides($list);
}
$depRow = Db::name('game_config')->where('config_key', self::CONFIG_KEY)->find();
$depRaw = is_array($depRow) ? ($depRow['config_value'] ?? null) : null;
return self::parseOverridesFromConfigValue($depRaw);
}
/**
* 与注册表合并并排序后的有效渠道行(业务侧统一入口)
*
* @return list<array{code: string, sort: int, status: int, tier_ids: list<string>}>
*/
public static function effectiveRowsFromDb(): array
{
$stored = self::parseStoredOverridesFromDb();
return self::effectiveOverrides(self::expandRowsForAdmin($stored));
}
/**
* @param list<mixed> $items
*
* @return list<array{code: string, sort: int, status: int, tier_ids: list<string>}>
*/
public static function normalizeOverrides(array $items): array
{
$out = [];
foreach ($items as $row) {
if (!is_array($row)) {
continue;
}
$code = isset($row['code']) && is_string($row['code']) ? strtolower(trim($row['code'])) : '';
if ($code === '') {
continue;
}
$sort = isset($row['sort']) && is_numeric($row['sort']) ? intval($row['sort']) : 0;
$status = isset($row['status']) && is_numeric($row['status']) ? intval($row['status']) : 1;
$status = $status === 1 ? 1 : 0;
$tierIds = [];
if (isset($row['tier_ids']) && is_array($row['tier_ids'])) {
foreach ($row['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));
}
$out[] = [
'code' => $code,
'sort' => $sort,
'status' => $status,
'tier_ids' => $tierIds,
];
}
return $out;
}
/**
* 合并注册表与运营覆盖;若库中无覆盖则对注册表内全部渠道启用默认行
*
* @param list<array{code: string, sort: int, status: int, tier_ids: list<string>}> $overrides
*
* @return list<array{code: string, sort: int, status: int, tier_ids: list<string>}>
*/
public static function effectiveOverrides(array $overrides): array
{
$registry = self::codeRegistry();
$byCode = [];
foreach ($overrides as $row) {
if (!isset($registry[$row['code']])) {
continue;
}
$byCode[$row['code']] = $row;
}
if ($byCode === []) {
foreach ($registry as $code => $meta) {
$sortMeta = $meta['sort'] ?? 10;
$sortVal = is_numeric($sortMeta) ? intval($sortMeta) : 10;
$byCode[$code] = [
'code' => $code,
'sort' => $sortVal,
'status' => 1,
'tier_ids' => [],
];
}
}
$list = array_values($byCode);
usort($list, static function (array $a, array $b): int {
if ($a['sort'] !== $b['sort']) {
return $a['sort'] <=> $b['sort'];
}
return strcmp($a['code'], $b['code']);
});
return $list;
}
/**
* @param array{code: string, sort: int, status: int, tier_ids: list<string>} $overrideRow
*/
public static function isTierAllowed(array $overrideRow, string $tierId): bool
{
$ids = $overrideRow['tier_ids'] ?? [];
if (!is_array($ids) || $ids === []) {
return true;
}
foreach ($ids as $id) {
if (is_string($id) && $id === $tierId) {
return true;
}
}
return false;
}
/**
* @param list<array<string, mixed>> $effectiveRows
*/
public static function findMergedByCode(array $effectiveRows, string $code): ?array
{
$norm = strtolower(trim($code));
foreach ($effectiveRows as $row) {
if (!is_array($row)) {
continue;
}
$c = isset($row['code']) && is_string($row['code']) ? strtolower(trim($row['code'])) : '';
if ($c === $norm) {
return $row;
}
}
return null;
}
/**
* @param list<array{code: string, sort: int, status: int, tier_ids: list<string>}> $overrideRows
*
* @return list<array{code: string, name: string, sort: int}>
*/
public static function channelsForTier(string $tierId, array $overrideRows, string $lang): array
{
$registry = self::codeRegistry();
$out = [];
foreach ($overrideRows as $row) {
if (($row['status'] ?? 0) !== 1) {
continue;
}
$code = $row['code'];
if (!isset($registry[$code])) {
continue;
}
if (!self::isTierAllowed($row, $tierId)) {
continue;
}
$meta = $registry[$code];
$name = self::pickLangName($meta, $lang);
$sortRaw = $row['sort'] ?? 0;
$sortVal = is_numeric($sortRaw) ? intval($sortRaw) : 0;
$out[] = [
'code' => $code,
'name' => $name,
'sort' => $sortVal,
];
}
usort($out, static function (array $a, array $b): int {
if ($a['sort'] !== $b['sort']) {
return $a['sort'] <=> $b['sort'];
}
return strcmp($a['code'], $b['code']);
});
return $out;
}
/**
* @param array<string, string> $meta
*/
private static function pickLangName(array $meta, string $lang): string
{
$name = isset($meta['name']) && is_string($meta['name']) ? $meta['name'] : '';
$nameEn = isset($meta['name_en']) && is_string($meta['name_en']) ? $meta['name_en'] : '';
$normalized = strtolower(str_replace('_', '-', trim($lang)));
$isEn = $normalized === 'en' || str_starts_with($normalized, 'en-');
if ($isEn) {
return $nameEn !== '' ? $nameEn : $name;
}
return $name !== '' ? $name : $nameEn;
}
/**
* @param list<array{code: string, sort: int, status: int, tier_ids: list<string>}> $effectiveRows
*/
public static function assertChannelAllowsTier(string $channelCode, string $tierId, array $effectiveRows): bool
{
$row = self::findMergedByCode($effectiveRows, $channelCode);
if ($row === null) {
return false;
}
if (($row['status'] ?? 0) !== 1) {
return false;
}
return self::isTierAllowed($row, $tierId);
}
/**
* @param list<array{code: string, sort: int, status: int, tier_ids: list<string>}> $effectiveRows
*
* @return list<string>
*/
public static function enabledPayChannelCodes(array $effectiveRows): array
{
$registry = self::codeRegistry();
$codes = [];
foreach ($effectiveRows as $row) {
if (($row['status'] ?? 0) !== 1) {
continue;
}
$c = isset($row['code']) && is_string($row['code']) ? $row['code'] : '';
if ($c !== '' && isset($registry[$c])) {
$codes[] = $c;
}
}
return array_values(array_unique($codes));
}
/**
* @param list<array<string, mixed>> $items
*
* @return list<array{code: string, sort: int, status: int, tier_ids: list<string>}>
*/
public static function prepareOverridesForSave(array $items): array
{
$registry = self::codeRegistry();
$seen = [];
$out = [];
foreach ($items as $idx => $row) {
if (!is_array($row)) {
throw new InvalidArgumentException('第 ' . ($idx + 1) . ' 行格式错误');
}
$code = isset($row['code']) && is_string($row['code']) ? strtolower(trim($row['code'])) : '';
if ($code === '' || !isset($registry[$code])) {
throw new InvalidArgumentException('第 ' . ($idx + 1) . ' 行渠道 code 未注册');
}
if (isset($seen[$code])) {
throw new InvalidArgumentException('渠道 code 重复:' . $code);
}
$seen[$code] = true;
$norm = self::normalizeOverrides([array_merge($row, ['code' => $code])]);
if ($norm === []) {
throw new InvalidArgumentException('第 ' . ($idx + 1) . ' 行渠道数据无效');
}
$out[] = $norm[0];
}
usort($out, static function (array $a, array $b): int {
if ($a['sort'] !== $b['sort']) {
return $a['sort'] <=> $b['sort'];
}
return strcmp($a['code'], $b['code']);
});
return $out;
}
/**
* @param list<array{code: string, sort: int, status: int, tier_ids: list<string>}> $items
*/
public static function encodeForDb(array $items): string
{
$wrapped = ['channels' => $items];
$encoded = json_encode($wrapped, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
if ($encoded === false) {
throw new InvalidArgumentException('JSON 编码失败');
}
return $encoded;
}
/**
* 后台编辑用:为注册表内每个 code 补齐一行(合并库内覆盖)
*
* @param list<array{code: string, sort: int, status: int, tier_ids: list<string>}> $storedOverrides
*
* @return list<array{code: string, sort: int, status: int, tier_ids: list<string>}>
*/
public static function expandRowsForAdmin(array $storedOverrides): array
{
$registry = self::codeRegistry();
$effective = self::effectiveOverrides($storedOverrides);
$byCode = [];
foreach ($effective as $r) {
$byCode[$r['code']] = $r;
}
$items = [];
foreach ($registry as $code => $meta) {
$sortMeta = $meta['sort'] ?? 10;
$sortDefault = is_numeric($sortMeta) ? intval($sortMeta) : 10;
$items[] = $byCode[$code] ?? [
'code' => $code,
'sort' => $sortDefault,
'status' => 1,
'tier_ids' => [],
];
}
usort($items, static function (array $a, array $b): int {
if ($a['sort'] !== $b['sort']) {
return $a['sort'] <=> $b['sort'];
}
return strcmp($a['code'], $b['code']);
});
return $items;
}
}

View File

@@ -10,11 +10,13 @@ use InvalidArgumentException;
* 充值档位game_config.deposit_tier仅存 JSON 数组
*
* 每一项字段mock/第三方支付模式,已不再保存收款账户信息;支持中英文双语):
* - id : string档位稳定 ID(如 t_xxxxxxxx
* - id : string档位稳定唯一标识(如 t_xxxxxxxx,移动端下单 tier_id / tier_key
* - title : string档位中文名称必填前端中文环境展示
* - title_en : string档位英文名称可选前端英文环境展示为空时回退到 title
* - amount : string充值金额4 位小数
* - bonus_amount : string赠送金额4 位小数,可为 0
* - currency : string支付货币代码38 位大写字母,如 MYR、CNY
* - pay_amount : string玩家支付的法币/支付货币额度4 位小数)
* - amount : string到账基础平台币4 位小数)
* - bonus_amount : string赠送平台币4 位小数,可为 0
* - desc : string档位中文描述可空<=255
* - desc_en : string档位英文描述可空<=255为空时回退到 desc
* - sort : int排序权重小值在前
@@ -33,6 +35,8 @@ final class DepositTier
* id: string,
* title: string,
* title_en: string,
* currency: string,
* pay_amount: string,
* amount: string,
* bonus_amount: string,
* desc: string,
@@ -83,8 +87,18 @@ final class DepositTier
}
$titleEn = self::stringField($row, 'title_en');
$currency = self::normalizeCurrency($row['currency'] ?? '');
if ($currency === '') {
$currency = 'CNY';
}
$payAmount = self::normalizeAmount($row['pay_amount'] ?? '');
$amount = self::normalizeAmount($row['amount'] ?? '');
$bonus = self::normalizeAmount($row['bonus_amount'] ?? '0');
if (bccomp($payAmount, '0', 4) <= 0 && bccomp($amount, '0', 4) > 0) {
// 历史数据仅有 amount平台币用占位同步 pay_amount运营应在后台改为真实支付额度
$payAmount = $amount;
}
$desc = self::stringField($row, 'desc');
if ($desc === '') {
@@ -100,6 +114,8 @@ final class DepositTier
'id' => $id,
'title' => $title,
'title_en' => $titleEn,
'currency' => $currency,
'pay_amount' => $payAmount,
'amount' => $amount,
'bonus_amount' => $bonus,
'desc' => $desc,
@@ -165,9 +181,19 @@ final class DepositTier
throw new InvalidArgumentException('第 ' . $no . ' 行英文充值名称过长');
}
$currency = self::normalizeCurrency($row['currency'] ?? '');
if ($currency === '') {
throw new InvalidArgumentException('第 ' . $no . ' 行支付货币不能为空');
}
$payAmount = self::normalizeAmount($row['pay_amount'] ?? '');
if (bccomp($payAmount, '0', 4) <= 0) {
throw new InvalidArgumentException('第 ' . $no . ' 行支付货币额度必须大于 0');
}
$amount = self::normalizeAmount($row['amount'] ?? '');
if (bccomp($amount, '0', 4) <= 0) {
throw new InvalidArgumentException('第 ' . $no . ' 行充值金额必须大于 0');
throw new InvalidArgumentException('第 ' . $no . ' 行基础平台币到账必须大于 0');
}
$bonus = self::normalizeAmount($row['bonus_amount'] ?? '0');
@@ -193,6 +219,8 @@ final class DepositTier
'id' => $id,
'title' => $title,
'title_en' => $titleEn,
'currency' => $currency,
'pay_amount' => $payAmount,
'amount' => $amount,
'bonus_amount' => $bonus,
'desc' => $desc,
@@ -340,6 +368,26 @@ final class DepositTier
return is_string($v) ? trim($v) : '';
}
/**
* 支付货币代码38 位字母,统一大写
*
* @param mixed $raw
*/
private static function normalizeCurrency($raw): string
{
if (!is_string($raw)) {
return '';
}
$s = strtoupper(trim($raw));
if ($s === '') {
return '';
}
if (!preg_match('/^[A-Z]{3,8}$/', $s)) {
return '';
}
return $s;
}
private static function isEnglishLang(string $lang): bool
{
$normalized = strtolower(str_replace('_', '-', trim($lang)));

View File

@@ -0,0 +1,392 @@
<?php
declare(strict_types=1);
namespace app\common\library\game;
use InvalidArgumentException;
/**
* 移动端支付/收款展示game_config.finance_cashier含 channels 充值渠道)
*
* 货币列表每行含:
* - deposit_coins_per_fiat充值参考每支付 1 单位该货币到账的平台币数量
* - withdraw_coins_per_fiat提现换算每兑换 1 单位该货币所需平台币数量
*/
final class FinanceCashierConfig
{
public const CONFIG_KEY = 'finance_cashier';
/**
* @return array<string, mixed>
*/
public static function defaultPayload(): array
{
return [
'platform_coin' => [
'label_zh' => '钻石',
'label_en' => 'Diamonds',
],
'currencies' => [
[
'code' => 'MYR',
'label_zh' => '马来西亚林吉特',
'label_en' => 'Malaysian Ringgit',
'sort' => 10,
'deposit_coins_per_fiat' => '100',
'withdraw_coins_per_fiat' => '100',
],
[
'code' => 'VND',
'label_zh' => '越南盾',
'label_en' => 'Vietnamese Dong',
'sort' => 20,
'deposit_coins_per_fiat' => '10',
'withdraw_coins_per_fiat' => '10',
],
[
'code' => 'USDT',
'label_zh' => 'USDT',
'label_en' => 'USDT',
'sort' => 30,
'deposit_coins_per_fiat' => '1',
'withdraw_coins_per_fiat' => '1',
],
],
'withdraw_banks' => [
['code' => 'agrobank', 'name_zh' => 'Agrobank', 'name_en' => 'Agrobank', 'sort' => 10],
],
'withdraw_limits' => [
'min_ewallet' => '10',
'min_bank' => '10',
],
'withdraw_copy' => [
'rate_hint_zh' => '汇率为参考价格,实际以提现时为准。',
'rate_hint_en' => 'Exchange rates are for reference only; the actual rate at withdrawal time prevails.',
'processing_zh' => '9秒即可到账',
'processing_en' => 'Arrives in seconds',
'fee_note_zh' => '注意RM 10 — RM 99.99 之间的交易将收取最低 RM 1 的提现手续费。',
'fee_note_en' => 'A minimum RM 1 handling fee may apply for withdrawals between RM 10 and RM 99.99.',
'rate_mode' => 'fixed',
],
'withdraw_fields' => [
'require_cardholder' => true,
'require_bank_account' => true,
'require_email' => true,
'require_mobile' => true,
],
'channels' => [],
];
}
/**
* @return array<string, mixed>
*/
public static function parseFromConfigValue(mixed $raw): array
{
$out = self::defaultPayload();
if (!is_string($raw) || trim($raw) === '') {
return self::normalizePayload($out, []);
}
$decoded = json_decode($raw, true);
if (!is_array($decoded)) {
return self::normalizePayload($out, []);
}
if (isset($decoded['platform_coin']) && is_array($decoded['platform_coin'])) {
$out['platform_coin'] = array_replace($out['platform_coin'], array_intersect_key($decoded['platform_coin'], $out['platform_coin']));
}
$legacyRates = [];
if (isset($decoded['rates']) && is_array($decoded['rates'])) {
$legacyRates = array_values($decoded['rates']);
}
foreach (['currencies', 'withdraw_banks', 'channels'] as $listKey) {
if (isset($decoded[$listKey]) && is_array($decoded[$listKey])) {
$out[$listKey] = array_values($decoded[$listKey]);
}
}
if (isset($decoded['withdraw_limits']) && is_array($decoded['withdraw_limits'])) {
$out['withdraw_limits'] = array_replace($out['withdraw_limits'], array_intersect_key($decoded['withdraw_limits'], $out['withdraw_limits']));
}
if (isset($decoded['withdraw_copy']) && is_array($decoded['withdraw_copy'])) {
$out['withdraw_copy'] = array_replace($out['withdraw_copy'], array_intersect_key($decoded['withdraw_copy'], $out['withdraw_copy']));
}
if (isset($decoded['withdraw_fields']) && is_array($decoded['withdraw_fields'])) {
$out['withdraw_fields'] = array_replace($out['withdraw_fields'], array_intersect_key($decoded['withdraw_fields'], $out['withdraw_fields']));
}
return self::normalizePayload($out, $legacyRates);
}
public static function encodeForDb(array $payload): string
{
$legacyRates = [];
if (isset($payload['rates']) && is_array($payload['rates'])) {
$legacyRates = array_values($payload['rates']);
}
$normalized = self::normalizePayload($payload, $legacyRates);
$normalized['channels'] = DepositChannel::prepareOverridesForSave(
DepositChannel::expandRowsForAdmin($normalized['channels'] ?? [])
);
self::validate($normalized);
return json_encode($normalized, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}
/**
* @param array<string, mixed> $p
* @param list<array<string, mixed>> $legacyRates
*
* @return array<string, mixed>
*/
private static function normalizePayload(array $p, array $legacyRates): array
{
$defaults = self::defaultPayload();
$out = array_replace($defaults, $p);
unset($out['rates'], $out['fx_pairs']);
$withdrawFromLegacyRates = [];
foreach ($legacyRates as $r) {
if (!is_array($r)) {
continue;
}
$cur = isset($r['currency']) && is_string($r['currency']) ? strtoupper(trim($r['currency'])) : '';
$ratio = self::ratioStringFromRow($r, 'diamonds_per_fiat_unit');
if ($cur !== '' && $ratio !== '') {
$withdrawFromLegacyRates[$cur] = $ratio;
}
}
if (isset($out['platform_coin']) && is_array($out['platform_coin'])) {
$out['platform_coin'] = array_replace($defaults['platform_coin'], $out['platform_coin']);
}
if (isset($out['currencies']) && is_array($out['currencies'])) {
foreach ($out['currencies'] as $i => $row) {
if (!is_array($row)) {
unset($out['currencies'][$i]);
continue;
}
$code = isset($row['code']) && is_string($row['code']) ? strtoupper(trim($row['code'])) : '';
$dep = self::ratioStringFromRow($row, 'deposit_coins_per_fiat');
$wdr = self::ratioStringFromRow($row, 'withdraw_coins_per_fiat');
if ($wdr === '') {
$wdr = self::ratioStringFromRow($row, 'diamonds_per_fiat_unit');
}
if ($wdr === '' && $code !== '' && isset($withdrawFromLegacyRates[$code])) {
$wdr = $withdrawFromLegacyRates[$code];
}
if ($dep === '' && $wdr !== '') {
$dep = $wdr;
}
if ($wdr === '' && $dep !== '') {
$wdr = $dep;
}
if ($code !== '' && $dep === '' && $wdr === '') {
$dep = '100';
$wdr = '100';
}
$out['currencies'][$i] = [
'code' => $code,
'label_zh' => isset($row['label_zh']) && is_string($row['label_zh']) ? trim($row['label_zh']) : '',
'label_en' => isset($row['label_en']) && is_string($row['label_en']) ? trim($row['label_en']) : '',
'sort' => self::normalizeSort($row['sort'] ?? 0),
'deposit_coins_per_fiat' => $dep,
'withdraw_coins_per_fiat' => $wdr,
];
}
$out['currencies'] = array_values(array_filter($out['currencies'], static fn ($r) => is_array($r) && $r['code'] !== ''));
}
usort($out['currencies'], static function (array $a, array $b): int {
$sa = $a['sort'] ?? 0;
$sb = $b['sort'] ?? 0;
if ($sa !== $sb) {
return $sa <=> $sb;
}
$ca = $a['code'] ?? '';
$cb = $b['code'] ?? '';
return strcmp($ca, $cb);
});
if (isset($out['withdraw_banks']) && is_array($out['withdraw_banks'])) {
foreach ($out['withdraw_banks'] as $i => $row) {
if (!is_array($row)) {
unset($out['withdraw_banks'][$i]);
continue;
}
$code = isset($row['code']) && is_string($row['code']) ? strtolower(trim($row['code'])) : '';
$out['withdraw_banks'][$i] = [
'code' => $code,
'name_zh' => isset($row['name_zh']) && is_string($row['name_zh']) ? trim($row['name_zh']) : '',
'name_en' => isset($row['name_en']) && is_string($row['name_en']) ? trim($row['name_en']) : '',
'sort' => self::normalizeSort($row['sort'] ?? 0),
];
}
$out['withdraw_banks'] = array_values(array_filter($out['withdraw_banks'], static fn ($r) => is_array($r) && $r['code'] !== ''));
usort($out['withdraw_banks'], static function (array $a, array $b): int {
$sa = $a['sort'] ?? 0;
$sb = $b['sort'] ?? 0;
if ($sa !== $sb) {
return $sa <=> $sb;
}
$ca = $a['code'] ?? '';
$cb = $b['code'] ?? '';
return strcmp($ca, $cb);
});
}
if (isset($out['withdraw_limits']) && is_array($out['withdraw_limits'])) {
$wl = array_replace($defaults['withdraw_limits'], $out['withdraw_limits']);
foreach (['min_ewallet', 'min_bank'] as $k) {
if (isset($wl[$k]) && is_numeric($wl[$k]) && !is_string($wl[$k])) {
$wl[$k] = strval($wl[$k]);
}
if (!isset($wl[$k]) || !is_string($wl[$k])) {
$wl[$k] = $defaults['withdraw_limits'][$k];
}
}
$out['withdraw_limits'] = $wl;
}
if (isset($out['withdraw_copy']) && is_array($out['withdraw_copy'])) {
$out['withdraw_copy'] = array_replace($defaults['withdraw_copy'], array_intersect_key($out['withdraw_copy'], $defaults['withdraw_copy']));
$mode = isset($out['withdraw_copy']['rate_mode']) && is_string($out['withdraw_copy']['rate_mode'])
? strtolower(trim($out['withdraw_copy']['rate_mode']))
: 'fixed';
if (!in_array($mode, ['fixed', 'live'], true)) {
$mode = 'fixed';
}
$out['withdraw_copy']['rate_mode'] = $mode;
}
if (isset($out['withdraw_fields']) && is_array($out['withdraw_fields'])) {
$wf = array_replace($defaults['withdraw_fields'], array_intersect_key($out['withdraw_fields'], $defaults['withdraw_fields']));
foreach (array_keys($defaults['withdraw_fields']) as $fk) {
$wf[$fk] = !empty($wf[$fk]);
}
$out['withdraw_fields'] = $wf;
}
if (isset($out['channels']) && is_array($out['channels'])) {
$out['channels'] = DepositChannel::normalizeOverrides(array_values($out['channels']));
} else {
$out['channels'] = [];
}
usort($out['channels'], static function (array $a, array $b): int {
$sa = $a['sort'] ?? 0;
$sb = $b['sort'] ?? 0;
if ($sa !== $sb) {
return $sa <=> $sb;
}
return strcmp($a['code'] ?? '', $b['code'] ?? '');
});
unset($out['fx_pairs']);
return $out;
}
/**
* @param array<string, mixed> $row
*/
private static function ratioStringFromRow(array $row, string $key): string
{
if (!isset($row[$key])) {
return '';
}
if (is_string($row[$key])) {
return trim($row[$key]);
}
if (is_numeric($row[$key])) {
return strval($row[$key]);
}
return '';
}
private static function normalizeSort(mixed $v): int
{
$opt = filter_var($v, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0, 'max_range' => 99999]]);
if ($opt !== false) {
return $opt;
}
if (is_numeric($v)) {
$f = filter_var($v, FILTER_VALIDATE_FLOAT);
if ($f !== false) {
$rounded = round($f);
$opt2 = filter_var($rounded, FILTER_VALIDATE_INT, ['options' => ['min_range' => 0, 'max_range' => 99999]]);
if ($opt2 !== false) {
return $opt2;
}
}
}
return 0;
}
/**
* @param array<string, mixed> $p
*/
private static function validate(array $p): void
{
if (!isset($p['platform_coin']) || !is_array($p['platform_coin'])) {
throw new InvalidArgumentException('platform_coin 格式错误');
}
$lz = $p['platform_coin']['label_zh'] ?? '';
$le = $p['platform_coin']['label_en'] ?? '';
if (!is_string($lz) || trim($lz) === '' || !is_string($le) || trim($le) === '') {
throw new InvalidArgumentException('请填写平台币中英文名称');
}
if (!isset($p['currencies']) || !is_array($p['currencies']) || $p['currencies'] === []) {
throw new InvalidArgumentException('至少保留一条货币');
}
$seenCodes = [];
foreach ($p['currencies'] as $idx => $row) {
if (!is_array($row)) {
throw new InvalidArgumentException('货币列表第 ' . ($idx + 1) . ' 行格式错误');
}
$code = $row['code'] ?? '';
if (!is_string($code) || !preg_match('/^[A-Z0-9]{2,12}$/', $code)) {
throw new InvalidArgumentException('货币代码非法:' . (is_string($code) ? $code : ''));
}
if (isset($seenCodes[$code])) {
throw new InvalidArgumentException('货币代码不能重复:' . $code);
}
$seenCodes[$code] = true;
$dep = $row['deposit_coins_per_fiat'] ?? '';
$wdr = $row['withdraw_coins_per_fiat'] ?? '';
if (!is_string($dep) || $dep === '' || !is_numeric($dep) || bccomp($dep, '0', 8) <= 0) {
throw new InvalidArgumentException('第 ' . ($idx + 1) . ' 行:充值汇率须为大于 0 的数字');
}
if (!is_string($wdr) || $wdr === '' || !is_numeric($wdr) || bccomp($wdr, '0', 8) <= 0) {
throw new InvalidArgumentException('第 ' . ($idx + 1) . ' 行:提现汇率须为大于 0 的数字');
}
}
if (isset($p['withdraw_banks']) && is_array($p['withdraw_banks'])) {
$seen = [];
foreach ($p['withdraw_banks'] as $idx => $row) {
if (!is_array($row)) {
throw new InvalidArgumentException('银行第 ' . ($idx + 1) . ' 行格式错误');
}
$code = $row['code'] ?? '';
if (!is_string($code) || !preg_match('/^[a-z0-9][a-z0-9_\-]{0,31}$/', $code)) {
throw new InvalidArgumentException('银行代码非法');
}
if (isset($seen[$code])) {
throw new InvalidArgumentException('银行代码重复:' . $code);
}
$seen[$code] = true;
}
}
if (isset($p['withdraw_limits']) && is_array($p['withdraw_limits'])) {
foreach (['min_ewallet', 'min_bank'] as $k) {
$v = $p['withdraw_limits'][$k] ?? '0';
if (!is_string($v) || !is_numeric($v) || bccomp($v, '0', 4) < 0) {
throw new InvalidArgumentException('提现最低限额须为不小于 0 的数字');
}
}
}
$reg = DepositChannel::codeRegistry();
if ($reg !== [] && (!isset($p['channels']) || !is_array($p['channels']) || $p['channels'] === [])) {
throw new InvalidArgumentException('请配置充值渠道');
}
}
}