YqBook.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Modules\Book\Models\Chapter;
  4. use App\Modules\Book\Models\Book;
  5. use App\Modules\Book\Models\BookConfig;
  6. use App\Modules\YunQi\Services\BookYunqiService;
  7. use Illuminate\Console\Command;
  8. use GuzzleHttp\Client;
  9. use GuzzleHttp\Psr7\Request;
  10. use DB;
  11. use GuzzleHttp\Pool;
  12. use Log;
  13. class YqBook extends Command
  14. {
  15. /**
  16. * The name and signature of the console command.
  17. *
  18. * @var string
  19. */
  20. protected $signature = 'yqbook {--bid= : the id of a book} {--force}';
  21. /**
  22. * The console command description.
  23. *
  24. * @var string
  25. */
  26. protected $description = 'Command description';
  27. /**
  28. * Create a new command instance.
  29. *
  30. * @return void
  31. */
  32. public function __construct()
  33. {
  34. parent::__construct();
  35. }
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return mixed
  40. */
  41. public function handle()
  42. {
  43. $bid = $this->option('bid');
  44. if(empty($bid)) return 0;
  45. $force = $this->option('force');
  46. if(!$force && $this->isHadExists($bid)){
  47. echo 'had exist-'.PHP_EOL;
  48. return false;
  49. }
  50. $this->start($bid);
  51. }
  52. private function start($bid){
  53. $book = $this->bookInfo($bid);
  54. if(!$book) return false;
  55. $chapter = $book['chapter_count'];
  56. $page = ceil($chapter/30);
  57. $this->chapterInfo($bid,$page,$book['id']);
  58. $this->call('book:afs',['bid'=> [$book['id']]] );
  59. }
  60. private function bookInfo($bid){
  61. $client = new Client(['timeout' => 5.0]);
  62. $url = sprintf('http://47.90.126.243:8094/api/books/exportbookInfo?bid=%s',$bid);
  63. $request = new Request('get', $url);
  64. $response = null;
  65. try{
  66. $response = $client->send($request)->getBody()->getContents();
  67. }catch (\Exception $e){
  68. return false;
  69. }
  70. $result = json_decode($response,1);
  71. if(!isset($result['code']) || $result['code'] != 0){
  72. return false;
  73. }
  74. if(empty($result['data'])){
  75. return false;
  76. }
  77. $book_info = $result['data'];
  78. $book = Book::create([
  79. 'yq_bid'=>$book_info['id'],
  80. 'name'=>$book_info['book_name'],
  81. 'author'=>$book_info['author'],
  82. 'intro'=>$book_info['intro'],
  83. 'cover'=>$book_info['cover'],
  84. 'keyword'=>$book_info['keyword'],
  85. 'chapter_count'=>$book_info['chapter_count'],
  86. 'category_name'=>$book_info['category_name'],
  87. 'status'=>$book_info['status'],
  88. 'size'=>$book_info['size']
  89. ]);
  90. BookConfig::create([
  91. 'bid'=>$book->id,
  92. 'force_subscribe_chapter_seq'=>10,
  93. 'price'=>8.99,
  94. 'book_name'=>$book_info['book_name'],
  95. 'cover'=>$book_info['cover'],
  96. 'promotion_domain'=>'iycdm.com',
  97. 'charge_type'=>isset($book_info['charge_type'])?$book_info['charge_type']:'CHAPTER',
  98. 'is_on_shelf'=>10,
  99. 'cp_source'=>'yunqi',
  100. ]);
  101. BookYunqiService::create($book->id,$bid);
  102. return $book;
  103. }
  104. private function chapterInfo($bid,$page,$zy_bid){
  105. $client = new Client(['timeout' => 10]);
  106. $requests = function ($page,$bid) {
  107. for ($i = 0; $i < $page; $i++) {
  108. $url = sprintf('http://47.90.126.243:8094/api/books/exportchapterInfo?bid=%s&page=%s',$bid,$i+1);
  109. yield new Request('GET', $url);
  110. }
  111. };
  112. $pool = new Pool($client, $requests($page,$bid), [
  113. 'concurrency' => 10,
  114. 'fulfilled' => function ($response, $index) use ($zy_bid){
  115. $result = $response->getBody()->getContents();
  116. if(!$result){
  117. return ;
  118. }
  119. $result = \GuzzleHttp\json_decode($result,1);
  120. if(empty($result['data'])){
  121. return ;
  122. }
  123. foreach ($result['data']['data'] as $item){
  124. $chapter = [
  125. 'bid'=>$zy_bid,
  126. 'content'=>$item['content'],
  127. 'name'=>$item['name'],
  128. 'prev_cid'=>0,
  129. 'next_cid'=>0,
  130. 'size'=>$item['size'],
  131. 'is_vip'=>$item['is_vip'],
  132. 'sequence'=>$item['sequence'],
  133. 'recent_update_at'=>date('Y-m-d H:i:s'),
  134. 'ly_chapter_id'=>0
  135. ];
  136. Chapter::create($chapter);
  137. }
  138. },
  139. 'rejected' => function ($reason, $index) {
  140. //Log::info($reason);
  141. //Log::info($index);
  142. },
  143. ]);
  144. $promise = $pool->promise();
  145. $promise->wait();
  146. }
  147. private function isHadExists($bid){
  148. $old = Book::where('yq_bid',$bid)->select('id')->first();
  149. if($old){
  150. return true;
  151. }
  152. return false;
  153. }
  154. }