up
This commit is contained in:
74
app/controller/Auth.php
Normal file
74
app/controller/Auth.php
Normal file
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
declare (strict_types = 1);
|
||||
|
||||
namespace app\controller;
|
||||
|
||||
use app\BaseController;
|
||||
use app\service\UserService;
|
||||
use think\facade\Session;
|
||||
use think\facade\View;
|
||||
|
||||
/**
|
||||
* 认证控制器
|
||||
*/
|
||||
class Auth extends BaseController
|
||||
{
|
||||
/**
|
||||
* 显示登录页面
|
||||
*/
|
||||
public function login()
|
||||
{
|
||||
// 如果已登录,跳转到爬虫页面
|
||||
if (Session::has('username')) {
|
||||
return redirect('/crawler');
|
||||
}
|
||||
return View::fetch();
|
||||
}
|
||||
|
||||
/**
|
||||
* 处理登录
|
||||
*/
|
||||
public function doLogin()
|
||||
{
|
||||
$username = $this->request->param('username', '');
|
||||
$password = $this->request->param('password', '');
|
||||
|
||||
if (empty($username) || empty($password)) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '请输入用户名和密码',
|
||||
]);
|
||||
}
|
||||
|
||||
$service = new UserService();
|
||||
$user = $service->verifyLogin($username, $password);
|
||||
|
||||
if ($user === false) {
|
||||
return json([
|
||||
'code' => 0,
|
||||
'msg' => '用户名或密码错误',
|
||||
]);
|
||||
}
|
||||
|
||||
// 保存登录信息到Session
|
||||
Session::set('username', $user['username']);
|
||||
Session::set('is_admin', $user['is_admin']);
|
||||
|
||||
return json([
|
||||
'code' => 1,
|
||||
'msg' => '登录成功',
|
||||
'data' => [
|
||||
'is_admin' => $user['is_admin'],
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* 退出登录
|
||||
*/
|
||||
public function logout()
|
||||
{
|
||||
Session::clear();
|
||||
return redirect('/login');
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,7 @@ declare (strict_types = 1);
|
||||
namespace app\controller;
|
||||
|
||||
use app\BaseController;
|
||||
use app\middleware\Auth;
|
||||
use app\service\CrawlerService;
|
||||
use think\facade\View;
|
||||
|
||||
@@ -12,6 +13,7 @@ use think\facade\View;
|
||||
*/
|
||||
class Crawler extends BaseController
|
||||
{
|
||||
protected $middleware = [Auth::class];
|
||||
/**
|
||||
* 显示爬虫工具首页
|
||||
*/
|
||||
|
||||
114
app/controller/User.php
Normal file
114
app/controller/User.php
Normal file
@@ -0,0 +1,114 @@
|
||||
<?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