ServerConfigLogic.php
5.0 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
<?php
namespace App\Http\Logic\Aside\Devops;
use App\Http\Logic\Aside\BaseLogic;
use App\Http\Logic\Aside\Project\ProjectLogic;
use App\Models\Devops\DevopsTask;
use App\Models\Project\Project;
use App\Models\Devops\ServerConfig;
use App\Services\ProjectServer;
use App\Utils\EncryptUtils;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use Symfony\Component\Process\Process;
/**
* Class ServerConfigLogic
* @package App\Http\Logic\Aside
* @author zbj
* @date 2023/4/23
*/
class ServerConfigLogic extends BaseLogic
{
public function __construct()
{
parent::__construct();
$this->param = $this->requestAll;
$this->model = new ServerConfig();
}
/**
* @remark :获取域名列表
* @name :serviceConfigList
* @author :lyh
* @method :post
* @time :2023/8/2 14:51
*/
public function serviceConfigList($map,$page,$row,$order = 'id',$filed = ['*']){
$lists = $this->model->lists($map,$page,$row,$order,$filed);
return $this->success($lists);
}
/**
* @remark :获取当前数据详情
* @name :getServiceConfig
* @author :lyh
* @method :post
* @time :2023/8/2 17:53
*/
public function getServiceConfig(){
$info = $this->model->read(['id'=>$this->param['id']]);
if($info === false){
$this->fail('当前数据不存在或者被删除');
}
return $this->success($info);
}
/**
* @remark :保存
* @name :save
* @author :lyh
* @method :post
* @time :2023/8/2 17:55
*/
public function serviceConfigSave()
{
DB::beginTransaction();
try {
$encrypt = new EncryptUtils();
$this->param['user'] = $encrypt->lock_url($this->param['user']);
$this->param['password'] = $encrypt->lock_url($this->param['password']);
$this->param['port'] = $encrypt->lock_url($this->param['port']);
//保存配置
if(isset($this->param['id']) && !empty($this->param['id'])){
$this->model->edit($this->param,['id'=>$this->param['id']]);
}else{
$this->model->add($this->param);
}
//初始化数据库
DB::commit();
} catch (\Exception $e) {
DB::rollBack();
$this->fail('保存失败');
}
return $this->success();
}
/**
* @remark :验证数据库
* @name :mysqlConfig
* @author :lyh
* @method :post
* @time :2023/8/28 11:57
*/
public function mysqlConfig($data){
// 构建数据库配置数组
$dbConfig = [
'driver' => 'mysql', // 数据库类型
'host' => $data['host'], // 数据库主机
'database' => $data['host'], // 数据库名
'username' => $data['username'], // 数据库用户名
'password' => $data['password'], // 数据库密码
];
// 临时设置数据库配置
config(['database.connections.temp' => $dbConfig]);
try {
// 使用数据库配置信息尝试建立数据库连接
DB::connection()->getPdo();
// 如果连接成功,表示账号、密码和主机信息正确
echo "Database connection successful!";
} catch (\Exception $e) {
// 连接失败,输出错误信息
echo "Database connection failed: " . $e->getMessage();
}
}
/**
* @remark :删除记录
* @name :delServiceConfig
* @author :lyh
* @method :post
* @time :2023/8/2 15:43
*/
public function delServiceConfig(){
$info = $this->model->read(['id'=>$this->requestAll['id']]);
if($info === false){
$this->fail('当前数据不存在或已被删除');
}
$rs = $this->model->del(['id'=>$this->requestAll['id']]);
if($rs === false){
$this->fail('删除失败');
}
return $this->success();
}
/**
* 更新指定项目 数据库
* @param $param
* @return array
* @author zbj
* @date 2023/4/24
*/
public function updateTable($param){
$project = ProjectServer::useProject($param['project_id']);
if(!$project){
$this->fail('项目不存在或数据库未配置');
}
DB::connection('custom_mysql')->statement($param['sql']);
return $this->success();
}
/**
* 更新本地 数据库
* @param $param
* @return array
* @author zbj
* @date 2023/4/24
*/
public function updateLocalTable($param){
DB::statement($param['sql']);
return $this->success();
}
/**
* 更新所有项目 数据库
* @param $param
* @return array
* @author zbj
* @date 2023/4/24
*/
public function updateAllTable($param){
DevopsTask::addTask($param['sql']);
//执行命令行
Artisan::call('php artisan devops_task');
return $this->success();
}
}