BookUpdate.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. <?php
  2. namespace App\Console\Commands;
  3. use Illuminate\Console\Command;
  4. use GuzzleHttp\Client;
  5. use App\Modules\Book\Models\Book;
  6. use App\Modules\Book\Models\Chapter;
  7. use Log;
  8. use App\Modules\Book\Services\BookConfigService;
  9. class BookUpdate extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'book:update';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = '更新所有图书章节内容';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. $this->client = new Client(['timeout' => 10.0,'allow_redirects'=>true]);
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. $this->updateAll();
  41. }
  42. /**
  43. * 更新所有书的章节
  44. */
  45. public function updateAll(){
  46. set_time_limit(0);
  47. $exist_book = Book::where('ly_bid','!=','0')->where('status',0)->select('id','ly_bid')->get();
  48. foreach ($exist_book as $v){
  49. //Log::info('updateAll bid is: '.$v->id.'$ly_bid is :'.$v->ly_bid.'----------------------------');
  50. $this->updateOne($v->id,$v->ly_bid);
  51. }
  52. }
  53. /**
  54. * 更新章节内容 一本书的
  55. * @param $bid
  56. * @return int|string
  57. */
  58. public function updateOne($bid,$ly_bid)
  59. {
  60. set_time_limit(0);
  61. $max_sequence = Chapter::where('bid', $bid)->max('sequence');
  62. $max_sequence = $max_sequence ? $max_sequence : -1;
  63. $max_sequence_chapter = Chapter::where('bid', $bid)->where('sequence', $max_sequence)->select('id')->first();
  64. $max_sequence_chapter_id = isset($max_sequence_chapter->id) ? $max_sequence_chapter->id : 0;
  65. $this->bookStatus($bid,$ly_bid);
  66. try {
  67. $chapter_list_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapterlist&bid=%s&token=sefaf23h7face";
  68. $res = $this->client->get(sprintf($chapter_list_fromat, $ly_bid));
  69. $res = $res->getBody()->getContents();
  70. } catch (\Exception $e) {
  71. Log::info($e);
  72. return '';
  73. }
  74. if (!$res) return '';
  75. $res = json_decode($res, true);
  76. if($res && isset($res['code']) && $res['code'] == 200){
  77. }else{
  78. return '';
  79. }
  80. $j = 0;
  81. $local_count = Chapter::where('bid', $bid)->count();
  82. $remote_count = 0;
  83. if(isset($res['data']) && $res['data'] && is_array($res['data'])){
  84. }else{
  85. return '';
  86. }
  87. foreach ($res['data'] as $item1) {
  88. $remote_count += count($item1['chapters']);
  89. $item1 = null;
  90. }
  91. Log::info('$remote_count is: ' . $remote_count . '====$local_count :' . $local_count . '----');
  92. if ($remote_count == $local_count) return '';
  93. Log::info('$bid =: ' . $bid . '---is lack---');
  94. foreach ($res['data'] as $v1) {
  95. foreach ($v1['chapters'] as $v) {
  96. if ($v['chapter_order_number'] <= $max_sequence-1) {
  97. continue;
  98. }
  99. if (Chapter::where('bid', $bid)->where('ly_chapter_id', $v['chapter_id'])->count()) {
  100. continue;
  101. }
  102. Log::info('bid: ' . $bid . '----ly_chapter_id: ' . $v['chapter_id']);
  103. $chapter_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapter&bid=%s&cid=%s&token=sefaf23h7face";
  104. $cahpter_content_res = $this->client->get(sprintf($chapter_fromat, $ly_bid, $v['chapter_id']));
  105. $cahpter_content = $cahpter_content_res->getBody()->getContents();
  106. $temp = [
  107. 'bid' => $bid,
  108. 'name' => $v['chapter_name'],
  109. 'sequence' => $v['chapter_order_number']+1,
  110. 'is_vip' => $v['chapter_need_pay'],
  111. 'prev_cid' => $max_sequence_chapter_id,
  112. 'next_cid' => '',
  113. 'recent_update_at' => date('Y-m-d H:i:s', $v['chapter_last_update_time']),
  114. 'ly_chapter_id' => $v['chapter_id']
  115. ];
  116. if ($cahpter_content) {
  117. $cahpter_content = json_decode($cahpter_content, true);
  118. $temp['size'] = ceil(strlen($cahpter_content['data']['chapter_content']) / 3);
  119. $temp['content'] = $cahpter_content['data']['chapter_content'];
  120. }
  121. $insert_res = Chapter::create($temp);
  122. Chapter::where('id', $max_sequence_chapter_id)->update(['next_cid' => $insert_res->id]);
  123. $max_sequence_chapter_id = $insert_res->id;
  124. $temp = null;
  125. $j++;
  126. }
  127. }
  128. $chapter_count = Chapter::where('bid',$bid)->count();
  129. $chapter_size = Chapter::where('bid',$bid)->sum('size');
  130. if($j>0){
  131. Book::where('id',$bid)->update(['chapter_count'=>$chapter_count,'last_cid'=>$max_sequence_chapter_id,'size'=>$chapter_size,'last_chapter'=>$v['chapter_name']]);
  132. }
  133. return $j;
  134. }
  135. private function bookStatus($bid,$ly_bid){
  136. $status = 0;
  137. try{
  138. $book_info_url_format = "http://www.leyuee.com/services/zwfx.aspx?method=bookinfo&token=sefaf23h7face&bid=%s";
  139. $res = $this->client->get(sprintf($book_info_url_format,$ly_bid));
  140. $res = $res->getBody()->getContents();
  141. if($res){
  142. $res = json_decode($res,true);
  143. if(isset($res['data']) && isset($res['data']['book_state'])){
  144. $status = $res['data']['book_state'];
  145. }
  146. }
  147. }catch (\Exception $e){
  148. }
  149. if($status){
  150. Book::where('id',$bid)->update(['status'=>$status]);
  151. }
  152. }
  153. }