chore: 重写初始提交(清空历史,整理后全量提交)
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
<?php
|
||||
// +----------------------------------------------------------------------
|
||||
// | YwxApp [ WE CAN DO IT JUST THINK ]
|
||||
// +----------------------------------------------------------------------
|
||||
// | Copyright (c) 2026-2036 http://ywxapp.cn All rights reserved.
|
||||
// +----------------------------------------------------------------------
|
||||
// | Author: ywxapp<admin@ywxapp.cn>
|
||||
// +----------------------------------------------------------------------
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace addon\blog\middleware;
|
||||
|
||||
use Closure;
|
||||
use think\facade\Session;
|
||||
use think\Request;
|
||||
use ywxapp\library\Result;
|
||||
|
||||
/**
|
||||
* 博客后台管理员鉴权中间件
|
||||
*
|
||||
* 插件应用(blog)在 MultiApp 中走 setAddon(),框架默认仅在 backend 应用
|
||||
* 把容器中的 auth 绑定为 \ywxapp\library\AdminAuth。本中间件在 blog 后台路由
|
||||
* 分组生效,将 auth 重新绑定为管理员鉴权,使后台控制器继承的 BaseController
|
||||
* 能通过 app('auth') 完成登录态校验(与 backend 应用同源、共享 Session 登录态)。
|
||||
*
|
||||
* 未登录处理:
|
||||
* - AJAX / JSON 请求:返回 401 JSON(交由前端处理)
|
||||
* - 整页访问(blog 后台以 iframe 嵌入 backend):跳转后台登录页,
|
||||
* 若处于 iframe 内则自动 top.location 跳出
|
||||
*/
|
||||
class AdminAuth
|
||||
{
|
||||
public function handle($request, Closure $next)
|
||||
{
|
||||
// 绑定后台管理员鉴权实例(与 app/backend 应用一致)
|
||||
app()->bind('auth', \ywxapp\library\AdminAuth::class);
|
||||
|
||||
$auth = app('auth');
|
||||
if (! $auth->isLogin) {
|
||||
// AJAX / JSON 请求:保持 401,交由前端逻辑处理
|
||||
if ($request->isAjax() || $request->isJson()) {
|
||||
return Result::instance()->setStatusCode(401)->error(lang('Please login first'));
|
||||
}
|
||||
// 整页加载:跳转到后台登录页(iframe 内跳出到顶层)
|
||||
// 登录路径跟随 app_map 中 backend 对应的别名(默认 admin),避免硬编码失效;
|
||||
// 用 url() 助手生成完整绝对 URL,避免子目录部署 / 跨域时解析错误
|
||||
$appMap = config('app.app_map', []);
|
||||
$alias = array_search('backend', $appMap, true);
|
||||
$loginPath = '/' . ($alias === false ? 'backend' : $alias) . '/login';
|
||||
$url = (string) url($loginPath, [], false, true);
|
||||
$html = sprintf(
|
||||
'<!DOCTYPE html><html><head><meta charset="utf-8">'
|
||||
. '<title>Redirect</title>'
|
||||
. '<script>var u=%s;'
|
||||
. 'if(window.self!==window.top){window.top.location.href=u;}'
|
||||
. 'else{window.location.href=u;}</script>'
|
||||
. '</head><body></body></html>',
|
||||
json_encode($url)
|
||||
);
|
||||
|
||||
return response($html)->contentType('text/html; charset=utf-8');
|
||||
}
|
||||
|
||||
return $next($request);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user