llmbackend/app/Http/Controllers/Api/Admin/ClientController.php
Jethro Lin c44c25d86f 4. [API 详细说明](#4-api-详细说明)
- [4.1 认证 API](#41-认证-api)
     - [4.1.1 获取访问令牌](#411-获取访问令牌)
   - [4.2 客户用户 API](#42-客户用户-api)
     - [4.2.1 发送提示词请求](#421-发送提示词请求)
   - [4.3 管理员 API](#43-管理员-api)
     - [4.3.1 LLM 提供商管理](#431-llm-提供商管理)
       - [4.3.1.1 新增 LLM 提供商](#4311-新增-llm-提供商)
       - [4.3.1.2 修改 LLM 提供商](#4312-修改-llm-提供商)
       - [4.3.1.3 删除 LLM 提供商](#4313-删除-llm-提供商)
       - [4.3.1.4 获取 LLM 提供商列表](#4314-获取-llm-提供商列表)
2024-12-04 12:01:56 +08:00

202 lines
6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Controllers\Api\Admin;
use App\Http\Controllers\Controller;
use App\Models\Client;
use App\Services\Auth\TokenService;
use App\Services\LogService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Validation\ValidationException;
class ClientController extends Controller
{
public function __construct(
private readonly LogService $logService,
private readonly TokenService $tokenService
) {}
public function index(Request $request): JsonResponse
{
$admin = $request->admin;
$query = $admin->isSuperAdmin() ? Client::query() : $admin->clients();
$clients = $query->with('llmProvider')->get();
return response()->json($clients);
}
public function store(Request $request): JsonResponse
{
try {
$validated = $request->validate([
'name' => 'required|string|max:100',
'llm_provider_id' => 'required|exists:llm_providers,id',
]);
DB::beginTransaction();
$client = Client::create($validated);
// Associate client with admin
$request->admin->clients()->attach($client->id);
$this->logService->logOperation(
'admin',
$request->admin->id,
"Created client: {$client->name}"
);
DB::commit();
return response()->json($client, 201);
} catch (ValidationException $e) {
DB::rollBack();
return response()->json([
'error' => 'validation_error',
'message' => '请求参数验证失败。',
'errors' => $e->errors(),
], 422);
} catch (\Exception $e) {
DB::rollBack();
Log::error('Error creating client', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
return response()->json([
'error' => 'server_error',
'message' => '服务器内部错误。',
], 500);
}
}
public function update(Request $request, int $id): JsonResponse
{
try {
$client = $this->getAuthorizedClient($request->admin, $id);
$validated = $request->validate([
'name' => 'required|string|max:100',
'llm_provider_id' => 'required|exists:llm_providers,id',
]);
$client->update($validated);
$this->logService->logOperation(
'admin',
$request->admin->id,
"Updated client: {$client->name}"
);
return response()->json($client);
} catch (ValidationException $e) {
return response()->json([
'error' => 'validation_error',
'message' => '请求参数验证失败。',
'errors' => $e->errors(),
], 422);
} catch (\Exception $e) {
Log::error('Error updating client', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
return response()->json([
'error' => 'server_error',
'message' => '服务器内部错误。',
], 500);
}
}
public function destroy(Request $request, int $id): JsonResponse
{
try {
$client = $this->getAuthorizedClient($request->admin, $id);
$clientName = $client->name;
$client->delete();
$this->logService->logOperation(
'admin',
$request->admin->id,
"Deleted client: {$clientName}"
);
return response()->json([
'message' => '客户用户已删除。',
]);
} catch (\Exception $e) {
Log::error('Error deleting client', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
return response()->json([
'error' => 'server_error',
'message' => '服务器内部错误。',
], 500);
}
}
public function generateAuthToken(Request $request, int $id): JsonResponse
{
try {
$client = $this->getAuthorizedClient($request->admin, $id);
$validated = $request->validate([
'expires_in_days' => 'nullable|integer|min:1',
]);
$token = $this->tokenService->generateAuthToken(
$client,
$validated['expires_in_days'] ?? null
);
$this->logService->logOperation(
'admin',
$request->admin->id,
"Generated auth token for client: {$client->name}"
);
return response()->json([
'client_id' => $client->id,
'auth_token' => $token->token,
'created_at' => $token->created_at,
'expires_at' => $token->expires_at,
]);
} catch (ValidationException $e) {
return response()->json([
'error' => 'validation_error',
'message' => '请求参数验证失败。',
'errors' => $e->errors(),
], 422);
} catch (\Exception $e) {
Log::error('Error generating auth token', [
'error' => $e->getMessage(),
'trace' => $e->getTraceAsString(),
]);
return response()->json([
'error' => 'server_error',
'message' => '服务器内部错误。',
], 500);
}
}
private function getAuthorizedClient($admin, int $id): Client
{
$query = $admin->isSuperAdmin() ? Client::query() : $admin->clients();
$client = $query->findOrFail($id);
return $client;
}
}