LinkDataLogic.php
3.4 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
<?php
/**
* @remark :
* @name :LinkDataLogic.php
* @author :lyh
* @method :post
* @time :2025/3/14 17:20
*/
namespace App\Http\Logic\Bside\SeoSetting;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Geo\DomainDa;
use App\Models\SeoSetting\LinkData;
use App\Services\Geo\GeoService;
use Illuminate\Support\Carbon;
use Nette\Utils\DateTime;
/**
* @remark :获取外链数据
* @name :LinkDataLogic
* @author :lyh
* @method :post
* @time :2025/3/14 17:21
*/
class LinkDataLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
$this->model = new LinkData();
}
/**
* @remark :保存数据
* @name :batchSave
* @author :lyh
* @method :post
* @time :2025/3/14 17:22
* @param :url->外链;da_values->da值
*/
public function batchSave(){
$data = [];
foreach ($this->param['data'] as $v){
$data[] = [
'url'=>$v['url'],
'da_values'=>$v['da_values'],
'send_time'=>$v['send_time'] ?? date('Y-m-d H:i:s')
];
}
if(!empty($data)){
$this->model->insertAll($data);
}
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){
//判断时间是否大于60天
$start = Carbon::parse(date('Y-m-d', strtotime($daInfo['updated_at'])));
$end = Carbon::parse(date('Y-m-d'));
$diff = $start->diffInDays($end);
if($diff <= 60){
$info['da'] = $daInfo['da'];
$this->model->edit(['da'=>$daInfo['da'],'time'=>date('Y-m-d')], ['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'],'time'=>date('Y-m-d')], ['id'=>$info['id']]);
return $this->success($info);
}
/**
* @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;
}
}