| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443 |
- <?php
- namespace App\Services;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Log;
- class TextbookApiService
- {
- protected string $baseUrl;
- public function __construct()
- {
- // QuestionBankService API 地址
- $baseUrl = config('services.question_bank.base_url', 'http://localhost:5015/api');
- // 移除末尾的 /,保持 baseUrl 以 /api 结尾
- $this->baseUrl = rtrim($baseUrl, '/');
- }
- /**
- * 获取教材系列列表
- */
- public function getTextbookSeries(array $params = []): array
- {
- try {
- $response = Http::timeout(30)->get($this->baseUrl . '/textbooks/series', $params);
- if ($response->successful()) {
- return $response->json();
- }
- Log::error('Failed to fetch textbook series', [
- 'status' => $response->status(),
- 'body' => $response->body()
- ]);
- return ['data' => [], 'meta' => []];
- } catch (\Exception $e) {
- Log::error('Error fetching textbook series', ['error' => $e->getMessage()]);
- return ['data' => [], 'meta' => []];
- }
- }
- /**
- * 创建教材系列
- */
- public function createTextbookSeries(array $data): array
- {
- try {
- $response = Http::timeout(30)->post($this->baseUrl . '/textbooks/series', $data);
- if ($response->successful()) {
- return $response->json();
- }
- Log::error('Failed to create textbook series', [
- 'status' => $response->status(),
- 'body' => $response->body()
- ]);
- throw new \Exception('Failed to create textbook series');
- } catch (\Exception $e) {
- Log::error('Error creating textbook series', ['error' => $e->getMessage()]);
- throw $e;
- }
- }
- /**
- * 更新教材系列
- */
- 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');
- } catch (\Exception $e) {
- Log::error('Error updating textbook series', ['error' => $e->getMessage()]);
- throw $e;
- }
- }
- /**
- * 删除教材系列
- */
- 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;
- } catch (\Exception $e) {
- Log::error('Error deleting textbook series', ['error' => $e->getMessage()]);
- return false;
- }
- }
- /**
- * 删除教材
- */
- 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;
- } catch (\Exception $e) {
- Log::error('Error deleting textbook', ['error' => $e->getMessage(), 'textbook_id' => $textbookId]);
- return false;
- }
- }
- /**
- * 获取教材列表
- */
- 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' => []];
- } catch (\Exception $e) {
- Log::error('Error fetching textbooks', ['error' => $e->getMessage()]);
- return ['data' => [], 'meta' => []];
- }
- }
- /**
- * 获取单个教材
- */
- 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;
- } catch (\Exception $e) {
- Log::error('Error fetching textbook', ['error' => $e->getMessage()]);
- return null;
- }
- }
- /**
- * 创建教材
- */
- public function createTextbook(array $data): array
- {
- try {
- $response = Http::timeout(30)->post($this->baseUrl . '/textbooks', $data);
- if ($response->successful()) {
- return $response->json();
- }
- Log::error('Failed to create textbook', [
- 'status' => $response->status(),
- 'body' => $response->body()
- ]);
- throw new \Exception('Failed to create textbook');
- } catch (\Exception $e) {
- Log::error('Error creating textbook', ['error' => $e->getMessage()]);
- throw $e;
- }
- }
- /**
- * 更新教材
- */
- 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');
- } catch (\Exception $e) {
- Log::error('Error updating textbook', ['error' => $e->getMessage()]);
- throw $e;
- }
- }
- /**
- * 删除教材目录节点
- */
- 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;
- } catch (\Exception $e) {
- Log::error('Error deleting textbook catalog', ['error' => $e->getMessage(), 'catalog_id' => $catalogId]);
- return false;
- }
- }
- /**
- * 获取教材目录树
- */
- 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 [];
- } catch (\Exception $e) {
- Log::error('Error fetching textbook catalog', ['error' => $e->getMessage()]);
- return [];
- }
- }
- /**
- * 预览教材命名
- */
- public function previewTextbookNaming(array $textbookData, array $seriesData): array
- {
- try {
- $response = Http::timeout(30)->post($this->baseUrl . '/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 [];
- } catch (\Exception $e) {
- Log::error('Error previewing textbook naming', ['error' => $e->getMessage()]);
- return [];
- }
- }
- /**
- * 导入教材元信息
- */
- 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()
- ]);
- throw new \Exception('Failed to import textbook metadata');
- } catch (\Exception $e) {
- Log::error('Error importing textbook metadata', ['error' => $e->getMessage()]);
- throw $e;
- }
- }
- /**
- * 导入教材目录
- */
- public function importTextbookCatalog(int $textbookId, $file, 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
- ]);
- if ($response->successful()) {
- return $response->json();
- }
- Log::error('Failed to import textbook catalog', [
- 'status' => $response->status(),
- 'body' => $response->body()
- ]);
- throw new \Exception('Failed to import textbook catalog');
- } catch (\Exception $e) {
- Log::error('Error importing textbook catalog', ['error' => $e->getMessage()]);
- throw $e;
- }
- }
- /**
- * 提交导入任务
- */
- 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');
- } catch (\Exception $e) {
- Log::error('Error committing import job', ['error' => $e->getMessage()]);
- throw $e;
- }
- }
- /**
- * 获取导入任务列表
- */
- 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' => []];
- } catch (\Exception $e) {
- Log::error('Error fetching import jobs', ['error' => $e->getMessage()]);
- return ['data' => [], 'meta' => []];
- }
- }
- /**
- * 获取单个导入任务
- */
- 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;
- } catch (\Exception $e) {
- Log::error('Error fetching import job', ['error' => $e->getMessage()]);
- return null;
- }
- }
- }
|