作者 李美松

Merge branch 'master' into lms

  1 +<?php
  2 +
  3 +namespace App\Console\Commands\Update;
  4 +
  5 +use App\Helper\Arr;
  6 +use App\Http\Logic\Bside\Product\CategoryLogic;
  7 +use App\Models\Blog\Blog;
  8 +use App\Models\Collect\CollectTask;
  9 +use App\Models\Com\UpdateLog;
  10 +use App\Models\News\News;
  11 +use App\Models\Product\Category;
  12 +use App\Models\Product\Keyword;
  13 +use App\Models\Product\Product;
  14 +use App\Models\RouteMap\RouteMap;
  15 +use App\Models\Template\BCustomTemplate;
  16 +use App\Models\WebSetting\WebSettingReceiving;
  17 +use App\Services\ProjectServer;
  18 +use Illuminate\Console\Command;
  19 +use Illuminate\Support\Facades\DB;
  20 +use Illuminate\Support\Facades\Redis;
  21 +
  22 +/**
  23 + * 4.0,5.0升级到6.0,内容同步
  24 + * Class ProjectImport
  25 + * @package App\Console\Commands
  26 + * @author Akun
  27 + * @date 2023/10/9 15:04
  28 + */
  29 +class Temp extends Command
  30 +{
  31 + /**
  32 + * The name and signature of the console command.
  33 + *
  34 + * @var string
  35 + */
  36 + protected $signature = 'project_update_temp';
  37 +
  38 + /**
  39 + * The console command description.
  40 + *
  41 + * @var string
  42 + */
  43 + protected $description = '执行项目升级任务';
  44 +
  45 +
  46 + public function handle()
  47 + {
  48 + $this->start_update();
  49 + }
  50 +
  51 + protected function start_update()
  52 + {
  53 +
  54 +// $data = UpdateLog::where('project_id','<=',530)->where('api_type','category')->get();
  55 + $data = UpdateLog::where('project_id', '=', 298)->where('api_type', 'category')->get();
  56 + foreach ($data as $task) {
  57 + $project_id = $task->project_id;
  58 + $api_type = $task->api_type;
  59 + $api_url_arr = explode('?', $task->api_url);
  60 + $api_url = $api_url_arr[0];
  61 +
  62 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update start' . PHP_EOL;
  63 +
  64 + //设置数据库
  65 + $project = ProjectServer::useProject($project_id);
  66 + if ($project) {
  67 + //分类
  68 + $url = $api_url . '?' . http_build_query(['w' => 'category']);
  69 + $data = curl_c($url);
  70 + if (isset($data['code']) && $data['code'] == 200) {
  71 + $items = $data['data'] ?? [];
  72 + $this->category_insert($project_id, $items, 0);
  73 + } else {
  74 + continue;
  75 + }
  76 + }
  77 + //关闭数据库
  78 + DB::disconnect('custom_mysql');
  79 +
  80 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update end ' . PHP_EOL;
  81 + }
  82 + }
  83 +
  84 + //获取地址路由
  85 + protected function get_url_route($url)
  86 + {
  87 + $arr = parse_url($url);
  88 + if (empty($arr['path'])) {
  89 + return '';
  90 + }
  91 + $path = $arr['path'];
  92 +
  93 + if (strpos($path, '.') !== false) {
  94 + $path = substr($path, 0, strpos($path, '.'));
  95 + }
  96 +
  97 + $path_arr = explode('/', $path);
  98 +
  99 + return end($path_arr) ? end($path_arr) : $path_arr[count($path_arr) - 2];
  100 + }
  101 +
  102 + //多级分类入库
  103 + protected function category_insert($project_id, $items, $pid = 0)
  104 + {
  105 + $model = new Category();
  106 + foreach ($items as $item) {
  107 + if ($item['name'] ?? '') {
  108 + $parent = $model->read(['pid' => $pid, 'title' => $item['name']], 'id');
  109 + if (!$parent) {
  110 + try {
  111 + $item['name'] = $this->special2str($item['name']);
  112 + $parent_id = $model->addReturnId([
  113 + 'project_id' => $project_id,
  114 + 'title' => $item['name'],
  115 + 'pid' => $pid,
  116 + 'keywords' => $item['keywords'] ?? '',
  117 + 'describe' => $item['description'] ?? '',
  118 + 'route' => $this->get_url_route($item['url'])
  119 + ]);
  120 + $this->set_map($this->get_url_route($item['url']), RouteMap::SOURCE_PRODUCT_CATE, $parent_id, $project_id);
  121 + } catch (\Exception $e) {
  122 + echo 'date:' . date('Y-m-d H:i:s') . ', category_insert error: ' . $e->getMessage() . PHP_EOL;
  123 + continue;
  124 + }
  125 + } else {
  126 + $parent_id = $parent['id'];
  127 + }
  128 +
  129 + if (!empty($item['children'])) {
  130 + $this->category_insert($project_id, $item['children'], $parent_id);
  131 + }
  132 + }
  133 + }
  134 + }
  135 +
  136 + //获取分类名称数组
  137 + protected function get_category_name_arr($category, $pid = 0)
  138 + {
  139 + foreach ($category as $k => $v) {
  140 + if ($v['parent'] == $pid) {
  141 + return $v;
  142 + }
  143 + }
  144 +
  145 + return [];
  146 + }
  147 +
  148 + //特殊字符转换
  149 + protected function special2str($str)
  150 + {
  151 + if (strpos($str, ';') === false) {
  152 + return $str;
  153 + }
  154 +
  155 + $list = [
  156 + '&lt;' => '<',
  157 + '&gt;' => '>',
  158 + '&amp;' => '&',
  159 + '&acute;' => '´',
  160 + '&quot;' => '“',
  161 + '&nbsp;' => ' '
  162 + ];
  163 +
  164 + foreach ($list as $k => $v) {
  165 + $str = str_replace($k, $v, $str);
  166 + }
  167 +
  168 + return $str;
  169 + }
  170 +
  171 + //路由入库
  172 + protected function set_map($route, $source, $source_id, $project_id)
  173 + {
  174 + if ($route) {
  175 + $route_map = RouteMap::where('project_id', $project_id)->where('source', $source)->where('source_id', $source_id)->first();
  176 + if (!$route_map) {
  177 + $route_map = new RouteMap();
  178 + $route_map->project_id = $project_id;
  179 + $route_map->source = $source;
  180 + $route_map->source_id = $source_id;
  181 + $route_map->route = $route;
  182 +
  183 + if ($source == RouteMap::SOURCE_NEWS) {
  184 + $route_map->path = RouteMap::SOURCE_NEWS;
  185 + } elseif ($source == RouteMap::SOURCE_BLOG) {
  186 + $route_map->path = RouteMap::SOURCE_BLOG;
  187 + }
  188 +
  189 + $route_map->save();
  190 + }
  191 + }
  192 + }
  193 +}