'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); } }