DomainSettingLogic.php
9.2 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
<?php
/**
* @remark :
* @name :DomainSettingLogic.php
* @author :lyh
* @method :post
* @time :2025/3/20 16:01
*/
namespace App\Http\Logic\Bside\SeoSetting;
use App\Helper\Arr;
use App\Http\Logic\Bside\BaseLogic;
use App\Models\Devops\ServerConfig;
use App\Models\Devops\Servers;
use App\Models\Devops\ServersIp;
use App\Models\Domain\DomainCreateTask;
use App\Models\Domain\DomainInfo;
use App\Models\Project\DeployOptimize;
use App\Models\Project\Project;
use App\Models\WebSetting\WebSetting;
use Illuminate\Support\Facades\DB;
class DomainSettingLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
$this->model = new WebSetting();
}
/**
* 获取域名设置及目标解析记录
* @return array
* @author Akun
* @date 2025/03/21 10:09
*/
public function infoDomain()
{
$domain_info = $this->model->read(['project_id' => $this->user['project_id']], ['seo_type', 'seo_domain', 'seo_ftp']);
$type = $domain_info['seo_type'] ?? WebSetting::SEO_TYPE_PUBLIC;
$domain = $domain_info['seo_domain'] ?? '';
$ftp = isset($domain_info['seo_ftp']) && $domain_info['seo_ftp'] ? Arr::s2a($domain_info['seo_ftp']) : ['url' => '', 'username' => '', 'password' => '', 'port' => ''];
$record = '';
if ($type == WebSetting::SEO_TYPE_PUBLIC) {
//公共服务器
if ($domain) {
//已经绑定了主域名,获取域名解析
$project_model = new Project();
$project_info = $project_model->read(['id' => $this->user['project_id']], ['serve_id']);
if ($project_info) {
$server_model = new ServersIp();
$record_info = $server_model->read(['id' => $project_info['serve_id']], ['domain']);
$record = $record_info ? $record_info['domain'] : '';
}
} else {
//未绑定主域名,分配域名解析
$record = Servers::where('init_domain', 'like', 'seo%')->whereRaw('being_number < ip_total')->orderBy('being_number', 'desc')->orderBy('id', 'asc')->value('init_domain') ?? '';
}
}
return ['type' => $type, 'domain' => $domain, 'record' => $record, 'ftp' => $ftp];
}
/**
* @remark :保存设置
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
* @author :lyh
* @method :post
* @time :2025/3/20 16:12
*/
public function saveDomain()
{
//获取主域名
$domain_parse = parse_url(trim($this->param['domain']));
$domain = $domain_parse['host'] ?? $domain_parse['path'];
if (!$domain) {
$this->fail('主域名填写错误');
}
//获取项目详情
$project_model = new Project();
$project_info = $project_model->read(['id' => $this->user['project_id']], ['company']);
if (!$project_info) {
$this->fail('获取项目数据失败');
}
$server_ip_model = new ServersIp();
$domain_model = new DomainInfo();
$server_model = new Servers();
$optimize_model = new DeployOptimize();
if ($this->param['type'] == WebSetting::SEO_TYPE_PUBLIC) {
//公共服务器
$record = $this->param['record'] ?? '';
if (empty($record)) {
$this->fail('解析记录不能为空');
}
//获取解析服务器详情
$record_info = $server_ip_model->read(['domain' => $record], ['id', 'servers_id', 'ip', 'domain', 'total']);
if (!$record_info) {
$this->fail('解析记录不存在');
}
//构建blog二级域名
$domain_array = explode('.', $domain);
if (count($domain_array) == 1) {
$this->fail('请填写正确的主域名');
} elseif (count($domain_array) == 2) {
array_unshift($domain_array, 'blog');
} else {
$domain_array[0] = 'blog';
}
$blog_domain = implode('.', $domain_array);
//判断blog二级域名是否已经解析
if (!check_domain_record($blog_domain, $record_info)) {
$this->fail($blog_domain . '还未解析cname至' . $record);
}
DB::beginTransaction();
try {
//保存一条主域名记录
$setting_info = $this->model->read(['project_id' => $this->user['project_id']]);
if ($setting_info === false) {
$this->model->add(['seo_type' => WebSetting::SEO_TYPE_PUBLIC, 'seo_domain' => $domain, 'project_id' => $this->user['project_id']]);
} else {
$this->model->edit(['seo_type' => WebSetting::SEO_TYPE_PUBLIC, 'seo_domain' => $domain], ['id' => $setting_info['id']]);
}
//添加域名到域名管理
$info = $domain_model->read(['domain' => $blog_domain]);
if ($info === false) {
$id = $domain_model->addReturnId(['domain' => $blog_domain, 'project_id' => $this->user['project_id'], 'belong_to' => 2, 'status' => 1, 'remark' => $project_info['company']]);
//保存优化设置
$optimize_model->edit(['domain' => $id], ['project_id' => $this->user['project_id']]);
//保存项目ip
$project_model->edit(['serve_id' => $record_info['id']], ['id' => $this->user['project_id']]);
//变更ip使用数量
$server_ip_model->edit(['total' => $record_info['total'] + 1], ['id' => $record_info['id']]);
$server_model->edit(['being_number' => $record_info['total'] + 1], ['id' => $record_info['servers_id']]);
//创建建站任务
$domain_create_model = new DomainCreateTask();
$domain_create_model->add([
'server_id' => $record_info['servers_id'],
'project_id' => $this->user['project_id'],
'domain_id' => $id,
'type' => DomainCreateTask::TYPE_BLOG
]);
}
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
$this->fail('保存失败,请联系管理员');
}
} else {
//个人服务器
$ftp = $this->param['ftp'] ?? [];
if (empty($ftp) || empty($ftp['url']) || empty($ftp['username']) || empty($ftp['password']) || empty($ftp['port'])) {
$this->fail('ftp信息填写未完整');
}
DB::beginTransaction();
try {
//获取自建站服务器详情
$record_info = $server_ip_model->read(['servers_id' => ServerConfig::SELF_SITE_ID, 'ip' => $ftp['url']], ['id', 'servers_id', 'total']);
if (!$record_info) {
$record_id = $server_ip_model->addReturnId([
'servers_id' => ServerConfig::SELF_SITE_ID,
'ip' => $ftp['url'],
'domain' => $domain,
'remark' => $project_info['company']
]);
$record_info = $server_ip_model->read(['id' => $record_id], ['id', 'servers_id', 'total']);
}
//保存一条主域名记录
$setting_info = $this->model->read(['project_id' => $this->user['project_id']]);
if ($setting_info === false) {
$this->model->add(['seo_type' => WebSetting::SEO_TYPE_OWN, 'seo_domain' => $domain, 'seo_ftp' => Arr::a2s($ftp), 'project_id' => $this->user['project_id']]);
} else {
$this->model->edit(['seo_type' => WebSetting::SEO_TYPE_OWN, 'seo_domain' => $domain, 'seo_ftp' => Arr::a2s($ftp)], ['id' => $setting_info['id']]);
}
//添加域名到域名管理
$info = $domain_model->read(['domain' => $domain]);
if ($info === false) {
$id = $domain_model->addReturnId(['domain' => $domain, 'project_id' => $this->user['project_id'], 'belong_to' => 2, 'status' => 1, 'remark' => $project_info['company']]);
//保存优化设置
$optimize_model->edit(['domain' => $id], ['project_id' => $this->user['project_id']]);
//保存项目ip
$project_model->edit(['serve_id' => $record_info['id']], ['id' => $this->user['project_id']]);
//变更ip使用数量
$server_ip_model->edit(['total' => $record_info['total'] + 1], ['id' => $record_info['id']]);
$server_model->edit(['being_number' => $record_info['total'] + 1], ['id' => $record_info['servers_id']]);
}
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
$this->fail('保存失败,请联系管理员');
}
}
return $this->success();
}
}