2323use App \Models \User ;
2424use App \Repositories \AttachmentRepository ;
2525use Illuminate \Http \Request ;
26+ use Illuminate \Support \Facades \DB ;
2627use Illuminate \Support \Facades \Storage ;
2728use Illuminate \Support \Facades \View ;
2829use Intervention \Image \Facades \Image ;
@@ -48,7 +49,9 @@ public function __construct (AttachmentRepository $repository)
4849 */
4950 public function index (Request $ request )
5051 {
51-
52+ if (!check_admin_auth ($ this ->module_name . '_ ' . __FUNCTION__ )) {
53+ return auth_error_return ();
54+ }
5255 $ category_id = $ request ->input ('category_id ' , 0 );
5356 if (request ()->wantsJson ()) {
5457 $ limit = $ request ->input ('limit ' , 15 );
@@ -68,7 +71,7 @@ public function index (Request $request)
6871 $ path = $ item ->storage_path ;
6972 $ exits = Storage::disk ('public ' )->exists ($ path );
7073 if ($ exits ) {
71- $ src = asset ($ item ->path );
74+ $ src = $ item -> url ? $ item -> url : asset ($ item ->path );
7275 $ size = Storage::disk ('public ' )->size ($ path );
7376 $ size = format_size ($ size );
7477 $ mineType = mime_content_type ($ item ->path );
@@ -99,131 +102,6 @@ public function index (Request $request)
99102 return view ('admin. ' . $ this ->module_name . '.index ' , compact ('attachment ' , 'category_id ' ));
100103 }
101104 }
102-
103- /**
104- * Show the form for creating a new resource.
105- *
106- * @return \Illuminate\Http\Response
107- */
108- public function create (Request $ request )
109- {
110- $ attachment = $ this ->repository ->makeModel ();
111- $ _method = 'POST ' ;
112- $ attachment ->status = StatusEnum::NORMAL ;
113-
114- return view ('admin. ' . $ this ->module_name . '.add ' , compact ('attachment ' , '_method ' ));
115- }
116-
117- /**
118- * Store a newly created resource in storage.
119- *
120- * @param \Illuminate\Http\Request $request
121- * @return \Illuminate\Http\Response
122- */
123- public function store (Request $ request )
124- {
125- $ request ->validate ([
126- 'Attachment.title ' => 'required ' ,
127- 'Attachment.name ' => 'unique:attachments,name ' ,
128- 'Attachment.content ' => 'required ' ,
129- 'Attachment.status ' => 'required ' ,
130- ], [], [
131- 'Attachment.title ' => '标题 ' ,
132- 'Attachment.name ' => '英文标识 ' ,
133- 'Attachment.content ' => '内容 ' ,
134- 'Attachment.status ' => '状态 ' ,
135- ]);
136- if (!check_admin_auth ($ this ->module_name . '_edit ' )) {
137- return auth_error_return ();
138- }
139- $ input = $ request ->input ('Attachment ' );
140- $ input = $ this ->formatRequestInput (__FUNCTION__ , $ input );
141- try {
142- $ input ['user_id ' ] = get_login_user_id ();
143- $ attachment = $ this ->repository ->create ($ input );
144- if ($ attachment ) {
145- $ log_title = '添加附件记录 ' ;
146- Log::createLog (Log::ADD_TYPE , $ log_title , '' , $ attachment ->id , Attachment::class);
147-
148- return ajax_success_result ('添加成功 ' );
149- } else {
150- return ajax_success_result ('添加失败 ' );
151- }
152-
153- } catch (BusinessException $ e ) {
154- return ajax_error_result ($ e ->getMessage ());
155- }
156- }
157-
158- private function formatRequestInput (string $ __FUNCTION__ , $ input )
159- {
160- return $ input ;
161- }
162-
163- /**
164- * Display the specified resource.
165- *
166- * @param \App\Models\Attachment $attachment
167- * @return \Illuminate\Http\Response
168- */
169- public function show (Attachment $ attachment )
170- {
171- //
172- }
173-
174- /**
175- * Show the form for editing the specified resource.
176- *
177- * @param \App\Models\Attachment $attachment
178- * @return \Illuminate\Http\Response
179- */
180- public function edit (Attachment $ attachment )
181- {
182- $ _method = 'PUT ' ;
183-
184- return view ('admin. ' . $ this ->module_name . '.add ' , compact ('attachment ' , '_method ' ));
185- }
186-
187- /**
188- * Update the specified resource in storage.
189- *
190- * @param \Illuminate\Http\Request $request
191- * @param \App\Models\Attachment $attachment
192- * @return \Illuminate\Http\Response
193- */
194- public function update (Request $ request , Attachment $ attachment )
195- {
196- $ request ->validate ([
197- 'Attachment.title ' => 'required ' ,
198- 'Attachment.name ' => 'unique:attachments,name, ' . $ attachment ->id ,
199- 'Attachment.content ' => 'required ' ,
200- 'Attachment.status ' => 'required ' ,
201- ], [], [
202- 'Attachment.title ' => '标题 ' ,
203- 'Attachment.name ' => '英文标识 ' ,
204- 'Attachment.content ' => '内容 ' ,
205- 'Attachment.status ' => '状态 ' ,
206- ]);
207- $ input = $ request ->input ('Attachment ' );
208- $ input = $ this ->formatRequestInput (__FUNCTION__ , $ input );
209- try {
210- $ input ['user_id ' ] = get_login_user_id ();
211- $ attachment = $ this ->repository ->update ($ input , $ attachment ->id );
212- if ($ attachment ) {
213- $ content = $ attachment ->toArray () ?? '' ;
214- $ log_title = '修改附件记录 ' ;
215- Log::createLog (Log::EDIT_TYPE , $ log_title , $ content , $ attachment ->id , Attachment::class);
216-
217- return ajax_success_result ('修改成功 ' );
218- } else {
219- return ajax_success_result ('修改失败 ' );
220- }
221-
222- } catch (BusinessException $ e ) {
223- return ajax_error_result ($ e ->getMessage ());
224- }
225- }
226-
227105 /**
228106 * Remove the specified resource from storage.
229107 *
@@ -232,31 +110,30 @@ public function update (Request $request, Attachment $attachment)
232110 */
233111 public function destroy ($ id , Request $ request )
234112 {
113+ if (!check_admin_auth ($ this ->module_name . '_delete ' )) {
114+ return auth_error_return ();
115+ }
235116 $ ids = $ request ->input ('ids ' , []);
236117 if (empty ($ ids )) {
237118 $ ids [] = $ id ;
238119 }
120+ DB ::beginTransaction ();
239121 $ ids = (array )$ ids ;
240122 $ M = $ this ->repository ->makeModel ();
241123 $ lists = $ M ->whereIn ('id ' , $ ids )->get ();
242124 $ num = 0 ;
243125 foreach ($ lists as $ item ) {
244- try {
245- $ this ->repository ->checkAuth ($ item );
246- } catch (BusinessException $ e ) {
247- return ajax_error_result ($ e ->getMessage ());
248- }
249126 $ log_title = '删除附件[ ' . ($ item ->category ->title ?? '' ) . '-> ' . $ item ->title . ']记录 ' ;
250127 $ check = $ this ->repository ->allowDelete ($ item ->id );
251128 if ($ check ) {
252- $ ret = $ this -> repository -> delete ($ item-> id );
129+ $ ret = Attachment:: deleteFile ($ item );
253130 if ($ ret ) {
254131 Log::createLog (Log::DELETE_TYPE , $ log_title , $ item , $ item ->id , Attachment::class);
255132 $ num ++;
256133 }
257134 }
258135 }
259-
136+ DB :: commit ();
260137 return ajax_success_result ('成功删除 ' . $ num . '条记录 ' );
261138 }
262139}
0 commit comments