90 lines
2.2 KiB
PHP
90 lines
2.2 KiB
PHP
<?php
|
|
|
|
/**
|
|
* 文件功能:系统参数模型
|
|
* 对应原版 ASP 聊天室的 sysparam 配置表
|
|
* 管理员可在后台修改等级经验阈值等系统参数
|
|
*
|
|
* @author ChatRoom Laravel
|
|
*
|
|
* @version 1.0.0
|
|
*/
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Facades\Cache;
|
|
|
|
class Sysparam extends Model
|
|
{
|
|
/** @var string 表名 */
|
|
protected $table = 'sysparam';
|
|
|
|
/** @var array 可批量赋值的字段 */
|
|
protected $fillable = ['alias', 'body', 'guidetxt'];
|
|
|
|
/**
|
|
* 获取指定参数的值
|
|
* 带缓存,避免频繁查库
|
|
*
|
|
* @param string $alias 参数别名
|
|
* @param string $default 默认值
|
|
*/
|
|
public static function getValue(string $alias, string $default = ''): string
|
|
{
|
|
return Cache::remember("sysparam:{$alias}", 300, function () use ($alias, $default) {
|
|
$param = static::where('alias', $alias)->first();
|
|
|
|
return $param ? ($param->body ?? $default) : $default;
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 获取等级经验阈值数组
|
|
* 返回格式:[0 => 10, 1 => 50, 2 => 150, ...] 索引即为目标等级-1
|
|
*
|
|
* @return array<int, int>
|
|
*/
|
|
public static function getLevelExpThresholds(): array
|
|
{
|
|
$str = static::getValue('levelexp', '10,50,150,400,800,1500,3000,5000,8000,12000,18000,25000,35000,50000,80000');
|
|
|
|
return array_map('intval', explode(',', $str));
|
|
}
|
|
|
|
/**
|
|
* 根据经验值计算应该达到的等级
|
|
*
|
|
* @param int $expNum 当前经验值
|
|
* @return int 对应的等级
|
|
*/
|
|
public static function calculateLevel(int $expNum): int
|
|
{
|
|
$thresholds = static::getLevelExpThresholds();
|
|
$level = 0;
|
|
|
|
foreach ($thresholds as $threshold) {
|
|
if ($expNum >= $threshold) {
|
|
$level++;
|
|
} else {
|
|
break;
|
|
}
|
|
}
|
|
|
|
// 不超过最大等级
|
|
$maxLevel = (int) static::getValue('maxlevel', '99');
|
|
|
|
return min($level, $maxLevel);
|
|
}
|
|
|
|
/**
|
|
* 清除指定参数的缓存
|
|
*
|
|
* @param string $alias 参数别名
|
|
*/
|
|
public static function clearCache(string $alias): void
|
|
{
|
|
Cache::forget("sysparam:{$alias}");
|
|
}
|
|
}
|