作者 李宇航

合并分支 'lyh-server' 到 'master'

Lyh server



查看合并请求 !3277
... ... @@ -26,7 +26,7 @@ use function Symfony\Component\String\s;
/***
* @remark :根据项目更新blog列表
* @name :AiBlogListTask
* @name :AiBlogListProjectTask
* @author :lyh
* @method :post
* @time :2025/3/6 9:45
... ...
<?php
/**
* @remark :
* @name :AiBlogTask.php
* @author :lyh
* @method :post
* @time :2025/2/14 11:14
*/
namespace App\Console\Commands\Ai;
use App\Models\Ai\AiBlog;
use App\Models\Ai\AiBlogAuthor;
use App\Models\Ai\AiBlogList;
use App\Models\Domain\DomainInfo;
use App\Models\Project\ProjectAiSetting;
use App\Models\RouteMap\RouteMap;
use App\Services\AiBlogService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use App\Models\Project\AiBlogTask as AiBlogTaskModel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use function Symfony\Component\String\s;
/***
* @remark :根据项目更新blog列表
* @name :AiBlogListProjectTask
* @author :lyh
* @method :post
* @time :2025/3/6 9:45
*/
class AiBlogListProjectTask extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'save_ai_blog_list {project_id}';
/**
* The console command description.
*
* @var string
*/
protected $description = '根据项目生成blog列表';
public function handle(){
$project_id = $this->argument('project_id');
@file_put_contents(storage_path('logs/lyh_error.log'), var_export('执行的项目id->'.$project_id, true) . PHP_EOL, FILE_APPEND);
ProjectServer::useProject($project_id);
$projectAiSettingModel = new ProjectAiSetting();
$aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]);
$this->updateBlogList($aiSettingInfo);
$this->curlDelRoute($project_id);
DB::disconnect('custom_mysql');
return true;
}
/**
* @remark :更新列表页数据
* @name :updateBlogList
* @author :lyh
* @method :post
* @time :2025/3/5 11:07
*/
public function updateBlogList($aiSettingInfo){
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = $aiSettingInfo['mch_id'];
$aiBlogService->key = $aiSettingInfo['key'];
$page = 1;
$saveData = [];
$result = $aiBlogService->getAiBlogList($page,15);
if(!isset($result['status']) || $result['status'] != 200){
echo '请示失败。'.json_encode($result, JSON_UNESCAPED_UNICODE);
return true;
}
$total_page = $result['data']['total_page'];
//组装数据保存
$saveData[] = [
'route'=>$page,
'text'=>$result['data']['section'],
];
while ($total_page > $page){
$page++;
$result = $aiBlogService->getAiBlogList($page,15);
if(isset($result['status']) && $result['status'] == 200){
$saveData[] = [
'route'=>$page,
'text'=>$result['data']['section'],
];
}
}
$aiBlogListModel = new AiBlogList();
if(!empty($saveData)){
//写一条路由信息
$aiBlogListModel->truncate();
$aiBlogListModel->insertAll($saveData);
}
return true;
}
/**
* @remark :通知C端生成界面
* @name :sendNotice
* @author :lyh
* @method :post
* @time :2025/3/6 11:51
*/
public function curlDelRoute($project_id){
$domainModel = new DomainInfo();
//获取项目域名
$domain = $domainModel->getProjectIdDomain($project_id);
if(!empty($domain)){
$c_url = $domain.'api/update_page/';
$param = [
'project_id' => $project_id,
'type' => 1,
'route' => 3,
'url' => ['top-blog'],
'language'=> [],
'is_sitemap' => 0
];
http_post($c_url, json_encode($param));
}
return true;
}
}
... ...
... ... @@ -21,11 +21,12 @@ use Illuminate\Console\Command;
use App\Models\Project\AiBlogTask as AiBlogTaskModel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use function Symfony\Component\String\s;
/***
* @remark :根据项目更新blog列表
* @name :AiBlogListTask
* @name :AiBlogListProjectTask
* @author :lyh
* @method :post
* @time :2025/3/6 9:45
... ... @@ -37,24 +38,45 @@ class AiBlogListTask extends Command
*
* @var string
*/
protected $signature = 'save_ai_blog_list {project_id}';
protected $signature = 'save_ai_blog_list_task';
/**
* The console command description.
*
* @var string
*/
protected $description = '生成blog列表';
protected $description = '生成blog列表';
public function handle(){
$project_id = $this->argument('project_id');
@file_put_contents(storage_path('logs/lyh_error.log'), var_export('执行的项目id->'.$project_id, true) . PHP_EOL, FILE_APPEND);
while (true){
$task_id = $this->getTaskId();
if(empty($task_id)){
sleep(200);
continue;
}
$aiBlogTaskModel = new AiBlogTaskModel();
$info = $aiBlogTaskModel->read(['id'=>$task_id]);
if($info === false){
echo date('Y-m-d H:i:s').',当前数据不存在或者已被删除。'.PHP_EOL;
}
$project_id = $info['project_id'];
echo '执行的项目ID:'.$info['project_id'].PHP_EOL;
ProjectServer::useProject($project_id);
$projectAiSettingModel = new ProjectAiSetting();
$aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]);
$this->updateBlogList($aiSettingInfo);
$res = $this->updateBlogList($aiSettingInfo);
if($res){
$aiBlogTaskModel->edit(['status'=>2],['id'=>$task_id]);
}else{
if($info['sort'] >= 5){
$aiBlogTaskModel->edit(['status'=>9],['id'=>$task_id]);
}else{
$aiBlogTaskModel->edit(['status'=>9,'sort'=>($info['sort'] + 1)],['id'=>$task_id]);
}
}
$this->curlDelRoute($project_id);
DB::disconnect('custom_mysql');
}
return true;
}
... ... @@ -73,8 +95,8 @@ class AiBlogListTask extends Command
$saveData = [];
$result = $aiBlogService->getAiBlogList($page,15);
if(!isset($result['status']) || $result['status'] != 200){
echo '请示失败。'.json_encode($result, JSON_UNESCAPED_UNICODE);
return true;
echo '请求失败。'.json_encode($result, JSON_UNESCAPED_UNICODE);
return false;
}
$total_page = $result['data']['total_page'];
//组装数据保存
... ... @@ -90,6 +112,9 @@ class AiBlogListTask extends Command
'route'=>$page,
'text'=>$result['data']['section'],
];
}else{
echo '请求失败。'.json_encode($result, JSON_UNESCAPED_UNICODE);
return false;
}
}
$aiBlogListModel = new AiBlogList();
... ... @@ -97,6 +122,7 @@ class AiBlogListTask extends Command
//写一条路由信息
$aiBlogListModel->truncate();
$aiBlogListModel->insertAll($saveData);
return true;
}
return true;
}
... ... @@ -126,4 +152,27 @@ class AiBlogListTask extends Command
}
return true;
}
/**
* 获取任务id
* @param int $finish_at
* @return mixed
*/
public function getTaskId($finish_at = 2)
{
$keys = 'ai_blog_list_task';
$task_id = Redis::rpop($keys);
if (empty($task_id)) {
$aiBlogTaskModel = new AiBlogTaskModel();
$finish_at = date('Y-m-d H:i:s', strtotime('-' . $finish_at . ' hour'));
$ids = $aiBlogTaskModel->formatQuery(['status'=>$aiBlogTaskModel::STATUS_RUNNING, 'type'=>$aiBlogTaskModel::TYPE_LIST, 'updated_at'=>['<=',$finish_at]])->pluck('id');
if(!empty($ids)){
foreach ($ids as $id) {
Redis::lpush($keys, $id);
}
}
$task_id = Redis::rpop($keys);
}
return $task_id;
}
}
... ...
... ... @@ -26,10 +26,8 @@ use App\Services\DingService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;
use App\Models\Project\AiBlogTask as AiBlogTaskModel;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;
use function Symfony\Component\String\s;
class AiBlogTask extends Command
{
... ... @@ -183,7 +181,7 @@ class AiBlogTask extends Command
ProjectServer::useProject($project_id);
$aiSettingInfo = $this->getSetting($project_id);
$this->output('sync: list start, project_id: ' . $project_id);
$this->updateBlogList($aiSettingInfo);
$this->updateBlogList($aiSettingInfo,$project_id);
$this->output('sync: list end');
//更新作者
$this->output('sync: author start, project_id: ' . $project_id);
... ... @@ -249,7 +247,7 @@ class AiBlogTask extends Command
* @param $aiSettingInfo
* @return bool
*/
public function updateBlogList($aiSettingInfo){
public function updateBlogList($aiSettingInfo,$project_id = 0){
$aiBlogService = new AiBlogService();
$aiBlogService->mch_id = $aiSettingInfo['mch_id'];
$aiBlogService->key = $aiSettingInfo['key'];
... ... @@ -257,6 +255,21 @@ class AiBlogTask extends Command
$saveData = [];
$result = $aiBlogService->getAiBlogList($page,15);
if(!isset($result['status']) || $result['status'] != 200){
try {
// 钉钉通知
$dingService = new DingService();
$body = [
'keyword' => 'AI_BLOG列表页未生成拉取失败',
'msg' => '项目ID:' . $project_id . PHP_EOL . '返回信息:' . json_encode($result,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
'isAtAll' => false, // 是否@所有人
];
$dingService->handle($body);
//写一条更新记录
$aiBlogTaskModel = new AiBlogTaskModel();
$aiBlogTaskModel->addReturnId(['project_id'=>$project_id,'task_id'=>$project_id,'status'=>$aiBlogTaskModel::STATUS_RUNNING,'type'=>$aiBlogTaskModel::TYPE_LIST]);
}catch (\Exception $e){
$this->output('更新列表页失败同时通知失败--error:' . $e->getMessage());
}
return true;
}
$total_page = $result['data']['total_page'];
... ...
... ... @@ -27,7 +27,7 @@ use function Symfony\Component\String\s;
/***
* @remark :根据项目更新blog列表
* @name :AiBlogListTask
* @name :AiBlogListProjectTask
* @author :lyh
* @method :post
* @time :2025/3/6 9:45
... ...
... ... @@ -366,7 +366,7 @@ class TranslateController extends BaseController
$count = 0;
} else {
$cateRelateModel = new CategoryRelated();
$count = $cateRelateModel->whereIn('cateid', $ids)->distinct('product_id')->count();
$count = $cateRelateModel->whereIn('cate_id', $ids)->distinct('product_id')->count();
}
}
$this->pageSixList($data,$count,$v,1,15);
... ...
... ... @@ -27,4 +27,5 @@ class AiBlogTask extends Base
const TYPE_AUTHOR = 1;
const TYPE_BLOG = 2;
const TYPE_AUTHOR_ID = 3;//根据对应id页面
const TYPE_LIST = 4;//更新列表页
}
... ...