作者 刘锟

update

  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Update;
  4 +
  5 +use App\Helper\Arr;
  6 +use App\Http\Logic\Bside\News\NewsLogic;
  7 +use App\Http\Logic\Bside\Product\CategoryLogic;
  8 +use App\Models\Blog\Blog;
  9 +use App\Models\Collect\CollectSource;
  10 +use App\Models\Collect\CollectTask;
  11 +use App\Models\Com\UpdateLog;
  12 +use App\Models\News\News;
  13 +use App\Models\News\NewsCategory;
  14 +use App\Models\Product\Category;
  15 +use App\Models\Product\Keyword;
  16 +use App\Models\Product\Product;
  17 +use App\Models\RouteMap\RouteMap;
  18 +use App\Models\Template\BCustomTemplate;
  19 +use App\Models\WebSetting\WebSettingReceiving;
  20 +use App\Services\CosService;
  21 +use App\Services\ProjectServer;
  22 +use Illuminate\Console\Command;
  23 +use Illuminate\Support\Facades\DB;
  24 +use Illuminate\Support\Facades\Redis;
  25 +
  26 +/**
  27 + * 4.0,5.0升级到6.0,内容同步
  28 + * Class ProjectImport
  29 + * @package App\Console\Commands
  30 + * @author Akun
  31 + * @date 2023/10/9 15:04
  32 + */
  33 +class ProjectUpdateTemp extends Command
  34 +{
  35 + /**
  36 + * The name and signature of the console command.
  37 + *
  38 + * @var string
  39 + */
  40 + protected $signature = 'project_update_temp';
  41 +
  42 + /**
  43 + * The console command description.
  44 + *
  45 + * @var string
  46 + */
  47 + protected $description = '执行项目升级任务';
  48 +
  49 +
  50 + public function handle()
  51 + {
  52 + $data = UpdateLog::where('api_type', 'category_news')->where('project_id', 298)->get();
  53 + foreach ($data as $task) {
  54 + $this->start_update($task);
  55 + }
  56 + }
  57 +
  58 + protected function start_update($task)
  59 + {
  60 + $project_id = $task->project_id;
  61 + $api_type = $task->api_type;
  62 + $api_url_arr = explode('?', $task->api_url);
  63 + $api_url = $api_url_arr[0];
  64 +
  65 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update start' . PHP_EOL;
  66 +
  67 + //设置数据库
  68 + $project = ProjectServer::useProject($project_id);
  69 + if ($project) {
  70 + // 新闻分类
  71 + $url = $api_url . '?' . http_build_query(['w' => 'category_news']);
  72 + $data = curl_c($url);
  73 + if (isset($data['code']) && $data['code'] == 200) {
  74 + $items = $data['data'] ?? [];
  75 + $this->category_news_insert($project_id, $items, 0);
  76 + } else {
  77 + return true;
  78 + }
  79 + }
  80 + //关闭数据库
  81 + DB::disconnect('custom_mysql');
  82 +
  83 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update end ' . PHP_EOL;
  84 +
  85 + sleep(2);
  86 + }
  87 +
  88 + //获取地址路由
  89 + protected function get_url_route($url)
  90 + {
  91 + if (strpos($url, '%ec') !== false) {
  92 + $url = str_replace('%ec', 'v6SpecialStr', $url);
  93 + $url = urldecode($url);
  94 + $url = str_replace('v6SpecialStr', '%ec', $url);
  95 + } else {
  96 + $url = urldecode($url);
  97 + }
  98 + $arr = parse_url($url);
  99 + if (empty($arr['path'])) {
  100 + return '';
  101 + }
  102 + $path = $arr['path'];
  103 +
  104 + $path_arr = explode('/', $path);
  105 +
  106 + return end($path_arr) ? end($path_arr) : $path_arr[count($path_arr) - 2];
  107 + }
  108 +
  109 + //新闻多级分类入库
  110 + protected function category_news_insert($project_id, $items, $pid = 0)
  111 + {
  112 + $model = new NewsCategory();
  113 + foreach ($items as $item) {
  114 + $route = $this->get_url_route($item['url'] ?? '');
  115 + if ($route) {
  116 + $item['name'] = $this->special2str($item['name'] ?? '');
  117 + $parent = $model->read(['pid' => $pid, 'name' => $item['name']], 'id');
  118 + if ($parent) {
  119 + $model->edit(['alias' => $route], ['id' => $parent['id']]);
  120 +
  121 + if (!empty($item['children'])) {
  122 + $this->category_news_insert($project_id, $item['children'], $parent['id']);
  123 + }
  124 + }
  125 + }
  126 + }
  127 + }
  128 +
  129 + //特殊字符转换
  130 + protected function special2str($str)
  131 + {
  132 + if (strpos($str, ';') === false) {
  133 + return $str;
  134 + }
  135 +
  136 + $list = [
  137 + '&lt;' => '<',
  138 + '&gt;' => '>',
  139 + '&amp;' => '&',
  140 + '&acute;' => '´',
  141 + '&quot;' => '“',
  142 + '&nbsp;' => ' '
  143 + ];
  144 +
  145 + foreach ($list as $k => $v) {
  146 + $str = str_replace($k, $v, $str);
  147 + }
  148 +
  149 + return $str;
  150 + }
  151 +}