ProjectUpdateTemp.php 4.3 KB
<?php

namespace App\Console\Commands\Update;

use App\Helper\Arr;
use App\Http\Logic\Bside\News\NewsLogic;
use App\Http\Logic\Bside\Product\CategoryLogic;
use App\Models\Blog\Blog;
use App\Models\Collect\CollectSource;
use App\Models\Collect\CollectTask;
use App\Models\Com\UpdateLog;
use App\Models\News\News;
use App\Models\News\NewsCategory;
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\CosService;
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 ProjectUpdateTemp 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()
    {
        $data = UpdateLog::where('api_type', 'category_news')->where('project_id', 298)->get();
        foreach ($data as $task) {
            $this->start_update($task);
        }
    }

    protected function start_update($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_news']);
            $data = curl_c($url);
            if (isset($data['code']) && $data['code'] == 200) {
                $items = $data['data'] ?? [];
                $this->category_news_insert($project_id, $items, 0);
            } else {
                return true;
            }
        }
        //关闭数据库
        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;

        sleep(2);
    }

    //获取地址路由
    protected function get_url_route($url)
    {
        if (strpos($url, '%ec') !== false) {
            $url = str_replace('%ec', 'v6SpecialStr', $url);
            $url = urldecode($url);
            $url = str_replace('v6SpecialStr', '%ec', $url);
        } else {
            $url = urldecode($url);
        }
        $arr = parse_url($url);
        if (empty($arr['path'])) {
            return '';
        }
        $path = $arr['path'];

        $path_arr = explode('/', $path);

        return end($path_arr) ? end($path_arr) : $path_arr[count($path_arr) - 2];
    }

    //新闻多级分类入库
    protected function category_news_insert($project_id, $items, $pid = 0)
    {
        $model = new NewsCategory();
        foreach ($items as $item) {
            $route = $this->get_url_route($item['url'] ?? '');
            if ($route) {
                $item['name'] = $this->special2str($item['name'] ?? '');
                $parent = $model->read(['pid' => $pid, 'name' => $item['name']], 'id');
                if ($parent) {
                    $model->edit(['alias' => $route], ['id' => $parent['id']]);

                    if (!empty($item['children'])) {
                        $this->category_news_insert($project_id, $item['children'], $parent['id']);
                    }
                }
            }
        }
    }

    //特殊字符转换
    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;
    }
}