123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace App\Console\Commands;
- use App\Log;
- use App\Models\Account;
- use App\Models\Order;
- use App\Services\HttpService;
- use App\Services\YWService;
- use Illuminate\Console\Command;
- class YWOrderList extends Command
- {
- protected $signature = 'YWOrderList {label} {year?}';
- protected $description = '阅文订单数据爬取';
- protected $lastMinId = '';
- protected $lastMaxId = '';
- protected $lastPage = '';
- protected $totalCount = '';
- protected $label;
- protected $year;
- protected $page;
- public function handle()
- {
- \DB::connection()->disableQueryLog();
- $this->label = $this->argument('label') ? $this->argument('label') : 1;
- $this->year = $this->argument('year') ? $this->argument('year') : null;
- $this->getOrderList();
- }
- public function getOrderList()
- {
- if(is_null($this->year)) {
- if(date('Y-m-d H:i:s') <= date('Y-m-d') . ' 00:00:10') {
- $date = date('Y-m-d', strtotime('-1 days'));
- } else {
- $date = date('Y-m-d');
- }
-
- $this->order($date);
- } else {
- for ($month = 1; $month <= 12; $month++) {
- $days = getDaysByMonth($this->year, $month);
- foreach ($days as $day) {
- $this->lastMinId = '';
- $this->lastMaxId = '';
- $this->lastPage = '';
- $this->totalCount = '';
- if($day > date("Y-m-d"))
- return false;
- $this->order($day);
- }
- }
- }
- return true;
- }
- public function order($date)
- {
- try {
- $this->info($date);
- $sTime = $date . ' 00:00:00';
- $eTime = (($date . ' 23:59:59') > date('Y-m-d H:i:s')) ? date('Y-m-d H:i:s') : $date . ' 23:59:59';
- $this->page = 1;
- do {
- $this->info('当前页码数:'.$this->page);
- $params = [
- 'start_time' => strtotime($sTime),
- 'end_time' => strtotime($eTime),
- 'page' => $this->page,
- 'last_min_id' => $this->lastMinId,
- 'last_max_id' => $this->lastMaxId,
- 'total_count' => $this->totalCount,
- 'last_page' => $this->lastPage,
- ];
- # 签名
- $params = YWService::sign($params, $this->label);
- $requestUri = YWService::API_BASE_URL . YWService::ORDER_LIST . '?' . http_build_query($params);
- # 获取列表
- $response = HttpService::httpGet($requestUri);
- if($response === false)
- Log::logError('阅文订单数据获取失败' . $requestUri, $params, 'YWOrderList');
- $responseData = json_decode($response, true);
- $total = 0;
- if(isset($responseData['code']) && $responseData['code']== 0) {
- $list = $responseData['data']['list'];
- $total = count($list);
- $this->info('本次获取订单条数:'.$total);
- $this->lastMinId = $responseData['data']['min_id'];
- $this->lastMaxId = $responseData['data']['max_id'];
- $this->lastPage = $this->page;
- $this->totalCount = $responseData['data']['total_count'];
- $platformId = YWService::PLATFORM_ID;
- $orderStatus = YWService::ORDER_STATUS;
- # 获取渠道信息
- $channelList = Account::getChannelIdByPlatform($platformId);
- foreach ($list as $item) {
- $insertData = [
- 'platform_id' => $platformId,
- 'channel_id' => isset($channelList[$item['app_name']]['channel_id']) ? $channelList[$item['app_name']]['channel_id'] : null,
- 'app_id' => isset($channelList[$item['app_name']]['app_id']) ? $channelList[$item['app_name']]['app_id'] : null,
- 'order_id' => $item['yworder_id'],
- 'border_id' => $item['order_id'],
- 'member_openid' => isset($item['openid']) ? $item['openid'] : null,
- 'subscribed_at' => isset($item['sub_time']) ? $item['sub_time'] : null,
- 'user_created_at' => isset($item['reg_time']) ? $item['reg_time'] : null,
- 'product' => null,
- 'price' => $item['amount'] * 100,
- 'status' => isset($orderStatus[$item['order_status']]) ?
- $orderStatus[$item['order_status']] : -1,
- 'agent_uid' => null,
- 'from_novel' => isset($item['book_name']) ? $item['book_name'] : null,
- 'referral_link_id' => null,
- 'from_novel_id' => $item['channel_id'],
- 'platform_created_at' => $item['order_time'],
- 'paid_at' => isset($item['pay_time']) ? $item['pay_time'] : null,
- ];
- if(empty($insertData['channel_id'])) {
- Log::logError('阅文订单数据存在异常数据', $insertData, 'YWOrderList');
- continue;
- }
- # 创建或更新
- Order::updateOrCreate(
- ['platform_id'=>$platformId, 'order_id'=>$insertData['order_id'], 'channel_id'=>$insertData['channel_id']], $insertData
- );
- }
- $this->page++;
- } else {
- Log::logError('阅文平台订单数据获取发生异常'.$response, $params, 'YWOrderList');
- return false;
- }
- } while ($total == YWService::PER_PAGE);
- } catch (\Exception $e) {
- Log::logError('阅文平台订单数据异常', [
- 'msg' => $e->getMessage(),
- 'line' => $e->getLine()
- ], 'YWOrderList');
- return false;
- }
- return true;
- }
- }
|