作者 赵彬吉

update

  1 +<?php
  2 +
  3 +namespace App\Console\Commands;
  4 +
  5 +use App\Helper\Common;
  6 +use App\Services\ProjectServer;
  7 +use Illuminate\Console\Command;
  8 +use Illuminate\Support\Facades\DB;
  9 +use Illuminate\Support\Facades\Redis;
  10 +
  11 +/**
  12 + * 初始化项目
  13 + * Class InitProject
  14 + * @package App\Console\Commands
  15 + * @author zbj
  16 + * @date 2023/10/8
  17 + */
  18 +class UpdateSeoTdk extends Command
  19 +{
  20 + /**
  21 + * The name and signature of the console command.
  22 + *
  23 + * @var string
  24 + */
  25 + protected $signature = 'update_seo_tdk';
  26 +
  27 + /**
  28 + * The console command description.
  29 + *
  30 + * @var string
  31 + */
  32 + protected $description = '';
  33 +
  34 + /**
  35 + * Create a new command instance.
  36 + *
  37 + * @return void
  38 + */
  39 + public function __construct()
  40 + {
  41 + parent::__construct();
  42 + }
  43 +
  44 + /**
  45 + * @return bool
  46 + */
  47 + public function handle()
  48 + {
  49 + while (true){
  50 + $project_id = Redis::rpop('updateSeoTdk');
  51 + if(!$project_id){
  52 + sleep(2);
  53 + }
  54 + ProjectServer::useProject($project_id);
  55 + $this->updateProduct($project_id);
  56 + $this->updateBlogs($project_id);
  57 + $this->updateNews($project_id);
  58 + DB::disconnect('custom_mysql');
  59 + }
  60 + }
  61 +
  62 + /**
  63 + * @remark :更新产品tdk
  64 + * @name :updateProduct
  65 + * @author :lyh
  66 + * @method :post
  67 + * @time :2023/8/19 9:25
  68 + */
  69 + public function updateProduct($project_id){
  70 + $list = DB::connection('custom_mysql')->table('gl_product')->where(['status'=>1,'project_id'=>$project_id])->get()->toArray();
  71 + if(!empty($list)){
  72 + foreach ($list as $v){
  73 + $v = (array)$v;
  74 + if(!empty($v['seo_mate'])){
  75 + $seo_arr = json_decode($v['seo_mate'], true);
  76 + //更新seo_title
  77 + if(!isset($seo_arr['title'])){
  78 + //生成seo_title
  79 + $seo_arr['title'] = $this->ai_send('product_seo_title',$v['title']);
  80 + }
  81 + //更新seo_keyword
  82 + if(!isset($seo_arr['keyword'])){
  83 + $seo_arr['keyword'] = $this->ai_send('product_seo_keyword',$v['title']);
  84 + }
  85 + //更新seo_keyword
  86 + if(!isset($seo_arr['description'])){
  87 + $seo_arr['description'] = $this->ai_send('product_seo_description',$v['title']);
  88 + }
  89 + $ser_str = json_encode($seo_arr,true);
  90 + DB::connection('custom_mysql')->table('gl_product')->where(['id'=>$v['id']])->update(['seo_mate'=>$ser_str]);
  91 + }
  92 + }
  93 + }
  94 + return true;
  95 + }
  96 +
  97 + /**
  98 + * @remark :更新新闻Tdk
  99 + * @name :updateNews
  100 + * @author :lyh
  101 + * @method :post
  102 + * @time :2023/8/19 10:06
  103 + */
  104 + public function updateNews($project_id){
  105 + $list = DB::connection('custom_mysql')->table('gl_news')->where(['status'=>1,'project_id'=>$project_id])->get()->toArray();
  106 + if(!empty($list)){
  107 + foreach ($list as $k => $v){
  108 + $v = (array)$v;
  109 + $data = [];
  110 + if(empty($v['seo_title'])){
  111 + $data['seo_title'] = $this->ai_send('news_seo_title',$v['name']);
  112 + }
  113 + if(empty($v['seo_keywords'])){
  114 + $data['seo_keywords'] = $this->ai_send('news_seo_keyword',$v['name']);
  115 + }
  116 + if(empty($v['seo_description'])){
  117 + $data['seo_description'] = $this->ai_send('news_seo_description',$v['name']);
  118 + }
  119 + DB::connection('custom_mysql')->table('gl_news')->where(['id'=>$v['id']])->update($data);
  120 + }
  121 + }
  122 + return true;
  123 + }
  124 +
  125 + /**
  126 + * @remark :更新blogTdk
  127 + * @name :updateBlogs
  128 + * @author :lyh
  129 + * @method :post
  130 + * @time :2023/8/19 10:07
  131 + */
  132 + public function updateBlogs($project_id){
  133 + $list = DB::connection('custom_mysql')->table('gl_blog')->where(['status'=>1,'project_id'=>$project_id])->get()->toArray();
  134 + if(!empty($list)){
  135 + foreach ($list as $k => $v){
  136 + $v = (array)$v;
  137 + $data = [];
  138 + if(empty($v['seo_title'])){
  139 + $data['seo_title'] = $this->ai_send('blog_seo_title',$v['name']);
  140 + }
  141 + if(empty($v['seo_keywords'])){
  142 + $data['seo_keywords'] = $this->ai_send('blog_seo_keyword',$v['name']);
  143 + }
  144 + if(empty($v['seo_description'])){
  145 + $data['seo_description'] = $this->ai_send('blog_seo_description',$v['name']);
  146 + }
  147 + DB::connection('custom_mysql')->table('gl_product')->where(['id'=>$v['id']])->update($data);
  148 + }
  149 + }
  150 + return true;
  151 + }
  152 +
  153 + /**
  154 + * @remark :AI发送
  155 + * @name :ai_send
  156 + * @author :lyh
  157 + * @method :post
  158 + * @time :2023/8/19 10:40
  159 + */
  160 + public function ai_send($key,$keywords){
  161 + $chat_url = 'v2/openai_chat';
  162 + $param = [
  163 + 'key'=>$key,
  164 + 'keywords'=>$keywords,
  165 + ];
  166 + $data = Common::send_openai_msg($chat_url,$param);
  167 + return $data['text'];
  168 + }
  169 +}
