小说推广数据系统

YWOrderList.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Log;
  4. use App\Models\Account;
  5. use App\Models\Order;
  6. use App\Services\HttpService;
  7. use App\Services\YWService;
  8. use Illuminate\Console\Command;
  9. class YWOrderList extends Command
  10. {
  11. protected $signature = 'YWOrderList {label} {year?}';
  12. protected $description = '阅文订单数据爬取';
  13. protected $lastMinId = '';
  14. protected $lastMaxId = '';
  15. protected $lastPage = '';
  16. protected $totalCount = '';
  17. protected $label;
  18. protected $year;
  19. protected $page;
  20. public function handle()
  21. {
  22. \DB::connection()->disableQueryLog();
  23. $this->label = $this->argument('label') ? $this->argument('label') : 1;
  24. $this->year = $this->argument('year') ? $this->argument('year') : null;
  25. $this->getOrderList();
  26. }
  27. public function getOrderList()
  28. {
  29. if(is_null($this->year)) {
  30. if(date('Y-m-d H:i:s') <= date('Y-m-d') . ' 00:00:10') {
  31. $date = date('Y-m-d', strtotime('-1 days'));
  32. } else {
  33. $date = date('Y-m-d');
  34. }
  35. $this->order($date);
  36. } else {
  37. for ($month = 1; $month <= 12; $month++) {
  38. $days = getDaysByMonth($this->year, $month);
  39. foreach ($days as $day) {
  40. $this->lastMinId = '';
  41. $this->lastMaxId = '';
  42. $this->lastPage = '';
  43. $this->totalCount = '';
  44. if($day > date("Y-m-d"))
  45. return false;
  46. $this->order($day);
  47. }
  48. }
  49. }
  50. return true;
  51. }
  52. public function order($date)
  53. {
  54. try {
  55. $this->info($date);
  56. $sTime = $date . ' 00:00:00';
  57. $eTime = (($date . ' 23:59:59') > date('Y-m-d H:i:s')) ? date('Y-m-d H:i:s') : $date . ' 23:59:59';
  58. $this->page = 1;
  59. do {
  60. $this->info('当前页码数:'.$this->page);
  61. $params = [
  62. 'start_time' => strtotime($sTime),
  63. 'end_time' => strtotime($eTime),
  64. 'page' => $this->page,
  65. 'last_min_id' => $this->lastMinId,
  66. 'last_max_id' => $this->lastMaxId,
  67. 'total_count' => $this->totalCount,
  68. 'last_page' => $this->lastPage,
  69. ];
  70. # 签名
  71. $params = YWService::sign($params, $this->label);
  72. $requestUri = YWService::API_BASE_URL . YWService::ORDER_LIST . '?' . http_build_query($params);
  73. # 获取列表
  74. $response = HttpService::httpGet($requestUri);
  75. if($response === false)
  76. Log::logError('阅文订单数据获取失败' . $requestUri, $params, 'YWOrderList');
  77. $responseData = json_decode($response, true);
  78. $total = 0;
  79. if(isset($responseData['code']) && $responseData['code']== 0) {
  80. $list = $responseData['data']['list'];
  81. $total = count($list);
  82. $this->info('本次获取订单条数:'.$total);
  83. $this->lastMinId = $responseData['data']['min_id'];
  84. $this->lastMaxId = $responseData['data']['max_id'];
  85. $this->lastPage = $this->page;
  86. $this->totalCount = $responseData['data']['total_count'];
  87. $platformId = YWService::PLATFORM_ID;
  88. $orderStatus = YWService::ORDER_STATUS;
  89. # 获取渠道信息
  90. $channelList = Account::getChannelIdByPlatform($platformId);
  91. foreach ($list as $item) {
  92. $insertData = [
  93. 'platform_id' => $platformId,
  94. 'channel_id' => isset($channelList[$item['app_name']]['channel_id']) ? $channelList[$item['app_name']]['channel_id'] : null,
  95. 'app_id' => isset($channelList[$item['app_name']]['app_id']) ? $channelList[$item['app_name']]['app_id'] : null,
  96. 'order_id' => $item['yworder_id'],
  97. 'border_id' => $item['order_id'],
  98. 'member_openid' => isset($item['openid']) ? $item['openid'] : null,
  99. 'subscribed_at' => isset($item['sub_time']) ? $item['sub_time'] : null,
  100. 'user_created_at' => isset($item['reg_time']) ? $item['reg_time'] : null,
  101. 'product' => null,
  102. 'price' => $item['amount'] * 100,
  103. 'status' => isset($orderStatus[$item['order_status']]) ?
  104. $orderStatus[$item['order_status']] : -1,
  105. 'agent_uid' => null,
  106. 'from_novel' => isset($item['book_name']) ? $item['book_name'] : null,
  107. 'referral_link_id' => null,
  108. 'from_novel_id' => $item['channel_id'],
  109. 'platform_created_at' => $item['order_time'],
  110. 'paid_at' => isset($item['pay_time']) ? $item['pay_time'] : null,
  111. ];
  112. if(empty($insertData['channel_id'])) {
  113. Log::logError('阅文订单数据存在异常数据', $insertData, 'YWOrderList');
  114. continue;
  115. }
  116. # 创建或更新
  117. Order::updateOrCreate(
  118. ['platform_id'=>$platformId, 'order_id'=>$insertData['order_id'], 'channel_id'=>$insertData['channel_id']], $insertData
  119. );
  120. }
  121. $this->page++;
  122. } else {
  123. Log::logError('阅文平台订单数据获取发生异常'.$response, $params, 'YWOrderList');
  124. return false;
  125. }
  126. } while ($total == YWService::PER_PAGE);
  127. } catch (\Exception $e) {
  128. Log::logError('阅文平台订单数据异常', [
  129. 'msg' => $e->getMessage(),
  130. 'line' => $e->getLine()
  131. ], 'YWOrderList');
  132. return false;
  133. }
  134. return true;
  135. }
  136. }