logService = $logService; } /** * 管理员登录 * * @param Request $request * @return JsonResponse * @throws ValidationException */ public function login(Request $request): JsonResponse { try { /** @var array{email: string, password: string} $validated */ $validated = $request->validate([ 'email' => 'required|email', 'password' => 'required|string', ]); // 添加請求日誌 Log::info('Login attempt details', [ 'email' => $validated['email'], 'request_data' => $request->all() ]); /** @var Admin|null $admin */ $admin = Admin::where('email', $validated['email'])->first(); // 添加用戶查詢日誌 Log::info('Admin query result', [ 'admin_found' => $admin ? 'yes' : 'no', 'admin_data' => $admin ? [ 'id' => $admin->id, 'email' => $admin->email, 'role' => $admin->role ] : null ]); if (!$admin || !Hash::check($validated['password'], $admin->password)) { // 添加密碼驗證日誌 Log::info('Password verification failed', [ 'has_admin' => $admin ? 'yes' : 'no', 'password_check' => $admin ? Hash::check($validated['password'], $admin->password) : 'admin not found' ]); return $this->error( ErrorCode::INVALID_CREDENTIALS, ErrorCode::getMessage(ErrorCode::INVALID_CREDENTIALS) ); } /** @var string $token */ $token = $admin->createToken('admin-token')->plainTextToken; $this->logService->logOperation( 'admin', $admin->id, 'Admin logged in' ); return $this->success([ 'token' => $token, 'admin' => [ 'id' => $admin->id, 'email' => $admin->email, ], ]); } catch (ValidationException $e) { Log::error('Validation error during login', [ 'errors' => $e->errors(), ]); return $this->error( ErrorCode::VALIDATION_ERROR, ErrorCode::getMessage(ErrorCode::VALIDATION_ERROR), $e->errors() ); } catch (\Exception $e) { Log::error('Error during admin login', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), 'file' => $e->getFile(), 'line' => $e->getLine() ]); return $this->error( ErrorCode::SERVER_ERROR, ErrorCode::getMessage(ErrorCode::SERVER_ERROR) ); } } /** * 管理员登出 * * @param Request $request * @return JsonResponse */ public function logout(Request $request): JsonResponse { try { /** @var Admin|null $admin */ $admin = $request->user(); if (!$admin) { return $this->error( ErrorCode::UNAUTHORIZED, '未登錄或會話已過期。' ); } $admin->currentAccessToken()->delete(); $this->logService->logOperation( 'admin', $admin->id, 'Admin logged out' ); return $this->success(null, '已成功登出。'); } catch (\Exception $e) { Log::error('Error during admin logout', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), 'admin_id' => $request->user()?->id, ]); return $this->error( ErrorCode::SERVER_ERROR, ErrorCode::getMessage(ErrorCode::SERVER_ERROR) ); } } /** * 修改管理员密码 * * @param Request $request * @return JsonResponse * @throws ValidationException */ public function changePassword(Request $request): JsonResponse { try { /** @var Admin|null $admin */ $admin = $request->user(); if (!$admin) { return $this->error( ErrorCode::UNAUTHORIZED, '未登錄或會話已過期。' ); } /** @var array{current_password: string, new_password: string} $validated */ $validated = $request->validate([ 'current_password' => 'required|string', 'new_password' => 'required|string|min:8|confirmed', ]); if (!Hash::check($validated['current_password'], $admin->password)) { return $this->error( ErrorCode::INVALID_CREDENTIALS, '当前密码错误。' ); } $admin->password = Hash::make($validated['new_password']); $admin->save(); $this->logService->logOperation( 'admin', $admin->id, 'Admin changed password' ); return $this->success(null, '密码修改成功。'); } catch (ValidationException $e) { return $this->error( ErrorCode::VALIDATION_ERROR, ErrorCode::getMessage(ErrorCode::VALIDATION_ERROR), $e->errors() ); } catch (\Exception $e) { Log::error('Error changing admin password', [ 'error' => $e->getMessage(), 'trace' => $e->getTraceAsString(), 'admin_id' => $request->user()?->id, ]); return $this->error( ErrorCode::SERVER_ERROR, ErrorCode::getMessage(ErrorCode::SERVER_ERROR) ); } } }