ProjectUpdate.php 22.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
<?php

namespace App\Console\Commands\Update;

use App\Helper\Arr;
use App\Http\Logic\Bside\Product\CategoryLogic;
use App\Http\Logic\Bside\Product\KeywordLogic;
use App\Models\Blog\Blog;
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 ProjectUpdate extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'project_update';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '执行项目升级任务';


    public function handle()
    {
        while (true) {
            $this->start_update();
        }
    }

    protected function start_update()
    {
        $task_id = $this->get_task();
        if (!$task_id) {
            sleep(60);
            return true;
        }

        $task = UpdateLog::where('id', $task_id)->where('status', UpdateLog::STATUS_UN)->first();
        if (!$task) {
            sleep(2);
            return true;
        }

        $project_id = $task->project_id;
        $api_type = $task->api_type;
        $api_url_arr = explode('?', $task->api_url);
        $api_url = $api_url_arr[0];

        $page_size = 20;

        echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update start' . PHP_EOL;

        $task->status = UpdateLog::STATUS_ING;//同步中
        $task->save();

        //设置数据库
        $project = ProjectServer::useProject($task->project_id);
        if ($project) {
            if ($api_type == 'category') {
                //分类
                $url = $api_url . '?' . http_build_query(['w' => 'category']);
                $data = http_get($url, ['charset' => 'UTF-8']);
                if (isset($data['code']) && $data['code'] == 200) {
                    $items = $data['data'] ?? [];
                    $this->category_insert($project_id, $items, 0);
//                    $model = new Category();
//                    foreach ($items as $item) {
//                        $parent = $model->read(['pid' => 0, 'title' => $item['name']], 'id');
//                        if (!$parent) {
//                            try {
//                                $parent_id = $model->addReturnId([
//                                    'project_id' => $project_id,
//                                    'title' => $item['name'],
//                                    'pid' => 0,
//                                    'keywords' => $item['keywords'],
//                                    'describe' => $item['description']
//                                ]);
//                                $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['name'], RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id);
//                                $model->edit(['route' => $route], ['id' => $parent_id]);
//                            } catch (\Exception $e) {
//                                echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
//                                continue;
//                            }
//                        } else {
//                            $parent_id = $parent['id'];
//                        }
//
//                        foreach ($item['children'] as $child) {
//                            $child_info = $model->read(['pid' => $parent_id, 'title' => $child['name']]);
//                            if (!$child_info) {
//                                try {
//                                    $child_id = $model->addReturnId([
//                                        'project_id' => $project_id,
//                                        'title' => $child['name'],
//                                        'pid' => $parent_id,
//                                        'keywords' => $child['keywords'],
//                                        'describe' => $child['description']
//                                    ]);
//                                    $route = RouteMap::setRoute($child['url'] ? $this->get_url_route($child['url']) : $child['name'], RouteMap::SOURCE_PRODUCT_CATE, $child_id, $project_id);
//                                    $model->edit(['route' => $route], ['id' => $child_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;
                }
            } elseif ($api_type == 'tag') {
                //关键词
                $url = $api_url . '?' . http_build_query(['w' => 'tag', 'page' => 1, 'pagesize' => 0]);
                $data = http_get($url, ['charset' => 'UTF-8']);
                if (isset($data['code']) && $data['code'] == 200) {
                    $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' => 'tag', 'page' => $page, 'pagesize' => $page_size]);
                        $data_page = http_get($url_page, ['charset' => 'UTF-8']);
                        if (isset($data_page['code']) && $data_page['code'] == 200) {
                            $items = $data_page['data']['data'] ?? [];

                            $model = new Keyword();
                            foreach ($items as $item) {
                                if ($item['name'] ?? '') {
                                    $keyword = $model->read(['title' => $item['name']], 'id');
                                    if (!$keyword) {
                                        try {
                                            $id = $model->addReturnId([
                                                'project_id' => $project_id,
                                                'title' => $item['name'],
                                                'seo_title' => $item['seo_title'] ?? '',
                                                'seo_keywords' => $item['seo_keywords'] ?? '',
                                                'seo_description' => $item['seo_description'] ?? '',
                                            ]);
                                            $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['name'], RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $project_id);
                                            $model->edit(['route' => $route], ['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;
                }
            } elseif ($api_type == 'website_info') {
                //网站信息
                $url = $api_url . '?' . http_build_query(['w' => 'website_info']);
                $data = http_get($url, ['charset' => 'UTF-8']);
                if (isset($data['code']) && $data['code'] == 200) {
                    $phones = $data['data']['phones'] ?? '';
                    $emails = $data['data']['emails'] ?? '';

                    $model = new WebSettingReceiving();
                    if ($phones) {
                        $phone_arr = explode(',', $phones);
                        foreach ($phone_arr as $v_phone) {
                            if ($v_phone) {
                                $receiving_phones = $model->read(['type' => 2, 'values' => $v_phone]);
                                if (!$receiving_phones) {
                                    $model->add([
                                        'type' => 1,
                                        'values' => $v_phone,
                                        'project_id' => $project_id
                                    ]);
                                }
                            }
                        }
                    }
                    if ($emails) {
                        $email_arr = explode(',', $emails);
                        foreach ($email_arr as $v_email) {
                            if ($v_email) {
                                $receiving_emails = $model->read(['type' => 1, 'values' => $v_email]);
                                if (!$receiving_emails) {
                                    $model->add([
                                        'type' => 2,
                                        'values' => $v_email,
                                        'project_id' => $project_id
                                    ]);
                                }
                            }
                        }
                    }
                } else {
                    return true;
                }
            } elseif ($api_type == 'post') {
                //产品
                $url = $api_url . '?' . http_build_query(['w' => 'post', 'page' => 1, 'pagesize' => 0]);
                $data = http_get($url, ['charset' => 'UTF-8']);
                if (isset($data['code']) && $data['code'] == 200) {
                    $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' => 'post', 'page' => $page, 'pagesize' => $page_size]);
                        $data_page = http_get($url_page, ['charset' => 'UTF-8']);
                        if (isset($data_page['code']) && $data_page['code'] == 200) {
                            $items = $data_page['data']['data'] ?? [];

                            $model = new Product();

                            foreach ($items as $item) {
                                if ($item['ttile'] ?? '') {
                                    $product = $model->read(['title' => $item['ttile']], 'id');
                                    if (!$product) {
                                        //图片
                                        $gallery = [];
                                        if ($item['images'] ?? []) {
                                            foreach ($item['images'] as $k_img => $img) {
                                                $gallery[] = ['alt' => '这是一张产品图', 'url' => $img];
                                            }
                                        }
                                        //分类
                                        $category_id = '';
                                        if ($item['category'] ?? []) {
                                            $category_arr = [];
                                            foreach ($item['category'] as $cate) {
                                                if ($cate['parent'] == 0) {
                                                    array_unshift($category_arr, $cate['name']);
                                                } else {
                                                    array_push($category_arr, $cate['name']);
                                                }
                                            }
                                            if ($category_arr) {
                                                $categoryLogic = new CategoryLogic();
                                                $category_id = $categoryLogic->importProductCategory($project_id, implode('/', $category_arr));
                                            }
                                        }
                                        try {
                                            $id = $model->addReturnId([
                                                'project_id' => $project_id,
                                                'title' => $item['ttile'],
                                                'intro' => $item['description'] ?? '',
                                                'content' => $item['content'] ?? '',
                                                'category_id' => $category_id,
                                                'thumb' => isset($gallery[0]) ? Arr::a2s($gallery[0]) : '',
                                                'gallery' => Arr::a2s($gallery),
                                                'seo_mate' => Arr::a2s([
                                                    'title' => $item['ttile'],
                                                    'keyword' => $item['keywords'] ?? '',
                                                    'description' => $item['description'] ?? ''
                                                ]),
                                                'status' => Product::STATUS_ON
                                            ]);
                                            $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['ttile'], RouteMap::SOURCE_PRODUCT, $id, $project_id);
                                            $model->edit(['route' => $route], ['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;
                }
            } elseif ($api_type == 'news' || $api_type == 'blog') {
                //新闻或博客
                $url = $api_url . '?' . http_build_query(['w' => $api_type, 'page' => 1, 'pagesize' => 0]);
                $data = http_get($url, ['charset' => 'UTF-8']);
                if (isset($data['code']) && $data['code'] == 200) {
                    $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 = http_get($url_page, ['charset' => 'UTF-8']);
                        if (isset($data_page['code']) && $data_page['code'] == 200) {
                            $items = $data_page['data']['data'] ?? [];

                            if ($api_type == 'news') {
                                $model = new News();
                            } else {
                                $model = new Blog();
                            }

                            foreach ($items as $item) {

                                if ($item['ttile'] ?? '') {
                                    $news = $model->read(['name' => $item['ttile']], 'id');
                                    if (!$news) {
                                        try {
                                            $id = $model->addReturnId([
                                                'project_id' => $project_id,
                                                'name' => $item['ttile'],
                                                'seo_title' => $item['ttile'],
                                                '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
                                            ]);
                                            $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['ttile'], $api_type == 'news' ? RouteMap::SOURCE_NEWS : RouteMap::SOURCE_BLOG, $id, $project_id);
                                            $model->edit(['url' => $route], ['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;
                }
            } else {
                //单页
                $url = $api_url . '?' . http_build_query(['w' => 'page', 'page' => 1, 'pagesize' => 0]);
                $data = http_get($url, ['charset' => 'UTF-8']);
                if (isset($data['code']) && $data['code'] == 200) {
                    $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' => 'page', 'page' => $page, 'pagesize' => $page_size]);
                        $data_page = http_get($url_page, ['charset' => 'UTF-8']);
                        if (isset($data_page['code']) && $data_page['code'] == 200) {
                            $items = $data_page['data']['data'] ?? [];

                            $model = new BCustomTemplate();

                            foreach ($items as $item) {
                                if ($item['ttile'] ?? '') {
                                    $custom = $model->read(['name' => $item['ttile']], 'id');
                                    if (!$custom) {
                                        try {
                                            $id = $model->addReturnId([
                                                'project_id' => $project_id,
                                                'name' => $item['ttile'],
                                                'title' => $item['ttile'],
                                                'keywords' => $item['keywords'] ?? '',
                                                'description' => $item['description'] ?? '',
                                                'html' => $item['content'] ?? '',
                                                'status' => 1
                                            ]);
                                            $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['ttile'], RouteMap::SOURCE_PAGE, $id, $project_id);
                                            $model->edit(['url' => $route], ['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;
                }
            }
        }
        //关闭数据库
        DB::disconnect('custom_mysql');

        $task->status = UpdateLog::STATUS_COM;//同步完成
        $task->save();

        echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update end ' . PHP_EOL;

        sleep(2);
    }

    //获取任务
    protected function get_task()
    {
        $key = 'console_update_task';
        $task_id = Redis::rpop($key);
        if ($task_id) {
            return $task_id;
        }

        $task_list = UpdateLog::where('status', UpdateLog::STATUS_UN)->orderBy('project_id', 'asc')->orderBy('sort', 'asc')->limit(7)->get();
        if ($task_list->count() == 0) {
            return false;
        }

        foreach ($task_list as $value) {
            Redis::lpush($key, $value->id);
        }

        $task_id = Redis::rpop($key);
        return $task_id;
    }

    //获取地址路由
    protected function get_url_route($url)
    {
        $arr = explode('/', $url);
        return $arr[count($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) {
                    $parent_id = $model->addReturnId([
                        'project_id' => $project_id,
                        'title' => $item['name'],
                        'pid' => $pid,
                        'keywords' => $item['keywords'] ?? '',
                        'describe' => $item['description'] ?? ''
                    ]);
                    $route = RouteMap::setRoute($item['url'] ? $this->get_url_route($item['url']) : $item['name'], RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id);
                    $model->edit(['route' => $route], ['id' => $parent_id]);
                } else {
                    $parent_id = $parent['id'];
                }

                if (!empty($item['children'])) {
                    $this->category_insert($project_id, $item['children'], $parent_id);
                }
            }
        }
    }
}