BookUpdate.php 7.3 KB

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