Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit f87b1af

Browse files
committed
简单的代码生成自动化功能
1 parent 3f97f6d commit f87b1af

32 files changed

+1341
-263
lines changed

‎api/app/Http/Controllers/Admin/CodeSnippetController.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ class CodeSnippetController extends Controller
1313
{
1414
use Tool;
1515
protected $model = 'App\Models\CodeSnippet'; // 当前模型
16-
protected $fillable = []; // 当前模型可以修改和新增的字段
16+
protected $fillable = ['name', 'desc', 'front_api', 'front_model', 'front_page', 'back_routes', 'back_model',
17+
'back_resource', 'back_api']; // 当前模型可以修改和新增的字段
1718
protected $resource = 'App\Http\Resources\CodeSnippet'; // 显示个体资源
1819
protected $resourceCollection = 'App\Http\Resources\CodeSnippetCollection'; // 显示资源集合
1920
protected $map = []; // 导入导出时候 数据表字段与说明的映射表

‎api/app/Http/Controllers/Admin/HZip.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,50 @@
66

77
class HZip
88
{
9+
/**
10+
* Add files and sub-directories in a folder to zip file.
11+
* @param string $folder
12+
* @param ZipArchive $zipFile
13+
* @param int $exclusiveLength Number of text to be exclusived from the file path.
14+
*/
15+
private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
16+
$handle = opendir($folder);
17+
while (false !== $f = readdir($handle)) {
18+
if ($f != '.' && $f != '..') {
19+
$filePath = "$folder/$f";
20+
// Remove prefix from file path before add to zip.
21+
$localPath = substr($filePath, $exclusiveLength);
22+
if (is_file($filePath)) {
23+
$zipFile->addFile($filePath, $localPath);
24+
} elseif (is_dir($filePath)) {
25+
// Add sub-directory.
26+
$zipFile->addEmptyDir($localPath);
27+
self::folderToZip($filePath, $zipFile, $exclusiveLength);
28+
}
29+
}
30+
}
31+
closedir($handle);
32+
}
933

34+
/**
35+
* Zip a folder (include itself).
36+
* Usage:
37+
* HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip');
38+
*
39+
* @param string $sourcePath Path of directory to be zip.
40+
* @param string $outZipPath Path of output zip file.
41+
*/
42+
public static function zipDir($sourcePath, $outZipPath)
43+
{
44+
45+
$pathInfo = pathInfo($sourcePath);
46+
$parentPath = $pathInfo['dirname'];
47+
$dirName = $pathInfo['basename'];
48+
49+
$z = new \ZipArchive();
50+
$z->open($outZipPath, \ZIPARCHIVE::CREATE);
51+
$z->addEmptyDir($dirName);
52+
self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
53+
$z->close();
54+
}
1055
}

