作者 刘锟

Merge remote-tracking branch 'origin/master' into akun

@@ -53,7 +53,6 @@ class InitProject extends Command @@ -53,7 +53,6 @@ class InitProject extends Command
53 echo 'start:' . $item['id'] . PHP_EOL; 53 echo 'start:' . $item['id'] . PHP_EOL;
54 try { 54 try {
55 $project = Project::find($item['data']['project_id']); 55 $project = Project::find($item['data']['project_id']);
56 -  
57 $project_logic = new ProjectLogic(); 56 $project_logic = new ProjectLogic();
58 //初始化数据库 57 //初始化数据库
59 if(!empty($project['mysql_id'])){ 58 if(!empty($project['mysql_id'])){
@@ -5,6 +5,8 @@ namespace App\Http\Logic\Aside\Project; @@ -5,6 +5,8 @@ namespace App\Http\Logic\Aside\Project;
5 use App\Models\Com\NoticeLog; 5 use App\Models\Com\NoticeLog;
6 use App\Models\Devops\ServerConfig; 6 use App\Models\Devops\ServerConfig;
7 use App\Models\Project\ProjectRenew; 7 use App\Models\Project\ProjectRenew;
  8 +use App\Models\Template\BSettingTemplate;
  9 +use App\Models\Template\Setting;
8 use App\Models\User\ProjectMenu; 10 use App\Models\User\ProjectMenu;
9 use App\Models\User\ProjectRole; 11 use App\Models\User\ProjectRole;
10 use App\Services\SyncService; 12 use App\Services\SyncService;
@@ -32,6 +34,7 @@ use Hashids\Hashids; @@ -32,6 +34,7 @@ use Hashids\Hashids;
32 use App\Models\User\User as UserModel; 34 use App\Models\User\User as UserModel;
33 use Illuminate\Support\Facades\DB; 35 use Illuminate\Support\Facades\DB;
34 use Illuminate\Support\Facades\Log; 36 use Illuminate\Support\Facades\Log;
  37 +use Illuminate\Support\Facades\Schema;
35 38
36 /** 39 /**
37 * Class ProjectLogic 40 * Class ProjectLogic
@@ -525,17 +528,104 @@ class ProjectLogic extends BaseLogic @@ -525,17 +528,104 @@ class ProjectLogic extends BaseLogic
525 //复制初始项目 528 //复制初始项目
526 $data = $this->model::where('id', $this->param['project_id'])->first(); 529 $data = $this->model::where('id', $this->param['project_id'])->first();
527 $data = $data->getAttributes(); 530 $data = $data->getAttributes();
  531 + $data['type'] = 0;
  532 + $data['title'] = $data['title'].'-copy';
528 unset($data['id']); 533 unset($data['id']);
529 $project_id = $this->model->insertGetId($data); 534 $project_id = $this->model->insertGetId($data);
530 //复制部署表 535 //复制部署表
531 $buildModel = new DeployBuild(); 536 $buildModel = new DeployBuild();
532 - $buildData = $buildModel::where('id', $this->param['project_id'])->first();  
533 - $buildData = $buildData->getAttributes();  
534 - $buildData['project_id'] = $project_id;  
535 - unset($buildData['id']);  
536 - $buildModel->insert($buildData); 537 + $buildData = $buildModel::where('project_id', $this->param['project_id'])->first();
  538 + if(!empty($buildData)){
  539 + $buildData = $buildData->getAttributes();
  540 + $buildData['project_id'] = $project_id;
  541 + $hashids = new Hashids('test_domain', 5, 'abcdefghjkmnpqrstuvwxyz1234567890');
  542 + $code = $hashids->encode($project_id);
  543 + $buildData['test_domain'] = 'https://v6-' . $code . '.globalso.site/';
  544 + unset($buildData['id']);
  545 + $buildModel->insert($buildData);
  546 + }
537 //复制优化表 547 //复制优化表
538 $optimizeModel = new DeployOptimize(); 548 $optimizeModel = new DeployOptimize();
  549 + $optimizeData = $optimizeModel::where('project_id', $this->param['project_id'])->first();
  550 + if(!empty($optimizeData)){
  551 + $optimizeData = $optimizeData->getAttributes();
  552 + unset($optimizeData['id'],$optimizeData['domain']);
  553 + $optimizeData['project_id'] = $project_id;
  554 + $optimizeModel->insert($optimizeData);
  555 + }
  556 + //复制付费表
  557 + $paymentModel = new Payment();
  558 + $paymentData = $paymentModel::where('project_id', $this->param['project_id'])->first();
  559 + if(!empty($paymentData)){
  560 + $paymentData = $paymentData->getAttributes();
  561 + unset($paymentData['id']);
  562 + $paymentData['project_id'] = $project_id;
  563 + $paymentModel->insert($paymentData);
  564 + }
  565 + //复制用户
  566 + $userModel = new UserModel();
  567 + $userData = $userModel::where('project_id', $this->param['project_id'])->where('role_id',0)->first();
  568 + if(!empty($userData)){
  569 + $userData = $userData->getAttributes();
  570 + unset($userData['id']);
  571 + $userData['project_id'] = $project_id;
  572 + $userModel->insert($userData);
  573 + }
  574 + //复制设置的模版
  575 + $settingTemplateModel = new Setting();
  576 + $settingData = $settingTemplateModel::where('project_id', $this->param['project_id'])->first();
  577 + if(!empty($settingData)){
  578 + $settingData = $settingData->getAttributes();
  579 + unset($settingData['id']);
  580 + $settingData['project_id'] = $project_id;
  581 + $settingTemplateModel->insert($settingData);
  582 + }
  583 + $this->copyMysql($this->param['project_id'],$project_id);
539 return $this->success($data); 584 return $this->success($data);
540 } 585 }
  586 +
  587 + //复制数据库
  588 + public function copyMysql($project_id,$new_project_id){
  589 + //切换数据库配置
  590 + $project = ProjectServer::useProject($new_project_id);
  591 + //创建数据库
  592 + ProjectServer::createDatabase($project);
  593 + //创建表
  594 + $this->initTable($project_id,$new_project_id);
  595 + }
  596 +
  597 +
  598 + /**
  599 + * @param $project
  600 + * @return bool
  601 + * @author zbj
  602 + * @date 2023/4/23
  603 + */
  604 + public function initTable($project_id,$news_project_id)
  605 + {
  606 + config(['database.connections.custom_tmp_mysql_copy.database' => 'gl_data_'.$project_id]);
  607 + $database_name = DB::connection('custom_tmp_mysql_copy')->getDatabaseName();
  608 + $tables = Schema::connection('custom_tmp_mysql_copy')->getAllTables();
  609 + $tables = array_column($tables, 'Tables_in_' . $database_name);
  610 + foreach ($tables as $table) {
  611 + $has_table = Schema::connection('custom_mysql')->hasTable($table);
  612 + if (!$has_table) {
  613 + $sql = DB::connection('custom_tmp_mysql_copy')->select("SHOW CREATE TABLE {$table}");
  614 + DB::connection('custom_mysql')->statement(get_object_vars($sql[0])['Create Table']);
  615 + }
  616 + DB::connection('custom_mysql')->table($table)->truncate(); // 清空目标表数据
  617 + DB::connection('custom_mysql')->table($table)->insertUsing(
  618 + [], // 列名数组,留空表示插入所有列
  619 + function ($query) use ($table,$project_id) {
  620 + $name = 'gl_data_'.$project_id.'.'.$table;
  621 + $query->select('*')->from("{$name}");
  622 + }
  623 + );
  624 + if (Schema::connection('custom_mysql')->hasColumn($table, 'project_id')) {
  625 + DB::connection('custom_mysql')->table($table)->update(['project_id' => $news_project_id]);
  626 + }
  627 + }
  628 + return true;
  629 + }
  630 +
541 } 631 }
@@ -39,7 +39,6 @@ class BlogLogic extends BaseLogic @@ -39,7 +39,6 @@ class BlogLogic extends BaseLogic
39 $this->editNewsRoute($this->param['id'],$this->param['url']); 39 $this->editNewsRoute($this->param['id'],$this->param['url']);
40 $this->edit($this->param,['id'=>$this->param['id']]); 40 $this->edit($this->param,['id'=>$this->param['id']]);
41 }else{ 41 }else{
42 - $this->param['url'] = $this->param['url'].'-'.RouteMap::SOURCE_BLOG;  
43 $id = $this->model->addReturnId($this->param); 42 $id = $this->model->addReturnId($this->param);
44 } 43 }
45 $route = RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $id, $this->user['project_id']); 44 $route = RouteMap::setRoute($this->param['url'], RouteMap::SOURCE_BLOG, $id, $this->user['project_id']);
@@ -68,7 +68,6 @@ class NewsLogic extends BaseLogic @@ -68,7 +68,6 @@ class NewsLogic extends BaseLogic
68 $id = $this->param['id']; 68 $id = $this->param['id'];
69 $this->edit($this->param, ['id' => $this->param['id']]); 69 $this->edit($this->param, ['id' => $this->param['id']]);
70 } else { 70 } else {
71 - $this->param['url'] = $this->param['url'].'-'.RouteMap::SOURCE_NEWS;  
72 $id = $this->model->addReturnId($this->param); 71 $id = $this->model->addReturnId($this->param);
73 } 72 }
74 //更新路由 73 //更新路由
@@ -60,7 +60,6 @@ class KeywordLogic extends BaseLogic @@ -60,7 +60,6 @@ class KeywordLogic extends BaseLogic
60 $this->param['project_id'] = $this->user['project_id']; 60 $this->param['project_id'] = $this->user['project_id'];
61 $this->param['created_at'] = date('Y-m-d H:i:s'); 61 $this->param['created_at'] = date('Y-m-d H:i:s');
62 $this->param['updated_at'] = $this->param['created_at']; 62 $this->param['updated_at'] = $this->param['created_at'];
63 - $this->param['route'] = $this->param['route'].'-'.RouteMap::SOURCE_PRODUCT;  
64 $id = $this->model->insertGetId($this->param); 63 $id = $this->model->insertGetId($this->param);
65 //路由映射 64 //路由映射
66 $route = RouteMap::setRoute($this->param['title'], RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']); 65 $route = RouteMap::setRoute($this->param['title'], RouteMap::SOURCE_PRODUCT_KEYWORD, $id, $this->user['project_id']);
@@ -83,6 +83,26 @@ return [ @@ -83,6 +83,26 @@ return [
83 ]) : [], 83 ]) : [],
84 ], 84 ],
85 85
  86 + 'custom_tmp_mysql_copy' => [
  87 + 'driver' => 'mysql',
  88 + 'url' => env('DATABASE_URL'),
  89 + 'host' => env('DB_HOST', '127.0.0.1'),
  90 + 'port' => env('DB_PORT', '3306'),
  91 + 'database' => env('DB_DATABASE_TMP', ''),
  92 + 'username' => env('DB_USERNAME', 'forge'),
  93 + 'password' => env('DB_PASSWORD', ''),
  94 + 'unix_socket' => env('DB_SOCKET', ''),
  95 + 'charset' => 'utf8mb4',
  96 + 'collation' => 'utf8mb4_unicode_ci',
  97 + 'prefix' => '',
  98 + 'prefix_indexes' => true,
  99 + 'strict' => true,
  100 + 'engine' => null,
  101 + 'options' => extension_loaded('pdo_mysql') ? array_filter([
  102 + PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
  103 + ]) : [],
  104 + ],
  105 +
86 'custom_mysql' => [ 106 'custom_mysql' => [
87 'driver' => 'mysql', 107 'driver' => 'mysql',
88 'url' => '', // DB_DATABASE_URL 108 'url' => '', // DB_DATABASE_URL