This commit is contained in:
杨志
2026-01-21 08:57:26 +08:00
parent 91460a7bdc
commit dd99d0b397
3 changed files with 55 additions and 30 deletions

View File

@@ -29,6 +29,7 @@ class Admin extends BaseController
*/ */
public function getUsers() public function getUsers()
{ {
try {
$service = new UserService(); $service = new UserService();
$users = $service->getAllUsers(); $users = $service->getAllUsers();
@@ -46,6 +47,12 @@ class Admin extends BaseController
'data' => $result, 'data' => $result,
'msg' => '获取成功', 'msg' => '获取成功',
]); ]);
} catch (\Exception $e) {
return json([
'code' => 0,
'msg' => '获取失败: ' . $e->getMessage(),
]);
}
} }
/** /**
@@ -87,6 +94,7 @@ class Admin extends BaseController
*/ */
public function getBaseUrl() public function getBaseUrl()
{ {
try {
$service = new ConfigService(); $service = new ConfigService();
$baseUrl = $service->getBaseUrl(); $baseUrl = $service->getBaseUrl();
@@ -97,6 +105,12 @@ class Admin extends BaseController
], ],
'msg' => '获取成功', 'msg' => '获取成功',
]); ]);
} catch (\Exception $e) {
return json([
'code' => 0,
'msg' => '获取失败: ' . $e->getMessage(),
]);
}
} }
/** /**

View File

@@ -19,12 +19,18 @@ class Admin
*/ */
public function handle($request, \Closure $next) public function handle($request, \Closure $next)
{ {
// 获取路径信息
$pathinfo = $request->pathinfo();
// 判断是否为API请求非index页面
$isApiRequest = $pathinfo !== 'admin' && strpos($pathinfo, 'admin/') === 0;
// 检查是否已登录 // 检查是否已登录
$username = Session::get('username'); $username = Session::get('username');
if (empty($username)) { if (empty($username)) {
// 如果是AJAX请求返回JSON // 如果是API请求返回JSON
if ($request->isAjax()) { if ($isApiRequest || $request->isAjax()) {
return json([ return json([
'code' => 0, 'code' => 0,
'msg' => '请先登录', 'msg' => '请先登录',
@@ -38,8 +44,8 @@ class Admin
$isAdmin = Session::get('is_admin', false); $isAdmin = Session::get('is_admin', false);
if (!$isAdmin) { if (!$isAdmin) {
// 如果是AJAX请求返回JSON // 如果是API请求返回JSON
if ($request->isAjax()) { if ($isApiRequest || $request->isAjax()) {
return json([ return json([
'code' => 0, 'code' => 0,
'msg' => '无权限访问,需要管理员权限', 'msg' => '无权限访问,需要管理员权限',

View File

@@ -232,6 +232,7 @@
method: 'GET', method: 'GET',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
} }
}) })
.then(response => response.json()) .then(response => response.json())
@@ -260,6 +261,7 @@
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
'X-Requested-With': 'XMLHttpRequest',
}, },
body: `base_url=${encodeURIComponent(baseUrl)}` body: `base_url=${encodeURIComponent(baseUrl)}`
}) })
@@ -282,6 +284,7 @@
method: 'GET', method: 'GET',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'X-Requested-With': 'XMLHttpRequest',
} }
}) })
.then(response => response.json()) .then(response => response.json())
@@ -345,6 +348,7 @@
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
'X-Requested-With': 'XMLHttpRequest',
}, },
body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}` body: `username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`
}) })
@@ -374,6 +378,7 @@
method: 'POST', method: 'POST',
headers: { headers: {
'Content-Type': 'application/x-www-form-urlencoded', 'Content-Type': 'application/x-www-form-urlencoded',
'X-Requested-With': 'XMLHttpRequest',
}, },
body: `username=${encodeURIComponent(username)}` body: `username=${encodeURIComponent(username)}`
}) })