BookUpdate.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. private $client;
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. $this->client = new Client(['timeout' => 10.0, 'allow_redirects' => true]);
  33. }
  34. /**
  35. * Execute the console command.
  36. *
  37. * @return mixed
  38. */
  39. public function handle()
  40. {
  41. $this->updateAll();
  42. }
  43. /**
  44. * 更新所有书的章节
  45. */
  46. public function updateAll()
  47. {
  48. set_time_limit(0);
  49. $exist_book = Book::where('ly_bid', '!=', '0')->where('status', 0)->select('id', 'ly_bid')->get();
  50. foreach ($exist_book as $v) {
  51. //Log::info('updateAll bid is: '.$v->id.'$ly_bid is :'.$v->ly_bid.'----------------------------');
  52. $this->updateOne($v->id, $v->ly_bid);
  53. }
  54. }
  55. /**
  56. * 更新章节内容 一本书的
  57. * @param $bid
  58. * @return int|string
  59. */
  60. public function updateOne($bid, $ly_bid)
  61. {
  62. set_time_limit(0);
  63. //$max_sequence = Chapter::where('bid', $bid)->max('sequence');
  64. //$max_sequence = $max_sequence ? $max_sequence : -1;
  65. //$max_sequence_chapter = Chapter::where('bid', $bid)->where('sequence', $max_sequence)->select('id')->first();
  66. //$max_sequence_chapter_id = isset($max_sequence_chapter->id) ? $max_sequence_chapter->id : 0;
  67. $this->bookStatus($bid, $ly_bid);
  68. $last_chapter = Chapter::where('bid', $bid)
  69. ->select('id', 'name', 'sequence', 'prev_cid', 'next_cid')
  70. ->orderBy('sequence', 'desc')
  71. ->first();
  72. $last_chapter_from_third = Chapter::where('bid', $bid)
  73. ->where('ly_chapter_id', '>', 0)
  74. ->select('id', 'name', 'sequence', 'prev_cid', 'next_cid', 'ly_chapter_id')
  75. ->orderBy('sequence', 'desc')
  76. ->first();
  77. $max_sequence_chapter_id = isset($last_chapter->id) ? $last_chapter->id : 0;
  78. $max_sequence = isset($last_chapter->sequence) ? $last_chapter->sequence : 0;
  79. try {
  80. $chapter_list_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapterlist&bid=%s&token=sefaf23h7face";
  81. $res = $this->client->get(sprintf($chapter_list_fromat, $ly_bid));
  82. $res = $res->getBody()->getContents();
  83. } catch (\Exception $e) {
  84. Log::info($e);
  85. return '';
  86. }
  87. if (!$res) return '';
  88. $res = json_decode($res, true);
  89. if ($res && isset($res['code']) && $res['code'] == 200) {
  90. } else {
  91. return '';
  92. }
  93. $j = 0;
  94. if (isset($res['data']) && $res['data'] && is_array($res['data'])) {
  95. } else {
  96. return '';
  97. }
  98. $local_count = Chapter::where('bid', $bid)->where('ly_chapter_id', '>', 0)->count();
  99. $remote_count = 0;
  100. foreach ($res['data'] as $item1) {
  101. $remote_count += count($item1['chapters']);
  102. $item1 = null;
  103. }
  104. Log::info('$remote_count is: ' . $remote_count . '====$local_count :' . $local_count . '----');
  105. if ($remote_count == $local_count) return '';
  106. Log::info('$bid =: ' . $bid . '---is lack---');
  107. $start = false;
  108. if (!$last_chapter) {
  109. $start = true;
  110. }
  111. $ly_last_chapter_id = 0;
  112. if ($last_chapter_from_third && isset($last_chapter_from_third->ly_chapter_id)) {
  113. $ly_last_chapter_id = $last_chapter_from_third->ly_chapter_id;
  114. }
  115. foreach ($res['data'] as $v1) {
  116. foreach ($v1['chapters'] as $v) {
  117. /*if ($v['chapter_order_number'] <= $max_sequence-1) {
  118. continue;
  119. }
  120. if (Chapter::where('bid', $bid)->where('ly_chapter_id', $v['chapter_id'])->count()) {
  121. continue;
  122. }*/
  123. if (!$start) {
  124. if ($v['chapter_id'] != $ly_last_chapter_id) {
  125. continue;
  126. } else {
  127. \Log::info($v);
  128. $start = true;
  129. continue;
  130. }
  131. }
  132. Log::info('bid: ' . $bid . '----ly_chapter_id: ' . $v['chapter_id']);
  133. $chapter_fromat = "http://www.leyuee.com/services/zwfx.aspx?method=chapter&bid=%s&cid=%s&token=sefaf23h7face";
  134. $cahpter_content_res = $this->client->get(sprintf($chapter_fromat, $ly_bid, $v['chapter_id']));
  135. $cahpter_content = $cahpter_content_res->getBody()->getContents();
  136. $temp = [
  137. 'bid' => $bid,
  138. 'name' => $v['chapter_name'],
  139. 'sequence' => ++$max_sequence,
  140. 'is_vip' => $v['chapter_need_pay'],
  141. 'prev_cid' => $max_sequence_chapter_id,
  142. 'next_cid' => '',
  143. 'recent_update_at' => date('Y-m-d H:i:s', $v['chapter_last_update_time']),
  144. 'ly_chapter_id' => $v['chapter_id']
  145. ];
  146. if ($cahpter_content) {
  147. $cahpter_content = json_decode($cahpter_content, true);
  148. $temp['size'] = ceil(strlen($cahpter_content['data']['chapter_content']) / 3);
  149. $temp['content'] = $cahpter_content['data']['chapter_content'];
  150. }
  151. $insert_res = Chapter::create($temp);
  152. Chapter::where('id', $max_sequence_chapter_id)->update(['next_cid' => $insert_res->id]);
  153. $max_sequence_chapter_id = $insert_res->id;
  154. $temp = null;
  155. $j++;
  156. }
  157. }
  158. $chapter_count = Chapter::where('bid', $bid)->count();
  159. $chapter_size = Chapter::where('bid', $bid)->sum('size');
  160. if ($j > 0) {
  161. Book::where('id', $bid)->update(['chapter_count' => $chapter_count, 'last_cid' => $max_sequence_chapter_id, 'size' => $chapter_size, 'last_chapter' => $v['chapter_name']]);
  162. }
  163. return $j;
  164. }
  165. private function bookStatus($bid, $ly_bid)
  166. {
  167. $status = 0;
  168. try {
  169. $book_info_url_format = "http://www.leyuee.com/services/zwfx.aspx?method=bookinfo&token=sefaf23h7face&bid=%s";
  170. $res = $this->client->get(sprintf($book_info_url_format, $ly_bid));
  171. $res = $res->getBody()->getContents();
  172. if ($res) {
  173. $res = json_decode($res, true);
  174. if (isset($res['data']) && isset($res['data']['book_state'])) {
  175. $status = $res['data']['book_state'];
  176. }
  177. }
  178. } catch (\Exception $e) {
  179. }
  180. if ($status) {
  181. Book::where('id', $bid)->update(['status' => $status]);
  182. }
  183. }
  184. }