GeoLinkLogic.php 4.6 KB
<?php
/**
 * @remark :
 * @name   :GeoLinkLogic.php
 * @author :lyh
 * @method :post
 * @time   :2025/7/14 16:20
 */

namespace App\Http\Logic\Aside\Geo;

use App\Http\Logic\Aside\BaseLogic;
use App\Models\Geo\DomainDa;
use App\Models\Geo\GeoLink;
use App\Services\Geo\GeoService;
use Illuminate\Support\Carbon;

/**
 * @remark :geo权威新闻(链接数据)
 * @name   :GeoLinkLogic
 * @author :lyh
 * @method :post
 * @time   :2025/7/14 16:20
 */
class GeoLinkLogic extends BaseLogic
{
    public function __construct()
    {
        parent::__construct();
        $this->param = $this->requestAll;
        $this->model = new GeoLink();
    }

    /**
     * @remark :获取权威新闻链接数据
     * @name   :getLinkList
     * @author :lyh
     * @method :post
     * @time   :2025/7/14 16:47
     */
    public function getLinkList($map = [],$page = 1,$row = 20,$order = 'id'){
        $filed = ['*'];
        if(isset($map['url']) && !empty($map['url'])){
            $map['url'] = ['like','%'.$map['url'].'%'];
        }
        $lists = $this->model->lists($map,$page,$row,$order,$filed);
        return $this->success($lists);
    }

    /**
     * @remark :获取链接数据详情
     * @name   :getLinkInfo
     * @author :lyh
     * @method :post
     * @time   :2025/7/14 16:51
     */
    public function getLinkInfo(){
        $info = $this->model->read($this->param);
        if($info === false){
            $this->fail('当前数据不存在或者已被删除');
        }
        return $this->success($info);
    }

    /**
     * @remark :保存链接数据
     * @name   :saveLink
     * @author :lyh
     * @method :post
     * @time   :2025/7/14 16:50
     */
    public function saveLink(){
        try {
            if(!empty($this->param['data'])){
                $data = [];
                foreach ($this->param['data'] as $item){
                    $data[] = [
                        'project_id'=>$this->param['project_id'],
                        'da'=>$item['da'] ?? 0,
                        'url'=>$item['url'],
                        'send_time'=>$item['send_time']
                    ];
                }
                $this->model->insertAll($data);
            }
        }catch (\Exception $e){
            $this->fail('保存失败,请联系管理员');
        }
        return $this->success();
    }

    /**
     * @remark :删除数据
     * @name   :delLink
     * @author :lyh
     * @method :post
     * @time   :2025/7/14 16:51
     */
    public function delLink(){
        try {
            $this->model->del(['id'=>['in',$this->param['ids']]]);
        }catch (\Exception $e){
            $this->fail('删除失败,请联系管理员.');
        }
        return $this->success();
    }

    /**
     * @remark :返回数据data
     * @name   :daResultData
     * @author :lyh
     * @method :post
     * @time   :2025/10/9 09:43
     */
    public function daResultData()
    {
        $info = $this->model->read(['id'=>$this->param['id']]);
        if($info === false){
            $this->fail('当前数据不存在或者已被删除');
        }
        $host = $this->getDomainWithWWW($info['url']);
        $domainDaModel = new DomainDa();
        $daInfo = $domainDaModel->read(['domain'=>$host]);
        if($daInfo !== false){
            $info['da'] = $daInfo['da'];
            $this->model->edit(['da'=>$daInfo['da']], ['id'=>$info['id']]);
            return $this->success($info);
        }
        $geoService = new GeoService();
        $result = $geoService->daResult($host);
        if(!isset($result['data']) || empty($result['data'])){
            return $this->success($info);
        }
        $info['da'] = (int)$result['data']['mozDA'];//获取数据中的da值
        //保存数据
        $domainDaModel->addReturnId(['da'=>$info['da'],'domain'=>$host,'result'=>json_encode($result,true)]);
        $this->model->edit(['da'=>$info['da']], ['id'=>$info['id']]);
        return $this->success($result);
    }

    /**
     * @remark :获取域名
     * @name   :getDomainWithWWW
     * @author :lyh
     * @method :post
     * @time   :2025/10/9 10:28
     */
    public function getDomainWithWWW($url) {
        // 获取 host
        $host = parse_url($url, PHP_URL_HOST);
        // 去掉端口号等情况
        $host = preg_replace('/:\d+$/', '', $host);
        // 分割域名
        $parts = explode('.', $host);
        // 判断是几段
        $count = count($parts);
        // 如果只有两段,比如 fox8.com、theamericawatch.com,就拼接 www.
        if ($count === 2) {
            return 'www.' . $host;
        }
        return $host;
    }
}