up
This commit is contained in:
@@ -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(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -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' => '无权限访问,需要管理员权限',
|
||||
|
||||
Reference in New Issue
Block a user