作者 zhl

创建外链链接

  1 +<?php
  2 +/**
  3 + * Created by PhpStorm.
  4 + * User: zhl
  5 + * Date: 2025/9/15
  6 + * Time: 10:09
  7 + */
  8 +namespace App\Console\Commands\Product;
  9 +
  10 +use App\Models\Domain\DomainInfo;
  11 +use App\Models\Product\Keyword;
  12 +use App\Models\Project\Project;
  13 +use App\Services\ProjectServer;
  14 +use Illuminate\Console\Command;
  15 +use Illuminate\Support\Facades\DB;
  16 +
  17 +/**
  18 + * Class ExternalLinkMake
  19 + * @package App\Console\Commands\Product
  20 + */
  21 +class ExternalLinkMake extends Command
  22 +{
  23 + /**
  24 + * The name and signature of the console command.
  25 + *
  26 + * @var string
  27 + */
  28 + protected $signature = 'external_link_make';
  29 +
  30 + /**
  31 + * The console command description.
  32 + *
  33 + * @var string
  34 + */
  35 + protected $description = '统计不达标项目,500条 聚合页, 作为外链提交';
  36 +
  37 + /**
  38 + * @return bool
  39 + */
  40 + public function handle()
  41 + {
  42 + $date = date('Y-m-d H:i:s', strtotime('-75 day'));
  43 + $projects = Project::where(['type' => 2, 'delete_status' => 0, 'site_status' => 0, 'extend_type' => 0, 'is_remain_today' => 0])->get();
  44 +
  45 + foreach ($projects as $project) {
  46 + if ($project->id <= 1)
  47 + continue;
  48 + $this->output('项目开始: ' . $project->id);
  49 +
  50 + $log = DB::table('gl_project_external_link_make')->where(['project_id' => $project->id])->first();
  51 + if ($log) {
  52 + $this->output('项目已推送过: ' . $project->id . ', 跳过');
  53 + continue;
  54 + }
  55 +
  56 + if(empty($project->deploy_optimize['start_date']) || $project->deploy_optimize['start_date'] > $date){
  57 + $this->output('项目推广不到75天: ' . $project->id . ', 推广开始时间:' . $project->deploy_optimize['start_date']);
  58 + continue;
  59 + }
  60 +
  61 + $domain = DomainInfo::where(['project_id' => $project->id, 'status' => DomainInfo::STATUS_ONE])->first();
  62 + if (empty($domain)) {
  63 + $this->output('项目未找到域名: ' . $project->id);
  64 + continue;
  65 + }
  66 + $host = 'https://' . $domain->domain . '/';
  67 + ProjectServer::useProject($project->id);
  68 + $keywords = Keyword::select(['id', 'title', 'route', 'seo_title'])->where('route', '<>', null)->orderBy('id', 'desc')->limit(500)->get();
  69 + $updated_at = $created_at = date('Y-m-d H:i:s');
  70 + $data = [];
  71 + foreach ($keywords as $keyword) {
  72 + $data[] = [
  73 + 'project_id' => $project->id,
  74 + 'keyword_id' => $keyword->id,
  75 + 'keyword' => $keyword->title,
  76 + 'route' => $keyword->route,
  77 + 'seo_title' => $keyword->seo_title,
  78 + 'external_link' => $host . $keyword->route . '/{' . $keyword->seo_title . '}',
  79 + 'updated_at' => $updated_at,
  80 + 'created_at' => $created_at
  81 + ];
  82 + }
  83 + DB::table('gl_project_external_link_make')->insert($data);
  84 + DB::disconnect('custom_mysql');
  85 + }
  86 +
  87 + return true;
  88 + }
  89 +
  90 + /**
  91 + * 输出message
  92 + * @param $message
  93 + */
  94 + public function output($message)
  95 + {
  96 + $message = date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
  97 + file_put_contents(storage_path('logs/external_link_make.log'), $message, FILE_APPEND);
  98 + echo $message;
  99 + }
  100 +}