作者 lyh

gx

... ... @@ -41,6 +41,7 @@ class AiBlogTask extends Command
public $updateProject = [];//需更新的列表
public $projectSetting = [];
public $routes = [];//需要更新的路由
/**
* The console command description.
... ... @@ -75,7 +76,7 @@ class AiBlogTask extends Command
public function sendRequest($task_id){
$aiBlogTaskModel = new AiBlogTaskModel();
$item = $aiBlogTaskModel->read(['id'=>$task_id]);
$this->output('start:project ID: ' . $item['project_id'] . ', task ID: ' . $task_id);
$this->output('start:project ID: ' . $item['project_id'] . ',task ID: ' . $task_id);
//获取配置
$aiSettingInfo = $this->getSetting($item['project_id']);
$aiBlogService = new AiBlogService();
... ... @@ -84,9 +85,7 @@ class AiBlogTask extends Command
$aiBlogService->task_id = $item['task_id'];
//拉取文章数据
$result = $aiBlogService->getDetail();
// 获取失败
if(empty($result['status']) || $result['status'] != 200){
if(empty($result['status']) || ($result['status'] != 200)){
if($item['sort'] < 5){
$aiBlogTaskModel->edit(['sort'=>$item['sort'] + 1],['id'=>$item['id']]);
}else{
... ... @@ -100,14 +99,13 @@ class AiBlogTask extends Command
];
$dingService->handle($body);
}
$this->output('error: 数据获取失败,status:' . $result['status'] . ', message: ' . ($result['message'] ?? 'null'));
$this->output('error: 数据获取失败,status:' . $result['status'] . ',message: ' . ($result['message'] ?? 'null'));
return false;
}
//保存当前项目ai_blog数据
ProjectServer::useProject($item['project_id']);
$aiBlogModel = new AiBlog();
$aiBlogInfo = $aiBlogModel->read(['task_id'=>$item['task_id']],['id']);
$aiBlogInfo = $aiBlogModel->read(['task_id'=>$item['task_id']],['id','route']);
if($aiBlogInfo === false){
// 钉钉通知
$dingService = new DingService();
... ... @@ -117,15 +115,18 @@ class AiBlogTask extends Command
'isAtAll' => false, // 是否@所有人
];
$dingService->handle($body);
$this->output('error: 子库获取数据失败, task id: ' . $task_id);
$aiBlogTaskModel->edit(['status'=>9],['id'=>$item['id']]);
DB::disconnect('custom_mysql');
return false;
}
//需要更新的路由
if (!in_array($result['data']['author_id'], $this->updateProject[$item['project_id']] ?? [])) {
$this->updateProject[$item['project_id']][] = $result['data']['author_id'];
}
if (!in_array($aiBlogInfo['route'], $this->routes[$item['project_id']] ?? [])) {
$this->routes[$item['project_id']][] = $aiBlogInfo['route'];
}
//拿到返回的路由查看是否重复
$route = RouteMap::setRoute($result['data']['url'], RouteMap::SOURCE_AI_BLOG, $aiBlogInfo['id'], $item['project_id']);
if($route != $result['data']['url']){
... ... @@ -134,7 +135,6 @@ class AiBlogTask extends Command
$aiBlogModel->edit(['new_title'=>$result['data']['title'], 'image'=>$result['data']['thumb'], 'text'=>$result['data']['section'], 'author_id'=>$result['data']['author_id'],'seo_title'=>$result['data']['title'],'seo_keyword'=>$result['data']['keyword'],'seo_description'=>$result['data']['description'], 'route'=>$route ,'status'=>$aiBlogModel::STATUS_FINISH], ['task_id'=>$item['task_id']]);
DB::disconnect('custom_mysql');
$aiBlogTaskModel->edit(['status'=>$aiBlogModel::STATUS_FINISH],['id'=>$item['id']]);
$this->output('success: task id: ' . $task_id);
return true;
}
... ... @@ -152,6 +152,10 @@ class AiBlogTask extends Command
$this->updateProject($this->updateProject);
$this->updateProject = [];
}
if(!empty($this->routes)){
$this->updateRoutes($this->routes);
$this->routes = [];
}
$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_BLOG, 'updated_at'=>['<=',$finish_at]])->pluck('id');
... ... @@ -174,7 +178,6 @@ class AiBlogTask extends Command
if(empty($updateProject)){
return true;
}
foreach ($updateProject as $project_id => $author){
ProjectServer::useProject($project_id);
$aiSettingInfo = $this->getSetting($project_id);
... ... @@ -184,11 +187,10 @@ class AiBlogTask extends Command
//更新作者
$this->output('sync: author start, project_id: ' . $project_id);
foreach ($author as $val){
$this->updateAiBlogAuthor($aiSettingInfo,$val);
$this->updateAiBlogAuthor($aiSettingInfo,$val,$project_id);
}
$this->output('sync: author end');
DB::disconnect('custom_mysql');
$this->curlDelRoute($project_id);
}
return true;
... ... @@ -202,12 +204,11 @@ class AiBlogTask extends Command
public function getSetting($project_id)
{
$project_setting = $this->projectSetting;
if (FALSE == empty($project_setting[$project_id]))
if (FALSE == empty($project_setting[$project_id])){
return $project_setting[$project_id];
}
$projectAiSettingModel = new ProjectAiSetting();
$aiSettingInfo = $projectAiSettingModel->read(['project_id'=>$project_id]);
$this->projectSetting[$project_id] = $aiSettingInfo;
return $aiSettingInfo;
}
... ... @@ -218,7 +219,7 @@ class AiBlogTask extends Command
* @param $author_id
* @return bool
*/
public function updateAiBlogAuthor($aiSettingInfo,$author_id){
public function updateAiBlogAuthor($aiSettingInfo,$author_id,$project_id){
if(empty($author_id)){
return true;
}
... ... @@ -230,7 +231,12 @@ class AiBlogTask extends Command
if(isset($result['status']) && $result['status'] == 200){
//当前作者的页面
$aiBlogAuthorModel = new AiBlogAuthor();
if(!empty($result['data']['section'])){
$authorInfo = $aiBlogAuthorModel->read(['author_id'=>$author_id],['id','route']);
if($authorInfo !== false && !empty($result['data']['section'])){
//需要更新的路由
if (!in_array($authorInfo['route'], $this->routes[$item['project_id']] ?? [])) {
$this->routes[$project_id][] = $authorInfo['route'];
}
$aiBlogAuthorModel->edit(['text'=>$result['data']['section']],['author_id'=>$author_id]);
}
}
... ... @@ -282,52 +288,58 @@ class AiBlogTask extends Command
* @param $project_id
* @return bool
*/
public function curlDelRoute($project_id){
public function updateRoutes($routes){
$domainModel = new DomainInfo();
//获取项目域名
$domain = $domainModel->getProjectIdDomain($project_id);
if (empty($domain)) {
$this->output('send: 域名不存在, project id: ' . $project_id);
return true;
}
//判断是否是自建站服务器,如果是,不请求C端接口,数据直接入库
$project_model = new Project();
$project_info = $project_model->read(['id'=>$project_id],['serve_id']);
if(!$project_info){
$this->output('send: 项目不存在, project id: ' . $project_id);
return true;
}
$serve_ip_model = new ServersIp();
$serve_ip_info = $serve_ip_model->read(['id'=>$project_info['serve_id']],['servers_id']);
$servers_id = $serve_ip_info ? $serve_ip_info['servers_id'] : 0;
if($servers_id == ServerConfig::SELF_SITE_ID){
//判断是否已有更新进行中
$notify_model = new Notify();
$data = [
'project_id' => $project_id,
'type' => Notify::TYPE_MASTER,
'route' => Notify::ROUTE_AI_BLOG,
'server_id' => ServerConfig::SELF_SITE_ID,
'status' => ['!=',Notify::STATUS_FINISH_SITEMAP]
];
$notify = $notify_model->read($data,['id']);
if(!$notify){
$domain_array = parse_url($domain);
$data['data'] = Arr::a2s(['domain'=>$domain_array['host'],'url'=>[],'language'=>[]]);
$data['status'] = Notify::STATUS_INIT;
$data['sort'] = 2;
$notify_model->add($data);
foreach ($routes as $project_id => $route){
$route[] = 'top-blog';
$domain = $domainModel->getProjectIdDomain($project_id);
if (empty($domain)) {
$this->output('send: 域名不存在, project id: ' . $project_id);
return true;
}
//判断是否是自建站服务器,如果是,不请求C端接口,数据直接入库
$project_info = $project_model->read(['id'=>$project_id],['serve_id']);
if(!$project_info){
$this->output('send: 项目不存在, project id: ' . $project_id);
return true;
}
$serve_ip_model = new ServersIp();
$serve_ip_info = $serve_ip_model->read(['id'=>$project_info['serve_id']],['servers_id']);
$servers_id = $serve_ip_info ? $serve_ip_info['servers_id'] : 0;
if($servers_id == ServerConfig::SELF_SITE_ID){
//判断是否已有更新进行中
$notify_model = new Notify();
$data = [
'project_id' => $project_id,
'type' => Notify::TYPE_MASTER,
'route' => Notify::ROUTE_AI_BLOG,
'server_id' => ServerConfig::SELF_SITE_ID,
'status' => ['!=',Notify::STATUS_FINISH_SITEMAP]
];
$notify = $notify_model->read($data,['id']);
if(!$notify){
$domain_array = parse_url($domain);
$data['data'] = Arr::a2s(['domain'=>$domain_array['host'],'url'=>$route,'language'=>[]]);
$data['status'] = Notify::STATUS_INIT;
$data['sort'] = 2;
$notify_model->add($data);
}
$this->output('send: 自建站项目, project id: ' . $project_id);
}else{
$c_url = $domain.'api/update_page/';
$param = [
'project_id' => $project_id,
'type' => 1,
'route' => 3,
'url' => $route,
'language'=> [],
'is_sitemap' => 0
];
$res = http_post($c_url, json_encode($param,true));
$this->output('notify: project id: ' . $project_id . ', result: ' . json_encode($res,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
}
$this->output('send: 自建站项目, project id: ' . $project_id);
}else{
$url = $domain . 'api/update_page/?project_id='.$project_id.'&route=7';
$res = http_get($url);
$this->output('notify: project id: ' . $project_id . ', result: ' . json_encode($res,JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES));
}
return true;
}
... ...