BookUpdateOne.php 7.7 KB

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