DomainInfoLogic.php
6.1 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
<?php
namespace App\Http\Logic\Aside\Domain;
use App\Http\Logic\Aside\BaseLogic;
use App\Models\Devops\ServerConfig;
use App\Models\Domain\DomainInfo;
use App\Models\Project\DeployOptimize;
use App\Models\Project\Project;
use App\Utils\HttpUtils;
use GuzzleHttp\Client;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Support\Carbon;
class DomainInfoLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->model = new DomainInfo();
$this->param = $this->requestAll;
}
/**
* @remark :保存域名
* @name :createDomain
* @author :lyh
* @method :post
* @time :2023/8/1 14:52
*/
public function saveDomain()
{
$domain = parse_url($this->param['domain'], PHP_URL_HOST);
if(!empty($domain)){
$this->param['domain'] = trim($domain['host']);
}
//验证域名
$this->verifyDomain($this->param['domain'],isset($this->param['id']) ?? '');
if(isset($this->param['id']) && !empty($this->param['id'])){
$rs = $this->model->edit($this->param,['id'=>$this->param['id']]);
}else{
$rs = $this->model->add($this->param);
}
if($rs === false){
$this->fail('error');
}
return $this->success();
}
/**
* @remark :验证域名是否存在
* @name :verifyDomain
* @author :lyh
* @method :post
* @time :2023/8/1 14:59
*/
public function verifyDomain($domain,$id = ''){
if(!empty($id)){
$info = $this->model->read(['domain'=>$domain,'id'=>['!=',$id]]);
if ($info !== false) {
$this->fail('当前域名已存在');
}
}else{
$info = $this->model->read(['domain'=>$domain]);
if ($info !== false) {
$this->fail('当前域名已存在');
}
}
return $this->success();
}
/**
* @remark :修改当前域名状态
* @name :editStatus
* @author :lyh
* @method :post
* @time :2023/8/1 15:43
*/
public function editDomainStatus(){
//查看当前域名是否有项目在使用
if($this->param['status'] != $this->model::STATUS_ONE){
$info = $this->model->read(['id'=>$this->param['id']]);
if($info === false){
$this->fail('当前域名有项目正在使用中');
}
}
$rs = $this->model->edit(['status'=>$this->param['status']],['id'=>$this->param['id']]);
if($rs === false){
$this->fail('error');
}
return $this->success();
}
/**
* @remark :获取所有项目列表
* @name :getProjectList
* @author :lyh
* @method :post
* @time :2023/8/1 16:10
*/
public function getProjectList($map){
$projectModel = new Project();
$lists = $projectModel->list($map,'id',['id','title']);
return $this->success($lists);
}
/**
* @remark :删除域名
* @name :delDomain
* @author :lyh
* @method :post
* @time :2023/8/1 15:41
*/
public function delDomain(){
$ids = $this->param['id'];
foreach ($ids as $k => $v){
$info = $this->model->read(['id'=>$v]);
$deployOptimizeModel = new DeployOptimize();
$domainInfo = $deployOptimizeModel->read(['domain'=>$info['domain']]);
if($domainInfo !== false){
$this->fail('当前域名正在使用中');
}
}
$this->param['id'] = ['in',$ids];
$rs = $this->model->del($this->param);
if($rs === false){
$this->fail('error');
}
return $this->success();
}
public function infoDomain(){
$info = $this->model->read(['id'=>$this->param['id']]);
if($info === false){
$this->fail('当前数据不存在或者已被删除');
}
if(!empty($info['project_id'])){
$info['company'] = (new Project())->read(['id'=>$info['project_id']],['title'])['title'];
}
return $this->success($info);
}
/**
* 编辑网站证书
* @return array
* @throws \App\Exceptions\AsideGlobalException
* @throws \App\Exceptions\BsideGlobalException
* @author Akun
* @date 2023/10/17 11:52
*/
public function setDomainSsl()
{
$info = $this->model->read(['id'=>$this->param['id']]);
if($info === false){
$this->fail('当前数据不存在或者已被删除');
}
$project_model = new Project();
$project_info = $project_model->read(['id'=>$info['project_id']],'serve_id');
if($project_info === false){
$this->fail('获取项目数据失败');
}
$server_model = new ServerConfig();
$server_info = $server_model->read(['id'=>$project_info['serve_id']],'init_domain');
if($server_info === false){
$this->fail('获取服务器数据失败');
}
if($this->param['type'] == 2){
if(empty($this->param['key'])){
$this->fail('证书KEY值不能为空');
}
if(empty($this->param['cert'])){
$this->fail('证书cert值不能为空');
}
$api_url = 'http://'.$server_info['init_domain'].'/api/setSsl';
$api_param = [
'domain' => $info['domain'],
'private_key' => $this->param['key'],
'cert' => $this->param['cert'],
];
}else{
$api_url = 'http://'.$server_info['init_domain'].'/api/applySsl';
$api_param = ['domain' => $info['domain']];
}
try {
$rs = HttpUtils::get($api_url, $api_param);
$rs = json_decode($rs, true);
if(isset($rs['status']) && $rs['status'] == 200){
return $this->success();
}else{
$this->fail($rs['message']??'');
}
} catch (\Exception | GuzzleException $e) {
errorLog('创建站点', $api_param, $e);
$this->fail('编辑证书失败');
}
}
}