diff --git a/app/controller/Auth.php b/app/controller/Auth.php new file mode 100644 index 0000000..313f420 --- /dev/null +++ b/app/controller/Auth.php @@ -0,0 +1,74 @@ +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'); + } +} diff --git a/app/controller/Crawler.php b/app/controller/Crawler.php index 6978354..52c5bc3 100644 --- a/app/controller/Crawler.php +++ b/app/controller/Crawler.php @@ -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]; /** * 显示爬虫工具首页 */ diff --git a/app/controller/User.php b/app/controller/User.php new file mode 100644 index 0000000..cbea179 --- /dev/null +++ b/app/controller/User.php @@ -0,0 +1,114 @@ + 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); + } +} diff --git a/app/middleware.php b/app/middleware.php index d2c3fda..19ac7ef 100644 --- a/app/middleware.php +++ b/app/middleware.php @@ -6,5 +6,5 @@ return [ // 多语言加载 // \think\middleware\LoadLangPack::class, // Session初始化 - // \think\middleware\SessionInit::class + \think\middleware\SessionInit::class, ]; diff --git a/app/middleware/Auth.php b/app/middleware/Auth.php new file mode 100644 index 0000000..d720be5 --- /dev/null +++ b/app/middleware/Auth.php @@ -0,0 +1,37 @@ +isAjax()) { + return json([ + 'code' => 0, + 'msg' => '请先登录', + ]); + } + // 否则跳转到登录页 + return redirect('/login'); + } + + return $next($request); + } +} diff --git a/app/service/UserService.php b/app/service/UserService.php new file mode 100644 index 0000000..e89acf9 --- /dev/null +++ b/app/service/UserService.php @@ -0,0 +1,189 @@ +getUsersFilePath(); + if (!file_exists($filePath)) { + $dir = dirname($filePath); + if (!is_dir($dir)) { + mkdir($dir, 0755, true); + } + file_put_contents($filePath, json_encode([], JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)); + } + } + + /** + * 获取账号数据文件路径 + * @return string + */ + private function getUsersFilePath(): string + { + // 获取项目根目录 + $rootPath = dirname(dirname(__DIR__)); + $runtimePath = $rootPath . '/runtime'; + if (!is_dir($runtimePath)) { + mkdir($runtimePath, 0755, true); + } + return $runtimePath . '/' . self::USERS_FILE; + } + + /** + * 读取所有账号 + * @return array + */ + public function getAllUsers(): array + { + $this->initUsersFile(); + $filePath = $this->getUsersFilePath(); + $content = file_get_contents($filePath); + $users = json_decode($content, true); + return is_array($users) ? $users : []; + } + + /** + * 保存账号数据 + * @param array $users + * @return bool + */ + private function saveUsers(array $users): bool + { + $this->initUsersFile(); + $filePath = $this->getUsersFilePath(); + return file_put_contents($filePath, json_encode($users, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE)) !== false; + } + + /** + * 验证登录(包括管理员和普通用户) + * @param string $username + * @param string $password + * @return array|false 返回用户信息或false + */ + public function verifyLogin(string $username, string $password) + { + // 验证管理员账号 + if ($username === self::ADMIN_USERNAME && $password === self::ADMIN_PASSWORD) { + return [ + 'username' => $username, + 'is_admin' => true, + ]; + } + + // 验证普通用户账号 + $users = $this->getAllUsers(); + foreach ($users as $user) { + if (isset($user['username']) && $user['username'] === $username) { + if (isset($user['password']) && $user['password'] === $password) { + return [ + 'username' => $username, + 'is_admin' => false, + ]; + } + } + } + + return false; + } + + /** + * 添加账号 + * @param string $username + * @param string $password + * @return array ['code' => 1|0, 'msg' => string] + */ + public function addUser(string $username, string $password): array + { + if (empty($username) || empty($password)) { + return ['code' => 0, 'msg' => '用户名和密码不能为空']; + } + + if ($username === self::ADMIN_USERNAME) { + return ['code' => 0, 'msg' => '不能添加管理员账号']; + } + + $users = $this->getAllUsers(); + + // 检查用户名是否已存在 + foreach ($users as $user) { + if (isset($user['username']) && $user['username'] === $username) { + return ['code' => 0, 'msg' => '用户名已存在']; + } + } + + // 添加新用户 + $users[] = [ + 'username' => $username, + 'password' => $password, + 'created_at' => date('Y-m-d H:i:s'), + ]; + + if ($this->saveUsers($users)) { + return ['code' => 1, 'msg' => '添加成功']; + } else { + return ['code' => 0, 'msg' => '保存失败']; + } + } + + /** + * 删除账号 + * @param string $username + * @return array ['code' => 1|0, 'msg' => string] + */ + public function deleteUser(string $username): array + { + if ($username === self::ADMIN_USERNAME) { + return ['code' => 0, 'msg' => '不能删除管理员账号']; + } + + $users = $this->getAllUsers(); + $newUsers = []; + + foreach ($users as $user) { + if (isset($user['username']) && $user['username'] !== $username) { + $newUsers[] = $user; + } + } + + if (count($newUsers) === count($users)) { + return ['code' => 0, 'msg' => '用户不存在']; + } + + if ($this->saveUsers($newUsers)) { + return ['code' => 1, 'msg' => '删除成功']; + } else { + return ['code' => 0, 'msg' => '保存失败']; + } + } + + /** + * 检查是否为管理员 + * @param string $username + * @return bool + */ + public function isAdmin(string $username): bool + { + return $username === self::ADMIN_USERNAME; + } +} diff --git a/route/app.php b/route/app.php index e840dc7..2bcf56e 100644 --- a/route/app.php +++ b/route/app.php @@ -16,10 +16,21 @@ Route::get('think', function () { Route::get('hello/:name', 'index/hello'); -// 爬虫工具路由 +// 认证路由(不需要登录) +Route::get('login', 'auth/login'); +Route::post('auth/doLogin', 'auth/doLogin'); +Route::get('auth/logout', 'auth/logout'); + +// 爬虫工具路由(需要登录) Route::get('crawler', 'crawler/index'); Route::post('crawler/getDsdmOptions', 'crawler/getDsdmOptions'); Route::post('crawler/getZwdmList', 'crawler/getZwdmList'); Route::post('crawler/getPositionInfo', 'crawler/getPositionInfo'); Route::post('crawler/batchGetPositionInfo', 'crawler/batchGetPositionInfo'); -Route::post('crawler/fetchAllPositions', 'crawler/fetchAllPositions'); \ No newline at end of file +Route::post('crawler/fetchAllPositions', 'crawler/fetchAllPositions'); + +// 用户管理路由(需要登录且为管理员) +Route::get('user', 'user/index'); +Route::get('user/getUsers', 'user/getUsers'); +Route::post('user/add', 'user/add'); +Route::post('user/delete', 'user/delete'); \ No newline at end of file diff --git a/view/crawler/index.html b/view/crawler/index.html index a4ba67c..d59853a 100644 --- a/view/crawler/index.html +++ b/view/crawler/index.html @@ -237,7 +237,13 @@
-

职位信息爬虫工具

+
+

职位信息爬虫工具

+
+ 账号管理 + 退出登录 +
+
@@ -323,6 +329,9 @@
+ + diff --git a/view/user/index.html b/view/user/index.html new file mode 100644 index 0000000..391caec --- /dev/null +++ b/view/user/index.html @@ -0,0 +1,338 @@ + + + + + + 账号管理 - 职位信息爬虫工具 + + + +
+
+

账号管理

+ +
+ + +
+

添加账号

+
+
+ + +
+
+ + +
+ +
+ + +
+

账号列表

+
+ + + + + + + + + + + + + +
用户名创建时间操作
加载中...
+
+
+ + + +