@@ -13,6 +13,7 @@ use App\Helper\Common; @@ -13,6 +13,7 @@ use App\Helper\Common;
13 use App\Http\Controllers\Bside\BaseController; 13 use App\Http\Controllers\Bside\BaseController;
14 use App\Services\ProjectServer; 14 use App\Services\ProjectServer;
15 use Illuminate\Support\Facades\DB; 15 use Illuminate\Support\Facades\DB;
  16 +use Illuminate\Support\Facades\Redis;
16 17
17 /** 18 /**
18 * @remark :b端网站更新相关 19 * @remark :b端网站更新相关
@@ -36,12 +37,13 @@ class UpdateController extends BaseController @@ -36,12 +37,13 @@ class UpdateController extends BaseController
36 ],[ 37 ],[
37 'project_id.required' => 'project_id不能为空', 38 'project_id.required' => 'project_id不能为空',
38 ]); 39 ]);
39 - ProjectServer::useProject($this->param['project_id']);  
40 - $this->updateProduct($this->param['project_id']);  
41 - $this->updateBlogs($this->param['project_id']);  
42 - $this->updateNews($this->param['project_id']);  
43 - DB::disconnect('custom_mysql');  
44 - $this->response('success'); 40 + Redis::lpush('updateSeoTdk', $this->param['project_id']);
  41 +// ProjectServer::useProject($this->param['project_id']);
  42 +// $this->updateProduct($this->param['project_id']);
  43 +// $this->updateBlogs($this->param['project_id']);
  44 +// $this->updateNews($this->param['project_id']);
  45 +// DB::disconnect('custom_mysql');
  46 + $this->response('任务添加成功');
45 } 47 }
46 48
47 /** 49 /**