作者 lyh

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

  1 +<?php
  2 +/**
  3 + * @remark :
  4 + * @name :UpdateKeyword.php
  5 + * @author :lyh
  6 + * @method :post
  7 + * @time :2024/7/3 9:23
  8 + */
  9 +
  10 +namespace App\Console\Commands\Update;
  11 +
  12 +use App\Models\Domain\DomainInfo;
  13 +use App\Models\Product\Keyword;
  14 +use App\Models\Product\KeywordPage;
  15 +use App\Services\ProjectServer;
  16 +use Illuminate\Console\Command;
  17 +use Illuminate\Support\Facades\DB;
  18 +
  19 +class UpdateKeyword extends Command
  20 +{
  21 + /**
  22 + * The name and signature of the console command.
  23 + *
  24 + * @var string
  25 + */
  26 + protected $signature = 'update_keyword';
  27 +
  28 + /**
  29 + * The console command description.
  30 + *
  31 + * @var string
  32 + */
  33 + protected $description = '批量更新关键词内容';
  34 +
  35 + public function handle(){
  36 + while (true){
  37 + $keywordPageModel = new KeywordPage();
  38 + $lists = $keywordPageModel->list(['status'=>0]);
  39 + if(empty($lists)){
  40 + sleep(100);
  41 + continue;
  42 + }
  43 + $domainModel = new DomainInfo();
  44 + foreach ($lists as $v){
  45 + ProjectServer::useProject($v['project_id']);
  46 + $this->saveKeywordContent($v);
  47 + DB::disconnect('custom_mysql');
  48 + //获取当前项目的域名
  49 + $domainInfo = $domainModel->read(['project_id'=>$v['project_id']]);
  50 + if($domainInfo !== false){
  51 + $this->curlDelRoute($domainInfo['domain'],$v['project_id']);
  52 + }
  53 + }
  54 + sleep(10);
  55 + }
  56 +
  57 + }
  58 +
  59 + /**
  60 + * @remark :更新关键词内容
  61 + * @name :saveKeywordContent
  62 + * @author :lyh
  63 + * @method :post
  64 + * @time :2024/7/3 10:25
  65 + */
  66 + public function saveKeywordContent($info){
  67 + $keywordModel = new Keyword();
  68 + $updateObject = json_decode($info['update_object'],true);
  69 + $text = json_decode($info['text'],true);
  70 + if(empty($text)){
  71 + return false;
  72 + }
  73 + $number = count($info['text']);
  74 + $randomNumber = rand(0, $number - 1);
  75 + if($updateObject['type'] == 0){//更新所有关键字
  76 + $keywordModel->edit(['keyword_content'=>$text[$randomNumber]],['status'=>1]);
  77 + }else{
  78 + //按传递的关键字更新
  79 + if(!empty($updateObject['keyword'])){
  80 + $updateObject['keyword'] = (array)$updateObject['keyword'];
  81 + $keywordModel->edit(['keyword_content'=>$text[$randomNumber]],['title'=>['in',$updateObject['keyword']]]);
  82 + }
  83 + //按给定的数量更新
  84 + if(!empty($updateObject['number']) && ($updateObject['number'] != 0)){
  85 + $keywordIdArr = $keywordModel->where("status",1)->inRandomOrder()->take($updateObject['number'])->pluck('id')->toArray();
  86 + $keywordModel->edit(['keyword_content'=>$text[$randomNumber]],['id'=>['in',$keywordIdArr]]);
  87 + }
  88 + }
  89 + return true;
  90 + }
  91 +
  92 + /**
  93 + * @remark :删除路由通知C端
  94 + * @name :curlDelRoute
  95 + * @author :lyh
  96 + * @method :post
  97 + * @time :2023/11/30 14:43
  98 + */
  99 + public function curlDelRoute($domain,$project_id){
  100 + if (strpos($domain, 'https://') === false) {
  101 + $domain = 'https://' . $domain . '/';
  102 + }
  103 + $url = $domain.'api/update_page/?project_id='.$project_id.'&route=4';
  104 + shell_exec('curl -k "'.$url.'"');
  105 + return true;
  106 + }
  107 +}
@@ -50,6 +50,8 @@ class Kernel extends ConsoleKernel @@ -50,6 +50,8 @@ class Kernel extends ConsoleKernel
50 $schedule->command('recommended_suppliers')->dailyAt('03:00')->withoutOverlapping(1); //每天凌晨1点执行一次生成推荐商 50 $schedule->command('recommended_suppliers')->dailyAt('03:00')->withoutOverlapping(1); //每天凌晨1点执行一次生成推荐商
51 // 每日推送视频任务 51 // 每日推送视频任务
52 $schedule->command('video_task')->hourly()->withoutOverlapping(1); 52 $schedule->command('video_task')->hourly()->withoutOverlapping(1);
  53 + // 每日推送视频任务
  54 + $schedule->command('update_keyword')->hourly()->withoutOverlapping(1);
53 // 每日推送已完成视频任务项目生成对应界面 55 // 每日推送已完成视频任务项目生成对应界面
54 $schedule->command('notice_c')->dailyAt('04:00')->withoutOverlapping(1); 56 $schedule->command('notice_c')->dailyAt('04:00')->withoutOverlapping(1);
55 } 57 }
@@ -256,6 +256,7 @@ class KeywordController extends BaseController @@ -256,6 +256,7 @@ class KeywordController extends BaseController
256 $keywordPageModel = new KeywordPage(); 256 $keywordPageModel = new KeywordPage();
257 $this->param['text'] = json_encode($this->param['text']); 257 $this->param['text'] = json_encode($this->param['text']);
258 $this->param['update_object'] = json_encode($this->param['update_object']); 258 $this->param['update_object'] = json_encode($this->param['update_object']);
  259 + $this->param['project_id'] = $this->user['project_id'];
259 $id = $keywordPageModel->addReturnId($this->param); 260 $id = $keywordPageModel->addReturnId($this->param);
260 $this->response('success',Code::SUCCESS,['id'=>$id]); 261 $this->response('success',Code::SUCCESS,['id'=>$id]);
261 } 262 }
@@ -14,6 +14,4 @@ use App\Models\Base; @@ -14,6 +14,4 @@ use App\Models\Base;
14 class KeywordPage extends Base 14 class KeywordPage extends Base
15 { 15 {
16 protected $table = 'gl_product_keyword_page'; 16 protected $table = 'gl_product_keyword_page';
17 - //连接数据库  
18 - protected $connection = 'custom_mysql';  
19 } 17 }