|
|
@@ -19,25 +19,71 @@ class TextbookApiService
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取教材系列列表
|
|
|
+ * 通用HTTP请求方法 - 减少重复代码,无缓存
|
|
|
*/
|
|
|
- public function getTextbookSeries(array $params = []): array
|
|
|
+ protected function request(string $method, string $endpoint, array $data = []): array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->get($this->baseUrl . '/textbooks/series', $params);
|
|
|
+ $httpMethod = strtolower($method);
|
|
|
+ $response = match($httpMethod) {
|
|
|
+ 'get' => Http::timeout(30)->get($this->baseUrl . $endpoint, $data),
|
|
|
+ 'post' => Http::timeout(300)->post($this->baseUrl . $endpoint, $data),
|
|
|
+ 'put' => Http::timeout(30)->put($this->baseUrl . $endpoint, $data),
|
|
|
+ 'delete' => Http::timeout(30)->delete($this->baseUrl . $endpoint, $data),
|
|
|
+ default => throw new \InvalidArgumentException("Unsupported HTTP method: {$method}")
|
|
|
+ };
|
|
|
+
|
|
|
+ // 处理文件上传
|
|
|
+ if (isset($data['file']) && $data['file'] instanceof \Illuminate\Http\UploadedFile) {
|
|
|
+ $response = Http::timeout(300)
|
|
|
+ ->attach('file', file_get_contents($data['file']->getPathname()), $data['file']->getClientOriginalName())
|
|
|
+ ->{$httpMethod}($this->baseUrl . $endpoint, $data);
|
|
|
+ }
|
|
|
|
|
|
if ($response->successful()) {
|
|
|
return $response->json();
|
|
|
}
|
|
|
|
|
|
- Log::error('Failed to fetch textbook series', [
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
+ // 记录错误并抛出异常
|
|
|
+ $error = $this->handleErrorResponse($response, $endpoint);
|
|
|
+ throw new \Exception($error);
|
|
|
+
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error("API request failed: {$method} {$endpoint}", [
|
|
|
+ 'error' => $e->getMessage(),
|
|
|
+ 'data' => $data
|
|
|
]);
|
|
|
+ throw $e;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- return ['data' => [], 'meta' => []];
|
|
|
+ /**
|
|
|
+ * 处理错误响应
|
|
|
+ */
|
|
|
+ private function handleErrorResponse($response, string $endpoint): string
|
|
|
+ {
|
|
|
+ $status = $response->status();
|
|
|
+ $body = $response->body();
|
|
|
+
|
|
|
+ // 常见错误类型的友好提示
|
|
|
+ return match($status) {
|
|
|
+ 404 => "未找到资源: {$endpoint}",
|
|
|
+ 422 => "数据验证失败: {$body}",
|
|
|
+ 500 => "服务器内部错误: {$body}",
|
|
|
+ default => "HTTP {$status}: {$body}"
|
|
|
+ };
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取教材系列列表
|
|
|
+ */
|
|
|
+ public function getTextbookSeries(array $params = []): array
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ return $this->request('GET', '/textbooks/series', $params);
|
|
|
} catch (\Exception $e) {
|
|
|
- Log::error('Error fetching textbook series', ['error' => $e->getMessage()]);
|
|
|
+ // 失败时返回空数据而不是抛出异常,保持向后兼容
|
|
|
+ Log::warning('Failed to fetch textbook series, returning empty result', ['error' => $e->getMessage()]);
|
|
|
return ['data' => [], 'meta' => []];
|
|
|
}
|
|
|
}
|
|
|
@@ -48,20 +94,13 @@ class TextbookApiService
|
|
|
public function getTextbookSeriesById(int $seriesId): ?array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->get($this->baseUrl . "/textbooks/series/{$seriesId}");
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json('data');
|
|
|
- }
|
|
|
-
|
|
|
- Log::warning('Series not found', [
|
|
|
+ $result = $this->request('GET', "/textbooks/series/{$seriesId}");
|
|
|
+ return $result['data'] ?? null;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::warning('Series not found or error occurred', [
|
|
|
'series_id' => $seriesId,
|
|
|
- 'status' => $response->status(),
|
|
|
+ 'error' => $e->getMessage()
|
|
|
]);
|
|
|
-
|
|
|
- return null;
|
|
|
- } catch (\Exception $e) {
|
|
|
- Log::error('Error fetching textbook series', ['error' => $e->getMessage(), 'series_id' => $seriesId]);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
@@ -72,23 +111,13 @@ class TextbookApiService
|
|
|
public function createTextbookSeries(array $data): array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->post($this->baseUrl . '/textbooks/series', $data);
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json();
|
|
|
- }
|
|
|
-
|
|
|
- $responseBody = $response->body();
|
|
|
- $status = $response->status();
|
|
|
-
|
|
|
- Log::error('Failed to create textbook series', [
|
|
|
- 'status' => $status,
|
|
|
- 'body' => $responseBody
|
|
|
- ]);
|
|
|
-
|
|
|
- throw new \Exception('Failed to create textbook series: ' . $responseBody);
|
|
|
+ return $this->request('POST', '/textbooks/series', $data);
|
|
|
} catch (\Exception $e) {
|
|
|
- Log::error('Error creating textbook series', ['error' => $e->getMessage()]);
|
|
|
+ // 检查是否是series不存在的错误(虽然这里不太可能,但保持向后兼容)
|
|
|
+ if (strpos($e->getMessage(), 'Series not found') !== false) {
|
|
|
+ $seriesId = $data['id'] ?? 'unknown';
|
|
|
+ throw new \Exception("系列ID {$seriesId} 不存在");
|
|
|
+ }
|
|
|
throw $e;
|
|
|
}
|
|
|
}
|
|
|
@@ -99,18 +128,7 @@ class TextbookApiService
|
|
|
public function updateTextbookSeries(int $seriesId, array $data): array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->put($this->baseUrl . "/textbooks/series/{$seriesId}", $data);
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json();
|
|
|
- }
|
|
|
-
|
|
|
- Log::error('Failed to update textbook series', [
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
- ]);
|
|
|
-
|
|
|
- throw new \Exception('Failed to update textbook series');
|
|
|
+ return $this->request('PUT', "/textbooks/series/{$seriesId}", $data);
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Error updating textbook series', ['error' => $e->getMessage()]);
|
|
|
throw $e;
|
|
|
@@ -123,18 +141,8 @@ class TextbookApiService
|
|
|
public function deleteTextbookSeries(int $seriesId): bool
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->delete($this->baseUrl . "/textbooks/series/{$seriesId}");
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- Log::error('Failed to delete textbook series', [
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
- ]);
|
|
|
-
|
|
|
- return false;
|
|
|
+ $this->request('DELETE', "/textbooks/series/{$seriesId}");
|
|
|
+ return true;
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Error deleting textbook series', ['error' => $e->getMessage()]);
|
|
|
return false;
|
|
|
@@ -147,19 +155,8 @@ class TextbookApiService
|
|
|
public function deleteTextbook(int $textbookId): bool
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->delete($this->baseUrl . "/textbooks/{$textbookId}");
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- Log::error('Failed to delete textbook', [
|
|
|
- 'textbook_id' => $textbookId,
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
- ]);
|
|
|
-
|
|
|
- return false;
|
|
|
+ $this->request('DELETE', "/textbooks/by-id/{$textbookId}");
|
|
|
+ return true;
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Error deleting textbook', ['error' => $e->getMessage(), 'textbook_id' => $textbookId]);
|
|
|
return false;
|
|
|
@@ -172,20 +169,9 @@ class TextbookApiService
|
|
|
public function getTextbooks(array $params = []): array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->get($this->baseUrl . '/textbooks', $params);
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json();
|
|
|
- }
|
|
|
-
|
|
|
- Log::error('Failed to fetch textbooks', [
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
- ]);
|
|
|
-
|
|
|
- return ['data' => [], 'meta' => []];
|
|
|
+ return $this->request('GET', '/textbooks', $params);
|
|
|
} catch (\Exception $e) {
|
|
|
- Log::error('Error fetching textbooks', ['error' => $e->getMessage()]);
|
|
|
+ Log::warning('Failed to fetch textbooks, returning empty result', ['error' => $e->getMessage()]);
|
|
|
return ['data' => [], 'meta' => []];
|
|
|
}
|
|
|
}
|
|
|
@@ -196,20 +182,13 @@ class TextbookApiService
|
|
|
public function getTextbook(int $textbookId): ?array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->get($this->baseUrl . "/textbooks/{$textbookId}");
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json('data');
|
|
|
- }
|
|
|
-
|
|
|
- Log::error('Failed to fetch textbook', [
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
- ]);
|
|
|
-
|
|
|
- return null;
|
|
|
+ $result = $this->request('GET', "/textbooks/by-id/{$textbookId}");
|
|
|
+ return $result['data'] ?? null;
|
|
|
} catch (\Exception $e) {
|
|
|
- Log::error('Error fetching textbook', ['error' => $e->getMessage()]);
|
|
|
+ Log::warning('Textbook not found or error occurred', [
|
|
|
+ 'textbook_id' => $textbookId,
|
|
|
+ 'error' => $e->getMessage()
|
|
|
+ ]);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
@@ -220,29 +199,13 @@ class TextbookApiService
|
|
|
public function createTextbook(array $data): array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->post($this->baseUrl . '/textbooks', $data);
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json();
|
|
|
- }
|
|
|
-
|
|
|
- $responseBody = $response->body();
|
|
|
- $status = $response->status();
|
|
|
-
|
|
|
- Log::error('Failed to create textbook', [
|
|
|
- 'status' => $status,
|
|
|
- 'body' => $responseBody
|
|
|
- ]);
|
|
|
-
|
|
|
+ return $this->request('POST', '/textbooks', $data);
|
|
|
+ } catch (\Exception $e) {
|
|
|
// 检查是否是series不存在的错误
|
|
|
- if ($status === 404 && strpos($responseBody, 'Series not found') !== false) {
|
|
|
+ if (strpos($e->getMessage(), 'Series not found') !== false) {
|
|
|
$seriesId = $data['series_id'] ?? 'unknown';
|
|
|
throw new \Exception("系列ID {$seriesId} 不存在,请先创建教材系列或检查ID是否正确");
|
|
|
}
|
|
|
-
|
|
|
- throw new \Exception('Failed to create textbook: ' . $responseBody);
|
|
|
- } catch (\Exception $e) {
|
|
|
- Log::error('Error creating textbook', ['error' => $e->getMessage()]);
|
|
|
throw $e;
|
|
|
}
|
|
|
}
|
|
|
@@ -253,18 +216,7 @@ class TextbookApiService
|
|
|
public function updateTextbook(int $textbookId, array $data): array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->put($this->baseUrl . "/textbooks/{$textbookId}", $data);
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json();
|
|
|
- }
|
|
|
-
|
|
|
- Log::error('Failed to update textbook', [
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
- ]);
|
|
|
-
|
|
|
- throw new \Exception('Failed to update textbook');
|
|
|
+ return $this->request('PUT', "/textbooks/by-id/{$textbookId}", $data);
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Error updating textbook', ['error' => $e->getMessage()]);
|
|
|
throw $e;
|
|
|
@@ -278,29 +230,13 @@ class TextbookApiService
|
|
|
public function createOrUpdateTextbook(array $data): array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->post($this->baseUrl . '/textbooks/upsert', $data);
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json();
|
|
|
- }
|
|
|
-
|
|
|
- $responseBody = $response->body();
|
|
|
- $status = $response->status();
|
|
|
-
|
|
|
- Log::error('Failed to create or update textbook', [
|
|
|
- 'status' => $status,
|
|
|
- 'body' => $responseBody
|
|
|
- ]);
|
|
|
-
|
|
|
+ return $this->request('POST', '/textbooks/upsert', $data);
|
|
|
+ } catch (\Exception $e) {
|
|
|
// 检查是否是series不存在的错误
|
|
|
- if ($status === 404 && strpos($responseBody, 'Series not found') !== false) {
|
|
|
+ if (strpos($e->getMessage(), 'Series not found') !== false) {
|
|
|
$seriesId = $data['series_id'] ?? 'unknown';
|
|
|
throw new \Exception("系列ID {$seriesId} 不存在,请先创建教材系列或检查ID是否正确");
|
|
|
}
|
|
|
-
|
|
|
- throw new \Exception('Failed to create or update textbook: ' . $responseBody);
|
|
|
- } catch (\Exception $e) {
|
|
|
- Log::error('Error creating or updating textbook', ['error' => $e->getMessage()]);
|
|
|
throw $e;
|
|
|
}
|
|
|
}
|
|
|
@@ -311,19 +247,8 @@ class TextbookApiService
|
|
|
public function deleteTextbookCatalog(int $catalogId): bool
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->delete($this->baseUrl . "/textbooks/catalog/{$catalogId}");
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return true;
|
|
|
- }
|
|
|
-
|
|
|
- Log::error('Failed to delete textbook catalog', [
|
|
|
- 'catalog_id' => $catalogId,
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
- ]);
|
|
|
-
|
|
|
- return false;
|
|
|
+ $this->request('DELETE', "/textbooks/catalog/{$catalogId}");
|
|
|
+ return true;
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Error deleting textbook catalog', ['error' => $e->getMessage(), 'catalog_id' => $catalogId]);
|
|
|
return false;
|
|
|
@@ -336,22 +261,10 @@ class TextbookApiService
|
|
|
public function getTextbookCatalog(int $textbookId, string $format = 'tree'): array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->get($this->baseUrl . "/textbooks/{$textbookId}/catalog", [
|
|
|
- 'format' => $format
|
|
|
- ]);
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json('data');
|
|
|
- }
|
|
|
-
|
|
|
- Log::error('Failed to fetch textbook catalog', [
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
- ]);
|
|
|
-
|
|
|
- return [];
|
|
|
+ $result = $this->request('GET', "/textbooks/by-id/{$textbookId}/catalog", ['format' => $format]);
|
|
|
+ return $result['data'] ?? [];
|
|
|
} catch (\Exception $e) {
|
|
|
- Log::error('Error fetching textbook catalog', ['error' => $e->getMessage()]);
|
|
|
+ Log::warning('Failed to fetch textbook catalog, returning empty result', ['error' => $e->getMessage()]);
|
|
|
return [];
|
|
|
}
|
|
|
}
|
|
|
@@ -362,23 +275,13 @@ class TextbookApiService
|
|
|
public function previewTextbookNaming(array $textbookData, array $seriesData): array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->post($this->baseUrl . '/textbooks/naming-preview', [
|
|
|
+ $result = $this->request('POST', '/textbooks/naming-preview', [
|
|
|
'textbook' => $textbookData,
|
|
|
'series' => $seriesData
|
|
|
]);
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json('data');
|
|
|
- }
|
|
|
-
|
|
|
- Log::error('Failed to preview textbook naming', [
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
- ]);
|
|
|
-
|
|
|
- return [];
|
|
|
+ return $result['data'] ?? [];
|
|
|
} catch (\Exception $e) {
|
|
|
- Log::error('Error previewing textbook naming', ['error' => $e->getMessage()]);
|
|
|
+ Log::warning('Failed to preview textbook naming, returning empty result', ['error' => $e->getMessage()]);
|
|
|
return [];
|
|
|
}
|
|
|
}
|
|
|
@@ -389,22 +292,10 @@ class TextbookApiService
|
|
|
public function importTextbookMetadata($file, string $commitMode = 'overwrite'): array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(300)
|
|
|
- ->attach('file', file_get_contents($file->getPathname()), $file->getClientOriginalName())
|
|
|
- ->post($this->baseUrl . '/textbooks/import/meta', [
|
|
|
- 'commit_mode' => $commitMode
|
|
|
- ]);
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json();
|
|
|
- }
|
|
|
-
|
|
|
- Log::error('Failed to import textbook metadata', [
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
+ return $this->request('POST', '/textbooks/import/meta', [
|
|
|
+ 'file' => $file,
|
|
|
+ 'commit_mode' => $commitMode
|
|
|
]);
|
|
|
-
|
|
|
- throw new \Exception('Failed to import textbook metadata');
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Error importing textbook metadata', ['error' => $e->getMessage()]);
|
|
|
throw $e;
|
|
|
@@ -414,26 +305,18 @@ class TextbookApiService
|
|
|
/**
|
|
|
* 导入教材目录
|
|
|
*/
|
|
|
- public function importTextbookCatalog(int $textbookId, $file, string $commitMode = 'overwrite'): array
|
|
|
+ public function importTextbookCatalog(int $textbookId, array $catalogData, string $commitMode = 'overwrite'): array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(300)
|
|
|
- ->attach('file', file_get_contents($file->getPathname()), $file->getClientOriginalName())
|
|
|
- ->post($this->baseUrl . '/textbooks/import/catalog', [
|
|
|
- 'textbook_id' => $textbookId,
|
|
|
- 'commit_mode' => $commitMode
|
|
|
- ]);
|
|
|
+ // 将数组转换为JSON字符串
|
|
|
+ $jsonData = json_encode($catalogData, JSON_UNESCAPED_UNICODE);
|
|
|
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json();
|
|
|
- }
|
|
|
-
|
|
|
- Log::error('Failed to import textbook catalog', [
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
+ // 直接发送 JSON 数据
|
|
|
+ return $this->request('POST', '/textbooks/import/catalog', [
|
|
|
+ 'textbook_id' => $textbookId,
|
|
|
+ 'data' => $jsonData,
|
|
|
+ 'commit_mode' => $commitMode
|
|
|
]);
|
|
|
-
|
|
|
- throw new \Exception('Failed to import textbook catalog');
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Error importing textbook catalog', ['error' => $e->getMessage()]);
|
|
|
throw $e;
|
|
|
@@ -446,18 +329,7 @@ class TextbookApiService
|
|
|
public function commitImportJob(int $jobId): array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(300)->post($this->baseUrl . "/api/textbooks/import/{$jobId}/commit");
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json();
|
|
|
- }
|
|
|
-
|
|
|
- Log::error('Failed to commit import job', [
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
- ]);
|
|
|
-
|
|
|
- throw new \Exception('Failed to commit import job');
|
|
|
+ return $this->request('POST', "/api/textbooks/import/{$jobId}/commit");
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Error committing import job', ['error' => $e->getMessage()]);
|
|
|
throw $e;
|
|
|
@@ -470,20 +342,9 @@ class TextbookApiService
|
|
|
public function getImportJobs(array $params = []): array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->get($this->baseUrl . '/textbooks/import/jobs', $params);
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json();
|
|
|
- }
|
|
|
-
|
|
|
- Log::error('Failed to fetch import jobs', [
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
- ]);
|
|
|
-
|
|
|
- return ['data' => [], 'meta' => []];
|
|
|
+ return $this->request('GET', '/textbooks/import/jobs', $params);
|
|
|
} catch (\Exception $e) {
|
|
|
- Log::error('Error fetching import jobs', ['error' => $e->getMessage()]);
|
|
|
+ Log::warning('Failed to fetch import jobs, returning empty result', ['error' => $e->getMessage()]);
|
|
|
return ['data' => [], 'meta' => []];
|
|
|
}
|
|
|
}
|
|
|
@@ -494,20 +355,10 @@ class TextbookApiService
|
|
|
public function getImportJob(int $jobId): ?array
|
|
|
{
|
|
|
try {
|
|
|
- $response = Http::timeout(30)->get($this->baseUrl . "/api/textbooks/import/jobs/{$jobId}");
|
|
|
-
|
|
|
- if ($response->successful()) {
|
|
|
- return $response->json('data');
|
|
|
- }
|
|
|
-
|
|
|
- Log::error('Failed to fetch import job', [
|
|
|
- 'status' => $response->status(),
|
|
|
- 'body' => $response->body()
|
|
|
- ]);
|
|
|
-
|
|
|
- return null;
|
|
|
+ $result = $this->request('GET', "/api/textbooks/import/jobs/{$jobId}");
|
|
|
+ return $result['data'] ?? null;
|
|
|
} catch (\Exception $e) {
|
|
|
- Log::error('Error fetching import job', ['error' => $e->getMessage()]);
|
|
|
+ Log::warning('Import job not found or error occurred', ['error' => $e->getMessage()]);
|
|
|
return null;
|
|
|
}
|
|
|
}
|
|
|
@@ -548,30 +399,14 @@ class TextbookApiService
|
|
|
}
|
|
|
|
|
|
// 调用API进行完全同步
|
|
|
- $response = Http::timeout(300)->post($this->baseUrl . '/textbooks/series/sync-all', [
|
|
|
+ $result = $this->request('POST', '/textbooks/series/sync-all', [
|
|
|
'series' => $seriesData
|
|
|
]);
|
|
|
|
|
|
- if ($response->successful()) {
|
|
|
- $result = $response->json();
|
|
|
- return [
|
|
|
- 'success' => true,
|
|
|
- 'synced_count' => count($seriesData),
|
|
|
- 'data' => $result
|
|
|
- ];
|
|
|
- }
|
|
|
-
|
|
|
- $responseBody = $response->body();
|
|
|
- $status = $response->status();
|
|
|
-
|
|
|
- Log::error('Failed to sync textbook series', [
|
|
|
- 'status' => $status,
|
|
|
- 'body' => $responseBody
|
|
|
- ]);
|
|
|
-
|
|
|
return [
|
|
|
- 'success' => false,
|
|
|
- 'message' => "同步失败: HTTP {$status} - {$responseBody}"
|
|
|
+ 'success' => true,
|
|
|
+ 'synced_count' => count($seriesData),
|
|
|
+ 'data' => $result
|
|
|
];
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Error syncing textbook series', ['error' => $e->getMessage()]);
|