@@ -34,16 +34,31 @@ public function index()
3434 $ table ->cols ->filter (fn ($ col ) => empty ($ col ->hide_in_search ))->column ('data_index ' ),
3535 ['filter ' , 'sorter ' ]
3636 ), 'get ' );
37+
3738 $ total = $ this ->model ->withSearch (array_keys ($ search ), $ search )->count ();
38- $ list = $ this ->model ->withSearch (array_keys ($ search ), $ search )->with (
39+ $ objs = $ this ->model ->withSearch (array_keys ($ search ), $ search )->with (
3940 $ table ->cols ->filter (fn ($ col ) => empty ($ col ->hide_in_table ) && !empty ($ col ->relation_name ))->column ('relation_name ' ) ?: []
4041 )->page ($ current , $ pageSize )->select ();
42+ $ data = [];
43+ foreach ($ objs as $ obj ) {
44+ $ data [] = array_merge_recursive ($ obj ->visible (array_merge ([$ this ->model ->getPk ()], $ table ->crud_index_cols ))->toArray (), $ this ->mergeIndex ($ obj ));
45+ }
4146
4247 return $ this ->success ([
4348 'total ' => $ total ,
44- 'data ' => $ list -> visible ( array_merge ([ $ this -> model -> getPk ()], $ table -> crud_index_cols ))-> toArray ()
49+ 'data ' => $ data
4550 ]);
4651 }
52+ /**
53+ * 列表的合并数据(扩展列表返回)
54+ * @access public
55+ * @param \think\Model $obj 模型对象
56+ * @return array
57+ */
58+ public function mergeIndex ($ obj )
59+ {
60+ return [];
61+ }
4762
4863 /**
4964 * @api {POST} /crud/:table 创建
@@ -57,7 +72,6 @@ public function create()
5772 $ table = TableModel::find (parse_name (string_remove_prefix ($ this ->request ->controller (), 'admin. ' ), 0 ));
5873 $ data = $ this ->request ->post ($ table ->cols ->filter (fn ($ col ) => empty ($ col ->hide_in_form ))->column ('data_index ' ));
5974
60- // 创建
6175 $ this ->model ->create ($ data );
6276
6377 return $ this ->success ();
@@ -78,7 +92,19 @@ public function read($id)
7892 throw new ModelNotFoundException ('数据不存在 ' );
7993 }
8094
81- return $ this ->success ($ obj ->visible (array_merge ([$ this ->model ->getPk ()], $ table ->crud_read_cols ))->toArray ());
95+ return $ this ->success (
96+ array_merge_recursive ($ obj ->visible (array_merge ([$ this ->model ->getPk ()], $ table ->crud_read_cols ))->toArray (), $ this ->mergeRead ($ obj ))
97+ );
98+ }
99+ /**
100+ * 读取的合并数据(扩展读取返回)
101+ * @access public
102+ * @param \think\Model $obj 模型对象
103+ * @return array
104+ */
105+ public function mergeRead ($ obj )
106+ {
107+ return [];
82108 }
83109
84110 /**
@@ -116,8 +142,18 @@ public function delete($ids)
116142 }
117143
118144 foreach ($ objs as $ obj ) {
119- $ obj -> delete ( );
145+ $ this -> eachDelete ( $ obj );
120146 }
147+
121148 return $ this ->success ();
122149 }
150+ /**
151+ * 删除的模型处理(扩展删除操作)
152+ * @access public
153+ * @param \think\Model $obj 模型对象
154+ */
155+ public function eachDelete ($ obj )
156+ {
157+ $ obj ->delete ();
158+ }
123159}
0 commit comments