Commit 727747fe authored by UtopiaXC's avatar UtopiaXC

🚠 登录与站点中间件

parent 13d116a8
......@@ -2,20 +2,46 @@
namespace App\Http\Middleware\View;
use App\Models\System\SiteProfile;
use Cache;
use Closure;
use HeaderKey;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Predis\Connection\ConnectionException;
use RedisCacheKey;
class SiteProfileMiddleware
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
//通过Redis获取网站配置信息,如果没有则存入Redis
try {
$site_profile = json_decode(Redis::get(RedisCacheKey::SITE_PROFILE), true);
} catch (ConnectionException $e) {
$site_profile = json_decode(Cache::get(RedisCacheKey::SITE_PROFILE), true);
}
if (!$site_profile) {
$site_profile = SiteProfile::all();
try {
Redis::set(RedisCacheKey::SITE_PROFILE, $site_profile);
} catch (ConnectionException $e) {
Cache::put(RedisCacheKey::SITE_PROFILE, $site_profile);
}
}
$profiles = [];
foreach ($site_profile as $profile) {
$profiles += [$profile['profile_type'] => $profile['profile_content']];
}
$request->attributes->add([HeaderKey::SITE_PROFILE => $profiles]);
return $next($request);
}
}
......@@ -3,6 +3,7 @@
namespace App\Http\Middleware\View;
use Closure;
use HeaderKey;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Predis\Connection\ConnectionException;
......@@ -20,7 +21,7 @@ class UserAuthMiddleware
{
$token = $request->cookie(\CookieKey::USER_TOKEN);
if (!$token) {
$request->attributes->add([\HeaderKey::LOGIN_STATUS => false]);
$request->attributes->add([HeaderKey::LOGIN_STATUS => false]);
}
try {
$user = json_decode(Redis::get(\RedisCacheKey::USER_TOKEN . $token), true);
......@@ -28,9 +29,9 @@ class UserAuthMiddleware
$user = json_decode(\Cache::get(\RedisCacheKey::USER_TOKEN . $token), true);
}
if (!$user)
$request->attributes->add([\HeaderKey::LOGIN_STATUS => false]);
$request->attributes->add([HeaderKey::LOGIN_STATUS => false]);
else
$request->attributes->add([\HeaderKey::LOGIN_STATUS => true,\HeaderKey::USER_INFO=>$user]);
$request->attributes->add([HeaderKey::LOGIN_STATUS => true, HeaderKey::USER_INFO=>$user]);
return $next($request);
}
}
......@@ -21,7 +21,8 @@ class ApiUrl
class Middleware
{
const AUTH_MIDDLEWARE = 'AuthMiddleWare';
const AUTH_MIDDLEWARE = 'AuthMiddleware';
const SITE_PROFILE_MIDDLEWARE='SiteProfileMiddleware';
}
class UserTypeEnum
......@@ -41,7 +42,7 @@ class UserStatusEnum
class RedisCacheKey
{
const SITE_PROFILE = "site_profile:";
const SITE_PROFILE = "site_profile";
const USER_TOKEN = "user_token:";
}
......@@ -54,6 +55,7 @@ class HeaderKey
{
const LOGIN_STATUS = "login_status";
const USER_INFO="user_info";
const SITE_PROFILE="site_profile";
}
class SiteProfileTypeEnum
......
......@@ -229,6 +229,7 @@ return [
'Uuid' => Webpatser\Uuid\Uuid::class,
Middleware::AUTH_MIDDLEWARE=>UserAuthMiddleware::class,
'Captcha' => Mews\Captcha\Facades\Captcha::class,
Middleware::SITE_PROFILE_MIDDLEWARE=>\App\Http\Middleware\View\SiteProfileMiddleware::class,
],
];
This diff is collapsed.
This diff is collapsed.
{{--@php--}}
@php
$site_profile=app('request')->get(HeaderKey::SITE_PROFILE);
{{-- use App\Models\System\SiteProfile;--}}
{{-- use Illuminate\Support\Facades\Redis;--}}
{{-- //通过Redis获取网站配置信息,如果没有则存入Redis--}}
{{-- $site_profile=json_decode(Redis::get(RedisKey::SITE_PROFILE),true);--}}
{{-- if($site_profile==null){--}}
{{-- $site_profile=SiteProfile::all();--}}
{{-- Redis::set(RedisKey::SITE_PROFILE,$site_profile);--}}
{{-- }--}}
{{-- $profiles=[];--}}
{{-- foreach($site_profile as $profile){--}}
{{-- $profiles+=[$profile['type']=>$profile['content']];--}}
{{-- }--}}
{{-- //侧边栏渲染--}}
{{-- $side_slider='';--}}
{{-- if (app('request')->get("user_login")==UserLoginEnum::LOGIN){--}}
{{-- $side_slider='<a href='.WebUrls::USER_CENTER.' class="mdui-list-item mdui-ripple ">'.trans("app.user_center").'</a>';--}}
{{-- $side_slider.='<a href='.WebUrls::USER_CENTER.' class="mdui-list-item mdui-ripple ">'.trans("app.account").'</a>';--}}
{{-- $side_slider.='<a href="javascript:logout();" class="mdui-list-item mdui-ripple ">'.trans("app.logout").'</a>';--}}
{{-- }else{--}}
{{-- $side_slider='<a href='.WebUrls::LOGIN.' class="mdui-list-item mdui-ripple ">'.trans("app.login").'</a>';--}}
{{-- $side_slider.='<a href='.WebUrls::REGISTER.' class="mdui-list-item mdui-ripple ">'.trans("app.register").'</a>';--}}
{{-- $side_slider.='<a href='.WebUrls::FIND_PASSWORD.' class="mdui-list-item mdui-ripple ">'.trans("app.find_password").'</a>';--}}
{{-- }--}}
{{--@endphp--}}
@endphp
......
<!-- JavaScript -->
<script src="{{asset('/plugins/jquery/jquery-3.4.1.min.js')}}"></script>
<script src="https://unpkg.com/@popperjs/core@2"></script>
<script src="{{asset('/js/popper.min.js')}}"></script>
<script src="{{asset('/plugins/bootstrap/js/bootstrap.min.js')}}"></script>
<script src="https://unpkg.com/feather-icons"></script>
<script src="{{asset('/js/feather.min.js')}}"></script>
<script src="{{asset('/plugins/perfectscroll/perfect-scrollbar.min.js')}}"></script>
<script src="{{asset('/plugins/apexcharts/apexcharts.min.js')}}"></script>
<script src="{{asset('/js/main.min.js')}}"></script>
......@@ -13,7 +13,7 @@ use Illuminate\Support\Facades\Route;
|
*/
Route::group(['prefix'=>'/','middleware'=>Middleware::AUTH_MIDDLEWARE],function (){
Route::group(['prefix'=>'/','middleware'=>[Middleware::SITE_PROFILE_MIDDLEWARE,Middleware::AUTH_MIDDLEWARE]],function (){
Route::get('/', function () {
return view('index');
});
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment