作者 刘锟

合并分支 'akun' 到 'master'

Akun



查看合并请求 !89
  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 Temp2 extends Command
  30 +{
  31 + /**
  32 + * The name and signature of the console command.
  33 + *
  34 + * @var string
  35 + */
  36 + protected $signature = 'project_update_temp2';
  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', 'post')->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 + $page_size = 20;
  63 +
  64 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update start' . PHP_EOL;
  65 +
  66 + //设置数据库
  67 + $project = ProjectServer::useProject($project_id);
  68 + if ($project) {
  69 + //产品
  70 + $url = $api_url . '?' . http_build_query(['w' => 'post', 'page' => 1, 'pagesize' => 0]);
  71 + $data = curl_c($url);
  72 + if (isset($data['code']) && $data['code'] == 200) {
  73 + $count = $data['data']['count'] ?? 0;
  74 +
  75 + $total_page = ceil($count / $page_size);
  76 + for ($page = 1; $page <= $total_page; $page++) {
  77 + $url_page = $api_url . '?' . http_build_query(['w' => 'post', 'page' => $page, 'pagesize' => $page_size]);
  78 + $data_page = curl_c($url_page);
  79 + if (isset($data_page['code']) && $data_page['code'] == 200) {
  80 + $items = $data_page['data']['data'] ?? [];
  81 +
  82 + $model = new Product();
  83 +
  84 + foreach ($items as $item) {
  85 + if ($item['ttile'] ?? '') {
  86 + $product = $model->read(['title' => $item['ttile']], 'id');
  87 + if ($product) {
  88 + //分类
  89 + $category_id = '';
  90 + if ($item['category'] ?? []) {
  91 + $category_arr = [];
  92 +
  93 + $pid = 0;
  94 + for ($i = 0; $i < count($item['category']); $i++) {
  95 + $return = $this->get_category_name_arr($item['category'], $pid);
  96 + if ($return) {
  97 + $category_arr[] = $return['name'];
  98 + $pid = $return['id'];
  99 + }
  100 + }
  101 +
  102 + if ($category_arr) {
  103 + $categoryLogic = new CategoryLogic();
  104 + $category_id = $categoryLogic->importProductCategory($project_id, implode('/', $category_arr));
  105 + }
  106 + }
  107 + try {
  108 + $model->edit(['category_id' => $category_id], ['id' => $product['id']]);
  109 + } catch (\Exception $e) {
  110 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', error: ' . $e->getMessage() . PHP_EOL;
  111 + continue;
  112 + }
  113 + }
  114 + }
  115 + }
  116 + }
  117 + }
  118 + } else {
  119 + continue;
  120 + }
  121 + }
  122 + //关闭数据库
  123 + DB::disconnect('custom_mysql');
  124 +
  125 + echo 'date:' . date('Y-m-d H:i:s') . ', task_id: ' . $task->id . ', task_type: ' . $api_type . ', update end ' . PHP_EOL;
  126 + }
  127 + }
  128 +
  129 +
  130 + //获取分类名称数组
  131 + protected function get_category_name_arr($category, $pid = 0)
  132 + {
  133 + foreach ($category as $k => $v) {
  134 + if ($v['parent'] == $pid) {
  135 + return $v;
  136 + }
  137 + }
  138 +
  139 + return [];
  140 + }
  141 +}