mirror of
https://github.com/lsky-org/lsky-pro.git
synced 2025-01-07 03:16:46 +08:00
🎨 改进结构
This commit is contained in:
parent
cebf64a4bb
commit
43f8900cdd
@ -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'));
|
||||
}
|
||||
}
|
||||
|
@ -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')]);
|
||||
|
@ -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);
|
||||
|
@ -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('保存成功');
|
||||
}
|
||||
|
@ -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'
|
||||
|
@ -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;
|
||||
|
@ -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'
|
||||
|
@ -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]);
|
||||
|
@ -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],
|
||||
|
@ -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('设置成功');
|
||||
|
@ -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);
|
||||
|
@ -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.');
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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())));
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
Loading…
Reference in New Issue
Block a user