3
3
4
4
namespace App \Http \Controllers \Admin ;
5
5
6
+ use App \Models \CodeSnippet ;
7
+ use Carbon \Carbon ;
6
8
use Illuminate \Pagination \LengthAwarePaginator ;
7
9
use Illuminate \Support \Facades \DB ;
10
+ use Illuminate \Support \Facades \Storage ;
8
11
use Rap2hpoutre \FastExcel \FastExcel ;
9
12
use Illuminate \Support \Facades \Validator ;
10
13
use Illuminate \Http \Request ;
@@ -13,7 +16,7 @@ class TableController extends Controller
13
16
{
14
17
use Tool;
15
18
protected $ model = 'App\Models\Table ' ; // 当前模型
16
- protected $ fillable = []; // 当前模型可以修改和新增的字段
19
+ protected $ fillable = [' table_name ' , ' table_comment ' , ' engine ' , ' table_collation ' , ' create_time ' , ' table_config ' ]; // 当前模型可以修改和新增的字段
17
20
protected $ resource = 'App\Http\Resources\Table ' ; // 显示个体资源
18
21
protected $ resourceCollection = 'App\Http\Resources\TableCollection ' ; // 显示资源集合
19
22
protected $ map = []; // 导入导出时候 数据表字段与说明的映射表
@@ -39,6 +42,10 @@ class TableController extends Controller
39
42
"roles " ,
40
43
"three_logins " ,
41
44
"users " ,
45
+ "code_configs " ,
46
+ "code_snippets " ,
47
+ "codes " ,
48
+ "tables "
42
49
];
43
50
44
51
public function index (Request $ request )
@@ -49,7 +56,7 @@ public function index(Request $request)
49
56
// dd($existsTable);
50
57
$ dbName = env ('DB_DATABASE ' );
51
58
$ 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
53
60
FROM INFORMATION_SCHEMA.TABLES
54
61
WHERE TABLE_SCHEMA = ' $ dbName' and table_comment <> 'VIEW'
55
62
SQL ;
@@ -78,8 +85,48 @@ protected function getListData($pageSize){
78
85
79
86
80
87
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 ;
83
130
}
84
131
85
132
public function store (Request $ request )
@@ -128,25 +175,87 @@ protected function getErrorInfo($validator)
128
175
129
176
public function update (Request $ request , $ id )
130
177
{
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
+ }
141
200
}
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 );
149
223
}
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 ;
150
259
}
151
260
152
261
protected function updateHandle ($ data ){
0 commit comments