Files
chatroom/app/Models/VipLevel.php
lkddi fd3214eaff 功能:VIP 赞助会员系统
- 新建 vip_levels 表(名称、图标、颜色、经验/金币倍率、专属进入/离开模板)
- 默认4个等级种子:白银🥈(×1.5)、黄金🥇(×2.0)、钻石💎(×3.0)、至尊👑(×5.0)
- 后台 VIP 等级 CRUD 管理(新增/编辑/删除,配置模板和倍率)
- 后台用户编辑弹窗支持设置 VIP 等级和到期时间
- ChatController 心跳经验按 VIP 倍率加成
- FishingController 正向奖励按 VIP 倍率加成(负面惩罚不变)
- 在线名单显示 VIP 图标和管理员🛡️标识
- VIP 用户进入/离开使用专属颜色和标题
- 后台侧栏新增「👑 VIP 会员等级」入口
2026-02-26 21:30:07 +08:00

99 lines
2.3 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
/**
* 文件功能VIP 会员等级模型
* 存储会员名称、图标、颜色、倍率、专属进入/离开模板
* 后台可完整 CRUD 管理
*
* @author ChatRoom Laravel
*
* @version 1.0.0
*/
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class VipLevel extends Model
{
/** @var string 表名 */
protected $table = 'vip_levels';
/** @var array 可批量赋值字段 */
protected $fillable = [
'name',
'icon',
'color',
'exp_multiplier',
'jjb_multiplier',
'join_templates',
'leave_templates',
'sort_order',
'price',
'duration_days',
];
/** @var array 类型转换 */
protected $casts = [
'exp_multiplier' => 'float',
'jjb_multiplier' => 'float',
'sort_order' => 'integer',
'price' => 'integer',
'duration_days' => 'integer',
];
/**
* 关联:该等级下的所有用户
*/
public function users(): HasMany
{
return $this->hasMany(User::class, 'vip_level_id');
}
/**
* 获取进入聊天室的专属欢迎语模板数组
*/
public function getJoinTemplatesArrayAttribute(): array
{
if (empty($this->join_templates)) {
return [];
}
$decoded = json_decode($this->join_templates, true);
return is_array($decoded) ? $decoded : [];
}
/**
* 获取离开聊天室的专属提示语模板数组
*/
public function getLeaveTemplatesArrayAttribute(): array
{
if (empty($this->leave_templates)) {
return [];
}
$decoded = json_decode($this->leave_templates, true);
return is_array($decoded) ? $decoded : [];
}
/**
* 从模板数组中随机选一条,替换 {username} 占位符
*
* @param array $templates 模板数组
* @param string $username 用户名
*/
public static function renderTemplate(array $templates, string $username): ?string
{
if (empty($templates)) {
return null;
}
$template = $templates[array_rand($templates)];
return str_replace('{username}', $username, $template);
}
}