77 lines
2.2 KiB
PHP
77 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace app\service;
|
|
|
|
use app\model\AiModel;
|
|
use think\exception\HttpResponseException;
|
|
|
|
class GuestAccessService
|
|
{
|
|
public const MODEL_NAME = 'qwen3.6';
|
|
|
|
public static function isGuest(array $user): bool
|
|
{
|
|
return !empty($user['is_guest']);
|
|
}
|
|
|
|
public static function model(): AiModel
|
|
{
|
|
$models = AiModel::where('enabled', 1)->order('sort_order')->order('id')->select();
|
|
foreach ($models as $model) {
|
|
$identity = strtolower(trim((string) ($model->model_id ?: $model->name)));
|
|
$name = strtolower(trim((string) $model->name));
|
|
if (str_starts_with($identity, self::MODEL_NAME) || str_starts_with($name, self::MODEL_NAME)) {
|
|
return $model;
|
|
}
|
|
}
|
|
|
|
self::abort('游客专用模型 qwen3.6 尚未启用,请联系管理员', 503);
|
|
}
|
|
|
|
public static function modelId(): int
|
|
{
|
|
return (int) self::model()->id;
|
|
}
|
|
|
|
public static function assertModelAllowed(array $user, ?int $modelId): int
|
|
{
|
|
if (!self::isGuest($user)) {
|
|
return $modelId ?: 0;
|
|
}
|
|
|
|
$guestModelId = self::modelId();
|
|
if ($modelId && $modelId !== $guestModelId) {
|
|
self::abort('游客仅可使用 qwen3.6 模型,登录后可使用全部模型', 403);
|
|
}
|
|
|
|
return $guestModelId;
|
|
}
|
|
|
|
public static function assertTextChatOnly(array $user, array $attachments, string $agentId, string $imageTool, bool $voiceMode): void
|
|
{
|
|
if (!self::isGuest($user)) {
|
|
return;
|
|
}
|
|
|
|
if ($attachments || $agentId !== '' || $imageTool !== '' || $voiceMode) {
|
|
self::abort('游客仅支持 qwen3.6 文本对话,登录后可使用全部模型和工具', 403);
|
|
}
|
|
}
|
|
|
|
public static function assertAccountRequired(array $user, string $feature): void
|
|
{
|
|
if (self::isGuest($user)) {
|
|
self::abort("游客不能使用{$feature},请先登录", 403);
|
|
}
|
|
}
|
|
|
|
private static function abort(string $message, int $httpCode): never
|
|
{
|
|
throw new HttpResponseException(json([
|
|
'code' => 1,
|
|
'message' => $message,
|
|
'data' => null,
|
|
], $httpCode));
|
|
}
|
|
}
|