添加登录和退出登录接口
parent
bbbf09f8ab
commit
9f8dec1843
|
|
@ -23,24 +23,65 @@ class HomeController extends AdminController
|
|||
{
|
||||
$page = $this->basePage()->css($this->css())->body([
|
||||
Grid::make()->columns([
|
||||
// $this->frameworkInfo()->md(5),
|
||||
$this->frameworkInfo()->md(8),
|
||||
Flex::make()->items([
|
||||
$this->pieChart(),
|
||||
$this->cube(),
|
||||
]),
|
||||
]),
|
||||
Grid::make()->columns([
|
||||
$this->lineChart()->md(8),
|
||||
Flex::make()->className('h-full')->items([
|
||||
$this->clock(),
|
||||
// $this->giteeWidget(),
|
||||
$this->hitokoto(),
|
||||
])->direction('column'),
|
||||
]),
|
||||
// Grid::make()->columns([
|
||||
// $this->lineChart()->md(8),
|
||||
// Flex::make()->className('h-full')->items([
|
||||
// $this->clock(),
|
||||
// // $this->giteeWidget(),
|
||||
// ])->direction('column'),
|
||||
// ]),
|
||||
]);
|
||||
|
||||
return $this->response()->success($page);
|
||||
}
|
||||
|
||||
/**
|
||||
* 一言
|
||||
*/
|
||||
public function hitokoto()
|
||||
{
|
||||
return Card::make()
|
||||
->className('h-full clear-card-mb')
|
||||
->body(
|
||||
Custom::make()->html(<<<HTML
|
||||
<div class="h-full flex flex-col mt-5 py-8 px-7">
|
||||
<div>『</div>
|
||||
<div class="flex flex-1 items-center w-full justify-center" id="hitokoto">
|
||||
<a class="text-dark" href="#" id="hitokoto_text" target="_blank"></a>
|
||||
</div>
|
||||
<div class="flex justify-end">』</div>
|
||||
</div>
|
||||
<div class="flex justify-end mt-3">
|
||||
——
|
||||
<span id="hitokoto_from_who"></span>
|
||||
<span>「</span>
|
||||
<span id="hitokoto_from"></span>
|
||||
<span>」</span>
|
||||
</div>
|
||||
HTML
|
||||
|
||||
)->onMount(<<<JS
|
||||
fetch('https://v1.hitokoto.cn?c=i')
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
const hitokoto = document.querySelector('#hitokoto_text')
|
||||
hitokoto.href = `https://hitokoto.cn/?uuid=\${data.uuid}`
|
||||
hitokoto.innerText = data.hitokoto
|
||||
document.querySelector('#hitokoto_from_who').innerText = data.from_who
|
||||
document.querySelector('#hitokoto_from').innerText = data.from
|
||||
})
|
||||
.catch(console.error)
|
||||
JS
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* gitee widget
|
||||
*/
|
||||
|
|
@ -123,25 +164,25 @@ JS
|
|||
Image::make()->src(url(config('admin.logo'))),
|
||||
Wrapper::make()->className('text-3xl mt-9')->body(config('admin.name')),
|
||||
Flex::make()->className('w-64 mt-5')->justify('space-around')->items([
|
||||
Action::make()
|
||||
->level('link')
|
||||
->label('Gitee')
|
||||
->blank(true)
|
||||
->actionType('url')
|
||||
->blank(true)
|
||||
->link('https://gitee.com/slowlyo/owl-admin'),
|
||||
Action::make()
|
||||
->level('link')
|
||||
->label('OwlAdmin 文档')
|
||||
->blank(true)
|
||||
->actionType('url')
|
||||
->link('https://slowlyo.gitee.io/owl-admin-doc/'),
|
||||
Action::make()
|
||||
->level('link')
|
||||
->label('Amis 文档')
|
||||
->blank(true)
|
||||
->actionType('url')
|
||||
->link('https://aisuda.bce.baidu.com/amis/zh-CN/docs/index'),
|
||||
// Action::make()
|
||||
// ->level('link')
|
||||
// ->label('Gitee')
|
||||
// ->blank(true)
|
||||
// ->actionType('url')
|
||||
// ->blank(true)
|
||||
// ->link('https://gitee.com/slowlyo/owl-admin'),
|
||||
// Action::make()
|
||||
// ->level('link')
|
||||
// ->label('OwlAdmin 文档')
|
||||
// ->blank(true)
|
||||
// ->actionType('url')
|
||||
// ->link('https://slowlyo.gitee.io/owl-admin-doc/'),
|
||||
// Action::make()
|
||||
// ->level('link')
|
||||
// ->label('Amis 文档')
|
||||
// ->blank(true)
|
||||
// ->actionType('url')
|
||||
// ->link('https://aisuda.bce.baidu.com/amis/zh-CN/docs/index'),
|
||||
]),
|
||||
]),
|
||||
])
|
||||
|
|
|
|||
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class BizException extends Exception
|
||||
{
|
||||
public function __construct(string $message, int $code = 400)
|
||||
{
|
||||
parent::__construct($message, $code);
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于响应的 HTTP 状态代码
|
||||
*
|
||||
* @var int
|
||||
*/
|
||||
public $status = 200;
|
||||
|
||||
/**
|
||||
* 设置用于响应的 HTTP 状态代码
|
||||
*
|
||||
* @param int $status
|
||||
* @return $this
|
||||
*/
|
||||
public function status(int $status)
|
||||
{
|
||||
$this->status = $status;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 报告异常
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function report()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
|
@ -2,34 +2,30 @@
|
|||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Illuminate\Auth\AuthenticationException;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
|
||||
use Throwable;
|
||||
use Illuminate\Http\Exceptions\ThrottleRequestsException;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use Throwable;
|
||||
|
||||
class Handler extends ExceptionHandler
|
||||
{
|
||||
/**
|
||||
* A list of exception types with their corresponding custom log levels.
|
||||
*
|
||||
* @var array<class-string<\Throwable>, \Psr\Log\LogLevel::*>
|
||||
*/
|
||||
protected $levels = [
|
||||
//
|
||||
];
|
||||
|
||||
/**
|
||||
* A list of the exception types that are not reported.
|
||||
*
|
||||
* @var array<int, class-string<\Throwable>>
|
||||
* @var string[]
|
||||
*/
|
||||
protected $dontReport = [
|
||||
//
|
||||
];
|
||||
|
||||
/**
|
||||
* A list of the inputs that are never flashed to the session on validation exceptions.
|
||||
* A list of the inputs that are never flashed for validation exceptions.
|
||||
*
|
||||
* @var array<int, string>
|
||||
* @var string[]
|
||||
*/
|
||||
protected $dontFlash = [
|
||||
'current_password',
|
||||
|
|
@ -37,6 +33,28 @@ class Handler extends ExceptionHandler
|
|||
'password_confirmation',
|
||||
];
|
||||
|
||||
/**
|
||||
* Get the default context variables for logging.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function context()
|
||||
{
|
||||
try {
|
||||
return array_merge(parent::context(), [
|
||||
'url' => request()->url(),
|
||||
'params' => request()->except($this->dontFlash),
|
||||
'accept' => request()->header('accept'),
|
||||
'client_app' => request()->header('client-app'),
|
||||
'client_version' => request()->header('client-version'),
|
||||
'client_platform' => request()->header('client-platform'),
|
||||
'client_os' => request()->header('client-os'),
|
||||
]);
|
||||
} catch (Throwable $e) {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register the exception handling callbacks for the application.
|
||||
*
|
||||
|
|
@ -44,17 +62,98 @@ class Handler extends ExceptionHandler
|
|||
*/
|
||||
public function register()
|
||||
{
|
||||
// Laravel9.x 无法捕获 ModelNotFoundException, 在父类 prepareException 方法中被转换为 Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
||||
$this->renderable(function (\Illuminate\Database\Eloquent\ModelNotFoundException $e, Request $request) {
|
||||
if ($request->acceptsJson()) {
|
||||
return response()->json(['status' => 404, 'msg' => '资源不存在', 'data' => null], 200);
|
||||
$this->map(ModelNotFoundException::class, function ($e) {
|
||||
$resource = __($key = 'models.'.$e->getModel());
|
||||
|
||||
if ($key === $resource) {
|
||||
$resource = str_ireplace(['App\\Models\\Admin\\', 'App\\Models\\'], '', $e->getModel());
|
||||
}
|
||||
|
||||
return new HttpException(404, __(':resource not found', ['resource' => $resource]), $e);
|
||||
});
|
||||
$this->renderable(function (\Illuminate\Validation\ValidationException $e, Request $request) {
|
||||
if ($request->acceptsJson()) {
|
||||
$errors = $e->validator->errors();
|
||||
return response()->json(['status' => 422, 'msg' => $errors->first(), 'data' => null, 'errors' => $errors], 200);
|
||||
|
||||
$this->renderable(function (BizException $e, $request) {
|
||||
if ($this->shouldReturnJson($request, $e)) {
|
||||
return response()->json($this->convertExceptionToArray($e), $e->status);
|
||||
}
|
||||
|
||||
if (! config('app.debug')) {
|
||||
$e = new HttpException($e->status, $e->getMessage(), $e);
|
||||
}
|
||||
|
||||
return $this->prepareResponse($request, $e);
|
||||
});
|
||||
|
||||
$this->renderable(function (ThrottleRequestsException $e, Request $request) {
|
||||
if ($request->acceptsJson()) {
|
||||
return response()->json(['code' => 429, 'message' => $e->getMessage()]);
|
||||
}
|
||||
|
||||
return $this->prepareResponse($request, $e);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function unauthenticated($request, AuthenticationException $exception)
|
||||
{
|
||||
return $this->shouldReturnJson($request, $exception)
|
||||
? response()->json([
|
||||
'code' => 401,
|
||||
'message' => $exception->getMessage(),
|
||||
], 401)
|
||||
: redirect()->guest($exception->redirectTo() ?? route('login'));
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function invalidJson($request, ValidationException $exception)
|
||||
{
|
||||
return response()->json([
|
||||
'code' => 422,
|
||||
'message' => $exception->getMessage(),
|
||||
'errors' => $exception->errors(),
|
||||
], 200);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
protected function convertExceptionToArray(Throwable $e)
|
||||
{
|
||||
$data = [
|
||||
'code' => 500,
|
||||
'message' => config('app.debug') ? $e->getMessage() : '服务器繁忙,请稍后再试',
|
||||
];
|
||||
if ($this->isBizException($e)) {
|
||||
$data = [
|
||||
'code' => $e->getCode(),
|
||||
'message' => $e->getMessage(),
|
||||
'data' => null,
|
||||
];
|
||||
|
||||
return $data;
|
||||
} elseif ($this->isHttpException($e)) {
|
||||
$data = [
|
||||
'code' => $e->getStatusCode(),
|
||||
'message' => $e->getMessage(),
|
||||
'data' => null,
|
||||
];
|
||||
}
|
||||
|
||||
return array_merge(parent::convertExceptionToArray($e), $data);
|
||||
}
|
||||
|
||||
/**
|
||||
* 确认给定的异常是否是自定义业务异常
|
||||
*
|
||||
* @param Throwable $e
|
||||
* @return bool
|
||||
*/
|
||||
protected function isBizException(Throwable $e): bool
|
||||
{
|
||||
return $e instanceof BizException;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -46,10 +46,8 @@ class MonitorModeFilter extends ModelFilter
|
|||
}
|
||||
return $q;
|
||||
});
|
||||
|
||||
}else{
|
||||
return $this->whereRaw("FIND_IN_SET('".$groupTags."',group_tags)");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Slowlyo\OwlAdmin\Models\AdminUser;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Hash;
|
||||
|
||||
class AuthController extends Controller
|
||||
{
|
||||
public function login(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'username' => 'required',
|
||||
'password' => 'required',
|
||||
]);
|
||||
|
||||
$user = AdminUser::where(['username' => $request->input('username')])->first();
|
||||
if (! $user) {
|
||||
return $this->error('用户名或密码错误');
|
||||
}
|
||||
if (! Hash::check($request->input('password'), $user->password)) {
|
||||
return $this->error('用户名或密码错误');
|
||||
}
|
||||
|
||||
// if ($user->is_enable !== 1) {
|
||||
// return $this->error('用户状态异常请联系管理员');
|
||||
// }
|
||||
|
||||
return $this->attemptUser($user);
|
||||
}
|
||||
|
||||
protected function attemptUser(AdminUser $user, $name = 'api')
|
||||
{
|
||||
$token = $user->createToken($name)->plainTextToken;
|
||||
|
||||
return $this->json(['token' => $token]);
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
|
||||
class UserController extends Controller
|
||||
{
|
||||
public function logout()
|
||||
{
|
||||
$user = auth()->user();
|
||||
// $user->tokens()->delete();
|
||||
$user->currentAccessToken()->delete();
|
||||
|
||||
return $this->success('退出成功');
|
||||
}
|
||||
}
|
||||
|
|
@ -40,6 +40,10 @@ return [
|
|||
'driver' => 'session',
|
||||
'provider' => 'users',
|
||||
],
|
||||
'api' => [
|
||||
'driver' => 'sanctum',
|
||||
'provider' => 'users',
|
||||
],
|
||||
],
|
||||
|
||||
/*
|
||||
|
|
|
|||
|
|
@ -1,8 +1,7 @@
|
|||
<?php
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Http\Controllers\Api\{BannerController, AdminNoticeController, ArticleController, RegionController};
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
|
|
@ -30,3 +29,14 @@ Route::get('region', [RegionController::class, 'index']);
|
|||
Route::get('region/{id}', [RegionController::class, 'show']);
|
||||
Route::get('region/{id}/plants', [RegionController::class, 'plants']);
|
||||
Route::get('region/{id}/harvests', [RegionController::class, 'harvests']);
|
||||
|
||||
|
||||
//大屏接口
|
||||
Route::post('auth/login', [AuthController::class, 'login']);
|
||||
|
||||
Route::group(['middleware' => 'auth:sanctum'], function () {
|
||||
|
||||
Route::prefix('users')->group(function () {
|
||||
Route::delete('logout', [UserController::class, 'logout']);
|
||||
});
|
||||
});
|
||||
Loading…
Reference in New Issue