YqBook.php 5.0 KB

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