UP
This commit is contained in:
@@ -7,6 +7,7 @@ use app\BaseController;
|
||||
use app\middleware\Admin as AdminMiddleware;
|
||||
use app\service\ConfigService;
|
||||
use app\service\UserService;
|
||||
use think\facade\Session;
|
||||
use think\facade\View;
|
||||
|
||||
/**
|
||||
@@ -16,6 +17,43 @@ class Admin extends BaseController
|
||||
{
|
||||
protected $middleware = [AdminMiddleware::class];
|
||||
|
||||
/**
|
||||
* 初始化方法
|
||||
*/
|
||||
protected function initialize()
|
||||
{
|
||||
parent::initialize();
|
||||
|
||||
// 对于API方法,直接检查权限并设置响应头
|
||||
$action = $this->request->action();
|
||||
$apiActions = ['getUsers', 'getBaseUrl', 'addUser', 'deleteUser', 'setBaseUrl'];
|
||||
|
||||
if (in_array($action, $apiActions)) {
|
||||
// 检查登录状态
|
||||
if (!Session::has('username')) {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode([
|
||||
'code' => 0,
|
||||
'msg' => '请先登录',
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 检查管理员权限
|
||||
if (!Session::get('is_admin', false)) {
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
echo json_encode([
|
||||
'code' => 0,
|
||||
'msg' => '无权限访问,需要管理员权限',
|
||||
], JSON_UNESCAPED_UNICODE);
|
||||
exit;
|
||||
}
|
||||
|
||||
// 设置JSON响应头
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 显示管理首页
|
||||
*/
|
||||
@@ -29,6 +67,9 @@ class Admin extends BaseController
|
||||
*/
|
||||
public function getUsers()
|
||||
{
|
||||
// 强制返回JSON,设置响应头
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
try {
|
||||
$service = new UserService();
|
||||
$users = $service->getAllUsers();
|
||||
@@ -60,6 +101,9 @@ class Admin extends BaseController
|
||||
*/
|
||||
public function addUser()
|
||||
{
|
||||
// 强制返回JSON,设置响应头
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
$username = $this->request->param('username', '');
|
||||
$password = $this->request->param('password', '');
|
||||
|
||||
@@ -74,6 +118,9 @@ class Admin extends BaseController
|
||||
*/
|
||||
public function deleteUser()
|
||||
{
|
||||
// 强制返回JSON,设置响应头
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
$username = $this->request->param('username', '');
|
||||
|
||||
if (empty($username)) {
|
||||
@@ -94,6 +141,9 @@ class Admin extends BaseController
|
||||
*/
|
||||
public function getBaseUrl()
|
||||
{
|
||||
// 强制返回JSON,设置响应头
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
try {
|
||||
$service = new ConfigService();
|
||||
$baseUrl = $service->getBaseUrl();
|
||||
@@ -118,6 +168,9 @@ class Admin extends BaseController
|
||||
*/
|
||||
public function setBaseUrl()
|
||||
{
|
||||
// 强制返回JSON,设置响应头
|
||||
header('Content-Type: application/json; charset=utf-8');
|
||||
|
||||
$baseUrl = $this->request->param('base_url', '');
|
||||
|
||||
$service = new ConfigService();
|
||||
|
||||
@@ -30,9 +30,10 @@ Route::post('crawler/batchGetPositionInfo', 'crawler/batchGetPositionInfo');
|
||||
Route::post('crawler/fetchAllPositions', 'crawler/fetchAllPositions');
|
||||
|
||||
// 管理员路由(需要登录且为管理员)
|
||||
Route::get('admin', 'admin/index');
|
||||
// 注意:API路由必须在index路由之前定义,确保优先匹配
|
||||
Route::get('admin/getUsers', 'admin/getUsers');
|
||||
Route::get('admin/getBaseUrl', 'admin/getBaseUrl');
|
||||
Route::post('admin/addUser', 'admin/addUser');
|
||||
Route::post('admin/deleteUser', 'admin/deleteUser');
|
||||
Route::get('admin/getBaseUrl', 'admin/getBaseUrl');
|
||||
Route::post('admin/setBaseUrl', 'admin/setBaseUrl');
|
||||
Route::post('admin/setBaseUrl', 'admin/setBaseUrl');
|
||||
Route::get('admin', 'admin/index');
|
||||
Reference in New Issue
Block a user