InquiryRelateDomain.php 2.6 KB
<?php

namespace App\Models\Inquiry;


use App\Helper\Arr;
use App\Helper\Translate;
use App\Models\Base;
use App\Utils\HttpUtils;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Str;

/**
 * Class InquiryRelateDomain
 * @package App\Models\Inquiry
 * @author zbj
 * @date 2025/4/12
 */
class InquiryRelateDomain extends Base
{

    //设置关联表名
    protected $table = 'gl_inquiry_relate_domain';

    /**
     * 获取关联域名
     * @throws \Exception
     * @author zbj
     * @date 2025/4/12
     */
    public static function getRelateDomain($domain, $k = 'domain'){
        $list_cache_key = 'RelateDomainList_' . $k;
        $data = Cache::get($list_cache_key);
        if(!$data){
            $data = [];
            $page = 1;
            while (true) {
                try {
                    $res = HttpUtils::get('https://www.cmer.site/api/globalso_site', ['pagesize' => 100, 'page' => $page]);
                    if($res) {
                        $res = Arr::s2a($res);
                        $arr = [];
                        foreach ($res['data']['data'] as $item){
                            if($k == 'domain'){
                                $arr[$item['domain']] = $item['globalso_domain'];
                            }else{
                                $arr[$item['globalso_domain']] = $item['domain'];
                            }
                        }
                        $data = array_merge($data, $arr);
                        if ($res['data']['last_page'] == $page) {
                            break;
                        }
                    }
                    $page++;
                } catch (\Exception | GuzzleException $e) {
                    throw new \Exception('关联域名接口错误' . $e->getMessage());
                }
            }
            Cache::put($list_cache_key, $data, 3600);
        }
        if(empty($data[$domain])){
            $domainPrefix = explode(".",$domain);
            if (!empty($domainPrefix)){
                if($domainPrefix[0] == 'm'){
                    $domain = "www.".$domainPrefix[1].".".$domainPrefix[2];
                }else{
                    $isLang = Translate::getTls($domainPrefix[0]);
                    if ($isLang) {
                        $domain = "www.".$domainPrefix[1].".".$domainPrefix[2];
                    }
                }
            }
            if(!Str::startsWith($domain, 'www')){
                $domain = 'www.' . $domain;
            }
        }
        return $data[$domain]??'';
    }
}