作者 lyh

Merge branch 'develop' of http://47.244.231.31:8099/zhl/globalso-v6 into develop

<?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;
}
}
... ...
... ... @@ -12,6 +12,8 @@ use Illuminate\Http\File;
class ProjectFilePDF extends Command
{
use CmdSignal;
/**
* The name and signature of the console command.
*
... ... @@ -26,12 +28,17 @@ class ProjectFilePDF extends Command
*/
protected $description = '网站项目数据,生成PDF文件';
protected $AiccWechat;
protected ProjectAssociation $AiccWechat;
protected $DataFile;
protected DataFile $DataFile;
protected $time;
// 最大支持5个进程
public int $maxRunNumber = 50;
public FileController $fileController;
/**
* Create a new command instance.
*
... ... @@ -39,73 +46,57 @@ class ProjectFilePDF extends Command
*/
public function __construct()
{
$this->AiccWechat = new ProjectAssociation();
$this->DataFile = new DataFile();
$this->time = date("Y-m");
$this->AiccWechat = new ProjectAssociation();
$this->DataFile = new DataFile();
$this->time = date("Y-m");
$this->fileController = new FileController();
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
public function start(): int
{
$data = $this->get_data();
# 详细数据
$items = $data['items'];
# 总分页
$totalPage = $data['totalPage'];
$this->dataPush($items);
if ($totalPage > 1) {
for ($page = 2; $page <= $totalPage; $page++) {
$da = $this->get_data();
$this->dataPush($da['items']);
}
}
$this->info('生成pdf完成');
return 0;
}
/**
* 数据生成并保存
* @param array $items
* @return void
*/
public function dataPush(array $items)
{
foreach ($items as $item) {
$project_id = $item->project_id;
$user_id = $item->user_id;
$friend_id = $item->friend_id;
// todo 根据项目查询数据
$project_data = [];
$html = $this->html($project_data);
$filename = hash('md5', $this->time . '-' . $project_id . '-' . $friend_id . '-' . $user_id);
$file_path = $this->savePDF($html, $filename);
var_dump($file_path);
$this->DataFile->saveData(compact('project_id', 'user_id', 'friend_id', 'file_path') + ['time' => $this->time]);
$status = 1; # 1 - 正常, 0 - 禁用
$is_pdf = 0; # 0 - 未生成 1 - 已生成 2 - 其它问题
$lists = $this->DataFile::query()->where('status', $status)
->where('project_id', '!=', 0)
->where('friend_id', '!=', 0)
->where('user_id', '!=', 0)
->where('is_pdf', $is_pdf)->where('created_at', 'like', $this->time . '%')->first();
if (is_null($lists)) {
$this->debug_echo('没有任务,等待中');
sleep(60);
return 0;
}
}
public function get_data($page = 1, $perPage = 20)
{
$data = $this->AiccWechat->allData($page, $perPage);
# 总条数
$total = $data['total'];
if (empty($total)) {
$this->error('暂无绑定AICC微信数据');
$key = $this->signature . '-' . $lists->id;
$count = redis_get($key);
$item = $lists->getOriginal();
$project_id = $item->project_id;
$user_id = $item->user_id;
$friend_id = $item->friend_id;
// todo 根据项目查询数据
$project_data = [];
$html = $this->html($project_data);
$filename = hash('md5', $this->time . '-' . $project_id . '-' . $friend_id . '-' . $user_id);
$file_path = $this->savePDF($html, $filename);
if (empty($file_path)) {
$this->debug_echo('pdf生成失败!');
return 0;
}
# 详细数据
$items = $data['items'];
# 总分页
$totalPage = $data['totalPage'];
# 当前页
$currentPage = $data['currentPage'];
return compact('total', 'items', 'totalPage', 'currentPage');
$isRes = $this->DataFile->saveData(compact('project_id', 'user_id', 'friend_id', 'file_path') + ['time' => $this->time]);
if (!$isRes) {
if ($count == 2) {
$lists->is_pdf = 2;
$lists->save();
$this->debug_echo('项目文件数据保存失败! - 其他原因 - ' . $key);
} else {
redis_add($key, $count + 1);
$this->debug_echo('项目文件数据保存失败! - ' . $key);
}
}
$lists->is_pdf = 1;
$lists->save();
$this->debug_echo('项目文件数据保存成功!');
return 0;
}
public function savePDF($html, $filename)
... ... @@ -138,16 +129,13 @@ class ProjectFilePDF extends Command
// 获取PDF内容
$pdfContent = $dompdf->output();
$fileController = new FileController();
// 将PDF内容保存到文件
@file_put_contents($savePath, $pdfContent);
// 创建一个文件实例
$file = new File($savePath);
var_dump($file->getFilename());
exit();
return $fileController->single($file);
return $this->fileController->single($file);
}
/**
... ...
<?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;
}
}
... ...
<?php
namespace App\Console\Commands\Update;
use App\Helper\Arr;
use App\Http\Logic\Bside\Product\CategoryLogic;
use App\Models\Blog\Blog;
use App\Models\Collect\CollectTask;
use App\Models\Com\UpdateLog;
use App\Models\News\News;
use App\Models\Product\Category;
use App\Models\Product\Keyword;
use App\Models\Product\Product;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BCustomTemplate;
use App\Models\WebSetting\WebSettingReceiving;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
/**
* 4.0,5.0升级到6.0,内容同步
* Class ProjectImport
* @package App\Console\Commands
* @author Akun
* @date 2023/10/9 15:04
*/
class Temp extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'project_update_temp';
/**
* The console command description.
*
* @var string
*/
protected $description = '执行项目升级任务';
public function handle()
{
$this->start_update();
}
protected function start_update()
{
// $data = UpdateLog::where('project_id','<=',530)->where('api_type','category')->get();
$data = UpdateLog::where('project_id', '=', 298)->where('api_type', 'category')->get();
foreach ($data as $task) {
$project_id = $task->project_id;
$api_type = $task->api_type;
$api_url_arr = explode('?', $task->api_url);
$api_url = $api_url_arr[0];
echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update start' . PHP_EOL;
//设置数据库
$project = ProjectServer::useProject($project_id);
if ($project) {
//分类
$url = $api_url . '?' . http_build_query(['w' => 'category']);
$data = curl_c($url);
if (isset($data['code']) && $data['code'] == 200) {
$items = $data['data'] ?? [];
$this->category_insert($project_id, $items, 0);
} else {
continue;
}
}
//关闭数据库
DB::disconnect('custom_mysql');
echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update end ' . PHP_EOL;
}
}
//获取地址路由
protected function get_url_route($url)
{
$arr = parse_url($url);
if (empty($arr['path'])) {
return '';
}
$path = $arr['path'];
if (strpos($path, '.') !== false) {
$path = substr($path, 0, strpos($path, '.'));
}
$path_arr = explode('/', $path);
return end($path_arr) ? end($path_arr) : $path_arr[count($path_arr) - 2];
}
//多级分类入库
protected function category_insert($project_id, $items, $pid = 0)
{
$model = new Category();
foreach ($items as $item) {
if ($item['name'] ?? '') {
$parent = $model->read(['pid' => $pid, 'title' => $item['name']], 'id');
if (!$parent) {
try {
$item['name'] = $this->special2str($item['name']);
$parent_id = $model->addReturnId([
'project_id' => $project_id,
'title' => $item['name'],
'pid' => $pid,
'keywords' => $item['keywords'] ?? '',
'describe' => $item['description'] ?? '',
'route' => $this->get_url_route($item['url'])
]);
$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;
}
} else {
$parent_id = $parent['id'];
}
if (!empty($item['children'])) {
$this->category_insert($project_id, $item['children'], $parent_id);
}
}
}
}
//获取分类名称数组
protected function get_category_name_arr($category, $pid = 0)
{
foreach ($category as $k => $v) {
if ($v['parent'] == $pid) {
return $v;
}
}
return [];
}
//特殊字符转换
protected function special2str($str)
{
if (strpos($str, ';') === false) {
return $str;
}
$list = [
'&lt;' => '<',
'&gt;' => '>',
'&amp;' => '&',
'&acute;' => '´',
'&quot;' => '“',
'&nbsp;' => ' '
];
foreach ($list as $k => $v) {
$str = str_replace($k, $v, $str);
}
return $str;
}
//路由入库
protected function set_map($route, $source, $source_id, $project_id)
{
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;
}
$route_map->save();
}
}
}
}
... ...
... ... @@ -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;
}
... ...
... ... @@ -24,10 +24,12 @@ class WebsiteData extends Command
protected $description = '向AICC推送数据';
// 最大支持5个进程
public $maxRunNumber = 50;
public int $maxRunNumber = 50;
protected $time;
protected $url;
/**
* Create a new command instance.
*
... ... @@ -35,13 +37,17 @@ class WebsiteData extends Command
*/
public function __construct()
{
$this->time = date('y-d');
$this->time = date('Y-m');
$this->url = env('AICC_URL');
parent::__construct();
}
public function start(): int
{
# 0 - 未推送
# 1 - 已推送
# 2 - 其他问题
$status = 0;
$lists = DataFile::query()->where('status', $status)
->where('created_at', 'like', $this->time . '%')->first();
... ... @@ -50,44 +56,23 @@ class WebsiteData extends Command
sleep(60);
return 0;
}
var_dump($lists);
exit();
$data = $lists['items'];
$url = env('AICC_URL');
$msg = http_post($url, json_encode(compact('data')));
}
/**
* Execute the console command.
*
* @return int
*/
// public function handle()
// {
// $DataFile = new DataFile();
// $data = $DataFile->allData();
// # 详细数据
// $items = $data['items'];
// # 总分页
// $totalPage = $data['totalPage'];
// $this->post_data($items);
// if ($totalPage > 1) {
// for ($page = 2; $page <= $totalPage; $page++) {
// $da = $DataFile->allData($page);
// $this->post_data($da['items']);
// }
// }
// $this->info('项目文件数据推送完成!');
// return 0;
// }
public function post_data($data)
{
$url = env('AICC_URL');
$msg = http_post($url, json_encode(compact('data')));
print_r($msg);
$key = $this->signature . '-' . $lists->id;
$count = redis_get($key);
$isRes = http_post($this->url, json_encode(['data' => $lists->getOriginal()]));
if (!$isRes) {
if ($count == 2) {
$lists->is_pdf = 2;
$lists->save();
$this->debug_echo('项目文件数据推送失败! - 其他原因 - ' . $key);
} else {
redis_add($key, $count + 1);
$this->debug_echo('项目文件数据推送失败! - ' . $key);
}
}
$lists->status = 1;
$lists->save();
$this->debug_echo('项目文件数据保存成功!');
return 0;
}
}
... ...
... ... @@ -213,7 +213,7 @@ class Common
}
}
}
return implode(',',$ar_keywords);
return implode(', ',$ar_keywords);
}
... ...
... ... @@ -9,6 +9,7 @@ use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Carbon;
use App\Models\File\File;
use Illuminate\Support\Facades\Redis;
define('HTTP_OPENAI_URL', 'http://openai.waimaoq.com/');
/**
... ... @@ -616,3 +617,29 @@ function getRouteMap($source,$source_id){
}
return $route;
}
function redis_get($key){
return Redis::connection()->client()->get($key);
}
function redis_del(...$key){
return Redis::connection()->client()->del(...$key);
}
function redis_set($key,$val,$ttl=3600){
return Redis::connection()->client()->set($key,$val,$ttl);
}
/**
* 添加缓存,存在则失败
* @param $key
* @param $val
* @param int $ttl
* @return mixed
* @author:dc
* @time 2023/10/25 9:48
*/
function redis_add($key,$val,$ttl=3600){
return Redis::connection()->client()->eval(
"return redis.call('exists',KEYS[1])<1 and redis.call('setex',KEYS[1],ARGV[2],ARGV[1])", [$key, $val, $ttl], 1
);
}
... ...
... ... @@ -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);
}
/**
... ...
<?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);
}
}
}
... ...