PayUserAutoSub.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace App\Console\Commands\Temp;
  3. use Log;
  4. use Illuminate\Console\Command;
  5. use DB;
  6. use GuzzleHttp\Client;
  7. use GuzzleHttp\Cookie\CookieJar;
  8. use Hashids;
  9. use Redis;
  10. class PayUserAutoSub extends Command
  11. {
  12. /**
  13. * 执行命令 php artisan temp:pay_user_auto_do
  14. *
  15. * The name and signature of the console command.
  16. *
  17. * @var string
  18. */
  19. protected $signature = 'temp:pay_user_auto_do';
  20. /**
  21. * The console command description.
  22. *
  23. * @var string
  24. */
  25. protected $description = '自动订阅';
  26. /**
  27. * Execute the console command.
  28. *
  29. * @return mixed
  30. */
  31. public function handle()
  32. {
  33. print_r("======自动订阅 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  34. Log::info("======自动订阅 【任务执行开始】=====".date("y-m-d H:i:s"."\n"));
  35. $date = date('Y-m-d');
  36. //$total_real_fee = 0;//总消耗
  37. $host = DB::table('sys_configs')->where('name','brush_host')->pluck('value')->first();
  38. if(!$host) dd('系统参数配置有误');
  39. DB::table('temp_pay_users')->where('to_do_date',$date)->where('has_done',0)->orderBy('id','asc')->chunk(1000,function ($users) use($host){
  40. foreach ($users as $user)
  41. {
  42. $brush_read_data = [];
  43. $record_data = $user;
  44. dump('user:');dump($user);
  45. $user_day_real_fee = 0;//用户日总消耗书币
  46. $brush_read_time = strtotime($user->to_do_date) + mt_rand(8*3600,9*3600);//阅读时间
  47. $read_record_info = Redis::hgetall('book_read:' . $user->uid);
  48. dump('read_record_fee');dump($read_record_info);
  49. $last_record = isset($read_record_info['last_read']) ? explode('_',$read_record_info['last_read']) : null;
  50. $latest_read_at = $last_record ? $last_record[2] : 0;
  51. //判断最近一个月是否有阅读 有则continue 无阅读记录可能存储有误
  52. dump('$latest_read_at');dump($latest_read_at);
  53. // if(!$latest_read_at || (time() - $latest_read_at < 30*86400)) continue;
  54. //阅读记录
  55. $to_do_bids = explode(',',$user->bids);//等待刷的bid
  56. //用户信息
  57. $user_info = DB::table('users')->find($user->uid);
  58. dump('user');dump($user_info);
  59. //已经订购的图书
  60. $ordered_books = [];
  61. $client = new Client();
  62. $cookieJar = CookieJar::fromArray([
  63. 'web_user_auth' => encrypt($user_info->id)
  64. ], $host);
  65. dump('$to_do_bids');dump($to_do_bids);
  66. //阅读图书
  67. foreach ($to_do_bids as $_bid)
  68. {
  69. $encode_bid = Hashids::encode($_bid);
  70. $latest_chapter_id = 0;
  71. //判断bid阅读按本付费或按章
  72. if(isset($read_record_info[$_bid]) && $read_record_info[$_bid] && $_dt = explode('_',$read_record_info[$_bid]))
  73. {
  74. //上次阅读章节位置
  75. $latest_chapter_id = $_dt[0];
  76. }
  77. $book_info = DB::table('book_configs')->where('bid',$_bid)->first();
  78. dump('$book_info');dump($book_info);
  79. //按本
  80. $is_pay_by_book = $book_info->charge_type == 'BOOK' ? true : false;
  81. if($is_pay_by_book)
  82. {
  83. $is_book_ordered = DB::table('book_orders')->where('uid',$user->uid)->where('bid',$_bid)->count();
  84. if(!$is_book_ordered) {
  85. if($user_day_real_fee >= $user->to_do_fee) break;//用户超过日上限则退出
  86. //直接订购最后一章
  87. $last_chapter = DB::table('chapters')->select('id')->where('bid', $_bid)->orderBy('sequence', 'desc')->first();
  88. $_cid = $last_chapter->id;
  89. $res = $client->request('GET', "http://{$host}/api/books/{$encode_bid}/balance/chapterOrders/{$_cid}",[
  90. 'cookies' => $cookieJar,
  91. 'delay'=>300,
  92. 'query'=>['inter_not_need_sub'=>1]
  93. ]);
  94. dump('http code :');dump($res->getStatusCode());
  95. $res_data = json_decode($res->getBody()->getContents(), true);
  96. dump('$res_data');dump($res_data);
  97. $book_ordered = DB::table('book_orders')->where('uid', $user->uid)->where('bid', $_bid)->first();
  98. dump('$book_ordered');dump($book_ordered);
  99. if ($book_ordered)//订购成功
  100. {
  101. $brush_read_time += mt_rand(60,180);//增加阅读时间
  102. $brush_read_data['book'][] = ['bid'=>$_bid,'read_time'=>date('Y-m-d H:i:s',$brush_read_time)];
  103. $ordered_books[] = $_bid;
  104. $user_day_real_fee += $book_ordered->fee;//用户单日消耗
  105. $ordered_books[] = $_bid;
  106. }
  107. }else {
  108. $ordered_books[] = $_bid;
  109. }
  110. }else{
  111. //按章节
  112. //查用户起始章节
  113. $table = $user->uid%512;
  114. dump('$table');dump($table);
  115. $latest_sub_chapter = DB::connection('chapter_order_mysql')->table('chapter_orders'.$table)->where('uid',$user->uid)->where('bid',$_bid)->orderBy('id','desc')->first();
  116. dump('$latest_sub_chapter');dump($latest_sub_chapter);
  117. $start_cid = $latest_sub_chapter ? $latest_sub_chapter->cid : 0;
  118. //获取章节目录 cid
  119. $chapters = DB::table('chapters')->select(['id'])->where('bid',$_bid)->where('is_vip',1)->orderBy('sequence')->get();
  120. $chapter_count = count($chapters);
  121. $start = false;
  122. //dump('$chapters');dump($chapters);
  123. foreach ($chapters as $seq=>$_chapter)
  124. {
  125. if(!$start_cid) $start = true;
  126. $_cid = $_chapter->id;
  127. if($start)
  128. {
  129. //订阅
  130. //判断是否已经订购
  131. $is_ordered = DB::connection('chapter_order_mysql')->table('chapter_orders'.$table)->where('uid',$user->uid)->where('bid',$_bid)->where('cid',$_cid)->count();
  132. if(!$is_ordered)
  133. {
  134. if($user_day_real_fee >= $user->to_do_fee) break;//用户超过日上限则退出
  135. dump("http://{$host}/api/books/{$encode_bid}/chapters/{$_cid}");
  136. $res = $client->request('GET', "http://{$host}/api/books/{$encode_bid}/chapters/{$_cid}",[
  137. 'cookies' => $cookieJar,
  138. 'delay'=>300,
  139. 'query'=>['inter_not_need_sub'=>1]
  140. ]);
  141. dump('http code :');dump($res->getStatusCode());
  142. $return_info = json_decode($res->getBody()->getContents(),true);
  143. dump($return_info);
  144. if(isset($return_info['code']) && $return_info['code'] == 0)
  145. {
  146. $chapter_order = DB::connection('chapter_order_mysql')->table('chapter_orders'.$table)->where('uid',$user->uid)->where('bid',$_bid)->where('cid',$_cid)->first();
  147. if($chapter_order)//判断是否订购成功
  148. {
  149. $brush_read_time += mt_rand(60,125);//增加阅读时间
  150. $brush_read_data['chapter'][] = ['bid'=>$_bid,'cid'=>$_cid,'read_time'=>date('Y-m-d H:i:s',$brush_read_time)];
  151. $user_day_real_fee += $chapter_order->fee;//用户单日消耗
  152. if($chapter_count-1 == $seq) $ordered_books[] = $_bid;
  153. }
  154. }elseif(isset($return_info['code']) && $return_info['code'] > 0){
  155. break;
  156. }
  157. }
  158. }
  159. if($start_cid && $_chapter->id == $start_cid)
  160. {
  161. $start = true;
  162. }
  163. }
  164. }
  165. }
  166. $real_do_fee = $user_day_real_fee;
  167. $done_bids = implode(',',$ordered_books);
  168. $has_done = 1;
  169. $done_num = $user->done_num+1;
  170. $done_time = date('Y-m-d H:i:s');
  171. $up_data = compact('real_do_fee','done_bids','has_done','done_num','done_time');
  172. //更新刷数据用户
  173. DB::table('temp_pay_users')->where('id',$user->id)->update($up_data);
  174. $record_data = array_merge((array)$record_data,$up_data);
  175. unset($record_data['id']);
  176. //插入到records
  177. dump('record_data');dump($record_data);
  178. DB::table('temp_pay_users_records')->insert($record_data);
  179. //更新订阅时间
  180. foreach ($brush_read_data as $type=>$_item)
  181. {
  182. if($type=='book')
  183. {
  184. foreach ($_item as $_sub)
  185. {
  186. DB::table('book_orders')->where('uid',$user->uid)->where('bid',$_sub['bid'])->update(['created_at'=>$_sub['read_time'],'updated_at'=>$_sub['read_time']]);
  187. }
  188. }
  189. if($type=='chapter')
  190. {
  191. foreach ($_item as $_sub)
  192. {
  193. DB::connection('chapter_order_mysql')->table('chapter_orders'.$table)->where('uid',$user->uid)->where('bid',$_sub['bid'])->where('cid',$_sub['cid'])->update(['created_at'=>$_sub['read_time'],'updated_at'=>$_sub['read_time']]);
  194. }
  195. }
  196. }
  197. }
  198. });
  199. Log::info("======自动订阅 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  200. print_r("======自动订阅 【任务执行结束】=====" . date("y-m-d H:i:s" . "\n"));
  201. }
  202. }