‎api/app/Http/Controllers/Admin/LoginController.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
use Illuminate\Http\Request;
1414
use Faker\Factory;
1515
use App\GatewayClient\Gateway;
16+
use Illuminate\Support\Facades\Storage;
17+
use ZipArchive;
18+
use App\Http\Controllers\Admin\HZip;
19+
1620
/**
1721
* @group 管理员登陆管理
1822
* 管理员登陆、退出、刷新和获取个人信息
@@ -29,6 +33,59 @@ public function __construct()
2933

3034
public function test()
3135
{
36+
// var_dump('控制器目录');
37+
// $os = php_uname('s');
38+
// $controller = 'api/Http/Controllers/Admin';
39+
// $model = 'api/Models';
40+
// $routes = "api/routes";
41+
// $resource = 'api/Http/Resources';
42+
// $api = 'element/src/api';
43+
// $front_model = 'element/src/model';
44+
// $page = 'element/src/view';
45+
// $back_controller_path = app_path($controller);
46+
// $back_model_path = app_path($model);
47+
// $back_routes_path = base_path($routes);
48+
// $back_resources_path = app_path($resource);
49+
// $app_path = app_path();
50+
// if (strpos($os, 'Windows')>=0){
51+
// $back_controller_path = str_replace("\\", "/", $back_controller_path);
52+
// $back_model_path = str_replace("\\", "/", $back_model_path);
53+
// $back_routes_path = str_replace("\\", "/", $back_routes_path);
54+
// $back_resources_path = str_replace("\\", "/", $back_resources_path);
55+
// }
56+
$tableName = 'wechats';
57+
if (Storage::disk('code')->exists($tableName)){
58+
Storage::disk('code')->deleteDirectory($tableName);
59+
}
60+
Storage::disk('code')->makeDirectory($tableName);
61+
Storage::disk('code')->makeDirectory($tableName.'/'.$controller);
62+
Storage::disk('code')->makeDirectory($tableName.'/'.$model);
63+
Storage::disk('code')->makeDirectory($tableName.'/'.$routes);
64+
Storage::disk('code')->makeDirectory($tableName.'/'.$resource);
65+
Storage::disk('code')->makeDirectory($tableName.'/'.$api);
66+
Storage::disk('code')->makeDirectory($tableName.'/'.$front_model);
67+
Storage::disk('code')->makeDirectory($tableName.'/'.$page);
68+
$zip = public_path("code/$tableName.zip");//压缩文件名,自己命名
69+
HZip::zipDir(public_path("code/$tableName"),$zip);
70+
return response()->download($zip, basename($zip))->deleteFileAfterSend(true);
71+
72+
// $code_root_dir = public_path('code/'.'wechats');
73+
// if (!file_exists($code_root_dir)){
74+
// mkdir($code_root_dir);
75+
// }
76+
// mkdir($code_root_dir.'/'.$controller);
77+
// mkdir($code_root_dir.'/'.$model);
78+
// mkdir($code_root_dir.'/'.$routes);
79+
// mkdir($code_root_dir.'/'.$resource);
80+
// var_dump($back_controller_path);
81+
// var_dump($back_model_path);
82+
// var_dump($back_routes_path);
83+
// var_dump($back_resources_path);
84+
85+
86+
87+
88+
3289

3390

3491
}

‎api/app/Http/Controllers/Admin/TableController.php

Lines changed: 130 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@
33

44
namespace App\Http\Controllers\Admin;
55

6+
use App\Models\CodeSnippet;
7+
use Carbon\Carbon;
68
use Illuminate\Pagination\LengthAwarePaginator;
79
use Illuminate\Support\Facades\DB;
10+
use Illuminate\Support\Facades\Storage;
811
use Rap2hpoutre\FastExcel\FastExcel;
912
use Illuminate\Support\Facades\Validator;
1013
use Illuminate\Http\Request;
@@ -13,7 +16,7 @@ class TableController extends Controller
1316
{
1417
use Tool;
1518
protected $model = 'App\Models\Table'; // 当前模型
16-
protected $fillable = []; // 当前模型可以修改和新增的字段
19+
protected $fillable = ['table_name', 'table_comment', 'engine', 'table_collation', 'create_time', 'table_config']; // 当前模型可以修改和新增的字段
1720
protected $resource = 'App\Http\Resources\Table'; // 显示个体资源
1821
protected $resourceCollection = 'App\Http\Resources\TableCollection'; // 显示资源集合
1922
protected $map = []; // 导入导出时候 数据表字段与说明的映射表
@@ -39,6 +42,10 @@ class TableController extends Controller
3942
"roles",
4043
"three_logins",
4144
"users",
45+
"code_configs",
46+
"code_snippets",
47+
"codes",
48+
"tables"
4249
];
4350

4451
public function index(Request $request)
@@ -49,7 +56,7 @@ public function index(Request $request)
4956
// dd($existsTable);
5057
$dbName = env('DB_DATABASE');
5158
$sql = <<<SQL
52-
SELECT table_name,engine, table_collation, table_comment, create_time
59+
SELECT 0 as id, table_name,engine, table_collation, table_comment, create_time, '' as table_config
5360
FROM INFORMATION_SCHEMA.TABLES
5461
WHERE TABLE_SCHEMA = '$dbName' and table_comment <> 'VIEW'
5562
SQL;
@@ -78,8 +85,48 @@ protected function getListData($pageSize){
7885

7986

8087
public function show($id){
81-
$data = $this->model::find($id);
82-
return new $this->resource($data);
88+
if ($id >= 1) {
89+
$result = $this->model::find($id);
90+
} else {
91+
$result = $this->getResultByTable();
92+
}
93+
return new $this->resource($result);
94+
}
95+
96+
protected function getResultByTable(){
97+
$table_name = request('table_name');
98+
$result = $this->model::where('table_name', $table_name)->first();
99+
if (!$result){
100+
$data = request()->only($this->fillable);
101+
$len = strlen($table_name);
102+
if ($table_name[$len-1] === "s"){
103+
$front_model = substr($table_name, 0,$len-1); // 去掉复数形式
104+
$back_model = ucfirst($front_model); // 首字母大写
105+
$component = $back_model.'Index';
106+
$config = [
107+
'back_model' => $back_model,
108+
'back_routes' => $table_name,
109+
'front_model' => $front_model,
110+
'front_component_name' => $component
111+
];
112+
} else {
113+
$back_model = ucfirst($table_name); // 首字母大写
114+
$component = $back_model.'Index';
115+
$config = [
116+
'back_model' => $back_model,
117+
'back_routes' => $table_name.'s',
118+
'front_model' => $table_name,
119+
'front_component_name' => $component
120+
];
121+
122+
}
123+
$data['create_time'] = Carbon::createFromFormat('Y-m-d H:i:s', $data['create_time']);
124+
$data['table_config'] = json_encode($config);
125+
$id = $this->model::insertGetId($data);
126+
$result = $this->model::find($id);
127+
}
128+
129+
return $result;
83130
}
84131

85132
public function store(Request $request)
@@ -128,25 +175,87 @@ protected function getErrorInfo($validator)
128175

129176
public function update(Request $request, $id)
130177
{
131-
$data = $request->only($this->fillable);
132-
if (method_exists($this, 'message')){
133-
$validator = Validator::make($data, $this->updateRule($id), $this->message());
134-
} else {
135-
$validator = Validator::make($data, $this->updateRule($id));
136-
}
137-
if ($validator->fails()){
138-
// 有错误,处理错误信息并且返回
139-
$errorTips = $this->getErrorInfo($validator);
140-
return $this->errorWithInfo($errorTips, 422);
178+
$action = request('action', 'default');
179+
if ($action === 'default') {
180+
// 普通的保存信息
181+
$data = $request->only($this->fillable);
182+
if (method_exists($this, 'message')){
183+
$validator = Validator::make($data, $this->updateRule($id), $this->message());
184+
} else {
185+
$validator = Validator::make($data, $this->updateRule($id));
186+
}
187+
if ($validator->fails()){
188+
// 有错误,处理错误信息并且返回
189+
$errorTips = $this->getErrorInfo($validator);
190+
return $this->errorWithInfo($errorTips, 422);
191+
}
192+
// 进一步处理数据
193+
$data = $this->updateHandle($data);
194+
// 更新到数据表
195+
if ($this->model::where('id', $id)->update($data)){
196+
return $this->successWithInfo('数据更新成功');
197+
} else {
198+
return $this->errorWithInfo('数据更新失败');
199+
}
141200
}
142-
// 进一步处理数据
143-
$data = $this->updateHandle($data);
144-
// 更新到数据表
145-
if ($this->model::where('id', $id)->update($data)){
146-
return $this->successWithInfo('数据更新成功');
147-
} else {
148-
return $this->errorWithInfo('数据更新失败');
201+
if ($action === 'download'){
202+
$result = $this->getResultByTable();
203+
$config = $result->table_config;
204+
$tableName = $result->table_name;
205+
$this->createDir($tableName);
206+
$snippet = CodeSnippet::whereNotNull('name')->first();
207+
// 处理后端控制器数据
208+
$code = $this->createCodeBySnippet($snippet->back_api,$config);
209+
210+
// 保存内容
211+
$fileName = $config['back_model'].'Controller.php';
212+
$controller = 'api/app/Http/Controllers/Admin';
213+
file_put_contents(public_path('code/'.$tableName.'/'.$controller)."/$fileName",$code);
214+
// 后端模型
215+
$code = $this->createCodeBySnippet($snippet->back_model,$config);
216+
217+
dd($code);
218+
219+
dd($config['back_model']);
220+
$zip = public_path("code/$tableName.zip");//压缩文件名,自己命名
221+
HZip::zipDir(public_path("code/$tableName"),$zip);
222+
return response()->download($zip, basename($zip))->deleteFileAfterSend(true);
149223
}
224+
225+
}
226+
227+
public function createDir($tableName)
228+
{
229+
// 建立保存文件的目录
230+
$controller = 'api/app/Http/Controllers/Admin';
231+
$model = 'api/app/Models';
232+
$routes = "api/routes";
233+
$resource = 'api/app/Http/Resources';
234+
$api = 'element/src/api';
235+
$front_model = 'element/src/model';
236+
$page = 'element/src/view';
237+
if (Storage::disk('code')->exists($tableName)){
238+
Storage::disk('code')->deleteDirectory($tableName);
239+
}
240+
Storage::disk('code')->makeDirectory($tableName);
241+
Storage::disk('code')->makeDirectory($tableName.'/'.$controller);
242+
Storage::disk('code')->makeDirectory($tableName.'/'.$model);
243+
Storage::disk('code')->makeDirectory($tableName.'/'.$routes);
244+
Storage::disk('code')->makeDirectory($tableName.'/'.$resource);
245+
Storage::disk('code')->makeDirectory($tableName.'/'.$api);
246+
Storage::disk('code')->makeDirectory($tableName.'/'.$front_model);
247+
Storage::disk('code')->makeDirectory($tableName.'/'.$page);
248+
}
249+
250+
protected function createCodeBySnippet($code, $config)
251+
{
252+
$keys = array_keys($config);
253+
foreach ($keys as $key){
254+
$format = "##".$key."##";
255+
$value = $config[$key];
256+
$code = str_replace($format, $value, $code);;
257+
}
258+
return $code;
150259
}
151260

152261
protected function updateHandle($data){

‎api/app/Http/Resources/Code.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ class Code extends JsonResource
1515
public function toArray($request)
1616
{
1717
$data = parent::toArray($request);
18-
$data['created_at'] = $data['created_at'] * 1000;
19-
$data['updated_at'] = $data['updated_at'] * 1000;
18+
2019
// 数据转换
2120

2221
return $data;

‎api/app/Http/Resources/CodeSnippet.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,7 @@ class CodeSnippet extends JsonResource
1515
public function toArray($request)
1616
{
1717
$data = parent::toArray($request);
18-
$data['created_at'] = $data['created_at'] * 1000;
19-
$data['updated_at'] = $data['updated_at'] * 1000;
2018
// 数据转换
21-
2219
return $data;
2320
}
2421

‎api/app/Http/Resources/Table.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ class Table extends JsonResource
1515
public function toArray($request)
1616
{
1717
$data = [
18+
'id' => $this->id,
1819
'table_name' => $this->table_name,
1920
'engine' => $this->engine,
2021
'table_collation' => $this->table_collation,
2122
'table_comment' => $this->table_comment,
22-
'create_time' => $this->create_time
23+
'create_time' => $this->create_time,
24+
'table_config' => $this->table_config
2325
];
2426
return $data;
2527
}

‎api/app/Models/Code.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
class Code extends Model
88
{
99
//
10-
protected $casts =[
11-
'created_at' => 'timestamp',
12-
'updated_at' => 'timestamp'
13-
];
10+
protected $dateFormat = "Y-m-d H:i:s";
1411

1512
protected $guarded = [];
1613

‎api/app/Models/CodeSnippet.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,7 @@
77
class CodeSnippet extends Model
88
{
99
//
10-
protected $casts =[
11-
'created_at' => 'timestamp',
12-
'updated_at' => 'timestamp'
13-
];
10+
protected $dateFormat = "Y-m-d H:i:s";
1411

1512
protected $guarded = [];
1613

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /