Files
chatroom/app/Services/VipService.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

109 lines
2.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
/**
* 文件功能VIP 会员服务层
* 提供倍率计算、会员授予/撤销、模板渲染等核心逻辑
*
* @author ChatRoom Laravel
*
* @version 1.0.0
*/
namespace App\Services;
use App\Models\User;
use App\Models\VipLevel;
class VipService
{
/**
* 获取用户的经验倍率(非 VIP 返回 1.0
*/
public function getExpMultiplier(User $user): float
{
if (! $user->isVip()) {
return 1.0;
}
return $user->vipLevel?->exp_multiplier ?? 1.0;
}
/**
* 获取用户的金币倍率(非 VIP 返回 1.0
*/
public function getJjbMultiplier(User $user): float
{
if (! $user->isVip()) {
return 1.0;
}
return $user->vipLevel?->jjb_multiplier ?? 1.0;
}
/**
* 授予用户 VIP 等级
*
* @param User $user 目标用户
* @param int $vipLevelId VIP 等级 ID
* @param int $days 天数0=永久)
*/
public function grantVip(User $user, int $vipLevelId, int $days = 30): void
{
$user->vip_level_id = $vipLevelId;
if ($days > 0) {
// 如果用户已有未过期的会员,在现有到期时间上延长
$baseTime = ($user->hy_time && $user->hy_time->isFuture())
? $user->hy_time
: now();
$user->hy_time = $baseTime->addDays($days);
} else {
// 永久会员
$user->hy_time = null;
}
$user->save();
}
/**
* 撤销用户 VIP 等级
*/
public function revokeVip(User $user): void
{
$user->vip_level_id = null;
$user->hy_time = null;
$user->save();
}
/**
* 获取用户专属进入聊天室的欢迎语(非 VIP 返回 null
*
* @param User $user 用户
* @return string|null 渲染后的欢迎语
*/
public function getJoinMessage(User $user): ?string
{
if (! $user->isVip() || ! $user->vipLevel) {
return null;
}
$templates = $user->vipLevel->join_templates_array;
return VipLevel::renderTemplate($templates, $user->username);
}
/**
* 获取用户专属离开聊天室的提示语(非 VIP 返回 null
*/
public function getLeaveMessage(User $user): ?string
{
if (! $user->isVip() || ! $user->vipLevel) {
return null;
}
$templates = $user->vipLevel->leave_templates_array;
return VipLevel::renderTemplate($templates, $user->username);
}
}