作者 赵彬吉

update

... ... @@ -2,7 +2,10 @@
namespace App\Console\Commands;
use App\Helper\Arr;
use App\Helper\Common;
use App\Helper\Gpt;
use App\Helper\Translate;
use App\Models\Ai\AiCommand;
use App\Models\Project\DeployOptimize;
use App\Services\ProjectServer;
... ... @@ -31,7 +34,7 @@ class UpdateSeoTdk extends Command
*
* @var string
*/
protected $description = '一键生成sdk';
protected $description = '一键生成tdk';
/**
* Create a new command instance.
... ... @@ -44,318 +47,212 @@ class UpdateSeoTdk extends Command
}
/**
* '表' => [
* '指令key' => '表字段'
* ]
* @return array
* @author zbj
* @date 2023/11/3
*/
protected $maps = [
'gl_web_custom_template' => [
'page_title' => 'title',
'page_meta_keywords' => 'keywords',
'page_meta_description' => 'description',
],
'gl_product' => [
'product_title' => 'seo_mate.title',
'product_meta_keywords' => 'seo_mate.keyword',
'product_meta_description' => 'seo_mate.description',
],
'gl_product_category' => [
'product_cat_title' => 'seo_title',
'product_cat_meta_keywords' => 'seo_keywords',
'product_cat_meta_description' => 'seo_des',
],
'gl_blog' => [
'blog_title' => 'seo_title',
'blog_meta_keywords' => 'seo_keywords',
'blog_meta_description' => 'seo_description',
],
'gl_blog_category' => [
'blog_cat_title' => 'seo_title',
'blog_cat_meta_keywords' => 'seo_keywords',
'blog_cat_meta_description' => 'seo_des',
],
'gl_news' => [
'news_title' => 'seo_title',
'news_meta_keywords' => 'seo_keywords',
'news_meta_description' => 'seo_description',
],
'gl_news_category' => [
'news_cat_title' => 'seo_title',
'news_cat_meta_keywords' => 'seo_keywords',
'news_cat_meta_description' => 'seo_des',
],
'gl_product_keyword' => [
'tags_title' => 'seo_title',
'tags_meta_keywords' => 'seo_keywords',
'tags_meta_description' => 'seo_description',
'tags_content_title' => 'keyword_title',
'tags_content_description' => 'keyword_content',
]
];
/**
* topic-对相应字段
* @var array
*/
protected $topic_fields = [
'gl_web_custom_template' => 'name',
'gl_product' => 'title',
'gl_product_category' => 'title',
'gl_blog' => 'name',
'gl_blog_category' => 'name',
'gl_news' => 'name',
'gl_news_category' => 'name',
'gl_product_keyword' => 'title'
];
/**
* 使用核心关键词的key 数量
* @var array
*/
protected $core_keyword_keys = [
'page_meta_keywords' => 1,
'blog_cat_meta_keywords' => 8,
'news_cat_meta_keywords' => 8,
];
/**
* @return bool
*/
public function handle()
{
while (true){
while (true) {
$project_id = Redis::rpop('updateSeoTdk');
if(!$project_id){
if (!$project_id) {
sleep(2);
continue;
}
echo date('Y-m-d H:i:s') . ' start: ' . $project_id . PHP_EOL;
echo date('Y-m-d H:i:s') . ' start project_id: ' . $project_id . PHP_EOL;
try {
ProjectServer::useProject($project_id);
$this->updateProduct($project_id);
$this->updateProductCate($project_id);
$this->updateBlogs($project_id);
$this->updateBlogCate($project_id);
$this->updateNews($project_id);
$this->updateNewsCate($project_id);
$this->updatePage($project_id);
DB::disconnect('custom_mysql');
}catch (\Exception $e){
$this->seo_tdk($project_id);
} catch (\Exception $e) {
echo date('Y-m-d H:i:s') . ' error: ' . $project_id . '->' . $e->getMessage() . PHP_EOL;
}
echo date('Y-m-d H:i:s') . ' end: ' . $project_id . PHP_EOL;
}
}
/**
* @remark :更新产品tdk
* @name :updateProduct
* @author :lyh
* @method :post
* @time :2023/8/19 9:25
*/
public function updateProduct($project_id){
echo date('Y-m-d H:i:s') . '更新产品--updateProduct: 项目id' . $project_id . PHP_EOL;
$list = DB::connection('custom_mysql')->table('gl_product')->where(['status'=>1,'project_id'=>$project_id])->get()->toArray();
if(!empty($list)){
foreach ($list as $v){
$v = (array)$v;
echo date('Y-m-d H:i:s') . ' updateProduct: ' . $v['id'] . PHP_EOL;
$seo_arr = json_decode($v['seo_mate'], true) ?: [];
//更新seo_title
if(!isset($seo_arr['title']) || empty($seo_arr['title'])){
//查看是否有指令
$aiCommandModel = new AiCommand();
$AiInfo = $aiCommandModel->read(['key'=>'product_seo_title'],['ai']);
if(!empty($AiInfo['ai'])){
$seo_arr['title'] = $this->ai_send('product_seo_title',$v['title']);
}else {
$seo_arr['title'] = $v['title'];
}
}
//更新seo_keyword
if(!isset($seo_arr['keyword']) || empty($seo_arr['keyword'])){
$seo_arr['keyword'] = $this->ai_send('seo_keywords',$v['title']);
}
//更新seo_keyword
if(!isset($seo_arr['description']) || empty($seo_arr['description'])){
$seo_arr['description'] = $this->ai_send('seo_meta_description',$v['title']);
}
$ser_str = json_encode($seo_arr,true);
DB::connection('custom_mysql')->table('gl_product')->where(['id'=>$v['id']])->update(['seo_mate'=>$ser_str]);
}
}
return true;
}
public function seo_tdk($project_id)
{
$ai_commands = AiCommand::select('key', 'scene', 'ai')->get()->toArray();
$ai_commands = Arr::setValueToKey($ai_commands, 'key');
/**
* @remark :更新产品tdk
* @name :updateProduct
* @author :lyh
* @method :post
* @time :2023/8/19 9:25
*/
public function updateProductCate($project_id){
echo date('Y-m-d H:i:s') . '更新产品分类--updateProductCate: 项目id' . $project_id . PHP_EOL;
$list = DB::connection('custom_mysql')->table('gl_product_category')->where(['status'=>1,'project_id'=>$project_id])->get()->toArray();
if(!empty($list)){
foreach ($list as $v){
$v = (array)$v;
echo date('Y-m-d H:i:s') . ' updateProductCate: ' . $v['id'] . PHP_EOL;
$data = [];
if(empty($v['seo_title'])){
$aiCommandModel = new AiCommand();
$AiInfo = $aiCommandModel->read(['key'=>'product_cate_seo_title'],['ai']);
if(!empty($AiInfo['ai'])){
$data['seo_title'] = $this->ai_send('product_cate_seo_title',$v['title']);
}else{
$data['seo_title'] = $v['title'];
}
}
if(empty($v['seo_keywords'])){
$data['seo_keywords'] = $this->ai_send('seo_keywords',$v['title']);
}
if(empty($v['seo_des'])){
$name = $this->companyName($project_id);
if(!empty($name)){
$data['seo_des'] = $this->ai_send('page_meta_description',$v['title'],$name);
}
}
if(!$data){
continue;
}
DB::connection('custom_mysql')->table('gl_product_category')->where(['id'=>$v['id']])->update($data);
}
}
return true;
}
foreach ($this->maps as $table => $map) {
echo date('Y-m-d H:i:s') . '更新--' . $table . ': 项目id' . $project_id . PHP_EOL;
$list = DB::connection('custom_mysql')->table($table)->get()->toArray();
if (!empty($list)) {
foreach ($list as $v) {
$v = (array)$v;
echo date('Y-m-d H:i:s') . '更新--' . $table . ':id' . $v['id'] . PHP_EOL;
$data = [];
$json_field = '';
/**
* @remark :更新新闻Tdk
* @name :updateNews
* @author :lyh
* @method :post
* @time :2023/8/19 10:06
*/
public function updateNews($project_id){
echo date('Y-m-d H:i:s') . '更新新闻--updateNews: 项目id' . $project_id . PHP_EOL;
$list = DB::connection('custom_mysql')->table('gl_news')->where(['status'=>1,'project_id'=>$project_id])->get()->toArray();
if(!empty($list)){
foreach ($list as $k => $v){
$v = (array)$v;
echo date('Y-m-d H:i:s') . ' updateNews: ' . $v['id'] . PHP_EOL;
$data = [];
if(empty($v['seo_title'])){
$aiCommandModel = new AiCommand();
$AiInfo = $aiCommandModel->read(['key'=>'news_seo_title'],['ai']);
if(!empty($AiInfo['ai'])){
$data['seo_title'] = $this->ai_send('news_seo_title',$v['name']);
}else{
$data['seo_title'] = $v['name'];
}
}
if(empty($v['seo_keywords'])){
$data['seo_keywords'] = $this->ai_send('seo_keywords',$v['name']);
}
if(empty($v['seo_description'])){
$data['seo_description'] = $this->ai_send('seo_meta_description',$v['name']);
}
if(!$data){
continue;
}
DB::connection('custom_mysql')->table('gl_news')->where(['id'=>$v['id']])->update($data);
}
}
return true;
}
foreach ($map as $ai_key => $field) {
$field_arr = explode('.', $field);
if (count($field_arr) > 1) {
$json_field = $field_arr[0];
$value = json_decode($v[$field_arr[0]], true)[$field_arr[1]] ?? '';
} else {
$value = $v[$field] ?? '';
}
//已有值的 跳过
if ($value) {
echo $field.'已有值 跳过' . PHP_EOL;
continue;
}
/**
* @remark :更新新闻Tdk
* @name :updateNews
* @author :lyh
* @method :post
* @time :2023/8/19 10:06
*/
public function updateNewsCate($project_id){
echo date('Y-m-d H:i:s') . '更新新闻分类--updateNewsCate: 项目id' . $project_id . PHP_EOL;
$list = DB::connection('custom_mysql')->table('gl_news_category')->where(['status'=>0,'project_id'=>$project_id])->get()->toArray();
if(!empty($list)){
foreach ($list as $k => $v){
$v = (array)$v;
echo date('Y-m-d H:i:s') . ' updateNewsCate: ' . $v['id'] . PHP_EOL;
$data = [];
if(empty($v['seo_title'])){
$aiCommandModel = new AiCommand();
$AiInfo = $aiCommandModel->read(['key'=>'news_cate_seo_title'],['ai']);
if(!empty($AiInfo['ai'])){
$data['seo_title'] = $this->ai_send('news_cate_seo_title',$v['name']);
}else{
$data['seo_title'] = $v['name'];
//AI生成
if (!empty($ai_commands[$ai_key]['ai'])) {
$prompt = $this->getPrompt($project_id, $ai_commands[$ai_key]['ai'], $table, $v);
if(!$prompt){
continue;
}
if (count($field_arr) > 1) {
$data[$field_arr[0]][$field_arr[1]] = $this->ai_send($prompt);
}else{
$data[$field] = $this->ai_send($prompt);
}
} else {
//直接使用topic
if (count($field_arr) > 1) {
$data[$field_arr[0]][$field_arr[1]] = $v[$this->topic_fields[$table]] ?? '';
//使用核心关键词
if(in_array($ai_key, array_keys($this->core_keyword_keys))){
$data[$field_arr[0]][$field_arr[1]] = $this->mainKeywords($project_id, $this->core_keyword_keys[$ai_key]);
}
}else{
$data[$field] = $v[$this->topic_fields[$table]] ?? '';
//使用核心关键词
if(in_array($ai_key, array_keys($this->core_keyword_keys))){
$data[$field] = $this->mainKeywords($project_id, $this->core_keyword_keys[$ai_key]);
}
}
}
}
}
if(empty($v['seo_keywords'])){
//获取核心关键词
$main_keyword = $this->mainKeywords($project_id);
if(!empty($main_keyword)){
$data['seo_keywords'] = $this->ai_send('seo_keywords',$main_keyword);
if(!$data){
continue;
}
}
if(empty($v['seo_des'])){
$name = $this->companyName($project_id);
if(!empty($name)){
$data['seo_des'] = $this->ai_send('page_meta_description',$v['title'],$name);
if($json_field){
$old_data = json_decode($v[$field_arr[0]], true);
foreach ($old_data as $kk=>$vv){
empty($data[$json_field][$kk]) && $data[$json_field][$kk] = $vv;
}
$data[$json_field] = json_encode($data[$json_field]);
}
DB::connection('custom_mysql')->table($table)->where(['id' => $v['id']])->update($data);
}
if(!$data){
continue;
}
DB::connection('custom_mysql')->table('gl_news_category')->where(['id'=>$v['id']])->update($data);
}
}
return true;
}
/**
* @remark :更新blogTdk
* @name :updateBlogs
* @author :lyh
* @method :post
* @time :2023/8/19 10:07
*/
public function updateBlogs($project_id){
echo date('Y-m-d H:i:s') . '更新博客--updateBlogs: 项目id' . $project_id . PHP_EOL;
$list = DB::connection('custom_mysql')->table('gl_blog')->where(['status'=>1,'project_id'=>$project_id])->get()->toArray();
if(!empty($list)){
foreach ($list as $k => $v){
$v = (array)$v;
echo date('Y-m-d H:i:s') . ' updateBlogs: ' . $v['id'] . PHP_EOL;
$data = [];
if(empty($v['seo_title'])){
$aiCommandModel = new AiCommand();
$AiInfo = $aiCommandModel->read(['key'=>'blog_seo_title'],['ai']);
if(!empty($AiInfo['ai'])){
$data['seo_title'] = $this->ai_send('blog_seo_title',$v['name']);
}else{
$data['seo_title'] = $v['name'];
}
}
if(empty($v['seo_keywords'])){
$data['seo_keywords'] = $this->ai_send('seo_keywords',$v['name']);
}
if(empty($v['seo_description'])){
$data['seo_description'] = $this->ai_send('seo_meta_description',$v['name']);
}
if(!$data){
continue;
}
DB::connection('custom_mysql')->table('gl_blog')->where(['id'=>$v['id']])->update($data);
public function getPrompt($project_id, $prompt, $table, $data){
$lang = '';
if(strpos($prompt, '{topic}') !== false){
$topic = $data[$this->topic_fields[$table]] ?? '';
if(!$topic){
echo 'topic为空 跳过' . PHP_EOL;
return false;
}
$prompt = str_replace('{topic}', $topic, $prompt);
$lang = $this->getLang($topic);
}
return true;
}
/**
* @remark :更新新闻Tdk
* @name :updateNews
* @author :lyh
* @method :post
* @time :2023/8/19 10:06
*/
public function updateBlogCate($project_id){
echo date('Y-m-d H:i:s') . '更新博客分类--updateBlogCate: 项目id' . $project_id . PHP_EOL;
$list = DB::connection('custom_mysql')->table('gl_blog_category')->where(['status'=>0,'project_id'=>$project_id])->get()->toArray();
if(!empty($list)){
foreach ($list as $k => $v){
$v = (array)$v;
echo date('Y-m-d H:i:s') . ' updateBlogCate: ' . $v['id'] . PHP_EOL;
$data = [];
if(empty($v['seo_title'])){
$data['seo_title'] = $v['name'];
}
if(empty($v['seo_keywords'])){
//获取核心关键词
$main_keyword = $this->mainKeywords($project_id);
if(!empty($main_keyword)){
$data['seo_keywords'] = $this->ai_send('seo_keywords',$main_keyword);
}
}
if(empty($v['seo_des'])){
$name = $this->companyName($project_id);
$data['seo_des'] = $this->ai_send('page_meta_description',$v['name'],$name);
}
if(!$data){
continue;
}
DB::connection('custom_mysql')->table('gl_blog_category')->where(['id'=>$v['id']])->update($data);
if(strpos($prompt, '{keyword}') !== false) {
$keyword = $this->mainKeywords($project_id, 8);
if(!$keyword){
echo '核心关键词为空 跳过' . PHP_EOL;
return false;
}
$prompt = str_replace('{keyword}', $keyword, $prompt);
!$lang && $lang = $this->getLang($keyword);
}
return true;
}
/**
* @remark :单页面更新tdk
* @name :updatePage
* @author :lyh
* @method :post
* @time :2023/10/30 11:04
*/
public function updatePage($project_id){
echo date('Y-m-d H:i:s') . '更新自定义界面--updatePage: 项目id' . $project_id . PHP_EOL;
$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();
if(!empty($list)){
foreach ($list as $v){
$v = (array)$v;
echo date('Y-m-d H:i:s') . ' updatePage: ' . $v['id'] . PHP_EOL;
$data = [];
if(empty($v['title'])){
//生成seo_title
$data['title'] = $v['name'];
}
if(empty($v['keywords'])){
//获取核心关键词
$main_keyword = $this->mainKeywords($project_id);
if(!empty($main_keyword)){
$data['keywords'] = $this->ai_send('seo_keywords',$main_keyword);
}
}
if(empty($v['description'])){
$name = $this->companyName($project_id);
if(!empty($name)){
$data['description'] = $this->ai_send('page_meta_description',$v['name'],$name);
}
}
if(!$data){
continue;
}
DB::connection('custom_mysql')->table('gl_web_custom_template')->where(['id'=>$v['id']])->update($data);
if(strpos($prompt, '{company name}') !== false) {
$company_name = $this->companyName($project_id);
if(!$company_name){
echo '公司英文全称为空 跳过' . PHP_EOL;
return false;
}
$prompt = str_replace('{company name}', $company_name, $prompt);
}
return true;
$prompt .= '.Please answer in ' . ($lang ?: 'English');
echo $prompt . PHP_EOL;
return $prompt;
}
/**
... ... @@ -365,15 +262,16 @@ class UpdateSeoTdk extends Command
* @method :post
* @time :2023/10/30 11:22
*/
public function companyName($project_id,$key = ''){
public function companyName($project_id, $key = '')
{
$data = [
'product_long_description',
];
$projectOptimizeModel = new DeployOptimize();
$info = $projectOptimizeModel->read(['project_id'=>$project_id],['id','company_en_name','company_en_description']);
if(in_array($key,$data)){
$info = $projectOptimizeModel->read(['project_id' => $project_id], ['id', 'company_en_name', 'company_en_description']);
if (in_array($key, $data)) {
return $info['company_en_description'];
}else{
} else {
return $info['company_en_name'];
}
}
... ... @@ -385,21 +283,31 @@ class UpdateSeoTdk extends Command
* @method :post
* @time :2023/10/30 11:22
*/
public function mainKeywords($project_id){
public function mainKeywords($project_id, $num)
{
$str = '';
$projectOptimizeModel = new DeployOptimize();
$info = $projectOptimizeModel->read(['project_id'=>$project_id],['id','main_keywords']);
if($info !== false){
if(!empty($info['main_keywords'])){
$arr = explode(',',$info['main_keywords']);
if(isset($arr[0])){
$str = $arr[0];
}
}
$info = $projectOptimizeModel->read(['project_id' => $project_id], ['id', 'main_keywords']);
if (!empty($info['main_keywords'])) {
$main_keywords = explode(',', $info['main_keywords']);
//随机取
shuffle($main_keywords);
$main_keywords = array_slice($main_keywords, 0, $num);
$str = implode(",", $main_keywords);
}
return $str;
}
public function getLang($content){
$result = Translate::translateSl($content);
if (isset($result['texts']['sl']) && isset(Translate::$tls_list[$result['texts']['sl']])) {
$lang = Translate::$tls_list[$result['texts']['sl']]['lang_en'];
} else {
$lang = 'English';
}
return $lang;
}
/**
* @remark :AI发送
* @name :ai_send
... ... @@ -407,16 +315,11 @@ class UpdateSeoTdk extends Command
* @method :post
* @time :2023/8/19 10:40
*/
public function ai_send($key,$keywords,$name = ''){
$chat_url = 'v2/openai_chat_qqs';
$param = [
'key'=>$key,
'keywords'=>$keywords,
];
$data = Common::send_openai_msg($chat_url,$param,$name);
$data['text'] = Common::deal_keywords($data['text']);
$data['text'] = Common::deal_str($data['text']);
return $data['text'];
public function ai_send($prompt)
{
$text = Gpt::instance()->openai_chat_qqs($prompt);
$text = Common::deal_keywords($text);
return Common::deal_str($text);
}
}
... ...
<?php
namespace App\Helper;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
/**
* Class Gpt
* @package App\Helper
* @author zbj
* @date 2023/11/3
*/
class Gpt
{
public $api = 'http://openai.waimaoq.com/';
public $header = [];
private static $instance;
public static function instance(){
if(!self::$instance){
self::$instance = new static;
}
return self::$instance;
}
/**
* 全球搜-文本生成接口。模型:gpt-3.5-turbo
* @author zbj
* @date 2023/8/25
*
*/
public function openai_chat_qqs($content, $system_content = '')
{
$url = $this->api . 'v2/openai_chat_qqs';
$data = [
'messages' => [],
];
if ($system_content) {
$data['messages'][] = [
"role" => "system",
"content" => $system_content
];
}
$data['messages'][] = [
"role" => "user",
"content" => $content
];
$time = time();
try {
$result = Http::withHeaders($this->header)->withOptions(['verify' => false])->acceptJson()
->withBody(json_encode($data, JSON_UNESCAPED_UNICODE), 'application/json')
->post($url);
$json = $result->json();
if (!isset($json['text'])) {
Log::error('openai_chat_qqs data:', $data);
Log::error('openai_chat_qqs result:' . (time() - $time), $json === null ? ['null'] : $json);
}
} catch (\Throwable $e) {
Log::error('openai_chat_qqs time ' . (time() - $time) . ' error:' . $e->getMessage());
$json = [];
}
return $json['text'] ?? '';
}
}
... ...