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

namespace App\Console\Commands\Ai;

use App\Models\Domain\DomainInfo;
use App\Models\Project\AiBlogTask as AiBlogTaskModel;
use App\Models\Ai\AiBlogAuthor as AiBlogAuthorModel;
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){
        $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);
    }
}