作者 李美松

Merge branch 'master' into lms

<?php
namespace App\Console\Commands;
use App\Helper\Arr;
use App\Helper\Common;
use App\Helper\Gpt;
use App\Helper\Translate;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\Ai\AiCommand;
use App\Models\Mail\Mail;
use App\Models\Project\DeployOptimize;
use App\Models\Project\ProjectUpdateTdk;
use App\Models\User\User;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
/**
* 清除项目sdk
* Class InitProject
* @package App\Console\Commands
* @author zbj
* @date 2023/10/8
*/
class ClearSeoTdk extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'clear_seo_tdk {project_id}';
/**
* The console command description.
*
* @var string
*/
protected $description = '清除项目sdk';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* '表' => [
* '指令key' => '表字段'
* ]
* @return array
* @author zbj
* @date 2023/11/3
*/
protected $maps = [
'gl_web_custom_template' => [
'title' => '',
'keywords' => '',
'description' => '',
],
'gl_product' => [
'seo_mate' => null
],
'gl_product_category' => [
'seo_title' => '',
'seo_keywords' => '',
'seo_des' => '',
],
'gl_blog' => [
'seo_title' => '',
'seo_keywords' => '',
'seo_description' => '',
],
'gl_blog_category' => [
'seo_title' => '',
'seo_keywords' => '',
'seo_des' => '',
],
'gl_news' => [
'seo_title' => '',
'seo_keywords' => '',
'seo_description' => '',
],
'gl_news_category' => [
'seo_title' => '',
'seo_keywords' => '',
'seo_des' => '',
],
'gl_product_keyword' => [
'seo_title' => '',
'seo_keywords' => '',
'seo_description' => '',
'keyword_title' => '',
'keyword_content' => '',
]
];
/**
* @return bool
*/
public function handle()
{
$project_id = $this->argument('project_id');
$project = ProjectServer::useProject($project_id);
if(!$project){
echo '项目不存在或数据库未配置' . PHP_EOL;
exit;
}
if ($this->confirm('你确认清空['. $project['title'] .']的sdk?')) {
foreach ($this->maps as $table => $data) {
echo date('Y-m-d H:i:s') . '清空SDK--' . $table . PHP_EOL;
DB::connection('custom_mysql')->table($table)->update($data);
}
}
echo date('Y-m-d H:i:s') . '清空完成' . PHP_EOL;
}
}
... ...
<?php
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\Mail\Mail;
use App\Models\Project\DeployOptimize;
use App\Models\Project\Project;
use App\Models\Project\ProjectUpdateTdk;
use App\Models\User\User;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
/**
* 初始化项目
* Class InitProject
* @package App\Console\Commands
* @author zbj
* @date 2023/10/8
*/
class TdkTest extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'tdk_test';
/**
* The console command description.
*
* @var string
*/
protected $description = 'keywords ,分割加空格';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* '表' => [
* '指令key' => '表字段'
* ]
* @return array
* @author zbj
* @date 2023/11/3
*/
protected $maps = [
'gl_web_custom_template' => 'keywords',
'gl_product' => 'seo_mate.keyword',
'gl_product_category' => 'seo_keywords',
'gl_blog' => 'seo_keywords',
'gl_blog_category' => 'seo_keywords',
'gl_news' => 'seo_keywords',
'gl_news_category' => 'seo_keywords',
'gl_product_keyword' => 'seo_keywords',
];
/**
* @return bool
*/
public function handle()
{
$project_ids = Project::where('type', Project::TYPE_TWO)->pluck('id')->toArray();
foreach ($project_ids as $project_id){
echo date('Y-m-d H:i:s') . ' start project_id: ' . $project_id . PHP_EOL;
ProjectServer::useProject($project_id);
foreach ($this->maps as $table=>$field){
$list = DB::connection('custom_mysql')->table($table)->get();
foreach ($list as $item){
$item = (array) $item;
$field_arr = explode('.', $field);
if ($field == 'seo_mate.keyword') {
$data = json_decode($item[$field_arr[0]], true);
$value = $data['keyword'] ?? '';
if(!$value){
continue;
}
$data['keyword'] = implode(', ', array_map('trim', explode(',', $value)));
DB::connection('custom_mysql')->table($table)->where('id', $item['id'])->update(['seo_mate' => json_encode($data)]);
} else {
$value = $item[$field];
if(!$value){
continue;
}
$value = implode(', ', array_map('trim', explode(',', $value)));
DB::connection('custom_mysql')->table($table)->where('id', $item['id'])->update([$field => $value]);
}
}
}
}
}
}
... ...
... ... @@ -139,18 +139,16 @@ class ProjectUpdate extends Command
'seo_title' => $item['seo_title'] ?? '',
'seo_keywords' => $item['seo_keywords'] ?? '',
'seo_description' => $item['seo_description'] ?? '',
'route' => $this->get_url_route($item['url'])
]);
$route = $this->set_map($this->get_url_route($item['url']), RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $project_id);
$model->edit(['route' => $route], ['id' => $id]);
$this->set_map($this->get_url_route($item['url']), RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $project_id);
CollectTask::_insert($item['url'], $project_id, RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $link_type, $language_list, $page_list);
} catch (\Exception $e) {
echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
continue;
}
} else {
$id = $keyword['id'];
}
CollectTask::_insert($item['url'], $project_id, RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $link_type, $language_list, $page_list);
}
}
}
... ... @@ -271,19 +269,17 @@ class ProjectUpdate extends Command
'keyword' => $item['keywords'] ?? '',
'description' => $item['description'] ?? ''
]),
'status' => Product::STATUS_ON
'status' => Product::STATUS_ON,
'route' => $this->get_url_route($item['url'])
]);
$route = $this->set_map($this->get_url_route($item['url']), RouteMap::SOURCE_PRODUCT, $id, $project_id);
$model->edit(['route' => $route], ['id' => $id]);
$this->set_map($this->get_url_route($item['url']), RouteMap::SOURCE_PRODUCT, $id, $project_id);
CollectTask::_insert($item['url'], $project_id, RouteMap::SOURCE_PRODUCT, $id, $link_type, $language_list, $page_list);
} catch (\Exception $e) {
echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
continue;
}
} else {
$id = $product['id'];
}
CollectTask::_insert($item['url'], $project_id, RouteMap::SOURCE_PRODUCT, $id, $link_type, $language_list, $page_list);
}
}
}
... ... @@ -318,6 +314,15 @@ class ProjectUpdate extends Command
if (!$news) {
try {
$item['ttile'] = $this->special2str($item['ttile']);
if (is_array($item['images'])) {
$image = $item['images'][0] ?? '';
} else {
$image = $item['images'] ?? '';
}
if(strpos($image,'//') === 0){
$image = 'https:'.$image;
}
$id = $model->addReturnId([
'project_id' => $project_id,
'name' => $item['ttile'],
... ... @@ -325,20 +330,18 @@ class ProjectUpdate extends Command
'seo_keywords' => $item['keywords'] ?? '',
'seo_description' => $item['description'] ?? '',
'text' => $item['content'] ?? '',
'image' => $item['images'][0] ?? '',
'status' => $api_type == 'news' ? News::STATUS_ONE : Blog::STATUS_ONE
'image' => $image,
'status' => $api_type == 'news' ? News::STATUS_ONE : Blog::STATUS_ONE,
'url' => $this->get_url_route($item['url'])
]);
$route = $this->set_map($this->get_url_route($item['url']), $api_type == 'news' ? RouteMap::SOURCE_NEWS : RouteMap::SOURCE_BLOG, $id, $project_id);
$model->edit(['url' => $route], ['id' => $id]);
$this->set_map($this->get_url_route($item['url']), $api_type == 'news' ? RouteMap::SOURCE_NEWS : RouteMap::SOURCE_BLOG, $id, $project_id);
CollectTask::_insert($item['url'], $project_id, $api_type == 'news' ? RouteMap::SOURCE_NEWS : RouteMap::SOURCE_BLOG, $id, $link_type, $language_list, $page_list);
} catch (\Exception $e) {
echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
continue;
}
} else {
$id = $news['id'];
}
CollectTask::_insert($item['url'], $project_id, $api_type == 'news' ? RouteMap::SOURCE_NEWS : RouteMap::SOURCE_BLOG, $id, $link_type, $language_list, $page_list);
}
}
}
... ... @@ -375,19 +378,17 @@ class ProjectUpdate extends Command
'keywords' => $item['keywords'] ?? '',
'description' => $item['description'] ?? '',
'html' => $item['content'] ?? '',
'status' => 1
'status' => 1,
'url' => $this->get_url_route($item['url'])
]);
$route = $this->set_map($this->get_url_route($item['url']), RouteMap::SOURCE_PAGE, $id, $project_id);
$model->edit(['url' => $route], ['id' => $id]);
$this->set_map($this->get_url_route($item['url']), RouteMap::SOURCE_PAGE, $id, $project_id);
CollectTask::_insert($item['url'], $project_id, RouteMap::SOURCE_PAGE, $id, $link_type, $language_list, $page_list);
} catch (\Exception $e) {
echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
continue;
}
} else {
$id = $custom['id'];
}
CollectTask::_insert($item['url'], $project_id, RouteMap::SOURCE_PAGE, $id, $link_type, $language_list, $page_list);
}
}
}
... ... @@ -463,10 +464,10 @@ class ProjectUpdate extends Command
'title' => $item['name'],
'pid' => $pid,
'keywords' => $item['keywords'] ?? '',
'describe' => $item['description'] ?? ''
'describe' => $item['description'] ?? '',
'route' => $this->get_url_route($item['url'])
]);
$route = $this->set_map($this->get_url_route($item['url']), RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id);
$model->edit(['route' => $route], ['id' => $parent_id]);
$this->set_map($this->get_url_route($item['url']), RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id);
} catch (\Exception $e) {
echo 'date:' . date('Y-m-d H:i:s') . ', category_insert error: ' . $e->getMessage() . PHP_EOL;
continue;
... ... @@ -520,24 +521,23 @@ class ProjectUpdate extends Command
//路由入库
protected function set_map($route, $source, $source_id, $project_id)
{
if (empty($route)) {
return '';
}
$route_map = new RouteMap();
$route_map->project_id = $project_id;
$route_map->source = $source;
$route_map->source_id = $source_id;
$route_map->route = $route;
if ($route) {
$route_map = RouteMap::where('project_id', $project_id)->where('source', $source)->where('source_id', $source_id)->first();
if (!$route_map) {
$route_map = new RouteMap();
$route_map->project_id = $project_id;
$route_map->source = $source;
$route_map->source_id = $source_id;
$route_map->route = $route;
if ($source == RouteMap::SOURCE_NEWS) {
$route_map->path = RouteMap::SOURCE_NEWS;
} elseif ($source == RouteMap::SOURCE_BLOG) {
$route_map->path = RouteMap::SOURCE_BLOG;
}
if ($source == RouteMap::SOURCE_NEWS) {
$route_map->path = RouteMap::SOURCE_NEWS;
} elseif ($source == RouteMap::SOURCE_BLOG) {
$route_map->path = RouteMap::SOURCE_BLOG;
$route_map->save();
}
}
$route_map->save();
return $route;
}
}
... ...
... ... @@ -360,7 +360,7 @@ class UpdateSeoTdk extends Command
//随机取
shuffle($main_keywords);
$main_keywords = array_slice($main_keywords, 0, $num);
$str = implode(",", $main_keywords);
$str = implode(", ", $main_keywords);
}
return $str;
}
... ...
... ... @@ -213,7 +213,7 @@ class Common
}
}
}
return implode(',',$ar_keywords);
return implode(', ',$ar_keywords);
}
... ...
... ... @@ -201,13 +201,13 @@ class ProjectController extends BaseController
*/
public function searchChannel(&$query){
if(isset($this->map['zone_id']) && !empty($this->map['zone_id'])){
$query->where('gl_project.channel','like','%"zone_id": "'.$this->map['channel_id'].'"%');
$query->where('gl_project.channel','like','%"zone_id": "'.$this->map['zone_id'].'"%');
}
if(isset($this->map['channel_id']) && !empty($this->map['channel_id'])){
$query->where('gl_project.channel','like','%"channel_id": "'.$this->map['channel_id'].'"%');
}
if(isset($this->map['user_id']) && !empty($this->map['user_id'])){
$query->where('gl_project.channel','like','%"user_id": "'.$this->map['channel_id'].'"%');
$query->where('gl_project.channel','like','%"user_id": "'.$this->map['user_id'].'"%');
}
return $query;
}
... ...
... ... @@ -25,7 +25,6 @@ class ATemplateController extends BaseController
*/
public function lists(ATemplateLogic $aTemplateLogic){
$filed = ['id','name','image','url','status','sort','deleted_status','test_model','created_at','project_id'];
$this->map['project_id'] = 0;
$lists = $aTemplateLogic->aTemplateList($this->map,$this->page,$this->row,$this->order,$filed);
if(!empty($lists) && !empty($lists['list'])){
foreach ($lists['list'] as $k => $v){
... ...
... ... @@ -7,18 +7,17 @@ use App\Helper\Common;
use App\Helper\Translate;
use App\Http\Controllers\Bside\BaseController;
use App\Http\Controllers\Bside\:写入日志;
use App\Http\Logic\Bside\Ai\AiCommandLogic;
use App\Models\Ai\AiCommand;
use App\Models\Ai\AiLog;
use App\Models\Project\DeployOptimize;
use App\Models\Project\Project;
class AiCommandController extends BaseController
{
//获取文本内容
public $chat_url = 'v2/openai_chat_qqs';
/**
* @name :ai生成
* @author :liyuhang
* @method
* @author zbj
* @date 2023/11/22
*/
public function ai_http_post(){
$this->request->validate([
... ... @@ -28,39 +27,19 @@ class AiCommandController extends BaseController
'keywords.required' => '关键字不能为空',
'key.required' => '场景不能为空',
]);
#TODO 通过key获取到ai指令对象
$data = Common::send_openai_msg($this->chat_url,$this->param,$this->companyName($this->param['key'],$this->user['project_id']));
$data['text'] = Common::deal_keywords($data['text']);
$data['text'] = Common::deal_str($data['text']);
$param = [
'key'=>$this->param['key'],
'keywords'=>$this->param['keywords'],
'remark'=>json_encode($data)
];
$this->set_ai_log($param);
$this->response('success',Code::SUCCESS,$data);
}
/**
* @remark :获取公司英文名称
* @name :companyName
* @author :lyh
* @method :post
* @time :2023/10/30 11:22
*/
public function companyName($key,$project_id){
$text = AiCommandLogic::instance()->ai_send();
$data = [
'news_remark',
'blog_remark',
'product_long_description'
'code' => $text ? 200 : 500,
'text' => $text
];
$projectOptimizeModel = new DeployOptimize();
$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{
return $info['company_en_name'];
}
$param = [
'key' => $this->param['key'],
'keywords' => $this->param['keywords'],
'remark' => $text
];
$this->set_ai_log($param);
$this->response('success', Code::SUCCESS, $data);
}
/**
... ...
... ... @@ -32,7 +32,7 @@ class BTemplateModuleController extends BaseController
$moduleProjectModel = new BModuleProject();
$module_list = $moduleProjectModel->list(['project_id'=>$this->user['project_id']]);
$data['module_list'] = $module_list;
$this->response('success',Code::SUCCESS,$list);
$this->response('success',Code::SUCCESS,$data);
}
/**
... ...
... ... @@ -28,6 +28,7 @@ class ATemplateLogic extends BaseLogic
* @time :2023/6/28 17:03
*/
public function aTemplateList($map,$page,$row,$order = ['created_at'],$filed = ['*']){
$map['project_id'] = 0;
$map['deleted_status'] = 0;
$map['status'] = 0;
$lists = $this->model->lists($map,$page,$row,$order,$filed);
... ...
<?php
namespace App\Http\Logic\Bside\Ai;
use App\Helper\Common;
use App\Helper\Gpt;
use App\Helper\Translate;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Ai\AiCommand;
use App\Models\Project\DeployOptimize;
use Illuminate\Support\Facades\Cache;
class AiCommandLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
$this->model = new AiCommand();
}
/**
* @author zbj
* @date 2023/11/22
*/
public function getPrompt($is_batch = 0){
$ai_command = $this->model->where('key', $this->param['key'])->where('is_batch', $is_batch)->first();
if(!$ai_command){
$this->fail('指令不存在');
}
$prompt = $ai_command->ai;
if(strpos($prompt, '{keyword}') !== false) {
$prompt = str_replace('{keyword}', $this->param['keywords'], $prompt);
}
if(strpos($prompt, '{company introduction}') !== false) {
$company_introduction = $this->getDeployOptimize('company_en_description');
$prompt = str_replace('{company introduction}', $company_introduction, $prompt);
}
if(strpos($prompt, '{company name}') !== false) {
$company_name = $this->getDeployOptimize('company_en_name');
$prompt = str_replace('{company name}', $company_name, $prompt);
}
if(strpos($prompt, '{core keywords 8}') !== false) {
$main_keywords = $this->getDeployOptimize('main_keywords');
if ($main_keywords) {
$main_keywords = explode("\r\n", $main_keywords);
//随机取
shuffle($main_keywords);
$main_keywords = array_slice($main_keywords, 0, 8);
$main_keywords = implode(", ", $main_keywords);
$prompt = str_replace('{core keywords 8}', $main_keywords, $prompt);
}else{
$prompt = '';
}
}
if(trim($ai_command->ai) == '{core keywords 8}'){
$ai_send = false;
}else{
$lang = $this->getLang($this->param['keywords']);
$prompt .= '.Please answer in ' . ($lang ?: 'English');
$ai_send = true;
}
return [
'prompt' => $prompt,
'scene' => $ai_command->scene,
'ai_send' => $ai_send,
];
}
/**
* @param $content
* @return string
* @author zbj
* @date 2023/11/22
*/
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;
}
/**
* @param string $key
* @return false|mixed|string
* @author zbj
* @date 2023/11/22
*/
public function getDeployOptimize($key = ''){
$project_id = $this->project['id'];
$cache_key = 'project_deploy_optimize_info_' . $project_id;
$info = Cache::get($cache_key);
if(!$info){
$projectOptimizeModel = new DeployOptimize();
$info = $projectOptimizeModel->read(['project_id' => $project_id], ['id', 'company_en_name', 'company_en_description', 'main_keywords']);
Cache::put($cache_key, $info, 600);
}
if($key){
return $info[$key] ??'';
}
return $info;
}
/**
* @param int $is_batch
* @return array|string|string[]
* @author zbj
* @date 2023/11/22
*/
public function ai_send($is_batch = 0){
$prompt = $this->getPrompt($is_batch);
if(!$prompt['ai_send']){
return $prompt['prompt'];
}
$text = Gpt::instance()->openai_chat_qqs($prompt['prompt'], $prompt['scene']);
$text = Common::deal_keywords($text);
return Common::deal_str($text);
}
}
... ...
... ... @@ -29,8 +29,6 @@ class CollectTask extends Base
'project_id' => $project_id,
'source' => $source,
'source_id' => $source_id,
'domain' => $url_arr['host'],
'route' => $url_arr['path'],
'language' => ''
];
... ... @@ -48,33 +46,33 @@ class CollectTask extends Base
'created_at' => $now,
'updated_at' => $now,
];
}
if ($link_type > 0 && $language_list && in_array($url_arr['path'], $page_list)) {
$domain_arr = explode('.', $url_arr['host']);
foreach ($language_list as $v_lan) {
if ($link_type == 1) {
//二级域名
$domain_arr[0] = $v_lan;
$new_domain = implode('.', $domain_arr);
} else {
//二级目录
$new_domain = $url_arr['host'] . '/' . $v_lan;
}
if ($link_type > 0 && $language_list && in_array($url_arr['path'], $page_list)) {
$domain_arr = explode('.', $url_arr['host']);
foreach ($language_list as $v_lan) {
if ($link_type == 1) {
//二级域名
$domain_arr[0] = $v_lan;
$new_domain = implode('.', $domain_arr);
} else {
//二级目录
$new_domain = $url_arr['host'] . '/' . $v_lan;
}
$data[] = [
'project_id' => $project_id,
'source' => $source,
'source_id' => $source_id,
'domain' => $new_domain,
'route' => $url_arr['path'],
'language' => $v_lan,
'created_at' => $now,
'updated_at' => $now,
];
$data[] = [
'project_id' => $project_id,
'source' => $source,
'source_id' => $source_id,
'domain' => $new_domain,
'route' => $url_arr['path'],
'language' => $v_lan,
'created_at' => $now,
'updated_at' => $now,
];
}
}
}
self::insert($data);
self::insert($data);
}
}
}
... ...