From 43f8900cdd59745354bc191de41b401409d4cfca Mon Sep 17 00:00:00 2001 From: Wisp X Date: Mon, 7 Mar 2022 08:19:43 +0800 Subject: [PATCH] =?UTF-8?q?:art:=20=E6=94=B9=E8=BF=9B=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Exceptions/Handler.php | 8 ++++---- app/Http/Controllers/Admin/GroupController.php | 8 ++++---- app/Http/Controllers/Admin/SettingController.php | 4 ++-- app/Http/Controllers/Admin/UserController.php | 2 +- app/Http/Controllers/Api/V1/ImageController.php | 6 +++--- app/Http/Controllers/Api/V1/TokenController.php | 4 ++-- app/Http/Controllers/Controller.php | 14 +++++++------- app/Http/Controllers/User/AlbumController.php | 6 +++--- app/Http/Controllers/User/ImageController.php | 4 ++-- app/Http/Controllers/User/UserController.php | 2 +- app/Http/Middleware/CheckIsEnableApi.php | 6 +++--- app/Http/Middleware/CheckIsInstalled.php | 6 +++--- app/Http/Requests/FormRequest.php | 6 +++--- app/Http/{Api.php => Result.php} | 4 ++-- 14 files changed, 40 insertions(+), 40 deletions(-) rename app/Http/{Api.php => Result.php} (84%) diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 034fe84f..eeef1d0e 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -2,7 +2,7 @@ namespace App\Exceptions; -use App\Http\Api; +use App\Http\Result; use Illuminate\Auth\AuthenticationException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; use Illuminate\Http\Exceptions\ThrottleRequestsException; @@ -10,7 +10,7 @@ use Throwable; class Handler extends ExceptionHandler { - use Api; + use Result; /** * A list of the exception types that are not reported. @@ -44,14 +44,14 @@ class Handler extends ExceptionHandler }); $this->renderable(function (ThrottleRequestsException $e) { - return $this->error($e->getMessage())->setStatusCode(429); + return $this->fail($e->getMessage())->setStatusCode(429); }); } protected function unauthenticated($request, AuthenticationException $exception) { return $this->shouldReturnJson($request, $exception) - ? $this->error($exception->getMessage())->setStatusCode(401) + ? $this->fail($exception->getMessage())->setStatusCode(401) : redirect()->guest($exception->redirectTo() ?? route('login')); } } diff --git a/app/Http/Controllers/Admin/GroupController.php b/app/Http/Controllers/Admin/GroupController.php index afe1e391..7022e16a 100644 --- a/app/Http/Controllers/Admin/GroupController.php +++ b/app/Http/Controllers/Admin/GroupController.php @@ -63,19 +63,19 @@ class GroupController extends Controller $group->fill($request->validated()); if ($group->isDirty('is_default') && ! $group->is_default) { if (! Group::query()->where('is_default', true)->where('id', '<>', $group->id)->exists()) { - return $this->error('系统至少需要保留一个默认组'); + return $this->fail('系统至少需要保留一个默认组'); } } if ($group->isDirty('is_guest') && ! $group->is_guest) { if (! Group::query()->where('is_guest', true)->where('id', '<>', $group->id)->exists()) { - return $this->error('系统至少需要保留一个游客组'); + return $this->fail('系统至少需要保留一个游客组'); } } $group->save(); DB::commit(); } catch (\Throwable $e) { DB::rollBack(); - return $this->error('保存失败'); + return $this->fail('保存失败'); } return $this->success('保存成功'); @@ -86,7 +86,7 @@ class GroupController extends Controller /** @var Group $group */ if ($group = Group::query()->find($request->route('id'))) { if ($group->is_default || $group->is_guest) { - return $this->error('默认组和游客组无法删除'); + return $this->fail('默认组和游客组无法删除'); } DB::transaction(function () use ($group) { $group->users()->update(['group_id' => Group::query()->where('is_default', true)->value('id')]); diff --git a/app/Http/Controllers/Admin/SettingController.php b/app/Http/Controllers/Admin/SettingController.php index b674af36..865e9fe6 100644 --- a/app/Http/Controllers/Admin/SettingController.php +++ b/app/Http/Controllers/Admin/SettingController.php @@ -36,7 +36,7 @@ class SettingController extends Controller try { Mail::to($request->post('email'))->send(new Test()); } catch (\Throwable $e) { - return $this->error($e->getMessage()); + return $this->fail($e->getMessage()); } return $this->success('发送成功'); } @@ -53,7 +53,7 @@ class SettingController extends Controller $data['version'] = $service->getVersions()->first(); } } catch (\Exception $e) { - return $this->error($e->getMessage()); + return $this->fail($e->getMessage()); } return $this->success('success', $data); diff --git a/app/Http/Controllers/Admin/UserController.php b/app/Http/Controllers/Admin/UserController.php index bc3c6245..3a314f97 100644 --- a/app/Http/Controllers/Admin/UserController.php +++ b/app/Http/Controllers/Admin/UserController.php @@ -55,7 +55,7 @@ class UserController extends Controller $user->group_id = $validated['group_id']; if (!$user->save()) { - return $this->error('保存失败'); + return $this->fail('保存失败'); } return $this->success('保存成功'); } diff --git a/app/Http/Controllers/Api/V1/ImageController.php b/app/Http/Controllers/Api/V1/ImageController.php index 6c72001a..fb7628ae 100644 --- a/app/Http/Controllers/Api/V1/ImageController.php +++ b/app/Http/Controllers/Api/V1/ImageController.php @@ -42,13 +42,13 @@ class ImageController extends Controller try { $image = $service->store($request); } catch (UploadException $e) { - return $this->error($e->getMessage()); + return $this->fail($e->getMessage()); } catch (\Throwable $e) { Log::error("Api 上传文件时发生异常,", ['message' => $e->getMessage(), 'trace' => $e->getTraceAsString()]); if (config('app.debug')) { - return $this->error($e->getMessage()); + return $this->fail($e->getMessage()); } - return $this->error('服务异常,请稍后再试'); + return $this->fail('服务异常,请稍后再试'); } return $this->success('上传成功', $image->setAppends(['pathname', 'links'])->only( 'key', 'name', 'pathname', 'origin_name', 'size', 'mimetype', 'extension', 'md5', 'sha1', 'links' diff --git a/app/Http/Controllers/Api/V1/TokenController.php b/app/Http/Controllers/Api/V1/TokenController.php index 3f79883b..2395918f 100644 --- a/app/Http/Controllers/Api/V1/TokenController.php +++ b/app/Http/Controllers/Api/V1/TokenController.php @@ -20,14 +20,14 @@ class TokenController extends Controller 'password' => 'required', ]); } catch (ValidationException $e) { - return $this->error($e->validator->errors()->first()); + return $this->fail($e->validator->errors()->first()); } /** @var User|null $user */ $user = User::query()->where('email', $request->email)->first(); if (! $user || ! Hash::check($request->password, $user->password)) { - return $this->error('The email address or password is incorrect.'); + return $this->fail('The email address or password is incorrect.'); } $token = $user->createToken($user->email)->plainTextToken; diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index 052c1efe..0d261a3e 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -5,7 +5,7 @@ namespace App\Http\Controllers; use App\Enums\GroupConfigKey; use App\Enums\UserStatus; use App\Exceptions\UploadException; -use App\Http\Api; +use App\Http\Result; use App\Models\Group; use App\Models\Image; use App\Models\Strategy; @@ -28,13 +28,13 @@ use Symfony\Component\HttpFoundation\StreamedResponse; class Controller extends BaseController { - use AuthorizesRequests, DispatchesJobs, ValidatesRequests, Api; + use AuthorizesRequests, DispatchesJobs, ValidatesRequests, Result; public function install(Request $request): View|Response { if (file_exists(base_path('installed.lock'))) { if ($request->expectsJson()) { - return $this->error('Already installed. if you want to reinstall, please remove installed.lock file.'); + return $this->fail('Already installed. if you want to reinstall, please remove installed.lock file.'); } abort(404); } @@ -106,7 +106,7 @@ class Controller extends BaseController 'msg' => $e->getMessage(), 'trace' => $e->getTraceAsString(), ]); - return $this->error($e->getMessage()); + return $this->fail($e->getMessage()); } return $this->success(); } @@ -119,13 +119,13 @@ class Controller extends BaseController try { $image = $service->store($request); } catch (UploadException $e) { - return $this->error($e->getMessage()); + return $this->fail($e->getMessage()); } catch (\Throwable $e) { Log::error("Web 上传文件时发生异常,", ['message' => $e->getMessage(), 'trace' => $e->getTraceAsString()]); if (config('app.debug')) { - return $this->error($e->getMessage()); + return $this->fail($e->getMessage()); } - return $this->error('服务异常,请稍后再试'); + return $this->fail('服务异常,请稍后再试'); } return $this->success('上传成功', $image->setAppends(['pathname', 'links'])->only( 'id', 'pathname', 'origin_name', 'size', 'mimetype', 'md5', 'sha1', 'links' diff --git a/app/Http/Controllers/User/AlbumController.php b/app/Http/Controllers/User/AlbumController.php index 74fb1a30..5650f5e1 100644 --- a/app/Http/Controllers/User/AlbumController.php +++ b/app/Http/Controllers/User/AlbumController.php @@ -31,7 +31,7 @@ class AlbumController extends Controller if ($user->albums()->create(array_filter($request->validated()))) { return $this->success('创建成功'); } - return $this->error('创建失败'); + return $this->fail('创建失败'); } public function update(AlbumRequest $request): Response @@ -40,7 +40,7 @@ class AlbumController extends Controller $user = Auth::user(); $album = $user->albums()->find($request->route('id')); if (is_null($album)) { - return $this->error('不存在的相册'); + return $this->fail('不存在的相册'); } $album->update(array_filter($request->validated())); return $this->success('修改成功'); @@ -53,7 +53,7 @@ class AlbumController extends Controller /** @var Album|null $album */ $album = $user->albums()->find($request->route('id')); if (is_null($album)) { - return $this->error('不存在的相册'); + return $this->fail('不存在的相册'); } DB::transaction(function () use ($album) { $album->images()->update(['album_id' => null]); diff --git a/app/Http/Controllers/User/ImageController.php b/app/Http/Controllers/User/ImageController.php index 64efe6ec..690a67c7 100644 --- a/app/Http/Controllers/User/ImageController.php +++ b/app/Http/Controllers/User/ImageController.php @@ -44,7 +44,7 @@ class ImageController extends Controller $user = Auth::user(); /** @var Image $image */ if (!$image = $user->images()->find($request->route('id'))) { - return $this->error('未找到该图片'); + return $this->fail('未找到该图片'); } $image->strategy->setVisible(['name']); $image->album?->setVisible(['name']); @@ -62,7 +62,7 @@ class ImageController extends Controller $permission = $request->input('permission'); $permissions = ['public' => ImagePermission::Public, 'private' => ImagePermission::Private]; if (!in_array($permission, array_keys($permissions))) { - return $this->error('设置失败'); + return $this->fail('设置失败'); } $user->images()->whereIn('id', (array) $request->input('ids'))->update([ 'permission' => $permissions[$permission], diff --git a/app/Http/Controllers/User/UserController.php b/app/Http/Controllers/User/UserController.php index 8e5d2fa7..18495aa9 100644 --- a/app/Http/Controllers/User/UserController.php +++ b/app/Http/Controllers/User/UserController.php @@ -56,7 +56,7 @@ class UserController extends Controller /** @var User $user */ $user = Auth::user(); if (! $strategy = $user->group->strategies()->find($request->id)) { - return $this->error('没有找到该策略'); + return $this->fail('没有找到该策略'); } $user->update(['configs->'.UserConfigKey::DefaultStrategy => $strategy->id]); return $this->success('设置成功'); diff --git a/app/Http/Middleware/CheckIsEnableApi.php b/app/Http/Middleware/CheckIsEnableApi.php index 31ddb6bc..f60de41a 100644 --- a/app/Http/Middleware/CheckIsEnableApi.php +++ b/app/Http/Middleware/CheckIsEnableApi.php @@ -3,19 +3,19 @@ namespace App\Http\Middleware; use App\Enums\ConfigKey; -use App\Http\Api; +use App\Http\Result; use App\Utils; use Closure; use Illuminate\Http\Request; class CheckIsEnableApi { - use Api; + use Result; public function handle(Request $request, Closure $next) { if (! Utils::config(ConfigKey::IsEnableApi)) { - return $this->error('管理员未启用 API')->setStatusCode(403); + return $this->fail('管理员未启用 API')->setStatusCode(403); } return $next($request); diff --git a/app/Http/Middleware/CheckIsInstalled.php b/app/Http/Middleware/CheckIsInstalled.php index cd26d197..0322e877 100644 --- a/app/Http/Middleware/CheckIsInstalled.php +++ b/app/Http/Middleware/CheckIsInstalled.php @@ -2,13 +2,13 @@ namespace App\Http\Middleware; -use App\Http\Api; +use App\Http\Result; use Closure; use Illuminate\Http\Request; class CheckIsInstalled { - use Api; + use Result; public function handle(Request $request, Closure $next) { @@ -17,7 +17,7 @@ class CheckIsInstalled if (! $request->expectsJson()) { return redirect('install'); } else { - return $this->error('It has already been installed.'); + return $this->fail('It has already been installed.'); } } diff --git a/app/Http/Requests/FormRequest.php b/app/Http/Requests/FormRequest.php index d1722b87..82cd167c 100644 --- a/app/Http/Requests/FormRequest.php +++ b/app/Http/Requests/FormRequest.php @@ -2,13 +2,13 @@ namespace App\Http\Requests; -use App\Http\Api; +use App\Http\Result; use Illuminate\Contracts\Validation\Validator; use Illuminate\Http\Exceptions\HttpResponseException; class FormRequest extends \Illuminate\Foundation\Http\FormRequest { - use Api; + use Result; public function authorize() { @@ -17,6 +17,6 @@ class FormRequest extends \Illuminate\Foundation\Http\FormRequest protected function failedValidation(Validator $validator) { - throw (new HttpResponseException($this->error($validator->errors()->first()))); + throw (new HttpResponseException($this->fail($validator->errors()->first()))); } } diff --git a/app/Http/Api.php b/app/Http/Result.php similarity index 84% rename from app/Http/Api.php rename to app/Http/Result.php index 27773070..bdd389eb 100644 --- a/app/Http/Api.php +++ b/app/Http/Result.php @@ -4,14 +4,14 @@ namespace App\Http; use Illuminate\Http\Response; -trait Api +trait Result { public function success(string $message = 'success', $data = []): Response { return $this->response(true, $message, $data); } - public function error(string $message = 'error', $data = []): Response + public function fail(string $message = 'fail', $data = []): Response { return $this->response(false, $message, $data); }