作者 刘锟

自定义模块次啊及

... ... @@ -10,6 +10,9 @@ use App\Models\Collect\CollectSource;
use App\Models\Collect\CollectTask;
use App\Models\Com\UpdateLog;
use App\Models\Com\UpdateOldInfo;
use App\Models\CustomModule\CustomModule;
use App\Models\CustomModule\CustomModuleCategory;
use App\Models\CustomModule\CustomModuleContent;
use App\Models\News\News;
use App\Models\News\NewsCategory;
use App\Models\Product\Category;
... ... @@ -54,9 +57,9 @@ class ProjectUpdate extends Command
public function handle()
{
ini_set('memory_limit', '512M');
while (true) {
$this->start_update();
}
// while (true) {
$this->start_update();
// }
}
protected function start_update()
... ... @@ -187,6 +190,7 @@ class ProjectUpdate extends Command
if (isset($data['code']) && $data['code'] == 200) {
$phones = $data['data']['phones'] ?? '';
$emails = $data['data']['emails'] ?? '';
$custom_types = $data['data']['customposttypes'] ?? '';
$model = new WebSettingReceiving();
if ($phones) {
... ... @@ -229,6 +233,25 @@ class ProjectUpdate extends Command
}
}
}
if ($custom_types) {
$custom_model = new CustomModule();
foreach ($custom_types as $v_custom) {
try {
$custom_info = $custom_model->read(['route' => $v_custom]);
if (!$custom_info) {
$custom_model->add([
'name' => $v_custom,
'project_id' => $project_id,
'route' => $v_custom
]);
}
UpdateLog::createLog($project_id, $v_custom, $task->api_url);
} catch (\Exception $e) {
echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
continue;
}
}
}
} else {
return true;
}
... ... @@ -392,7 +415,7 @@ class ProjectUpdate extends Command
$category_id = $logic->getCategory(array_column($category_arr, 'id'));
}
//图片
if (is_array($item['images'])) {
if (is_array($item['images'] ?? '')) {
$image = $item['images'][0] ?? '';
} else {
$image = $item['images'] ?? '';
... ... @@ -448,7 +471,7 @@ class ProjectUpdate extends Command
} else {
return true;
}
} else {
} elseif ($api_type == 'page') {
//单页
$url = $api_url . '?' . http_build_query(['w' => 'page', 'page' => 1, 'pagesize' => 0]);
$data = curl_c($url);
... ... @@ -510,6 +533,79 @@ class ProjectUpdate extends Command
} else {
return true;
}
} else {
//自定义模块
$custom_model = new CustomModule();
$custom_info = $custom_model->read(['route' => $api_type]);
if ($custom_info) {
$url = $api_url . '?' . http_build_query(['w' => $api_type, 'page' => 1, 'pagesize' => 0]);
$data = curl_c($url);
if (isset($data['code']) && $data['code'] == 200) {
$category = $data['data']['category'] ?? [];
$this->category_custom_insert($project_id, $custom_info['id'], $category, 0);
$count = $data['data']['count'] ?? 0;
$total_page = ceil($count / $page_size);
for ($page = 1; $page <= $total_page; $page++) {
$url_page = $api_url . '?' . http_build_query(['w' => $api_type, 'page' => $page, 'pagesize' => $page_size]);
$data_page = curl_c($url_page);
if (isset($data_page['code']) && $data_page['code'] == 200) {
$items = $data_page['data']['data'] ?? [];
$model = new CustomModuleContent();
$category_model = new CustomModuleCategory();
foreach ($items as $item) {
$route = $this->get_url_route($item['url'] ?? '');
if ($route) {
//分类
$category_id = '';
if ($item['category'] ?? []) {
$category_arr = $category_model->list(['original_id' => ['in', array_column($item['category'], 'id')]]);
$category_id = implode(',', array_column($category_arr, 'id'));
}
//名称去掉特殊符号
$item['title'] = $this->special2str($item['title'] ?? '');
try {
$custom_content = $model->read(['route' => $route], 'id');
if (!$custom_content) {
$id = $model->insertGetId([
'project_id' => $project_id,
'name' => $item['title'],
'module_id' => $custom_info['id'],
'category_id' => $category_id,
'content' => $item['content'] ?? '',
'created_at' => $item['post_time'] ?? date('Y-m-d H:i:s'),
'updated_at' => $item['post_time'] ?? date('Y-m-d H:i:s'),
'is_upgrade' => 1,
'six_read' => 1,
'route' => $route
]);
$this->set_map($route, RouteMap::SOURCE_MODULE, $id, $project_id);
CollectTask::_insert($item['url'], $project_id, RouteMap::SOURCE_MODULE, $id, $domain_arr['host'], $link_type, $language_list, $page_list);
} else {
$id = $custom_content['id'];
$model->edit([
'name' => $item['title'],
'category_id' => $category_id,
'content' => $item['content'] ?? '',
], ['id' => $id]);
}
} catch (\Exception $e) {
echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
continue;
}
}
}
}
}
} else {
return true;
}
}
}
}
//关闭数据库
... ... @@ -595,7 +691,7 @@ class ProjectUpdate extends Command
$parent_id = $parent['id'];
}
if (!empty($item['children'])) {
if (!empty($item['children'] ?? [])) {
$this->category_insert($project_id, $item['children'], $parent_id);
}
}
... ... @@ -629,13 +725,48 @@ class ProjectUpdate extends Command
$parent_id = $parent['id'];
}
if (!empty($item['children'])) {
if (!empty($item['children'] ?? [])) {
$this->category_news_insert($project_id, $item['children'], $parent_id);
}
}
}
}
//扩展模块多级分类入库
protected function category_custom_insert($project_id, $module_id, $items, $pid = 0)
{
$model = new CustomModuleCategory();
foreach ($items as $item) {
$route = $this->get_url_route($item['url'] ?? '');
if ($route) {
$parent = $model->read(['pid' => $pid, 'route' => $route], 'id');
if (!$parent) {
try {
$item['name'] = $this->special2str($item['name'] ?? '');
$parent_id = $model->addReturnId([
'project_id' => $project_id,
'module_id' => $module_id,
'name' => $item['name'],
'pid' => $pid,
'original_id' => $item['id'],
'route' => $route
]);
$this->set_map($route, RouteMap::SOURCE_MODULE_CATE, $parent_id, $project_id);
} catch (\Exception $e) {
echo 'date:' . date('Y-m-d H:i:s') . ', category_custom_insert error: ' . $e->getMessage() . PHP_EOL;
continue;
}
} else {
$parent_id = $parent['id'];
}
if (!empty($item['children'] ?? [])) {
$this->category_custom_insert($project_id, $module_id, $item['children'], $parent_id);
}
}
}
}
//特殊字符转换
protected function special2str($str)
{
... ...