UpdateKeyword.php 3.4 KB
<?php
/**
 * @remark :
 * @name   :UpdateKeyword.php
 * @author :lyh
 * @method :post
 * @time   :2024/7/3 9:23
 */

namespace App\Console\Commands\Update;

use App\Models\Domain\DomainInfo;
use App\Models\Product\Keyword;
use App\Models\Product\KeywordPage;
use App\Models\Product\Product;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

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

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '批量更新关键词内容';


    public function handle(){
        while (true){
            $keywordPageModel = new KeywordPage();
            $lists = $keywordPageModel->list(['status'=>0]);
            if(empty($lists)){
                sleep(100);
                continue;
            }
            $domainModel = new DomainInfo();
            foreach ($lists as $v){
                ProjectServer::useProject($v['project_id']);
                $this->saveKeywordContent($v);
                DB::disconnect('custom_mysql');
                //获取当前项目的域名
                $domainInfo = $domainModel->read(['project_id'=>$v['project_id']]);
                if($domainInfo !== false){
                    $this->curlDelRoute($domainInfo['domain'],$v['project_id']);
                }
                $keywordPageModel->edit(['status'=>1],['id'=>$v['id']]);
            }
            sleep(10);
        }

    }

    /**
     * @remark :更新关键词内容
     * @name   :saveKeywordContent
     * @author :lyh
     * @method :post
     * @time   :2024/7/3 10:25
     */
    public function saveKeywordContent($info){
        $keywordModel = new Keyword();
        $updateObject = json_decode($info['update_object'],true);
        $text = json_decode($info['text'],true);
        if(empty($text)){
            return false;
        }
        $number = count($text);
        $randomNumber = rand(0, $number - 1);
        if($updateObject['type'] == 0){//更新所有关键字
            $keywordModel->edit(['keyword_content'=>$text[$randomNumber]],['status'=>1]);
        }else{
            //按传递的关键字更新
            if(!empty($updateObject['keyword'])){
                $updateObject['keyword'] = (array)$updateObject['keyword'];
                $keywordModel->edit(['keyword_content'=>$text[$randomNumber]],['title'=>['in',$updateObject['keyword']]]);
            }
            //按给定的数量更新
            if(!empty($updateObject['number']) && ($updateObject['number'] != 0)){
                $keywordIdArr = $keywordModel->where("status",1)->inRandomOrder()->take($updateObject['number'])->pluck('id')->toArray();
                $keywordModel->edit(['keyword_content'=>$text[$randomNumber]],['id'=>['in',$keywordIdArr]]);
            }
        }
        return true;
    }

    /**
     * @remark :删除路由通知C端
     * @name   :curlDelRoute
     * @author :lyh
     * @method :post
     * @time   :2023/11/30 14:43
     */
    public function curlDelRoute($domain,$project_id){
        if (strpos($domain, 'https://') === false) {
            $domain = 'https://' . $domain . '/';
        }
        $url = $domain.'api/update_page/?project_id='.$project_id.'&route=4';
        shell_exec('curl -k "'.$url.'"');
        return true;
    }

}