33
44namespace App \Http \Controllers \Admin ;
55
6+ use App \Models \CodeSnippet ;
7+ use Carbon \Carbon ;
68use Illuminate \Pagination \LengthAwarePaginator ;
79use Illuminate \Support \Facades \DB ;
10+ use Illuminate \Support \Facades \Storage ;
811use Rap2hpoutre \FastExcel \FastExcel ;
912use Illuminate \Support \Facades \Validator ;
1013use 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 ){
0 commit comments