Files
webman-buildadmin-mall/app/common/library/MallPlayxRatios.php

59 lines
1.5 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\common\library;
use app\common\model\MallConfig;
/**
* PlayX 与积分商城相关的比例配置:优先读 mall_config无表记录时回退 .env
*/
final class MallPlayxRatios
{
private static ?array $cache = null;
/**
* @return array{return_ratio: float, unlock_ratio: float, points_to_cash_ratio: float}
*/
public static function get(): array
{
if (self::$cache !== null) {
return self::$cache;
}
$row = MallConfig::order('id', 'asc')->find();
if ($row) {
self::$cache = [
'return_ratio' => self::toFloat($row->return_ratio),
'unlock_ratio' => self::toFloat($row->unlock_ratio),
'points_to_cash_ratio' => self::toFloat($row->points_to_cash_ratio),
];
return self::$cache;
}
self::$cache = [
'return_ratio' => floatval(env('PLAYX_RETURN_RATIO', '0.1')),
'unlock_ratio' => floatval(env('PLAYX_UNLOCK_RATIO', '0.1')),
'points_to_cash_ratio' => floatval(env('PLAYX_POINTS_TO_CASH_RATIO', '0.1')),
];
return self::$cache;
}
public static function forget(): void
{
self::$cache = null;
}
private static function toFloat(mixed $v): float
{
if (is_numeric($v)) {
return floatval($v);
}
return 0.0;
}
}