GeneratePage.php 2.7 KB
<?php
/**
 * @remark :
 * @name   :GeneratePage.php
 * @author :lyh
 * @method :post
 * @time   :2025/2/10 17:00
 */

namespace App\Console\Commands\Project;

use App\Helper\Translate;
use App\Models\Com\NoticeLog;
use App\Models\Product\Keyword;
use App\Models\RouteMap\RouteMap;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class GeneratePage extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'generate_page';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '通知生成页面';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * @return bool
     */
    public function handle()
    {
        $noticeModel = new NoticeLog();
        while (true){
            $noticeInfo = $noticeModel->read(['status'=>0,'type'=>$noticeModel::GENERATE_PAGE,'start_at'=>['<=',date('Y-m-d H:i:s')]]);
            if (empty($noticeInfo)) {
                sleep(10);
                continue;
            }
            try {
                $this->output(' taskID: ' . $noticeInfo['id'] . ' start');
                $notice_data = json_decode($noticeInfo['data'],true);
                $c_url = $notice_data['c_url'];
                $c_params = json_encode($notice_data['c_params']);
                $re = http_post($c_url, $c_params, [], true);
                if (isset($re['status']) && $re['status'] == 200) {
                    $noticeModel->edit(['status'=>1],['id'=>$noticeInfo['id']]);
                    $this->output($c_url . ' | 请求成功');
                } else {
                    $noticeModel->edit(['status'=>2],['id'=>$noticeInfo['id']]);
                    $this->output($c_url . ' | ' . (json_encode($re,true) ?? '未返回失败原因'));
                }
                $this->output(' taskID: ' . $noticeInfo['id'] . ' end');
            } catch (\Exception $e) {
                $noticeModel->edit(['status'=>3],['id'=>$noticeInfo['id']]);
                @file_put_contents(storage_path('logs/lyh/lyh_error.log'), var_export('通知C端生成任务:'.date('Y-m-d H:i:s') . ' taskID: ' . $noticeInfo['id'] . ', error: ' . $e->getMessage() . PHP_EOL) . PHP_EOL, FILE_APPEND);
                $this->output(' taskID: ' . $noticeInfo['id'] . ', error: ' . $e->getMessage());
            }
            sleep(2);
        }
        return true;
    }


    /**
     * 输出message
     * @param $message
     */
    public function output($message)
    {
        echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
    }
}