187 lines
7.4 KiB
PHP
187 lines
7.4 KiB
PHP
<?php
|
||
|
||
/**
|
||
* 文件功能:聊天室入场/离场播报服务
|
||
* 负责构建进出播报文本与颜色,按优先级(职务 > VIP > 普通随机词)选择合适的播报样式。
|
||
*
|
||
* @author ChatRoom Laravel
|
||
*
|
||
* @version 1.0.0
|
||
*/
|
||
|
||
namespace App\Services;
|
||
|
||
use App\Models\User;
|
||
|
||
class RoomBroadcastService
|
||
{
|
||
/**
|
||
* 构造函数注入会员进退场主题服务。
|
||
*/
|
||
public function __construct(
|
||
private readonly VipPresenceService $vipPresenceService,
|
||
) {}
|
||
|
||
/**
|
||
* 构建入场播报,返回 [文本, 颜色]
|
||
* 优先级:有职务 > 有 VIP(专属模板优先)> 普通随机词
|
||
*
|
||
* @return array{string, string}
|
||
*/
|
||
public function buildEntryBroadcast(User $user): array
|
||
{
|
||
$position = $user->activePosition?->position;
|
||
|
||
// 有职务:显示职务图标 + 随机入场词
|
||
if ($position) {
|
||
$icon = $position->icon ?? '🎖️';
|
||
$name = $position->name;
|
||
$text = '【'.$icon.' '.$name.'】'.$this->randomWelcomeMsg($user);
|
||
|
||
return [$text, '#7c3aed']; // 紫色
|
||
}
|
||
|
||
// 有 VIP:优先用专属进入模板,无模板则随机词加前缀
|
||
if ($user->isVip() && $user->vipLevel) {
|
||
$theme = $this->vipPresenceService->buildJoinTheme($user);
|
||
$color = $theme['color'] ?: '#f59e0b';
|
||
$template = $theme['text'];
|
||
|
||
if ($template) {
|
||
return [(string) $template, $color];
|
||
}
|
||
|
||
$text = '【'.$user->vipIcon().' '.$user->vipName().'】'.$this->randomWelcomeMsg($user);
|
||
|
||
return [$text, $color];
|
||
}
|
||
|
||
// 普通用户:绿色随机词
|
||
return [$this->randomWelcomeMsg($user), '#16a34a'];
|
||
}
|
||
|
||
/**
|
||
* 构建离场播报,返回 [文本, 颜色]
|
||
* 优先级:有职务 > 有 VIP(专属模板优先)> 普通随机词
|
||
*
|
||
* @return array{string, string}
|
||
*/
|
||
public function buildLeaveBroadcast(User $user): array
|
||
{
|
||
$position = $user->activePosition?->position;
|
||
|
||
// 有职务:显示职务图标 + 随机离场词
|
||
if ($position) {
|
||
$icon = $position->icon ?? '🎖️';
|
||
$name = $position->name;
|
||
$text = '【'.$icon.' '.$name.'】'.$this->randomLeaveMsg($user);
|
||
|
||
return [$text, '#7c3aed']; // 紫色
|
||
}
|
||
|
||
// 有 VIP:优先用专属离场模板,无模板则随机词加前缀
|
||
if ($user->isVip() && $user->vipLevel) {
|
||
$theme = $this->vipPresenceService->buildLeaveTheme($user);
|
||
$color = $theme['color'] ?: '#f59e0b';
|
||
$template = $theme['text'];
|
||
|
||
if ($template) {
|
||
return [(string) $template, $color];
|
||
}
|
||
|
||
$text = '【'.$user->vipIcon().' '.$user->vipName().'】'.$this->randomLeaveMsg($user);
|
||
|
||
return [$text, $color];
|
||
}
|
||
|
||
// 普通用户:橙色随机词
|
||
return [$this->randomLeaveMsg($user), '#cc6600'];
|
||
}
|
||
|
||
/**
|
||
* 生成随机趣味入场词
|
||
*/
|
||
public function randomWelcomeMsg(User $user): string
|
||
{
|
||
$gender = $user->sex == 2 ? '美女' : '帅哥';
|
||
$uname = $user->username;
|
||
|
||
$templates = [
|
||
$gender.'【'.$uname.'】开着刚买不久的车来到了,见到各位大虾,拱手曰:"众位大虾,小生有礼了"',
|
||
$gender.'【'.$uname.'】骑着小毛驴哼着小调,悠闲地走了进来,对大家嘿嘿一笑',
|
||
$gender.'【'.$uname.'】坐着豪华轿车缓缓驶入,推门而出,拍了拍身上的灰,霸气说道:"我来也!"',
|
||
$gender.'【'.$uname.'】踩着七彩祥云从天而降,众人皆惊,抱拳道:"各位久等了!"',
|
||
$gender.'【'.$uname.'】划着小船飘然而至,微微一笑,翩然上岸',
|
||
$gender.'【'.$uname.'】骑着自行车铃铛叮当响,远远就喊:"我来啦!想我没?"',
|
||
$gender.'【'.$uname.'】开着拖拉机突突突地开了进来,下车后拍了拍手说:"交通不便,来迟了!"',
|
||
$gender.'【'.$uname.'】坐着火箭嗖的一声到了,吓了大家一跳,嘿嘿笑道:"别怕别怕,是我啊"',
|
||
$gender.'【'.$uname.'】骑着白马翩翩而来,英姿飒爽,拱手道:"江湖路远,各位有礼了"',
|
||
$gender.'【'.$uname.'】开着宝马一路飞驰到此,推开车门走了下来,向大家挥了挥手',
|
||
$gender.'【'.$uname.'】踩着风火轮呼啸而至,在人群中潇洒亮相',
|
||
$gender.'【'.$uname.'】乘坐滑翔伞从天空缓缓降落,对大家喊道:"hello,我从天上来!"',
|
||
$gender.'【'.$uname.'】从地下钻了出来,拍了拍土,说:"哎呀,走错路了,不过总算到了"',
|
||
$gender.'【'.$uname.'】蹦蹦跳跳地跑了进来,嘻嘻哈哈地跟大家打招呼',
|
||
$gender.'【'.$uname.'】悄悄地溜了进来,生怕被人发现,东张西望了一番',
|
||
$gender.'【'.$uname.'】迈着六亲不认的步伐走进来,气场两米八',
|
||
];
|
||
|
||
return $templates[array_rand($templates)];
|
||
}
|
||
|
||
/**
|
||
* 生成随机趣味离场词
|
||
*/
|
||
public function randomLeaveMsg(User $user): string
|
||
{
|
||
$gender = $user->sex == 2 ? '美女' : '帅哥';
|
||
$uname = $user->username;
|
||
|
||
$templates = [
|
||
$gender.'【'.$uname.'】潇洒地挥了挥手,骑着小毛驴哼着小调离去了',
|
||
$gender.'【'.$uname.'】开着跑车扬长而去,留下一路烟尘',
|
||
$gender.'【'.$uname.'】踩着七彩祥云飘然远去,消失在天际',
|
||
$gender.'【'.$uname.'】悄无声息地溜走了,连个招呼都不打',
|
||
$gender.'【'.$uname.'】跳上直升机螺旋桨呼呼作响,朝大家喊道:"我先走啦!"',
|
||
$gender.'【'.$uname.'】拱手告别:"各位大虾,后会有期!"随后翩然离去',
|
||
$gender.'【'.$uname.'】骑着自行车铃铛叮当响,远远就喊:"下次再聊!拜拜!"',
|
||
$gender.'【'.$uname.'】坐着热气球缓缓升空,朝大家挥手告别',
|
||
$gender.'【'.$uname.'】迈着六亲不认的步伐离开了,留下一众人目瞪口呆',
|
||
$gender.'【'.$uname.'】化作一缕青烟消散在空气中……',
|
||
];
|
||
|
||
return $templates[array_rand($templates)];
|
||
}
|
||
|
||
/**
|
||
* 构建会员进退场横幅与特效的前端载荷。
|
||
*
|
||
* @param string $type join|leave
|
||
* @return array<string, string|null>
|
||
*/
|
||
public function buildVipPresencePayload(User $user, string $type): array
|
||
{
|
||
$theme = $type === 'join'
|
||
? $this->vipPresenceService->buildJoinTheme($user)
|
||
: $this->vipPresenceService->buildLeaveTheme($user);
|
||
|
||
if (empty($theme['enabled'])) {
|
||
return [];
|
||
}
|
||
|
||
$text = trim((string) ($theme['text'] ?? ''));
|
||
if ($text === '') {
|
||
return [];
|
||
}
|
||
|
||
return [
|
||
'presence_type' => $type,
|
||
'presence_text' => $text,
|
||
'presence_color' => (string) ($theme['color'] ?? ''),
|
||
'presence_effect' => $theme['effect'] ? (string) $theme['effect'] : null,
|
||
'presence_banner_style' => (string) ($theme['banner_style'] ?? ''),
|
||
'presence_level_name' => (string) ($theme['level_name'] ?? ''),
|
||
'presence_icon' => (string) ($theme['icon'] ?? ''),
|
||
];
|
||
}
|
||
}
|