Temp.php 19.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
<?php

namespace App\Console\Commands\Test;

use App\Helper\Arr;
use App\Models\Com\Notify;
use App\Models\Devops\ServerConfig;
use App\Models\Devops\ServersIp;
use App\Models\Domain\DomainCreateTask;
use App\Models\Domain\DomainInfo;
use App\Models\Product\Keyword;
use App\Models\Product\KeywordRelated;
use App\Models\Product\Product;
use App\Models\Project\DeployBuild;
use App\Models\Project\Project;
use App\Services\BatchExportService;
use App\Services\ProjectServer;
use Illuminate\Console\Command;

class Temp extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test_temp';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '临时脚本(akun)';

    public function handle()
    {
        $this->create_server_update_page();
//        $servers_id = 14;
//
//        $arr = [
//            '173.245.87.74',
//            '173.245.87.75',
//            '173.245.87.76',
//            '173.245.87.77',
//            '173.245.87.78',
//        ];
//
//        $ip1 = '50.117.43.';
//
//        for ($i = 2; $i <= 254; $i++) {
//            $arr[] = $ip1 . $i;
//        }
//
//        $ip2 = '50.117.118.';
//
//        for ($j = 2; $j <= 254; $j++) {
//            $arr[] = $ip2 . $j;
//        }
//
//        $server_ips = ServersIp::where('servers_id', $servers_id)->get();
//
//        foreach ($server_ips as $k => $item) {
//            $item->ip = $arr[$k];
//            $item->save();
//        }
//
//        $this->output('success');
    }

    /**
     * 创建指定服务器所有项目主站生成任务
     * @author Akun
     * @date 2024/12/19 17:54
     */
    public function create_server_update_page()
    {
        $server_id = 15;

        $server_ip_model = new ServersIp();

        $server_ip_ids = $server_ip_model->where('servers_id', $server_id)->get()->pluck('id')->toArray();

        $project_list = Project::select(['id', 'serve_id'])->whereIn('serve_id', $server_ip_ids)->get();

        $domain_model = new DomainInfo();
        $notify_model = new Notify();

        foreach ($project_list as $value) {
            $project_id = $value->id;

            $domain_info = $domain_model->read(['project_id' => $project_id, 'status' => 1], ['id', 'domain']);
            if (!$domain_info) {
                //过滤未绑定正式域名的项目
                continue;
            }
            $domain = $domain_info['domain'];

            //获取项目所在服务器
            $serve_ip_info = $server_ip_model->read(['id' => $value->serve_id], ['domain', 'ip']);
            if (!$serve_ip_info) {
                $this->output('项目id:' . $project_id . ' | 未查询到服务器数据');
                continue;
            }

            if (!check_domain_record($domain, $serve_ip_info)) {
                $this->output('项目id:' . $project_id . ', domain:' . $domain . ' | 未解析到目标服务器');
                continue;
            }

            $data = [
                'project_id' => $project_id,
                'type' => 1,
                'route' => 1,
                'server_id' => $server_id,
                'status' => ['!=', Notify::STATUS_FINISH_SITEMAP]
            ];
            $notify = $notify_model->read($data, ['id']);

            if (!$notify) {
                $data['data'] = Arr::a2s(['domain' => $domain, 'url' => null, 'language' => []]);
                $data['status'] = Notify::STATUS_INIT;
                $notify_model->add($data);

                $this->output('项目id:' . $project_id . ' | success');
            }
        }
    }

    /**
     * 转移灾备服务器后,判断项目是否正常访问
     * @author Akun
     * @date 2024/12/11 10:15
     */
    public function check_zb_complete_project_site()
    {
        $server_id = 15;
        $server_name = '美服1';

        $server_ip_model = new ServersIp();

        $server_ip_ids = $server_ip_model->where('servers_id', $server_id)->get()->pluck('id')->toArray();

        $project_list = Project::select(['id', 'serve_id', 'title'])->whereIn('serve_id', $server_ip_ids)->get();

        $domain_model = new DomainInfo();
        $data = [];
        foreach ($project_list as $value) {
            $domain_info = $domain_model->read(['project_id' => $value->id, 'status' => 1], ['id', 'domain']);
            if (!$domain_info) {
                //过滤未绑定正式域名的项目
                continue;
            }
            $domain = $domain_info['domain'];

            $check = dns_get_record($domain, DNS_A);
            $ip = $check[0]['ip'] ?? '';
            $host = $check[0]['host'] ?? '';
            $code = check_curl_status($domain);
            $data[] = [
                $value->id,
                $value->title,
                $domain,
                $host,
                $ip,
                $code
            ];
        }
        $map = ['项目id', '名称', '域名', 'CNAME', 'IP', '站点状态'];
        if ($data) {
            $table = new BatchExportService($server_name . '站点解析情况及访问状态');
            $file = $table->head($map)->data($data)->save();
            if (!$file) {
                $this->output('文件生成失败,请重试');
            } else {
                $this->output('export success');
            }
        } else {
            $this->output('no data');
        }
    }

    /**
     * 检查服务器上没有解析cname的项目
     * @author Akun
     * @date 2024/12/11 10:15
     */
    public function check_no_cname_projects()
    {
        $server_id = 15;
        $server_name = '美服1';

        $server_ip_model = new ServersIp();

        $server_ip_ids = $server_ip_model->where('servers_id', $server_id)->get()->pluck('id')->toArray();

        $project_list = Project::select(['id', 'serve_id', 'title'])->whereIn('serve_id', $server_ip_ids)->get();

        $domain_model = new DomainInfo();
        $data = [];
        foreach ($project_list as $value) {
            $domain_info = $domain_model->read(['project_id' => $value->id, 'status' => 1], ['id', 'domain']);
            if (!$domain_info) {
                //过滤未绑定正式域名的项目
                continue;
            }
            $domain = $domain_info['domain'];

            $check = dns_get_record($domain, DNS_A);
            $ip = $check[0]['ip'] ?? '';
            $host = $check[0]['host'] ?? '';
            if (strpos($host, 'globalso.com') === false) {
                $data[] = [
                    $value->id,
                    $value->title,
                    $domain,
                    $ip
                ];
            }
        }
        $map = ['项目id', '名称', '域名', '解析IP'];
        if ($data) {
            $table = new BatchExportService($server_name . '没有解析cname的项目');
            $file = $table->head($map)->data($data)->save();
            if (!$file) {
                $this->output('文件生成失败,请重试');
            } else {
                $this->output('export success');
            }
        } else {
            $this->output('no data');
        }
    }

    /**
     * 伍欣组推广项目:开启AMP站点,首页不强制跳转,批量生成AMP站点页面
     * @author Akun
     * @date 2024/12/10 10:43
     */
    public function wx_tg_projects()
    {
        //获取所有组内推广项目
        $leader_mid = 16;
        $project_ids = DeployBuild::where('leader_mid', $leader_mid)->pluck('project_id')->toArray();
        $project_list = Project::where('type', 2)->whereIn('id', $project_ids)->get();

        $task_model = new DomainCreateTask();
        foreach ($project_list as $project) {
            $project_id = $project->id;

            //获取项目域名情况
            $domain_info = DomainInfo::where('project_id', $project_id)->where('status', 1)->first();
            if (!$domain_info) {
                $this->output('项目id:' . $project_id . ' | 未绑定正式域名');
                continue;
            }
            $domain_id = $domain_info->id;
            $domain = $domain_info->domain;

            //获取项目所在服务器
            $serve_ip_info = ServersIp::select(['id', 'ip', 'domain', 'servers_id'])->where('id', $project->serve_id)->first()->toArray();
            if (!$serve_ip_info) {
                $this->output('项目id:' . $project_id . ' | 未查询到服务器数据');
                continue;
            }
            $servers_id = $serve_ip_info['servers_id'];

            if ($servers_id == ServerConfig::SELF_SITE_ID) {
                $this->output('项目id:' . $project_id . ' | 自建站项目过滤');
                continue;
            }

            if ($servers_id == ServerConfig::SELF_TEST_ID) {
                $this->output('项目id:' . $project_id . ' | 测试项目过滤');
                continue;
            }

            if (!check_domain_record($domain_info['domain'], $serve_ip_info)) {
                $this->output('项目id:' . $project_id . ', domain:' . $domain . ' | 未解析到目标服务器');
                continue;
            }

            //开启AMP站点
            if ($domain_info->amp_status == 0) {
                $domain_info->amp_status = 1;
                $domain_info->amp_type = 1;
            }
            //首页不强制跳转
            $domain_info->is_redirect = 0;
            $domain_info->save();

            //创建主站建站任务
            $task_model->add([
                'server_id' => $servers_id,
                'project_id' => $project_id,
                'domain_id' => $domain_id,
                'type' => DomainCreateTask::TYPE_MAIN
            ]);

            //创建AMP站建站任务
            $task_model->add([
                'server_id' => $servers_id,
                'project_id' => $project_id,
                'domain_id' => $domain_id,
                'type' => DomainCreateTask::TYPE_AMP
            ]);

            //请求对应C端页面更新接口
            $c_url = 'https://' . $domain . '/api/update_page/';
            $c_params = [
                'project_id' => $project_id,
                'type' => 3
            ];
            $re = http_post($c_url, json_encode($c_params), [], true);
            if (isset($re['status']) && $re['status'] == 200) {
                $this->output($c_url . ' | 请求成功');
            } else {
                $this->output($c_url . ' | ' . ($re['message'] ?? '未返回失败原因'));
            }
        }
    }

    /**
     * 关联所有项目关键词
     * @author Akun
     * @date 2024/11/18 15:07
     */
    public function relation_all_keywords()
    {
        $projects = Project::select(['id'])->orderBy('id', 'asc')->get();
        foreach ($projects as $project) {
            ProjectServer::useProject($project->id);

            try {
                Product::select(['id', 'keyword_id'])->chunk(1000, function ($query) {
                    foreach ($query as $item) {
                        KeywordRelated::saveRelated($item->id, $item->keyword_id);
                    }
                });
            } catch (\Exception $e) {
                $this->output($e->getMessage());
                continue;
            }

            $this->output('ID:' . $project->id . ',success');
        }
    }

    /**
     * 关键词首字母
     * @author Akun
     * @date 2024/10/30 15:37
     */
    public function keywords_first_word()
    {
        $projects = Project::select(['id'])->orderBy('id', 'asc')->get();
        foreach ($projects as $project) {
            ProjectServer::useProject($project->id);

            //处理关键词首字母
            try {
                Keyword::select(['id', 'title', 'first_word'])->chunk(1000, function ($query) {
                    foreach ($query as $item) {
                        if ($item->first_word === null) {
                            $item->first_word = $this->getTitleFirstLetter($item->title);
                            $item->save();
                        }
                    }
                });
            } catch (\Exception $e) {
                $this->output($e->getMessage());
                continue;
            }


            $this->output('ID:' . $project->id . ',success');
        }
    }

    protected function getTitleFirstLetter($title)
    {
        $first = mb_strtolower(mb_substr($title, 0, 1, 'utf-8'), 'utf-8');
        if (in_array($first, ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9"])) {
            $firstLetter = 0;
        } else {
            if (in_array($first, ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'])) {
                $rule = [
                    "a" => 1,
                    "b" => 2,
                    "c" => 3,
                    "d" => 4,
                    "e" => 5,
                    "f" => 6,
                    "g" => 7,
                    "h" => 8,
                    "i" => 9,
                    "j" => 10,
                    "k" => 11,
                    "l" => 12,
                    "m" => 13,
                    "n" => 14,
                    "o" => 15,
                    "p" => 16,
                    "q" => 17,
                    "r" => 18,
                    "s" => 19,
                    "t" => 20,
                    "u" => 21,
                    "v" => 22,
                    "w" => 23,
                    "x" => 24,
                    "y" => 25,
                    "z" => 26,
                ];
                $firstLetter = $rule[$first];
            } else {
                $firstLetter = 27;
            }
        }
        return $firstLetter;
    }


    /**
     * 获取指定服务器所有项目
     * @author Akun
     * @date 2024/09/30 17:01
     */
    public function get_all_projects_by_server()
    {
        $server_id = 1;

        $server_ip_model = new ServersIp();

        $server_ip_ids = $server_ip_model->where('servers_id', $server_id)->get()->pluck('id')->toArray();

        $project_list = Project::select(['id', 'serve_id', 'title'])->whereIn('serve_id', $server_ip_ids)->get();

        $domain_model = new DomainInfo();
        $data = [];
        foreach ($project_list as $value) {
            $domain_info = $domain_model->read(['project_id' => $value->id, 'status' => 1], ['id', 'domain']);
            if (!$domain_info) {
                //过滤未绑定正式域名的项目
                continue;
            }
            $domain = $domain_info['domain'];

            $data[] = [
                $value->id,
                $value->title,
                $domain
            ];
        }
        $map = ['项目id', '名称', '域名'];
        if ($data) {
            $table = new BatchExportService("240云服务器项目");
            $file = $table->head($map)->data($data)->save();
            if (!$file) {
                $this->output('文件生成失败,请重试');
            } else {
                $this->output('export success');
            }
        } else {
            $this->output('no data');
        }
    }


    /**
     * 检查不在所属服务器解析上的域名
     * @author Akun
     * @date 2024/09/26 10:48
     */
    public function check_no_server_projects()
    {
        $server_id = 15;

        $server_ip_model = new ServersIp();

        $server_ip_ids = $server_ip_model->where('servers_id', $server_id)->get()->pluck('id')->toArray();

        $project_list = Project::select(['id', 'serve_id', 'title'])->whereIn('serve_id', $server_ip_ids)->get();

        $domain_model = new DomainInfo();
        $data = [];
        foreach ($project_list as $value) {
            $domain_info = $domain_model->read(['project_id' => $value->id, 'status' => 1], ['id', 'domain']);
            if (!$domain_info) {
                //过滤未绑定正式域名的项目
                continue;
            }
            $domain = $domain_info['domain'];

            $check = dns_get_record($domain, DNS_A);
            $ip = $check[0]['ip'] ?? '';
            $ip_info = $server_ip_model->read(['ip' => $ip]);
            if ($ip_info === false || $ip_info['servers_id'] != $server_id) {
                $data[] = [
                    $value->id,
                    $value->title,
                    $domain,
                    $ip,
                    $ip_info ? $ip_info['servers_id'] : '',
                ];
            }
        }
        $map = ['项目id', '名称', '域名', 'IP', '服务器ID',];
        if ($data) {
            $table = new BatchExportService("美服2项目解析未在当前服务器项目");
            $file = $table->head($map)->data($data)->save();
            if (!$file) {
                $this->output('文件生成失败,请重试');
            } else {
                $this->output('export success');
            }
        } else {
            $this->output('no data');
        }
    }

    /**
     * 创建所有amp站页面生成任务
     * @author Akun
     * @date 2024/09/26 10:48
     */
    public function create_all_amp_notify()
    {
        $notify_model = new Notify();
        $project_model = new Project();
        $serve_ip_model = new ServersIp();

        $domain_list = DomainInfo::where('status', 1)->where('amp_status', 1)->get();

        foreach ($domain_list as $domain_info) {
            $project_id = $domain_info->project_id;
            $domain = $domain_info->domain;

            $this->output('项目id:' . $project_id . ',start');

            //获取项目所在服务器
            $project_info = $project_model->read(['id' => $project_id], ['serve_id']);
            if (!$project_info) {
                $this->output('未查询到项目数据');
                continue;
            }
            $serve_ip_info = $serve_ip_model->read(['id' => $project_info['serve_id']], ['servers_id']);
            if (!$serve_ip_info) {
                $this->output('未查询到服务器数据');
                continue;
            }
            $servers_id = $serve_ip_info['servers_id'];

            if ($servers_id == ServerConfig::SELF_SITE_ID) {
                //自建站服务器:如果项目已经上线,不请求C端接口,数据直接入库
                //判断是否已有更新进行中
                $data = [
                    'project_id' => $project_id,
                    'type' => 3,
                    'route' => 1,
                    'server_id' => ServerConfig::SELF_SITE_ID,
                    'status' => ['!=', Notify::STATUS_FINISH_SITEMAP]
                ];
                $notify = $notify_model->read($data, ['id']);

                if (!$notify) {
                    $domain_array = parse_url($domain);
                    $host = $domain_array['host'] ?? $domain_array['path'];
                    $host_array = explode('.', $host);
                    if (count($host_array) <= 2) {
                        array_unshift($host_array, 'm');
                    } else {
                        $host_array[0] = 'm';
                    }
                    $domain = implode('.', $host_array);

                    $data['data'] = Arr::a2s(['domain' => $domain, 'url' => [], 'language' => []]);
                    $data['status'] = Notify::STATUS_INIT;
                    $notify_model->add($data);
                }
            } else {
                //其他服务器:请求对应C端接口
                $c_url = 'https://' . $domain . '/api/update_page/';
                $c_params = [
                    'project_id' => $project_id,
                    'type' => 3
                ];
                $re = http_post($c_url, json_encode($c_params), [], true);
                if (isset($re['status']) && $re['status'] == 200) {
                    $this->output($c_url . ' | 请求成功');
                } else {
                    $this->output($c_url . ' | ' . ($re['message'] ?? '未返回失败原因'));
                }
            }
            $this->output('项目id:' . $project_id . ',end');
        }
    }

    public function output($msg)
    {
        echo date('Y-m-d H:i:s') . ' | ' . $msg . PHP_EOL;
    }
}