23
23
use App \Models \User ;
24
24
use App \Repositories \AttachmentRepository ;
25
25
use Illuminate \Http \Request ;
26
+ use Illuminate \Support \Facades \DB ;
26
27
use Illuminate \Support \Facades \Storage ;
27
28
use Illuminate \Support \Facades \View ;
28
29
use Intervention \Image \Facades \Image ;
@@ -48,7 +49,9 @@ public function __construct (AttachmentRepository $repository)
48
49
*/
49
50
public function index (Request $ request )
50
51
{
51
-
52
+ if (!check_admin_auth ($ this ->module_name . '_ ' . __FUNCTION__ )) {
53
+ return auth_error_return ();
54
+ }
52
55
$ category_id = $ request ->input ('category_id ' , 0 );
53
56
if (request ()->wantsJson ()) {
54
57
$ limit = $ request ->input ('limit ' , 15 );
@@ -68,7 +71,7 @@ public function index (Request $request)
68
71
$ path = $ item ->storage_path ;
69
72
$ exits = Storage::disk ('public ' )->exists ($ path );
70
73
if ($ exits ) {
71
- $ src = asset ($ item ->path );
74
+ $ src = $ item -> url ? $ item -> url : asset ($ item ->path );
72
75
$ size = Storage::disk ('public ' )->size ($ path );
73
76
$ size = format_size ($ size );
74
77
$ mineType = mime_content_type ($ item ->path );
@@ -99,131 +102,6 @@ public function index (Request $request)
99
102
return view ('admin. ' . $ this ->module_name . '.index ' , compact ('attachment ' , 'category_id ' ));
100
103
}
101
104
}
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
-
227
105
/**
228
106
* Remove the specified resource from storage.
229
107
*
@@ -232,31 +110,30 @@ public function update (Request $request, Attachment $attachment)
232
110
*/
233
111
public function destroy ($ id , Request $ request )
234
112
{
113
+ if (!check_admin_auth ($ this ->module_name . '_delete ' )) {
114
+ return auth_error_return ();
115
+ }
235
116
$ ids = $ request ->input ('ids ' , []);
236
117
if (empty ($ ids )) {
237
118
$ ids [] = $ id ;
238
119
}
120
+ DB ::beginTransaction ();
239
121
$ ids = (array )$ ids ;
240
122
$ M = $ this ->repository ->makeModel ();
241
123
$ lists = $ M ->whereIn ('id ' , $ ids )->get ();
242
124
$ num = 0 ;
243
125
foreach ($ lists as $ item ) {
244
- try {
245
- $ this ->repository ->checkAuth ($ item );
246
- } catch (BusinessException $ e ) {
247
- return ajax_error_result ($ e ->getMessage ());
248
- }
249
126
$ log_title = '删除附件[ ' . ($ item ->category ->title ?? '' ) . '-> ' . $ item ->title . ']记录 ' ;
250
127
$ check = $ this ->repository ->allowDelete ($ item ->id );
251
128
if ($ check ) {
252
- $ ret = $ this -> repository -> delete ($ item-> id );
129
+ $ ret = Attachment:: deleteFile ($ item );
253
130
if ($ ret ) {
254
131
Log::createLog (Log::DELETE_TYPE , $ log_title , $ item , $ item ->id , Attachment::class);
255
132
$ num ++;
256
133
}
257
134
}
258
135
}
259
-
136
+ DB :: commit ();
260
137
return ajax_success_result ('成功删除 ' . $ num . '条记录 ' );
261
138
}
262
139
}
0 commit comments