正在显示
2 个修改的文件
包含
277 行增加
和
303 行删除
| @@ -2,7 +2,10 @@ | @@ -2,7 +2,10 @@ | ||
| 2 | 2 | ||
| 3 | namespace App\Console\Commands; | 3 | namespace App\Console\Commands; |
| 4 | 4 | ||
| 5 | +use App\Helper\Arr; | ||
| 5 | use App\Helper\Common; | 6 | use App\Helper\Common; |
| 7 | +use App\Helper\Gpt; | ||
| 8 | +use App\Helper\Translate; | ||
| 6 | use App\Models\Ai\AiCommand; | 9 | use App\Models\Ai\AiCommand; |
| 7 | use App\Models\Project\DeployOptimize; | 10 | use App\Models\Project\DeployOptimize; |
| 8 | use App\Services\ProjectServer; | 11 | use App\Services\ProjectServer; |
| @@ -31,7 +34,7 @@ class UpdateSeoTdk extends Command | @@ -31,7 +34,7 @@ class UpdateSeoTdk extends Command | ||
| 31 | * | 34 | * |
| 32 | * @var string | 35 | * @var string |
| 33 | */ | 36 | */ |
| 34 | - protected $description = '一键生成sdk'; | 37 | + protected $description = '一键生成tdk'; |
| 35 | 38 | ||
| 36 | /** | 39 | /** |
| 37 | * Create a new command instance. | 40 | * Create a new command instance. |
| @@ -44,318 +47,212 @@ class UpdateSeoTdk extends Command | @@ -44,318 +47,212 @@ class UpdateSeoTdk extends Command | ||
| 44 | } | 47 | } |
| 45 | 48 | ||
| 46 | /** | 49 | /** |
| 50 | + * '表' => [ | ||
| 51 | + * '指令key' => '表字段' | ||
| 52 | + * ] | ||
| 53 | + * @return array | ||
| 54 | + * @author zbj | ||
| 55 | + * @date 2023/11/3 | ||
| 56 | + */ | ||
| 57 | + protected $maps = [ | ||
| 58 | + 'gl_web_custom_template' => [ | ||
| 59 | + 'page_title' => 'title', | ||
| 60 | + 'page_meta_keywords' => 'keywords', | ||
| 61 | + 'page_meta_description' => 'description', | ||
| 62 | + ], | ||
| 63 | + 'gl_product' => [ | ||
| 64 | + 'product_title' => 'seo_mate.title', | ||
| 65 | + 'product_meta_keywords' => 'seo_mate.keyword', | ||
| 66 | + 'product_meta_description' => 'seo_mate.description', | ||
| 67 | + ], | ||
| 68 | + 'gl_product_category' => [ | ||
| 69 | + 'product_cat_title' => 'seo_title', | ||
| 70 | + 'product_cat_meta_keywords' => 'seo_keywords', | ||
| 71 | + 'product_cat_meta_description' => 'seo_des', | ||
| 72 | + ], | ||
| 73 | + 'gl_blog' => [ | ||
| 74 | + 'blog_title' => 'seo_title', | ||
| 75 | + 'blog_meta_keywords' => 'seo_keywords', | ||
| 76 | + 'blog_meta_description' => 'seo_description', | ||
| 77 | + ], | ||
| 78 | + 'gl_blog_category' => [ | ||
| 79 | + 'blog_cat_title' => 'seo_title', | ||
| 80 | + 'blog_cat_meta_keywords' => 'seo_keywords', | ||
| 81 | + 'blog_cat_meta_description' => 'seo_des', | ||
| 82 | + ], | ||
| 83 | + 'gl_news' => [ | ||
| 84 | + 'news_title' => 'seo_title', | ||
| 85 | + 'news_meta_keywords' => 'seo_keywords', | ||
| 86 | + 'news_meta_description' => 'seo_description', | ||
| 87 | + ], | ||
| 88 | + 'gl_news_category' => [ | ||
| 89 | + 'news_cat_title' => 'seo_title', | ||
| 90 | + 'news_cat_meta_keywords' => 'seo_keywords', | ||
| 91 | + 'news_cat_meta_description' => 'seo_des', | ||
| 92 | + ], | ||
| 93 | + 'gl_product_keyword' => [ | ||
| 94 | + 'tags_title' => 'seo_title', | ||
| 95 | + 'tags_meta_keywords' => 'seo_keywords', | ||
| 96 | + 'tags_meta_description' => 'seo_description', | ||
| 97 | + 'tags_content_title' => 'keyword_title', | ||
| 98 | + 'tags_content_description' => 'keyword_content', | ||
| 99 | + ] | ||
| 100 | + ]; | ||
| 101 | + | ||
| 102 | + /** | ||
| 103 | + * topic-对相应字段 | ||
| 104 | + * @var array | ||
| 105 | + */ | ||
| 106 | + protected $topic_fields = [ | ||
| 107 | + 'gl_web_custom_template' => 'name', | ||
| 108 | + 'gl_product' => 'title', | ||
| 109 | + 'gl_product_category' => 'title', | ||
| 110 | + 'gl_blog' => 'name', | ||
| 111 | + 'gl_blog_category' => 'name', | ||
| 112 | + 'gl_news' => 'name', | ||
| 113 | + 'gl_news_category' => 'name', | ||
| 114 | + 'gl_product_keyword' => 'title' | ||
| 115 | + ]; | ||
| 116 | + | ||
| 117 | + /** | ||
| 118 | + * 使用核心关键词的key 数量 | ||
| 119 | + * @var array | ||
| 120 | + */ | ||
| 121 | + protected $core_keyword_keys = [ | ||
| 122 | + 'page_meta_keywords' => 1, | ||
| 123 | + 'blog_cat_meta_keywords' => 8, | ||
| 124 | + 'news_cat_meta_keywords' => 8, | ||
| 125 | + ]; | ||
| 126 | + | ||
| 127 | + /** | ||
| 47 | * @return bool | 128 | * @return bool |
| 48 | */ | 129 | */ |
| 49 | public function handle() | 130 | public function handle() |
| 50 | { | 131 | { |
| 51 | - while (true){ | 132 | + while (true) { |
| 52 | $project_id = Redis::rpop('updateSeoTdk'); | 133 | $project_id = Redis::rpop('updateSeoTdk'); |
| 53 | - if(!$project_id){ | 134 | + if (!$project_id) { |
| 54 | sleep(2); | 135 | sleep(2); |
| 55 | continue; | 136 | continue; |
| 56 | } | 137 | } |
| 57 | - echo date('Y-m-d H:i:s') . ' start: ' . $project_id . PHP_EOL; | 138 | + echo date('Y-m-d H:i:s') . ' start project_id: ' . $project_id . PHP_EOL; |
| 58 | try { | 139 | try { |
| 59 | ProjectServer::useProject($project_id); | 140 | ProjectServer::useProject($project_id); |
| 60 | - $this->updateProduct($project_id); | ||
| 61 | - $this->updateProductCate($project_id); | ||
| 62 | - $this->updateBlogs($project_id); | ||
| 63 | - $this->updateBlogCate($project_id); | ||
| 64 | - $this->updateNews($project_id); | ||
| 65 | - $this->updateNewsCate($project_id); | ||
| 66 | - $this->updatePage($project_id); | ||
| 67 | - DB::disconnect('custom_mysql'); | ||
| 68 | - }catch (\Exception $e){ | 141 | + $this->seo_tdk($project_id); |
| 142 | + } catch (\Exception $e) { | ||
| 69 | echo date('Y-m-d H:i:s') . ' error: ' . $project_id . '->' . $e->getMessage() . PHP_EOL; | 143 | echo date('Y-m-d H:i:s') . ' error: ' . $project_id . '->' . $e->getMessage() . PHP_EOL; |
| 70 | } | 144 | } |
| 71 | echo date('Y-m-d H:i:s') . ' end: ' . $project_id . PHP_EOL; | 145 | echo date('Y-m-d H:i:s') . ' end: ' . $project_id . PHP_EOL; |
| 72 | } | 146 | } |
| 73 | } | 147 | } |
| 74 | - /** | ||
| 75 | - * @remark :更新产品tdk | ||
| 76 | - * @name :updateProduct | ||
| 77 | - * @author :lyh | ||
| 78 | - * @method :post | ||
| 79 | - * @time :2023/8/19 9:25 | ||
| 80 | - */ | ||
| 81 | - public function updateProduct($project_id){ | ||
| 82 | - echo date('Y-m-d H:i:s') . '更新产品--updateProduct: 项目id' . $project_id . PHP_EOL; | ||
| 83 | - $list = DB::connection('custom_mysql')->table('gl_product')->where(['status'=>1,'project_id'=>$project_id])->get()->toArray(); | ||
| 84 | - if(!empty($list)){ | ||
| 85 | - foreach ($list as $v){ | ||
| 86 | - $v = (array)$v; | ||
| 87 | - echo date('Y-m-d H:i:s') . ' updateProduct: ' . $v['id'] . PHP_EOL; | ||
| 88 | - $seo_arr = json_decode($v['seo_mate'], true) ?: []; | ||
| 89 | - //更新seo_title | ||
| 90 | - if(!isset($seo_arr['title']) || empty($seo_arr['title'])){ | ||
| 91 | - //查看是否有指令 | ||
| 92 | - $aiCommandModel = new AiCommand(); | ||
| 93 | - $AiInfo = $aiCommandModel->read(['key'=>'product_seo_title'],['ai']); | ||
| 94 | - if(!empty($AiInfo['ai'])){ | ||
| 95 | - $seo_arr['title'] = $this->ai_send('product_seo_title',$v['title']); | ||
| 96 | - }else { | ||
| 97 | - $seo_arr['title'] = $v['title']; | ||
| 98 | - } | ||
| 99 | - } | ||
| 100 | - //更新seo_keyword | ||
| 101 | - if(!isset($seo_arr['keyword']) || empty($seo_arr['keyword'])){ | ||
| 102 | - $seo_arr['keyword'] = $this->ai_send('seo_keywords',$v['title']); | ||
| 103 | - } | ||
| 104 | - //更新seo_keyword | ||
| 105 | - if(!isset($seo_arr['description']) || empty($seo_arr['description'])){ | ||
| 106 | - $seo_arr['description'] = $this->ai_send('seo_meta_description',$v['title']); | ||
| 107 | - } | ||
| 108 | - $ser_str = json_encode($seo_arr,true); | ||
| 109 | - DB::connection('custom_mysql')->table('gl_product')->where(['id'=>$v['id']])->update(['seo_mate'=>$ser_str]); | ||
| 110 | - } | ||
| 111 | 148 | ||
| 112 | - } | ||
| 113 | - return true; | ||
| 114 | - } | 149 | + public function seo_tdk($project_id) |
| 150 | + { | ||
| 151 | + $ai_commands = AiCommand::select('key', 'scene', 'ai')->get()->toArray(); | ||
| 152 | + $ai_commands = Arr::setValueToKey($ai_commands, 'key'); | ||
| 115 | 153 | ||
| 116 | - /** | ||
| 117 | - * @remark :更新产品tdk | ||
| 118 | - * @name :updateProduct | ||
| 119 | - * @author :lyh | ||
| 120 | - * @method :post | ||
| 121 | - * @time :2023/8/19 9:25 | ||
| 122 | - */ | ||
| 123 | - public function updateProductCate($project_id){ | ||
| 124 | - echo date('Y-m-d H:i:s') . '更新产品分类--updateProductCate: 项目id' . $project_id . PHP_EOL; | ||
| 125 | - $list = DB::connection('custom_mysql')->table('gl_product_category')->where(['status'=>1,'project_id'=>$project_id])->get()->toArray(); | ||
| 126 | - if(!empty($list)){ | ||
| 127 | - foreach ($list as $v){ | ||
| 128 | - $v = (array)$v; | ||
| 129 | - echo date('Y-m-d H:i:s') . ' updateProductCate: ' . $v['id'] . PHP_EOL; | ||
| 130 | - $data = []; | ||
| 131 | - if(empty($v['seo_title'])){ | ||
| 132 | - $aiCommandModel = new AiCommand(); | ||
| 133 | - $AiInfo = $aiCommandModel->read(['key'=>'product_cate_seo_title'],['ai']); | ||
| 134 | - if(!empty($AiInfo['ai'])){ | ||
| 135 | - $data['seo_title'] = $this->ai_send('product_cate_seo_title',$v['title']); | ||
| 136 | - }else{ | ||
| 137 | - $data['seo_title'] = $v['title']; | ||
| 138 | - } | ||
| 139 | - } | ||
| 140 | - if(empty($v['seo_keywords'])){ | ||
| 141 | - $data['seo_keywords'] = $this->ai_send('seo_keywords',$v['title']); | ||
| 142 | - } | ||
| 143 | - if(empty($v['seo_des'])){ | ||
| 144 | - $name = $this->companyName($project_id); | ||
| 145 | - if(!empty($name)){ | ||
| 146 | - $data['seo_des'] = $this->ai_send('page_meta_description',$v['title'],$name); | ||
| 147 | - } | ||
| 148 | - } | ||
| 149 | - if(!$data){ | ||
| 150 | - continue; | ||
| 151 | - } | ||
| 152 | - DB::connection('custom_mysql')->table('gl_product_category')->where(['id'=>$v['id']])->update($data); | ||
| 153 | - } | ||
| 154 | - } | ||
| 155 | - return true; | ||
| 156 | - } | 154 | + foreach ($this->maps as $table => $map) { |
| 155 | + echo date('Y-m-d H:i:s') . '更新--' . $table . ': 项目id' . $project_id . PHP_EOL; | ||
| 156 | + $list = DB::connection('custom_mysql')->table($table)->get()->toArray(); | ||
| 157 | + if (!empty($list)) { | ||
| 158 | + foreach ($list as $v) { | ||
| 159 | + $v = (array)$v; | ||
| 160 | + echo date('Y-m-d H:i:s') . '更新--' . $table . ':id' . $v['id'] . PHP_EOL; | ||
| 161 | + $data = []; | ||
| 162 | + $json_field = ''; | ||
| 157 | 163 | ||
| 158 | - /** | ||
| 159 | - * @remark :更新新闻Tdk | ||
| 160 | - * @name :updateNews | ||
| 161 | - * @author :lyh | ||
| 162 | - * @method :post | ||
| 163 | - * @time :2023/8/19 10:06 | ||
| 164 | - */ | ||
| 165 | - public function updateNews($project_id){ | ||
| 166 | - echo date('Y-m-d H:i:s') . '更新新闻--updateNews: 项目id' . $project_id . PHP_EOL; | ||
| 167 | - $list = DB::connection('custom_mysql')->table('gl_news')->where(['status'=>1,'project_id'=>$project_id])->get()->toArray(); | ||
| 168 | - if(!empty($list)){ | ||
| 169 | - foreach ($list as $k => $v){ | ||
| 170 | - $v = (array)$v; | ||
| 171 | - echo date('Y-m-d H:i:s') . ' updateNews: ' . $v['id'] . PHP_EOL; | ||
| 172 | - $data = []; | ||
| 173 | - if(empty($v['seo_title'])){ | ||
| 174 | - $aiCommandModel = new AiCommand(); | ||
| 175 | - $AiInfo = $aiCommandModel->read(['key'=>'news_seo_title'],['ai']); | ||
| 176 | - if(!empty($AiInfo['ai'])){ | ||
| 177 | - $data['seo_title'] = $this->ai_send('news_seo_title',$v['name']); | ||
| 178 | - }else{ | ||
| 179 | - $data['seo_title'] = $v['name']; | ||
| 180 | - } | ||
| 181 | - } | ||
| 182 | - if(empty($v['seo_keywords'])){ | ||
| 183 | - $data['seo_keywords'] = $this->ai_send('seo_keywords',$v['name']); | ||
| 184 | - } | ||
| 185 | - if(empty($v['seo_description'])){ | ||
| 186 | - $data['seo_description'] = $this->ai_send('seo_meta_description',$v['name']); | ||
| 187 | - } | ||
| 188 | - if(!$data){ | ||
| 189 | - continue; | ||
| 190 | - } | ||
| 191 | - DB::connection('custom_mysql')->table('gl_news')->where(['id'=>$v['id']])->update($data); | ||
| 192 | - } | ||
| 193 | - } | ||
| 194 | - return true; | ||
| 195 | - } | 164 | + foreach ($map as $ai_key => $field) { |
| 165 | + $field_arr = explode('.', $field); | ||
| 166 | + if (count($field_arr) > 1) { | ||
| 167 | + $json_field = $field_arr[0]; | ||
| 168 | + $value = json_decode($v[$field_arr[0]], true)[$field_arr[1]] ?? ''; | ||
| 169 | + } else { | ||
| 170 | + $value = $v[$field] ?? ''; | ||
| 171 | + } | ||
| 172 | + //已有值的 跳过 | ||
| 173 | + if ($value) { | ||
| 174 | + echo $field.'已有值 跳过' . PHP_EOL; | ||
| 175 | + continue; | ||
| 176 | + } | ||
| 196 | 177 | ||
| 197 | - /** | ||
| 198 | - * @remark :更新新闻Tdk | ||
| 199 | - * @name :updateNews | ||
| 200 | - * @author :lyh | ||
| 201 | - * @method :post | ||
| 202 | - * @time :2023/8/19 10:06 | ||
| 203 | - */ | ||
| 204 | - public function updateNewsCate($project_id){ | ||
| 205 | - echo date('Y-m-d H:i:s') . '更新新闻分类--updateNewsCate: 项目id' . $project_id . PHP_EOL; | ||
| 206 | - $list = DB::connection('custom_mysql')->table('gl_news_category')->where(['status'=>0,'project_id'=>$project_id])->get()->toArray(); | ||
| 207 | - if(!empty($list)){ | ||
| 208 | - foreach ($list as $k => $v){ | ||
| 209 | - $v = (array)$v; | ||
| 210 | - echo date('Y-m-d H:i:s') . ' updateNewsCate: ' . $v['id'] . PHP_EOL; | ||
| 211 | - $data = []; | ||
| 212 | - if(empty($v['seo_title'])){ | ||
| 213 | - $aiCommandModel = new AiCommand(); | ||
| 214 | - $AiInfo = $aiCommandModel->read(['key'=>'news_cate_seo_title'],['ai']); | ||
| 215 | - if(!empty($AiInfo['ai'])){ | ||
| 216 | - $data['seo_title'] = $this->ai_send('news_cate_seo_title',$v['name']); | ||
| 217 | - }else{ | ||
| 218 | - $data['seo_title'] = $v['name']; | 178 | + //AI生成 |
| 179 | + if (!empty($ai_commands[$ai_key]['ai'])) { | ||
| 180 | + $prompt = $this->getPrompt($project_id, $ai_commands[$ai_key]['ai'], $table, $v); | ||
| 181 | + if(!$prompt){ | ||
| 182 | + continue; | ||
| 183 | + } | ||
| 184 | + | ||
| 185 | + if (count($field_arr) > 1) { | ||
| 186 | + $data[$field_arr[0]][$field_arr[1]] = $this->ai_send($prompt); | ||
| 187 | + }else{ | ||
| 188 | + $data[$field] = $this->ai_send($prompt); | ||
| 189 | + } | ||
| 190 | + } else { | ||
| 191 | + //直接使用topic | ||
| 192 | + if (count($field_arr) > 1) { | ||
| 193 | + $data[$field_arr[0]][$field_arr[1]] = $v[$this->topic_fields[$table]] ?? ''; | ||
| 194 | + //使用核心关键词 | ||
| 195 | + if(in_array($ai_key, array_keys($this->core_keyword_keys))){ | ||
| 196 | + $data[$field_arr[0]][$field_arr[1]] = $this->mainKeywords($project_id, $this->core_keyword_keys[$ai_key]); | ||
| 197 | + } | ||
| 198 | + }else{ | ||
| 199 | + $data[$field] = $v[$this->topic_fields[$table]] ?? ''; | ||
| 200 | + //使用核心关键词 | ||
| 201 | + if(in_array($ai_key, array_keys($this->core_keyword_keys))){ | ||
| 202 | + $data[$field] = $this->mainKeywords($project_id, $this->core_keyword_keys[$ai_key]); | ||
| 203 | + } | ||
| 204 | + } | ||
| 205 | + } | ||
| 219 | } | 206 | } |
| 220 | - } | ||
| 221 | - if(empty($v['seo_keywords'])){ | ||
| 222 | - //获取核心关键词 | ||
| 223 | - $main_keyword = $this->mainKeywords($project_id); | ||
| 224 | - if(!empty($main_keyword)){ | ||
| 225 | - $data['seo_keywords'] = $this->ai_send('seo_keywords',$main_keyword); | 207 | + if(!$data){ |
| 208 | + continue; | ||
| 226 | } | 209 | } |
| 227 | - } | ||
| 228 | - if(empty($v['seo_des'])){ | ||
| 229 | - $name = $this->companyName($project_id); | ||
| 230 | - if(!empty($name)){ | ||
| 231 | - $data['seo_des'] = $this->ai_send('page_meta_description',$v['title'],$name); | 210 | + if($json_field){ |
| 211 | + $old_data = json_decode($v[$field_arr[0]], true); | ||
| 212 | + foreach ($old_data as $kk=>$vv){ | ||
| 213 | + empty($data[$json_field][$kk]) && $data[$json_field][$kk] = $vv; | ||
| 214 | + } | ||
| 215 | + $data[$json_field] = json_encode($data[$json_field]); | ||
| 232 | } | 216 | } |
| 217 | + DB::connection('custom_mysql')->table($table)->where(['id' => $v['id']])->update($data); | ||
| 233 | } | 218 | } |
| 234 | - if(!$data){ | ||
| 235 | - continue; | ||
| 236 | - } | ||
| 237 | - DB::connection('custom_mysql')->table('gl_news_category')->where(['id'=>$v['id']])->update($data); | ||
| 238 | } | 219 | } |
| 239 | } | 220 | } |
| 240 | - return true; | ||
| 241 | } | 221 | } |
| 242 | 222 | ||
| 243 | - /** | ||
| 244 | - * @remark :更新blogTdk | ||
| 245 | - * @name :updateBlogs | ||
| 246 | - * @author :lyh | ||
| 247 | - * @method :post | ||
| 248 | - * @time :2023/8/19 10:07 | ||
| 249 | - */ | ||
| 250 | - public function updateBlogs($project_id){ | ||
| 251 | - echo date('Y-m-d H:i:s') . '更新博客--updateBlogs: 项目id' . $project_id . PHP_EOL; | ||
| 252 | - $list = DB::connection('custom_mysql')->table('gl_blog')->where(['status'=>1,'project_id'=>$project_id])->get()->toArray(); | ||
| 253 | - if(!empty($list)){ | ||
| 254 | - foreach ($list as $k => $v){ | ||
| 255 | - $v = (array)$v; | ||
| 256 | - echo date('Y-m-d H:i:s') . ' updateBlogs: ' . $v['id'] . PHP_EOL; | ||
| 257 | - $data = []; | ||
| 258 | - if(empty($v['seo_title'])){ | ||
| 259 | - $aiCommandModel = new AiCommand(); | ||
| 260 | - $AiInfo = $aiCommandModel->read(['key'=>'blog_seo_title'],['ai']); | ||
| 261 | - if(!empty($AiInfo['ai'])){ | ||
| 262 | - $data['seo_title'] = $this->ai_send('blog_seo_title',$v['name']); | ||
| 263 | - }else{ | ||
| 264 | - $data['seo_title'] = $v['name']; | ||
| 265 | - } | ||
| 266 | - } | ||
| 267 | - if(empty($v['seo_keywords'])){ | ||
| 268 | - $data['seo_keywords'] = $this->ai_send('seo_keywords',$v['name']); | ||
| 269 | - } | ||
| 270 | - if(empty($v['seo_description'])){ | ||
| 271 | - $data['seo_description'] = $this->ai_send('seo_meta_description',$v['name']); | ||
| 272 | - } | ||
| 273 | - if(!$data){ | ||
| 274 | - continue; | ||
| 275 | - } | ||
| 276 | - DB::connection('custom_mysql')->table('gl_blog')->where(['id'=>$v['id']])->update($data); | 223 | + public function getPrompt($project_id, $prompt, $table, $data){ |
| 224 | + $lang = ''; | ||
| 225 | + if(strpos($prompt, '{topic}') !== false){ | ||
| 226 | + $topic = $data[$this->topic_fields[$table]] ?? ''; | ||
| 227 | + if(!$topic){ | ||
| 228 | + echo 'topic为空 跳过' . PHP_EOL; | ||
| 229 | + return false; | ||
| 277 | } | 230 | } |
| 231 | + $prompt = str_replace('{topic}', $topic, $prompt); | ||
| 232 | + $lang = $this->getLang($topic); | ||
| 278 | } | 233 | } |
| 279 | - return true; | ||
| 280 | - } | ||
| 281 | - | ||
| 282 | - /** | ||
| 283 | - * @remark :更新新闻Tdk | ||
| 284 | - * @name :updateNews | ||
| 285 | - * @author :lyh | ||
| 286 | - * @method :post | ||
| 287 | - * @time :2023/8/19 10:06 | ||
| 288 | - */ | ||
| 289 | - public function updateBlogCate($project_id){ | ||
| 290 | - echo date('Y-m-d H:i:s') . '更新博客分类--updateBlogCate: 项目id' . $project_id . PHP_EOL; | ||
| 291 | - $list = DB::connection('custom_mysql')->table('gl_blog_category')->where(['status'=>0,'project_id'=>$project_id])->get()->toArray(); | ||
| 292 | - if(!empty($list)){ | ||
| 293 | - foreach ($list as $k => $v){ | ||
| 294 | - $v = (array)$v; | ||
| 295 | - echo date('Y-m-d H:i:s') . ' updateBlogCate: ' . $v['id'] . PHP_EOL; | ||
| 296 | - $data = []; | ||
| 297 | - if(empty($v['seo_title'])){ | ||
| 298 | - $data['seo_title'] = $v['name']; | ||
| 299 | - } | ||
| 300 | - if(empty($v['seo_keywords'])){ | ||
| 301 | - //获取核心关键词 | ||
| 302 | - $main_keyword = $this->mainKeywords($project_id); | ||
| 303 | - if(!empty($main_keyword)){ | ||
| 304 | - $data['seo_keywords'] = $this->ai_send('seo_keywords',$main_keyword); | ||
| 305 | - } | ||
| 306 | - } | ||
| 307 | - if(empty($v['seo_des'])){ | ||
| 308 | - $name = $this->companyName($project_id); | ||
| 309 | - $data['seo_des'] = $this->ai_send('page_meta_description',$v['name'],$name); | ||
| 310 | - } | ||
| 311 | - if(!$data){ | ||
| 312 | - continue; | ||
| 313 | - } | ||
| 314 | - DB::connection('custom_mysql')->table('gl_blog_category')->where(['id'=>$v['id']])->update($data); | 234 | + if(strpos($prompt, '{keyword}') !== false) { |
| 235 | + $keyword = $this->mainKeywords($project_id, 8); | ||
| 236 | + if(!$keyword){ | ||
| 237 | + echo '核心关键词为空 跳过' . PHP_EOL; | ||
| 238 | + return false; | ||
| 315 | } | 239 | } |
| 240 | + $prompt = str_replace('{keyword}', $keyword, $prompt); | ||
| 241 | + !$lang && $lang = $this->getLang($keyword); | ||
| 316 | } | 242 | } |
| 317 | - return true; | ||
| 318 | - } | ||
| 319 | - | ||
| 320 | - /** | ||
| 321 | - * @remark :单页面更新tdk | ||
| 322 | - * @name :updatePage | ||
| 323 | - * @author :lyh | ||
| 324 | - * @method :post | ||
| 325 | - * @time :2023/10/30 11:04 | ||
| 326 | - */ | ||
| 327 | - public function updatePage($project_id){ | ||
| 328 | - echo date('Y-m-d H:i:s') . '更新自定义界面--updatePage: 项目id' . $project_id . PHP_EOL; | ||
| 329 | - $list = DB::connection('custom_mysql')->table('gl_web_custom_template')->select(['id','name','title','keywords','description','project_id'])->where(['project_id'=>$project_id])->get()->toArray(); | ||
| 330 | - if(!empty($list)){ | ||
| 331 | - foreach ($list as $v){ | ||
| 332 | - $v = (array)$v; | ||
| 333 | - echo date('Y-m-d H:i:s') . ' updatePage: ' . $v['id'] . PHP_EOL; | ||
| 334 | - $data = []; | ||
| 335 | - if(empty($v['title'])){ | ||
| 336 | - //生成seo_title | ||
| 337 | - $data['title'] = $v['name']; | ||
| 338 | - } | ||
| 339 | - if(empty($v['keywords'])){ | ||
| 340 | - //获取核心关键词 | ||
| 341 | - $main_keyword = $this->mainKeywords($project_id); | ||
| 342 | - if(!empty($main_keyword)){ | ||
| 343 | - $data['keywords'] = $this->ai_send('seo_keywords',$main_keyword); | ||
| 344 | - } | ||
| 345 | - } | ||
| 346 | - if(empty($v['description'])){ | ||
| 347 | - $name = $this->companyName($project_id); | ||
| 348 | - if(!empty($name)){ | ||
| 349 | - $data['description'] = $this->ai_send('page_meta_description',$v['name'],$name); | ||
| 350 | - } | ||
| 351 | - } | ||
| 352 | - if(!$data){ | ||
| 353 | - continue; | ||
| 354 | - } | ||
| 355 | - DB::connection('custom_mysql')->table('gl_web_custom_template')->where(['id'=>$v['id']])->update($data); | 243 | + if(strpos($prompt, '{company name}') !== false) { |
| 244 | + $company_name = $this->companyName($project_id); | ||
| 245 | + if(!$company_name){ | ||
| 246 | + echo '公司英文全称为空 跳过' . PHP_EOL; | ||
| 247 | + return false; | ||
| 356 | } | 248 | } |
| 249 | + $prompt = str_replace('{company name}', $company_name, $prompt); | ||
| 357 | } | 250 | } |
| 358 | - return true; | 251 | + $prompt .= '.Please answer in ' . ($lang ?: 'English'); |
| 252 | + | ||
| 253 | + echo $prompt . PHP_EOL; | ||
| 254 | + | ||
| 255 | + return $prompt; | ||
| 359 | } | 256 | } |
| 360 | 257 | ||
| 361 | /** | 258 | /** |
| @@ -365,15 +262,16 @@ class UpdateSeoTdk extends Command | @@ -365,15 +262,16 @@ class UpdateSeoTdk extends Command | ||
| 365 | * @method :post | 262 | * @method :post |
| 366 | * @time :2023/10/30 11:22 | 263 | * @time :2023/10/30 11:22 |
| 367 | */ | 264 | */ |
| 368 | - public function companyName($project_id,$key = ''){ | 265 | + public function companyName($project_id, $key = '') |
| 266 | + { | ||
| 369 | $data = [ | 267 | $data = [ |
| 370 | 'product_long_description', | 268 | 'product_long_description', |
| 371 | ]; | 269 | ]; |
| 372 | $projectOptimizeModel = new DeployOptimize(); | 270 | $projectOptimizeModel = new DeployOptimize(); |
| 373 | - $info = $projectOptimizeModel->read(['project_id'=>$project_id],['id','company_en_name','company_en_description']); | ||
| 374 | - if(in_array($key,$data)){ | 271 | + $info = $projectOptimizeModel->read(['project_id' => $project_id], ['id', 'company_en_name', 'company_en_description']); |
| 272 | + if (in_array($key, $data)) { | ||
| 375 | return $info['company_en_description']; | 273 | return $info['company_en_description']; |
| 376 | - }else{ | 274 | + } else { |
| 377 | return $info['company_en_name']; | 275 | return $info['company_en_name']; |
| 378 | } | 276 | } |
| 379 | } | 277 | } |
| @@ -385,21 +283,31 @@ class UpdateSeoTdk extends Command | @@ -385,21 +283,31 @@ class UpdateSeoTdk extends Command | ||
| 385 | * @method :post | 283 | * @method :post |
| 386 | * @time :2023/10/30 11:22 | 284 | * @time :2023/10/30 11:22 |
| 387 | */ | 285 | */ |
| 388 | - public function mainKeywords($project_id){ | 286 | + public function mainKeywords($project_id, $num) |
| 287 | + { | ||
| 389 | $str = ''; | 288 | $str = ''; |
| 390 | $projectOptimizeModel = new DeployOptimize(); | 289 | $projectOptimizeModel = new DeployOptimize(); |
| 391 | - $info = $projectOptimizeModel->read(['project_id'=>$project_id],['id','main_keywords']); | ||
| 392 | - if($info !== false){ | ||
| 393 | - if(!empty($info['main_keywords'])){ | ||
| 394 | - $arr = explode(',',$info['main_keywords']); | ||
| 395 | - if(isset($arr[0])){ | ||
| 396 | - $str = $arr[0]; | ||
| 397 | - } | ||
| 398 | - } | 290 | + $info = $projectOptimizeModel->read(['project_id' => $project_id], ['id', 'main_keywords']); |
| 291 | + if (!empty($info['main_keywords'])) { | ||
| 292 | + $main_keywords = explode(',', $info['main_keywords']); | ||
| 293 | + //随机取 | ||
| 294 | + shuffle($main_keywords); | ||
| 295 | + $main_keywords = array_slice($main_keywords, 0, $num); | ||
| 296 | + $str = implode(",", $main_keywords); | ||
| 399 | } | 297 | } |
| 400 | return $str; | 298 | return $str; |
| 401 | } | 299 | } |
| 402 | 300 | ||
| 301 | + public function getLang($content){ | ||
| 302 | + $result = Translate::translateSl($content); | ||
| 303 | + if (isset($result['texts']['sl']) && isset(Translate::$tls_list[$result['texts']['sl']])) { | ||
| 304 | + $lang = Translate::$tls_list[$result['texts']['sl']]['lang_en']; | ||
| 305 | + } else { | ||
| 306 | + $lang = 'English'; | ||
| 307 | + } | ||
| 308 | + return $lang; | ||
| 309 | + } | ||
| 310 | + | ||
| 403 | /** | 311 | /** |
| 404 | * @remark :AI发送 | 312 | * @remark :AI发送 |
| 405 | * @name :ai_send | 313 | * @name :ai_send |
| @@ -407,16 +315,11 @@ class UpdateSeoTdk extends Command | @@ -407,16 +315,11 @@ class UpdateSeoTdk extends Command | ||
| 407 | * @method :post | 315 | * @method :post |
| 408 | * @time :2023/8/19 10:40 | 316 | * @time :2023/8/19 10:40 |
| 409 | */ | 317 | */ |
| 410 | - public function ai_send($key,$keywords,$name = ''){ | ||
| 411 | - $chat_url = 'v2/openai_chat_qqs'; | ||
| 412 | - $param = [ | ||
| 413 | - 'key'=>$key, | ||
| 414 | - 'keywords'=>$keywords, | ||
| 415 | - ]; | ||
| 416 | - $data = Common::send_openai_msg($chat_url,$param,$name); | ||
| 417 | - $data['text'] = Common::deal_keywords($data['text']); | ||
| 418 | - $data['text'] = Common::deal_str($data['text']); | ||
| 419 | - return $data['text']; | 318 | + public function ai_send($prompt) |
| 319 | + { | ||
| 320 | + $text = Gpt::instance()->openai_chat_qqs($prompt); | ||
| 321 | + $text = Common::deal_keywords($text); | ||
| 322 | + return Common::deal_str($text); | ||
| 420 | } | 323 | } |
| 421 | 324 | ||
| 422 | } | 325 | } |
app/Helper/Gpt.php
0 → 100644
| 1 | +<?php | ||
| 2 | + | ||
| 3 | +namespace App\Helper; | ||
| 4 | + | ||
| 5 | +use Illuminate\Support\Facades\Http; | ||
| 6 | +use Illuminate\Support\Facades\Log; | ||
| 7 | + | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * Class Gpt | ||
| 11 | + * @package App\Helper | ||
| 12 | + * @author zbj | ||
| 13 | + * @date 2023/11/3 | ||
| 14 | + */ | ||
| 15 | +class Gpt | ||
| 16 | +{ | ||
| 17 | + public $api = 'http://openai.waimaoq.com/'; | ||
| 18 | + | ||
| 19 | + public $header = []; | ||
| 20 | + | ||
| 21 | + private static $instance; | ||
| 22 | + | ||
| 23 | + public static function instance(){ | ||
| 24 | + if(!self::$instance){ | ||
| 25 | + self::$instance = new static; | ||
| 26 | + } | ||
| 27 | + return self::$instance; | ||
| 28 | + } | ||
| 29 | + | ||
| 30 | + /** | ||
| 31 | + * 全球搜-文本生成接口。模型:gpt-3.5-turbo | ||
| 32 | + * @author zbj | ||
| 33 | + * @date 2023/8/25 | ||
| 34 | + * | ||
| 35 | + */ | ||
| 36 | + public function openai_chat_qqs($content, $system_content = '') | ||
| 37 | + { | ||
| 38 | + $url = $this->api . 'v2/openai_chat_qqs'; | ||
| 39 | + | ||
| 40 | + $data = [ | ||
| 41 | + 'messages' => [], | ||
| 42 | + ]; | ||
| 43 | + if ($system_content) { | ||
| 44 | + $data['messages'][] = [ | ||
| 45 | + "role" => "system", | ||
| 46 | + "content" => $system_content | ||
| 47 | + ]; | ||
| 48 | + } | ||
| 49 | + $data['messages'][] = [ | ||
| 50 | + "role" => "user", | ||
| 51 | + "content" => $content | ||
| 52 | + ]; | ||
| 53 | + | ||
| 54 | + $time = time(); | ||
| 55 | + try { | ||
| 56 | + $result = Http::withHeaders($this->header)->withOptions(['verify' => false])->acceptJson() | ||
| 57 | + ->withBody(json_encode($data, JSON_UNESCAPED_UNICODE), 'application/json') | ||
| 58 | + ->post($url); | ||
| 59 | + $json = $result->json(); | ||
| 60 | + if (!isset($json['text'])) { | ||
| 61 | + Log::error('openai_chat_qqs data:', $data); | ||
| 62 | + Log::error('openai_chat_qqs result:' . (time() - $time), $json === null ? ['null'] : $json); | ||
| 63 | + } | ||
| 64 | + } catch (\Throwable $e) { | ||
| 65 | + Log::error('openai_chat_qqs time ' . (time() - $time) . ' error:' . $e->getMessage()); | ||
| 66 | + $json = []; | ||
| 67 | + } | ||
| 68 | + return $json['text'] ?? ''; | ||
| 69 | + } | ||
| 70 | + | ||
| 71 | +} |
-
请 注册 或 登录 后发表评论