作者 lyh

更新自动更新关键字keyword脚本

<?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\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';
/**
* 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']);
}
}
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($info['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;
}
}
... ...
... ... @@ -50,6 +50,8 @@ class Kernel extends ConsoleKernel
$schedule->command('recommended_suppliers')->dailyAt('03:00')->withoutOverlapping(1); //每天凌晨1点执行一次生成推荐商
// 每日推送视频任务
$schedule->command('video_task')->hourly()->withoutOverlapping(1);
// 每日推送视频任务
$schedule->command('update_keyword')->hourly()->withoutOverlapping(1);
// 每日推送已完成视频任务项目生成对应界面
$schedule->command('notice_c')->dailyAt('04:00')->withoutOverlapping(1);
}
... ...
... ... @@ -256,6 +256,7 @@ class KeywordController extends BaseController
$keywordPageModel = new KeywordPage();
$this->param['text'] = json_encode($this->param['text']);
$this->param['update_object'] = json_encode($this->param['update_object']);
$this->param['project_id'] = $this->user['project_id'];
$id = $keywordPageModel->addReturnId($this->param);
$this->response('success',Code::SUCCESS,['id'=>$id]);
}
... ...
... ... @@ -14,6 +14,4 @@ use App\Models\Base;
class KeywordPage extends Base
{
protected $table = 'gl_product_keyword_page';
//连接数据库
protected $connection = 'custom_mysql';
}
... ...