UpdateService.php 4.3 KB
<?php


namespace App\Services\Html;

use App\Models\Com\UpdateLog;
use App\Models\Com\UpdateOldInfo;

class UpdateService
{
    /**
     * 获取项目升级情况
     * @param $project_id
     * @param $domain
     * @return array
     * @author Akun
     * @date 2023/12/01 10:32
     */
    public static function getUpdateInfo($project_id)
    {
        $is_update = 0;
        $is_language_update = 0;
        $old_domain_online = $old_domain_test = '';

        $update_info = UpdateLog::where('project_id', $project_id)->whereNotIn('api_type', ['category', 'website_info', 'tag', 'category_news'])->get();
        if ($update_info->count() > 0) {
            $is_update = 1;
            $is_language_update = 1;

            foreach ($update_info as $value) {
                if ($value->collect_status != 1) {
                    $is_language_update = 0;
                }
                if ($value->collect_status == 0) {
                    $is_update = 0;
                }
            }

            $old_domain_arr = parse_url($update_info[0]['api_url']);
            $old_info = self::getOldInfo($project_id, $old_domain_arr['host']);
            $old_domain_test = $old_info['old_domain_test'];
            $old_domain_online = $old_info['old_domain_online'];
        }

        return [
            'is_update' => $is_update,
            'is_language_update' => $is_language_update,
            'old_domain_test' => $old_domain_test,
            'old_domain_online' => $old_domain_online
        ];
    }

    public static function getOldInfo($project_id, $domain)
    {
        $return = [
            'old_domain_test' => $domain,
            'old_domain_online' => $domain
        ];

        $info = UpdateOldInfo::where('project_id', $project_id)->first();
        if (!$info) {
            $url_web_config = 'https://' . $domain . '/wp-content/cache/user_config.text';
            $data_config = [];

            try {
                $data_config = self::curlGet($url_web_config);
            } catch (\Exception $e) {
            }

            if ($data_config) {
                $link_type = $data_config['link_type'] ?? 0;

                $home_url_arr = parse_url($data_config['home_url'] ?? '');
                $old_domain_test = $home_url_arr['host'] ?? '';

                $web_url_arr = parse_url($data_config['web_url_domain'] ?? '');
                $old_domain_online = $web_url_arr['host'] ?? '';

                if ($old_domain_test || $old_domain_online) {
                    $info = new UpdateOldInfo();
                    $info->project_id = $project_id;
                    $info->link_type = $link_type;
                    $info->old_domain_test = $old_domain_test ?: $domain;
                    $info->old_domain_online = $old_domain_online ?: $domain;
                    $info->save();
                }

                $old_domain_test && $return['old_domain_test'] = $old_domain_test;
                $old_domain_online && $return['old_domain_test'] = $old_domain_online;
            }
        } else {
            $return['old_domain_test'] = $info['old_domain_test'];
            $return['old_domain_online'] = $info['old_domain_online'];
        }

        return $return;
    }

    /**
     * 远程请求
     * @param $url
     * @return mixed
     * @author Akun
     * @date 2023/11/29 14:23
     */
    public static function curlGet($url)
    {
        $header = array(
            'Expect:',
            'Content-Type: application/json; charset=utf-8'
        );
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HEADER, false);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36 Edge/12.246');
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
        curl_setopt($ch, CURLOPT_TIMEOUT, 120);
        curl_setopt($ch, CURLOPT_MAXREDIRS, 10);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_SSLVERSION, 'all');
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        $content = curl_exec($ch);
        curl_close($ch);
        return json_decode($content, true);
    }
}