up
This commit is contained in:
114
app/controller/Admin.php
Normal file
114
app/controller/Admin.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\controller;
|
||||
|
||||
use app\BaseController;
|
||||
use app\middleware\Admin;
|
||||
use app\service\ConfigService;
|
||||
use app\service\UserService;
|
||||
use think\facade\View;
|
||||
|
||||
/**
|
||||
* 管理员控制器
|
||||
*/
|
||||
class Admin extends BaseController
|
||||
{
|
||||
protected $middleware = [Admin::class];
|
||||
|
||||
/**
|
||||
* 显示管理首页
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有账号列表
|
||||
*/
|
||||
public function getUsers()
|
||||
{
|
||||
$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' => '获取成功',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加账号
|
||||
*/
|
||||
public function addUser()
|
||||
{
|
||||
$username = $this->request->param('username', '');
|
||||
$password = $this->request->param('password', '');
|
||||
|
||||
$service = new UserService();
|
||||
$result = $service->addUser($username, $password);
|
||||
|
||||
return json($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除账号
|
||||
*/
|
||||
public function deleteUser()
|
||||
{
|
||||
$username = $this->request->param('username', '');
|
||||
|
||||
if (empty($username)) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '用户名不能为空',
|
||||
]);
|
||||
}
|
||||
|
||||
$service = new UserService();
|
||||
$result = $service->deleteUser($username);
|
||||
|
||||
return json($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取BASE_URL配置
|
||||
*/
|
||||
public function getBaseUrl()
|
||||
{
|
||||
$service = new ConfigService();
|
||||
$baseUrl = $service->getBaseUrl();
|
||||
|
||||
return json([
|
||||
'code' => 1,
|
||||
'data' => [
|
||||
'base_url' => $baseUrl,
|
||||
],
|
||||
'msg' => '获取成功',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置BASE_URL配置
|
||||
*/
|
||||
public function setBaseUrl()
|
||||
{
|
||||
$baseUrl = $this->request->param('base_url', '');
|
||||
|
||||
$service = new ConfigService();
|
||||
$result = $service->setBaseUrl($baseUrl);
|
||||
|
||||
return json($result);
|
||||
}
|
||||
}
|
||||
@@ -18,9 +18,14 @@ class Auth extends BaseController
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
// 如果已登录,跳转到爬虫页面
|
||||
// 如果已登录,根据账号类型跳转到不同页面
|
||||
if (Session::has('username')) {
|
||||
return redirect('/crawler');
|
||||
$isAdmin = Session::get('is_admin', false);
|
||||
if ($isAdmin) {
|
||||
return redirect('/admin');
|
||||
} else {
|
||||
return redirect('/crawler');
|
||||
}
|
||||
}
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
@@ -1,114 +0,0 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\controller;
|
||||
|
||||
use app\BaseController;
|
||||
use app\middleware\Auth;
|
||||
use app\service\UserService;
|
||||
use think\facade\Session;
|
||||
use think\facade\View;
|
||||
|
||||
/**
|
||||
* 用户管理控制器
|
||||
*/
|
||||
class User extends BaseController
|
||||
{
|
||||
protected $middleware = [Auth::class];
|
||||
|
||||
/**
|
||||
* 显示账号管理页面
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
// 检查是否为管理员
|
||||
if (!Session::get('is_admin', false)) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '无权限访问',
|
||||
]);
|
||||
}
|
||||
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有账号列表
|
||||
*/
|
||||
public function getUsers()
|
||||
{
|
||||
// 检查是否为管理员
|
||||
if (!Session::get('is_admin', false)) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '无权限访问',
|
||||
]);
|
||||
}
|
||||
|
||||
$service = new UserService();
|
||||
$users = $service->getAllUsers();
|
||||
|
||||
// 隐藏密码
|
||||
foreach ($users as &$user) {
|
||||
if (isset($user['password'])) {
|
||||
$user['password'] = '******';
|
||||
}
|
||||
}
|
||||
|
||||
return json([
|
||||
'code' => 1,
|
||||
'data' => $users,
|
||||
'msg' => '获取成功',
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 添加账号
|
||||
*/
|
||||
public function add()
|
||||
{
|
||||
// 检查是否为管理员
|
||||
if (!Session::get('is_admin', false)) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '无权限访问',
|
||||
]);
|
||||
}
|
||||
|
||||
$username = $this->request->param('username', '');
|
||||
$password = $this->request->param('password', '');
|
||||
|
||||
$service = new UserService();
|
||||
$result = $service->addUser($username, $password);
|
||||
|
||||
return json($result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除账号
|
||||
*/
|
||||
public function delete()
|
||||
{
|
||||
// 检查是否为管理员
|
||||
if (!Session::get('is_admin', false)) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '无权限访问',
|
||||
]);
|
||||
}
|
||||
|
||||
$username = $this->request->param('username', '');
|
||||
|
||||
if (empty($username)) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '用户名不能为空',
|
||||
]);
|
||||
}
|
||||
|
||||
$service = new UserService();
|
||||
$result = $service->deleteUser($username);
|
||||
|
||||
return json($result);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user