作者 刘锟

白帽-二级域名绑定改版

@@ -45,11 +45,11 @@ class DomainSettingController extends BaseController @@ -45,11 +45,11 @@ class DomainSettingController extends BaseController
45 public function save(DomainSettingLogic $logic) 45 public function save(DomainSettingLogic $logic)
46 { 46 {
47 $this->request->validate([ 47 $this->request->validate([
  48 + 'type' => ['required'],
48 'domain' => ['required'], 49 'domain' => ['required'],
49 - 'record' => ['required'],  
50 ], [ 50 ], [
  51 + 'type.required' => '服务器类型不能为空',
51 'domain.required' => '主域名不能为空', 52 'domain.required' => '主域名不能为空',
52 - 'record.required' => '解析记录不能为空',  
53 ]); 53 ]);
54 $logic->saveDomain(); 54 $logic->saveDomain();
55 $this->response('success', Code::SUCCESS); 55 $this->response('success', Code::SUCCESS);
@@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
9 9
10 namespace App\Http\Logic\Bside\SeoSetting; 10 namespace App\Http\Logic\Bside\SeoSetting;
11 11
  12 +use App\Helper\Arr;
12 use App\Http\Logic\Bside\BaseLogic; 13 use App\Http\Logic\Bside\BaseLogic;
13 use App\Models\Devops\Servers; 14 use App\Models\Devops\Servers;
14 use App\Models\Devops\ServersIp; 15 use App\Models\Devops\ServersIp;
@@ -37,11 +38,11 @@ class DomainSettingLogic extends BaseLogic @@ -37,11 +38,11 @@ class DomainSettingLogic extends BaseLogic
37 public function infoDomain() 38 public function infoDomain()
38 { 39 {
39 $domain_info = $this->model->read(['project_id' => $this->user['project_id']], ['seo_type', 'seo_domain', 'seo_ftp']); 40 $domain_info = $this->model->read(['project_id' => $this->user['project_id']], ['seo_type', 'seo_domain', 'seo_ftp']);
40 - $domain = $domain_info['seo_domain'] ?: '';  
41 - $ftp = $domain_info['seo_ftp'] ? json_decode($domain['seo_ftp'], true) : ['url' => '', 'username' => '', 'password' => '', 'port' => ''];  
42 - $type = $domain_info['seo_type'] ?: 1;  
43 - $record = ''; 41 + $type = $domain_info['seo_type'] ?? WebSetting::SEO_TYPE_PUBLIC;
  42 + $domain = $domain_info['seo_domain'] ?? '';
  43 + $ftp = isset($domain_info['seo_ftp']) && $domain_info['seo_ftp'] ? Arr::s2a($domain_info['seo_ftp']) : ['url' => '', 'username' => '', 'password' => '', 'port' => ''];
44 44
  45 + $record = '';
45 if ($type == WebSetting::SEO_TYPE_PUBLIC) { 46 if ($type == WebSetting::SEO_TYPE_PUBLIC) {
46 //公共服务器 47 //公共服务器
47 if ($domain) { 48 if ($domain) {
@@ -65,7 +66,8 @@ class DomainSettingLogic extends BaseLogic @@ -65,7 +66,8 @@ class DomainSettingLogic extends BaseLogic
65 66
66 /** 67 /**
67 * @remark :保存设置 68 * @remark :保存设置
68 - * @name :saveDomain 69 + * @throws \App\Exceptions\AsideGlobalException
  70 + * @throws \App\Exceptions\BsideGlobalException
69 * @author :lyh 71 * @author :lyh
70 * @method :post 72 * @method :post
71 * @time :2025/3/20 16:12 73 * @time :2025/3/20 16:12
@@ -79,78 +81,102 @@ class DomainSettingLogic extends BaseLogic @@ -79,78 +81,102 @@ class DomainSettingLogic extends BaseLogic
79 $this->fail('主域名填写错误'); 81 $this->fail('主域名填写错误');
80 } 82 }
81 83
82 - //获取解析服务器详情  
83 - $server_ip_model = new ServersIp();  
84 - $record_info = $server_ip_model->read(['domain' => $this->param['record']], ['id', 'servers_id', 'ip', 'domain', 'total']);  
85 - if (!$record_info) {  
86 - $this->fail('解析记录不存在');  
87 - } 84 + if ($this->param['type'] = WebSetting::SEO_TYPE_PUBLIC) {
88 85
89 - //获取项目详情  
90 - $project_model = new Project();  
91 - $project_info = $project_model->read(['id' => $this->user['project_id']], ['company']);  
92 - if (!$project_info) {  
93 - $this->fail('获取项目数据失败');  
94 - } 86 + $record = $this->param['record'] ?? '';
  87 + if (empty($record)) {
  88 + $this->fail('解析记录不能为空');
  89 + }
  90 +
  91 + //获取解析服务器详情
  92 + $server_ip_model = new ServersIp();
  93 + $record_info = $server_ip_model->read(['domain' => $record], ['id', 'servers_id', 'ip', 'domain', 'total']);
  94 + if (!$record_info) {
  95 + $this->fail('解析记录不存在');
  96 + }
  97 +
  98 + //获取项目详情
  99 + $project_model = new Project();
  100 + $project_info = $project_model->read(['id' => $this->user['project_id']], ['company']);
  101 + if (!$project_info) {
  102 + $this->fail('获取项目数据失败');
  103 + }
  104 +
  105 + //构建blog二级域名
  106 + $domain_array = explode('.', $domain);
  107 + if (count($domain_array) == 1) {
  108 + $this->fail('请填写正确的主域名');
  109 + } elseif (count($domain_array) == 2) {
  110 + array_unshift($domain_array, 'blog');
  111 + } else {
  112 + $domain_array[0] = 'blog';
  113 + }
  114 + $blog_domain = implode('.', $domain_array);
  115 +
  116 + //判断blog二级域名是否已经解析
  117 + if (!check_domain_record($blog_domain, $record_info)) {
  118 + $this->fail($blog_domain . '还未解析cname至' . $record);
  119 + }
  120 +
  121 + DB::beginTransaction();
  122 + try {
  123 + //保存一条主域名记录
  124 + $setting_info = $this->model->read(['project_id' => $this->user['project_id']]);
  125 + if ($setting_info === false) {
  126 + $this->model->add(['seo_type' => WebSetting::SEO_TYPE_PUBLIC, 'seo_domain' => $domain, 'project_id' => $this->user['project_id']]);
  127 + } else {
  128 + $this->model->edit(['seo_type' => WebSetting::SEO_TYPE_PUBLIC, 'seo_domain' => $domain], ['id' => $setting_info['id']]);
  129 + }
  130 +
  131 + //添加域名到域名管理
  132 + $domain_model = new DomainInfo();
  133 + $info = $domain_model->read(['domain' => $blog_domain]);
  134 + if ($info === false) {
  135 + $id = $domain_model->addReturnId(['domain' => $blog_domain, 'project_id' => $this->user['project_id'], 'belong_to' => 2, 'status' => 1, 'remark' => $project_info['company']]);
  136 +
  137 + //保存优化设置
  138 + $optimize_model = new DeployOptimize();
  139 + $optimize_model->edit(['domain' => $id], ['project_id' => $this->user['project_id']]);
  140 +
  141 + //保存项目ip
  142 + $project_model->edit(['serve_id' => $record_info['id']], ['id' => $this->user['project_id']]);
  143 +
  144 + //变更ip使用数量
  145 + $server_ip_model->edit(['total' => $record_info['total'] + 1], ['id' => $record_info['id']]);
  146 + $server_model = new Servers();
  147 + $server_model->edit(['being_number' => $record_info['total'] + 1], ['id' => $record_info['servers_id']]);
  148 +
  149 + //创建建站任务
  150 + $domain_create_model = new DomainCreateTask();
  151 + $domain_create_model->add([
  152 + 'server_id' => $record_info['servers_id'],
  153 + 'project_id' => $this->user['project_id'],
  154 + 'domain_id' => $id,
  155 + 'type' => DomainCreateTask::TYPE_BLOG
  156 + ]);
  157 + }
  158 +
  159 + DB::commit();
  160 + } catch (\Exception $e) {
  161 + DB::rollBack();
  162 + $this->fail('保存失败,请联系管理员');
  163 + }
95 164
96 - //构建blog二级域名  
97 - $domain_array = explode('.', $domain);  
98 - if (count($domain_array) == 1) {  
99 - $this->fail('请填写正确的主域名');  
100 - } elseif (count($domain_array) == 2) {  
101 - array_unshift($domain_array, 'blog');  
102 } else { 165 } else {
103 - $domain_array[0] = 'blog';  
104 - }  
105 - $blog_domain = implode('.', $domain_array);  
106 166
107 - //判断blog二级域名是否已经解析  
108 - if (!check_domain_record($blog_domain, $record_info)) {  
109 - $this->fail($blog_domain . '还未解析cname至' . $this->param['record']);  
110 - } 167 + $ftp = $this->param['ftp'] ?? [];
  168 + if (empty($ftp) || empty($ftp['url']) || empty($ftp['username']) || empty($ftp['password']) || empty($ftp['port'])) {
  169 + $this->fail('ftp信息填写未完整');
  170 + }
111 171
112 - DB::beginTransaction();  
113 - try {  
114 //保存一条主域名记录 172 //保存一条主域名记录
115 $setting_info = $this->model->read(['project_id' => $this->user['project_id']]); 173 $setting_info = $this->model->read(['project_id' => $this->user['project_id']]);
116 if ($setting_info === false) { 174 if ($setting_info === false) {
117 - $this->model->add(['seo_domain' => $domain, 'project_id' => $this->user['project_id']]); 175 + $this->model->add(['seo_type' => WebSetting::SEO_TYPE_OWN, 'seo_domain' => $domain, 'seo_ftp' => Arr::a2s($ftp), 'project_id' => $this->user['project_id']]);
118 } else { 176 } else {
119 - $this->model->edit(['seo_domain' => $domain], ['id' => $setting_info['id']]);  
120 - }  
121 -  
122 - //添加域名到域名管理  
123 - $domain_model = new DomainInfo();  
124 - $info = $domain_model->read(['domain' => $blog_domain]);  
125 - if ($info === false) {  
126 - $id = $domain_model->addReturnId(['domain' => $blog_domain, 'project_id' => $this->user['project_id'], 'belong_to' => 2, 'status' => 1, 'remark' => $project_info['company']]);  
127 -  
128 - //保存优化设置  
129 - $optimize_model = new DeployOptimize();  
130 - $optimize_model->edit(['domain' => $id], ['project_id' => $this->user['project_id']]);  
131 -  
132 - //保存项目ip  
133 - $project_model->edit(['serve_id' => $record_info['id']], ['id' => $this->user['project_id']]);  
134 -  
135 - //变更ip使用数量  
136 - $server_ip_model->edit(['total' => $record_info['total'] + 1], ['id' => $record_info['id']]);  
137 - $server_model = new Servers();  
138 - $server_model->edit(['being_number' => $record_info['total'] + 1], ['id' => $record_info['servers_id']]);  
139 -  
140 - //创建建站任务  
141 - $domain_create_model = new DomainCreateTask();  
142 - $domain_create_model->add([  
143 - 'server_id' => $record_info['servers_id'],  
144 - 'project_id' => $this->user['project_id'],  
145 - 'domain_id' => $id,  
146 - 'type' => DomainCreateTask::TYPE_BLOG  
147 - ]); 177 + $this->model->edit(['seo_type' => WebSetting::SEO_TYPE_OWN, 'seo_domain' => $domain, 'seo_ftp' => Arr::a2s($ftp)], ['id' => $setting_info['id']]);
148 } 178 }
149 -  
150 - DB::commit();  
151 - } catch (\Exception $e) {  
152 - DB::rollBack();  
153 - $this->fail('保存失败,请联系管理员');  
154 } 179 }
  180 +
155 } 181 }
156 } 182 }