client response
This commit is contained in:
parent
74790f6180
commit
e1119a9426
1 changed files with 14 additions and 69 deletions
|
|
@ -38,17 +38,12 @@ public function __construct(
|
|||
public function index(): JsonResponse
|
||||
{
|
||||
try {
|
||||
$clients = Client::with('llmProvider:id,name,service_name')
|
||||
->select([
|
||||
'id',
|
||||
'name',
|
||||
'llm_provider_id',
|
||||
'rate_limit',
|
||||
'timeout',
|
||||
'status',
|
||||
'created_at',
|
||||
])
|
||||
->get();
|
||||
$clients = Client::select([
|
||||
'id',
|
||||
'name',
|
||||
'llm_provider_id',
|
||||
'created_at',
|
||||
])->get();
|
||||
|
||||
return $this->success($clients);
|
||||
|
||||
|
|
@ -83,21 +78,12 @@ public function store(Request $request): JsonResponse
|
|||
'integer',
|
||||
'exists:llm_providers,id',
|
||||
],
|
||||
'rate_limit' => [
|
||||
'required',
|
||||
'integer',
|
||||
'min:1',
|
||||
'max:1000',
|
||||
],
|
||||
'timeout' => [
|
||||
'required',
|
||||
'integer',
|
||||
'min:1',
|
||||
'max:300',
|
||||
],
|
||||
]);
|
||||
|
||||
$validated['status'] = Client::STATUS_ACTIVE;
|
||||
$validated['rate_limit'] = config('llm.default_rate_limit', 60); // 默認每分鐘 60 次
|
||||
$validated['timeout'] = config('llm.default_timeout', 30); // 默認 30 秒
|
||||
|
||||
$client = Client::create($validated);
|
||||
|
||||
// 加載 LLM 提供商信息
|
||||
|
|
@ -113,14 +99,6 @@ public function store(Request $request): JsonResponse
|
|||
'id' => $client->id,
|
||||
'name' => $client->name,
|
||||
'llm_provider_id' => $client->llm_provider_id,
|
||||
'llm_provider' => [
|
||||
'id' => $client->llmProvider->id,
|
||||
'name' => $client->llmProvider->name,
|
||||
'service_name' => $client->llmProvider->service_name,
|
||||
],
|
||||
'rate_limit' => $client->rate_limit,
|
||||
'timeout' => $client->timeout,
|
||||
'status' => $client->status,
|
||||
'created_at' => $client->created_at->toIso8601String(),
|
||||
]);
|
||||
|
||||
|
|
@ -171,36 +149,8 @@ public function update(Request $request, int $id): JsonResponse
|
|||
'integer',
|
||||
'exists:llm_providers,id',
|
||||
],
|
||||
'rate_limit' => [
|
||||
'required',
|
||||
'integer',
|
||||
'min:1',
|
||||
'max:1000',
|
||||
],
|
||||
'timeout' => [
|
||||
'required',
|
||||
'integer',
|
||||
'min:1',
|
||||
'max:300',
|
||||
],
|
||||
'status' => [
|
||||
'required',
|
||||
'string',
|
||||
'in:active,inactive',
|
||||
],
|
||||
]);
|
||||
|
||||
// 如果要停用客戶,檢查是否有活躍的令牌
|
||||
if ($validated['status'] === Client::STATUS_INACTIVE &&
|
||||
$client->status === Client::STATUS_ACTIVE) {
|
||||
if ($this->hasActiveTokens($client)) {
|
||||
return $this->error(
|
||||
ErrorCode::CLIENT_HAS_ACTIVE_TOKENS,
|
||||
'該客戶有活躍的認證令牌,無法停用。'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
$client->update($validated);
|
||||
|
||||
// 加載 LLM 提供商信息
|
||||
|
|
@ -216,14 +166,6 @@ public function update(Request $request, int $id): JsonResponse
|
|||
'id' => $client->id,
|
||||
'name' => $client->name,
|
||||
'llm_provider_id' => $client->llm_provider_id,
|
||||
'llm_provider' => [
|
||||
'id' => $client->llmProvider->id,
|
||||
'name' => $client->llmProvider->name,
|
||||
'service_name' => $client->llmProvider->service_name,
|
||||
],
|
||||
'rate_limit' => $client->rate_limit,
|
||||
'timeout' => $client->timeout,
|
||||
'status' => $client->status,
|
||||
'updated_at' => $client->updated_at->toIso8601String(),
|
||||
]);
|
||||
|
||||
|
|
@ -315,7 +257,7 @@ public function generateAuthToken(int $id): JsonResponse
|
|||
if (!$this->admin->canManageClient($client->id)) {
|
||||
return $this->error(
|
||||
ErrorCode::FORBIDDEN,
|
||||
'您無權管理該客戶。'
|
||||
'您無<EFBFBD><EFBFBD>管理該客戶。'
|
||||
);
|
||||
}
|
||||
|
||||
|
|
@ -334,7 +276,10 @@ public function generateAuthToken(int $id): JsonResponse
|
|||
"Generated auth token for client: {$client->name}"
|
||||
);
|
||||
|
||||
return $this->success($result);
|
||||
return $this->success([
|
||||
'auth_token' => $result['token'],
|
||||
'expires_at' => $result['expires_at'],
|
||||
]);
|
||||
|
||||
} catch (ModelNotFoundException $e) {
|
||||
return $this->error(
|
||||
|
|
|
|||
Loading…
Reference in a new issue