44
55use Exception ;
66use Intervention \Image \ImageManagerStatic as Image ;
7- use plugin \admin \app \controller \Base ;
8- use plugin \admin \app \controller \Crud ;
97use plugin \admin \app \model \Upload ;
10- use Random \RandomException ;
118use support \exception \BusinessException ;
129use support \Request ;
1310use support \Response ;
@@ -106,7 +103,7 @@ public function insert(Request $request): Response
106103 if (!$ file || !$ file ->isValid ()) {
107104 return $ this ->json (1 , '未找到文件 ' );
108105 }
109- $ data = $ this ->base ($ request , '/upload/files/ ' . date ('Ymd ' ));
106+ $ data = $ this ->base ($ request , '/upload/files/ ' . date ('Ymd ' ));
110107 $ upload = new Upload ;
111108 $ upload ->admin_id = admin_id ();
112109 $ upload ->name = $ data ['name ' ];
@@ -150,7 +147,7 @@ public function file(Request $request): Response
150147 if (in_array ($ file ->getUploadExtension (), $ img_exts )) {
151148 return $ this ->image ($ request );
152149 }
153- $ data = $ this ->base ($ request , '/upload/files/ ' . date ('Ymd ' ));
150+ $ data = $ this ->base ($ request , '/upload/files/ ' . date ('Ymd ' ));
154151 return $ this ->json (0 , '上传成功 ' , [
155152 'url ' => $ data ['url ' ],
156153 'name ' => $ data ['name ' ],
@@ -166,7 +163,7 @@ public function file(Request $request): Response
166163 */
167164 public function image (Request $ request ): Response
168165 {
169- $ data = $ this ->base ($ request , '/upload/img/ ' . date ('Ymd ' ));
166+ $ data = $ this ->base ($ request , '/upload/img/ ' . date ('Ymd ' ));
170167 $ realpath = $ data ['realpath ' ];
171168 try {
172169 $ img = Image::make ($ realpath );
@@ -178,17 +175,17 @@ public function image(Request $request): Response
178175 if ($ height > $ max_height || $ width > $ max_width ) {
179176 $ ratio = $ width > $ height ? $ max_width / $ width : $ max_height / $ height ;
180177 }
181- $ img ->resize ($ width* $ ratio , $ height* $ ratio )->save ($ realpath );
178+ $ img ->resize ($ width * $ ratio , $ height * $ ratio )->save ($ realpath );
182179 } catch (Exception $ e ) {
183180 unlink ($ realpath );
184- return json ( [
185- 'code ' => 500 ,
186- 'msg ' => '处理图片发生错误 '
181+ return json ([
182+ 'code ' => 500 ,
183+ 'msg ' => '处理图片发生错误 '
187184 ]);
188185 }
189- return json ( [
190- 'code ' => 0 ,
191- 'msg ' => '上传成功 ' ,
186+ return json ([
187+ 'code ' => 0 ,
188+ 'msg ' => '上传成功 ' ,
192189 'data ' => [
193190 'url ' => $ data ['url ' ],
194191 'name ' => $ data ['name ' ],
@@ -220,7 +217,7 @@ public function avatar(Request $request): Response
220217 if (!is_dir ($ real_path )) {
221218 mkdir ($ real_path , 0777 , true );
222219 }
223- $ name = bin2hex (pack ('Nn ' ,time (), random_int (1 , 65535 )));
220+ $ name = bin2hex (pack ('Nn ' , time (), random_int (1 , 65535 )));
224221 $ ext = $ file ->getUploadExtension ();
225222
226223 $ image ->crop ($ size , $ size )->resize (300 , 300 );
@@ -254,21 +251,22 @@ public function avatar(Request $request): Response
254251 * 删除附件
255252 * @param Request $request
256253 * @return Response
254+ * @throws BusinessException
257255 */
258256 public function delete (Request $ request ): Response
259257 {
260258 $ ids = $ this ->deleteInput ($ request );
261259 $ primary_key = $ this ->model ->getKeyName ();
262260 $ files = $ this ->model ->whereIn ($ primary_key , $ ids )->get ()->toArray ();
263261 $ file_list = array_map (function ($ item ) {
264- $ path =$ item ['url ' ];
265- if (preg_match ("#^/app/admin# " ,$ path )){
266- $ admin_public_path = config ('plugin.admin.app.public_path ' ) ?: base_path () . "/plugin/admin/public " ;
267- return $ admin_public_path . str_replace ("/app/admin " , "" , $ item ['url ' ]);
268- }
269- return null ;
270- },$ files );
271- $ file_list = array_filter ($ file_list ,function ($ item ){
262+ $ path = $ item ['url ' ];
263+ if (preg_match ("#^/app/admin# " , $ path )) {
264+ $ admin_public_path = config ('plugin.admin.app.public_path ' ) ?: base_path () . "/plugin/admin/public " ;
265+ return $ admin_public_path . str_replace ("/app/admin " , "" , $ item ['url ' ]);
266+ }
267+ return null ;
268+ }, $ files );
269+ $ file_list = array_filter ($ file_list , function ($ item ) {
272270 return !empty ($ item );
273271 });
274272 $ result = parent ::delete ($ request );
@@ -285,7 +283,7 @@ public function delete(Request $request): Response
285283 * @param Request $request
286284 * @param $relative_dir
287285 * @return array
288- * @throws BusinessException|RandomException
286+ * @throws BusinessException|\Random\ RandomException
289287 */
290288 protected function base (Request $ request , $ relative_dir ): array
291289 {
@@ -318,7 +316,7 @@ protected function base(Request $request, $relative_dir): array
318316 throw new BusinessException ('不支持该格式的文件上传 ' , 400 );
319317 }
320318
321- $ relative_path = $ relative_dir . '/ ' . bin2hex (pack ('Nn ' ,time (), random_int (1 , 65535 ))) . ". $ ext " ;
319+ $ relative_path = $ relative_dir . '/ ' . bin2hex (pack ('Nn ' , time (), random_int (1 , 65535 ))) . ". $ ext " ;
322320 $ full_path = $ base_dir . $ relative_path ;
323321 $ file ->move ($ full_path );
324322 $ image_with = $ image_height = 0 ;
@@ -327,10 +325,10 @@ protected function base(Request $request, $relative_dir): array
327325 $ mime_type = $ img_info ['mime ' ];
328326 }
329327 return [
330- 'url ' => "/app/admin/ $ relative_path " ,
331- 'name ' => $ file_name ,
328+ 'url ' => "/app/admin/ $ relative_path " ,
329+ 'name ' => $ file_name ,
332330 'realpath ' => $ full_path ,
333- 'size ' => $ file_size ,
331+ 'size ' => $ file_size ,
334332 'mime_type ' => $ mime_type ,
335333 'image_with ' => $ image_with ,
336334 'image_height ' => $ image_height ,
0 commit comments