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,23 +29,30 @@ class Admin extends BaseController
*/
public function getUsers()
{
$service = new UserService();
$users = $service->getAllUsers();
// 格式化数据
$result = [];
foreach ($users as $user) {
$result[] = [
'username' => $user['username'] ?? '',
'created_at' => $user['created_at'] ?? '',
];
try {
$service = new UserService();
$users = $service->getAllUsers();
// 格式化数据
$result = [];
foreach ($users as $user) {
$result[] = [
'username' => $user['username'] ?? '',
'created_at' => $user['created_at'] ?? '',
];
}
return json([
'code' => 1,
'data' => $result,
'msg' => '获取成功',
]);
} catch (\Exception $e) {
return json([
'code' => 0,
'msg' => '获取失败: ' . $e->getMessage(),
]);
}
return json([
'code' => 1,
'data' => $result,
'msg' => '获取成功',
]);
}
/**
@@ -87,16 +94,23 @@ class Admin extends BaseController
*/
public function getBaseUrl()
{
$service = new ConfigService();
$baseUrl = $service->getBaseUrl();
return json([
'code' => 1,
'data' => [
'base_url' => $baseUrl,
],
'msg' => '获取成功',
]);
try {
$service = new ConfigService();
$baseUrl = $service->getBaseUrl();
return json([
'code' => 1,
'data' => [
'base_url' => $baseUrl,
],
'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)
{
// 获取路径信息
$pathinfo = $request->pathinfo();
// 判断是否为API请求非index页面
$isApiRequest = $pathinfo !== 'admin' && strpos($pathinfo, 'admin/') === 0;
// 检查是否已登录
$username = Session::get('username');
if (empty($username)) {
// 如果是AJAX请求返回JSON
if ($request->isAjax()) {
// 如果是API请求返回JSON
if ($isApiRequest || $request->isAjax()) {
return json([
'code' => 0,
'msg' => '请先登录',
@@ -38,8 +44,8 @@ class Admin
$isAdmin = Session::get('is_admin', false);
if (!$isAdmin) {
// 如果是AJAX请求返回JSON
if ($request->isAjax()) {
// 如果是API请求返回JSON
if ($isApiRequest || $request->isAjax()) {
return json([
'code' => 0,
'msg' => '无权限访问,需要管理员权限',

View File

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