SyncBooksFromZwContent.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace App\Console\Book;
  3. use App\Models\Book\Book;
  4. use App\Models\Book\BookConfig;
  5. use App\Models\Book\Chapter;
  6. use App\Models\Book\ChapterContent;
  7. use GuzzleHttp\Client;
  8. use Illuminate\Console\Command;
  9. use GuzzleHttp\Exception\GuzzleException;
  10. class SyncBooksFromZwContent extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'book:sync:from:zw {--zw_ids=}';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = '从内容平台同步并更新图书信息';
  24. /**
  25. * @var Client
  26. */
  27. private $client;
  28. // 内容中台域名
  29. private $baseUrl = 'http://cp.yqsd.cn/api/book';
  30. /**
  31. * Create a new command instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct()
  36. {
  37. parent::__construct();
  38. $this->client = new Client(['timeout' => 20.0, 'allow_redirects' => true]);
  39. }
  40. /**
  41. * @return void
  42. * @throws GuzzleException
  43. */
  44. public function handle()
  45. {
  46. // 传参
  47. $zwIds = $this->filterParams();
  48. // 获取书籍列表
  49. if (empty($zwIds)) {
  50. $zwIds = Book::select('id', 'zw_id')->where('zw_id', '>', '0')->plunck('zw_id');
  51. }
  52. // 执行更新
  53. $this->syncZwBooks($zwIds);
  54. }
  55. /**
  56. * @return array
  57. */
  58. private function filterParams(): array
  59. {
  60. // 传参
  61. $idsStr = $this->option('zw_ids');
  62. $ids = explode(',', $idsStr);
  63. if (empty($ids)) {
  64. return [];
  65. }
  66. $result = [];
  67. foreach ($ids as $id) {
  68. if (in_array($id, $result) || !is_numeric($id) || (int)$id < 1) {
  69. continue;
  70. }
  71. $result[] = (int)$id;
  72. }
  73. return $result;
  74. }
  75. /**
  76. * @param $zwIds
  77. *
  78. * @return void
  79. * @throws GuzzleException
  80. */
  81. private function syncZwBooks($zwIds): void
  82. {
  83. // 判空
  84. if (empty($zwIds)) {
  85. return;
  86. }
  87. // 循环执行
  88. foreach ($zwIds as $zwId) {
  89. $this->syncZwBook($zwId);
  90. }
  91. }
  92. /**
  93. * @param $zwId
  94. * @return void
  95. * @throws GuzzleException
  96. */
  97. private function syncZwBook($zwId): void
  98. {
  99. dLog('sync')->info('book:sync-start', compact('zwId'));
  100. // 判空
  101. if (empty($zwId)) {
  102. return;
  103. }
  104. // 获取内容平台章节列表
  105. $zwBook = $this->request('/bookInfo/' . $zwId);
  106. if (empty($zwBook)) {
  107. return;
  108. }
  109. // 获取书籍
  110. $book = Book::where('zw_id', $zwId)->first();
  111. if (!$book) {
  112. $book = new Book();
  113. $book->zw_id = $zwBook['bid'];
  114. }
  115. $book->name = $zwBook['name'];
  116. $book->author = $zwBook['author'];
  117. $book->intro = trim(str_replace("&nbsp;", "", $zwBook['intro']));
  118. $book->cover = $zwBook['cover'];
  119. $book->keyword = $zwBook['keyword'];
  120. $book->gender = $zwBook['gender']; # 1男 2女 //TODO 是否要调整
  121. $book->category_id = $zwBook['category_id'];
  122. $book->category_name = $zwBook['category_name'];
  123. $book->ncategory_id = $zwBook['ncategory_id'];
  124. $book->size = $zwBook['size'];
  125. $book->status = $zwBook['status'];
  126. $book->chapter_count = $zwBook['chapter_count'];
  127. $book->save();
  128. // 获取书籍配置
  129. $bookConfig = BookConfig::where('bid', $book->id)->first();
  130. if (!$bookConfig) {
  131. $bookConfig = new BookConfig();
  132. $bookConfig->bid = $book->id;
  133. $bookConfig->charge_type = 'CHAPTER';
  134. $bookConfig->is_on_shelf = 0;
  135. $bookConfig->copyright_limit_data = '0000-00-00';
  136. }
  137. $bookConfig->cover = $zwBook['cover'];
  138. $bookConfig->book_name = $zwBook['name'];
  139. $bookConfig->source_domain = $zwBook['gender'] == 1 ? 'fanqqe.com' : 'fanqrj.com';
  140. $bookConfig->cp_source = $zwBook['cp'];
  141. $bookConfig->save();
  142. dLog('sync')->info('book:sync-end', compact('zwId'));
  143. }
  144. /**
  145. * 请求
  146. *
  147. * @param $url
  148. * @return array|mixed|string
  149. * @throws \GuzzleHttp\Exception\GuzzleException
  150. */
  151. private function request($url)
  152. {
  153. $url = $this->baseUrl . $url;
  154. try {
  155. $result = $this->client->request('get', $url)->getBody()->getContents();
  156. $result = json_decode($result, true);
  157. $code = getProp($result, 'code', 0);
  158. $data = getProp($result, 'data', []);
  159. } catch (\Exception $e) {
  160. return [];
  161. }
  162. return $code ? [] : $data;
  163. }
  164. }