ProjectServer.php
9.9 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<?php
/**
* Created by PhpStorm.
* User: zhl
* Date: 2023/4/17
* Time: 15:16
*/
namespace App\Services;
use App\Models\CustomModule\CustomModule;
use App\Models\Project\Project;
use App\Models\RouteMap\RouteMap;
use App\Models\Template\BCustomTemplate;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
/**
* Class ProjectServer
* @package App\Services
*/
class ProjectServer
{
public static $main404Html = '<main>
<section data-section="section" data-screen="screen-large" class="section-404-wrap-block section-block-error404"
id="sectionIdyxqu938">
<div class="layout" data-unable="demo01-error404">
<img src="https://ecdn6.globalso.com/upload/m/image_other/2023-10/6528a87e594db30162.png" />
</div>
<p style="text-align: center">SORRY. THE PAGE HAS EITHER MOVED OR CANNOT BE FOUND.</p>
<style>
.section-block-error404 .layout {
height: 700px;
display: flex;
align-items: center;
justify-content: center;
}
.section-block-error404 img {
width: 400px;
}
@media only screen and (max-width:500) {
.section-block-error404 img {
max-width: 100%;
}
}
</style>
<script>
</script>
</section>
</main>';
/**
* @param $project_id
* @return Project|false
*/
public static function useProject($project_id)
{
$project = Project::where(['id' => $project_id])->first();
if (empty($project)){
return false;
}
if(!$project->mysqlConfig){
return false;
}
// 设置 database.connections.custom_mysql 配置
config(['database.connections.custom_mysql.host' => $project->mysqlConfig->host]);
config(['database.connections.custom_mysql.port' => $project->mysqlConfig->port]);
if(env('SERVER_ID') == 7){
config(['database.connections.custom_mysql.host' => env('DB_HOST')]);
config(['database.connections.custom_mysql.port' => env('DB_PORT')]);
}
config(['database.connections.custom_mysql.database' => $project->databaseName()]);
config(['database.connections.custom_mysql.username' => $project->mysqlConfig->user]);
config(['database.connections.custom_mysql.password' => $project->mysqlConfig->password]);
//清除现有的数据库连接配置
DB::purge('custom_mysql');
//重连
DB::connection('custom_mysql')->reconnect();
// 设置 redis 配置
return $project;
}
/**
* 创建数据库
* @param $project
* @return bool
* @author zbj
* @date 2023/4/23
*/
public static function createDatabase($project)
{
$conn = new \mysqli(
$project->mysqlConfig->host,
$project->mysqlConfig->user,
$project->mysqlConfig->password,
'',
$project->mysqlConfig->port
);
$conn->query("CREATE DATABASE IF NOT EXISTS {$project->databaseName()}");
return true;
}
/**
* @param $project
* @return bool
* @author zbj
* @date 2023/4/23
*/
public static function initTable()
{
$database_name = DB::connection('custom_tmp_mysql')->getDatabaseName();
$table = Schema::connection('custom_tmp_mysql')->getAllTables();
$table = array_column($table, 'Tables_in_' . $database_name);
foreach ($table as $v) {
$has_table = Schema::connection('custom_mysql')->hasTable($v);
if ($has_table) {
continue;
}
$sql = DB::connection('custom_tmp_mysql')->select("SHOW CREATE TABLE {$v}");
DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
}
return true;
}
/**
* @remark :执行初始数据
* @name :saveInitParam
* @author :lyh
* @method :post
* @time :2023/9/19 14:45
*/
public static function saveInitParam($project_id){
self::initGroup($project_id);
//初始化单页
self::init404Page($project_id);
//初始化模块数据
self::initModule($project_id);
//初始化search页面
// self::initSearchPage($project_id);
DB::disconnect('custom_mysql');
return true;
}
/**
* @remark :初始化模块
* @name :initModule
* @author :lyh
* @method :post
* @time :2023/12/29 9:34
*/
public static function initModule($project_id){
$info = DB::connection('custom_mysql')->table('gl_custom_module')->first();
if(empty($info)){
$data = [
'name'=>'视频模块',
'project_id'=>$project_id,
'route'=>'video',
'created_at' => date('Y-m-d H:i:s'),
'updated_at' => date('Y-m-d H:i:s')
];
DB::connection('custom_mysql')->table('gl_custom_module')->insert($data);
}
return true;
}
/**
* @remark :菜单
* @name :initGroup
* @author :lyh
* @method :post
* @time :2023/12/29 9:30
*/
public static function initGroup($project_id){
$time = date('Y-m-d H:i:s');
$info = DB::connection('custom_mysql')->table('gl_web_nav')->first();
if(empty($info)) {
$data = [
['project_id' => $project_id, 'name' => 'Home', 'url' => 'Home', 'location' => 'header', 'group_id' => 1, 'created_at' => $time, 'updated_at' => $time],
['project_id' => $project_id, 'name' => 'Products', 'url' => 'Products', 'location' => 'header', 'group_id' => 1, 'created_at' => $time, 'updated_at' => $time],
['project_id' => $project_id, 'name' => 'News', 'url' => 'News', 'location' => 'header', 'group_id' => 1, 'created_at' => $time, 'updated_at' => $time],
['project_id' => $project_id, 'name' => 'ABOUT US', 'url' => 'about-us', 'location' => 'footer', 'group_id' => 2, 'created_at' => $time, 'updated_at' => $time],
['project_id' => $project_id, 'name' => 'Contact Us', 'url' => 'contact-us', 'location' => 'footer', 'group_id' => 2, 'created_at' => $time, 'updated_at' => $time],
['project_id' => $project_id, 'name' => 'FAQ', 'url' => 'faq', 'location' => 'footer', 'group_id' => 2, 'created_at' => $time, 'updated_at' => $time],
];
DB::connection('custom_mysql')->table('gl_web_nav')->insert($data);
}
//菜单组
$info = DB::connection('custom_mysql')->table('gl_web_nav_group')->first();
if(empty($info)) {
$data = [
['id' => 1, 'project_id' => $project_id, 'name' => '全局顶部菜单', 'created_at' => $time, 'updated_at' => $time],
['id' => 2, 'project_id' => $project_id, 'name' => '底部菜单', 'created_at' => $time, 'updated_at' => $time],
];
DB::connection('custom_mysql')->table('gl_web_nav_group')->insert($data);
}
return true;
}
/**
* @remark :初始化404页面
* @name :init404Page
* @author :lyh
* @method :post
* @time :2023/12/29 9:32
*/
public static function init404Page($project_id){
$time = date('Y-m-d H:i:s');
$info = DB::connection('custom_mysql')->table('gl_web_custom_template')->first();
if(empty($info)) {
$data = [
'project_id' => $project_id,
'name' => BCustomTemplate::NOT_FOUND_PAGE_URL,
'status' => 1,
'url' => BCustomTemplate::NOT_FOUND_PAGE_URL,
'html' => self::$main404Html,
'html_style' => '<style id="globalsojs-styles"></style>',
'title' => '404-Page not found',
'description' => 'Sorry. The page has either moved or cannot be found.',
'created_at' => $time, 'updated_at' => $time];
$id = DB::connection('custom_mysql')->table('gl_web_custom_template')->insertGetId($data);
//路由
$info = DB::connection('custom_mysql')->table('gl_route_map')->first();
if(empty($info)) {
$data = ['project_id' => $project_id, 'source' => RouteMap::SOURCE_PAGE, 'source_id' => $id, 'route' => BCustomTemplate::NOT_FOUND_PAGE_URL, 'created_at' => $time, 'updated_at' => $time];
DB::connection('custom_mysql')->table('gl_route_map')->insert($data);
}
}
}
/**
* @remark :初始化search页面
* @name :init404Page
* @author :lyh
* @method :post
* @time :2023/12/29 9:32
*/
public static function initSearchPage($project_id){
$time = date('Y-m-d H:i:s');
$info = DB::connection('custom_mysql')->table('gl_web_custom_template')->first();
if(empty($info)) {
$data = [
'project_id' => $project_id,
'name' => 'search',
'status' => 1,
'url' => 'search',
'html' => '',
'html_style' =>'search',
'title' => 'search',
'description' => 'Sorry. The page has either moved or cannot be found.',
'created_at' => $time, 'updated_at' => $time];
$id = DB::connection('custom_mysql')->table('gl_web_custom_template')->insertGetId($data);
//路由
$info = DB::connection('custom_mysql')->table('gl_route_map')->first();
if(empty($info)) {
$data = ['project_id' => $project_id, 'source' => RouteMap::SOURCE_PAGE, 'source_id' => $id, 'route' => 'search', 'created_at' => $time, 'updated_at' => $time];
DB::connection('custom_mysql')->table('gl_route_map')->insert($data);
}
}
}
}