AiBlogAuthorId.php 5.6 KB
<?php
/**
 * @remark :
 * @name   :AiBlogAuthorId.php
 * @author :lyh
 * @method :post
 * @time   :2025/5/26 15:57
 */

namespace App\Console\Commands\Ai;

use App\Helper\Arr;
use App\Models\Com\Notify;
use App\Models\Devops\ServerConfig;
use App\Models\Devops\ServersIp;
use App\Models\Domain\DomainInfo;
use App\Models\Project\AiBlogTask as AiBlogTaskModel;
use App\Models\Ai\AiBlogAuthor as AiBlogAuthorModel;
use App\Models\Project\Project;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '拉取对应作者的页面';

    public $route = [];

    public function handle(){
        while (true){
            //获取任务id
            $task_id = $this->getTaskId();
            if(empty($task_id)){
                sleep(300);
                continue;
            }
            $this->_action($task_id);
        }
    }

    public function getTaskId()
    {
        $task_id = Redis::rpop('ai_blog_author_id');
        if (empty($task_id)) {
            $aiBlogTaskModel = new AiBlogTaskModel();
            $ids = $aiBlogTaskModel->formatQuery(['status'=>$aiBlogTaskModel::STATUS_RUNNING, 'type'=>$aiBlogTaskModel::TYPE_AUTHOR_ID])->pluck('id');
            if(!empty($ids)){
                foreach ($ids as $id) {
                    Redis::lpush('ai_blog_author_id', $id);
                }
            }
            $task_id = Redis::rpop('ai_blog_author_id');
        }
        return $task_id;
    }

    /**
     * @remark :执行方法
     * @name   :_action
     * @author :lyh
     * @method :post
     * @time   :2025/5/26 16:06
     */
    public function _action($task_id){
        $aiBlogTaskModel = new AiBlogTaskModel();
        $item = $aiBlogTaskModel->read(['id'=>$task_id]);
        if($item === false){
            echo '当前数据不存在.'.$item['id'].PHP_EOL;
            return true;
        }
        $aiBlogService = new AiBlogService($item['project_id']);
        ProjectServer::useProject($item['project_id']);
        $aiBlogService->author_id = $item['task_id'];
        $result = $aiBlogService->getAuthorDetail();
        if(isset($result['status']) && $result['status'] == 200){
            //当前作者的页面
            $aiBlogAuthorModel = new AiBlogAuthorModel();
            $authorInfo = $aiBlogAuthorModel->read(['author_id'=>$item['task_id']],['id','route']);
            if($authorInfo !== false && !empty($result['data']['section'])){
                $this->route[] = $authorInfo['route'];
                $aiBlogAuthorModel->edit(['text'=>$result['data']['section']],['author_id'=>$item['task_id']]);
            }
        }
        DB::disconnect('custom_mysql');
        $aiBlogTaskModel->edit(['status'=>2],['id'=>$task_id]);
        $this->sendCPost($item['project_id']);
        return true;
    }

    /**
     * @remark :通知C端
     * @name   :sendCPost
     * @author :lyh
     * @method :post
     * @time   :2025/5/26 16:21
     */
    public function sendCPost($project_id){
        //获取项目所在服务器
        $project_model = new Project();
        $project_info = $project_model->read(['id'=>$project_id],['serve_id','is_upgrade', 'main_lang_id']);
        if(!$project_info){
            return false;
        }
        $serve_ip_model = new ServersIp();
        $serve_ip_info = $serve_ip_model->read(['id'=>$project_info['serve_id']],['servers_id']);
        $servers_id = $serve_ip_info ? $serve_ip_info['servers_id'] : 0;
        if($servers_id == ServerConfig::SELF_SITE_ID){
            //自建站服务器:如果项目已经上线,不请求C端接口,数据直接入库
            $domain_model = new DomainInfo();
            $domain_info = $domain_model->read(['project_id'=>$project_id],['domain']);
            if($domain_info){
                //判断是否已有更新进行中
                $notify_model = new Notify();
                $data = [
                    'project_id' => $project_id,
                    'type' => 1,
                    'route' => 3,
                    'server_id' => ServerConfig::SELF_SITE_ID,
                    'status' => ['!=',Notify::STATUS_FINISH_SITEMAP]
                ];
                $notify = $notify_model->read($data,['id']);
                if(!$notify){
                    $domain = $domain_info['domain'];
                    $data['data'] = Arr::a2s(['domain'=>$domain,'url'=>$this->route,'language'=>[]]);
                    $data['status'] = Notify::STATUS_INIT;
                    $data['is_pull_html_zip'] = Notify::IS_PULL_HTML_ZIP_FALSE;
                    $data['sort'] = 1;
                    $notify_model->add($data);
                }
            }
        }else{
            $domainModel = new DomainInfo();
            $domain = $domainModel->getProjectIdDomain($project_id);
            $c_url = $domain.'api/update_page/';
            $param = [
                'project_id' => $project_id,
                'type' => 1,
                'route' => 3,
                'url' => $this->route,
                'language'=> [],
                'is_sitemap' => 0
            ];
            $res = http_post($c_url, json_encode($param,true));
            echo 'notify: project id: ' . $project_id . ', result: ' . json_encode($res,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
        }
    }
}