diff --git a/app/controller/Admin.php b/app/controller/Admin.php index fe6c6ed..5e47098 100644 --- a/app/controller/Admin.php +++ b/app/controller/Admin.php @@ -26,7 +26,7 @@ class Admin extends BaseController // 对于API方法,直接检查权限并设置响应头 $action = $this->request->action(); - $apiActions = ['getUsers', 'getBaseUrl', 'addUser', 'deleteUser', 'setBaseUrl']; + $apiActions = ['getUsers', 'getBaseUrl', 'addUser', 'deleteUser', 'setBaseUrl', 'resetUserPassword']; if (in_array($action, $apiActions)) { // 检查登录状态 @@ -178,4 +178,20 @@ class Admin extends BaseController return json($result); } + + /** + * 重置普通用户密码(管理员) + */ + public function resetUserPassword() + { + header('Content-Type: application/json; charset=utf-8'); + + $username = $this->request->param('username', ''); + $newPassword = $this->request->param('new_password', ''); + + $service = new UserService(); + $result = $service->resetUserPassword($username, $newPassword); + return json($result); + } + } diff --git a/app/controller/Crawler.php b/app/controller/Crawler.php index 4299a66..ba34d08 100644 --- a/app/controller/Crawler.php +++ b/app/controller/Crawler.php @@ -7,6 +7,7 @@ use app\BaseController; use app\middleware\Auth; use app\service\CrawlerService; use app\service\UserConfigService; +use app\service\UserService; use think\facade\Session; use think\facade\View; @@ -87,6 +88,32 @@ class Crawler extends BaseController } } + /** + * 用户修改密码 + */ + public function changePassword() + { + try { + $username = Session::get('username', ''); + if (empty($username)) { + return json(['code' => 0, 'msg' => '未登录']); + } + + $old = $this->request->param('old_password', ''); + $new = $this->request->param('new_password', ''); + + if (empty($old) || empty($new)) { + return json(['code' => 0, 'msg' => '旧密码和新密码不能为空']); + } + + $service = new UserService(); + $result = $service->changeUserPassword($username, $old, $new); + return json($result); + } catch (\Exception $e) { + return json(['code' => 0, 'msg' => '修改失败: ' . $e->getMessage()]); + } + } + /** * 保存用户配置 */ diff --git a/app/service/UserService.php b/app/service/UserService.php index 85b291e..1f8de88 100644 --- a/app/service/UserService.php +++ b/app/service/UserService.php @@ -5,6 +5,7 @@ namespace app\service; use app\model\User; use think\facade\Db; +use app\service\ConfigService; /** * 用户服务类 @@ -16,7 +17,30 @@ class UserService * 管理员账号(固定) */ private const ADMIN_USERNAME = 'admin'; - private const ADMIN_PASSWORD = '123456'; + private const ADMIN_PASSWORD_DEFAULT = '123456'; + private const ADMIN_PASSWORD_KEY = 'ADMIN_PASSWORD'; + + /** + * 获取管理员密码(优先读取配置,未设置则使用默认值) + */ + private function getAdminPassword(): string + { + $configService = new ConfigService(); + $password = $configService->getConfig(self::ADMIN_PASSWORD_KEY, self::ADMIN_PASSWORD_DEFAULT); + return $password === '' ? self::ADMIN_PASSWORD_DEFAULT : $password; + } + + /** + * 设置管理员密码 + */ + private function setAdminPassword(string $newPassword): array + { + if (empty($newPassword)) { + return ['code' => 0, 'msg' => '新密码不能为空']; + } + $configService = new ConfigService(); + return $configService->setConfig(self::ADMIN_PASSWORD_KEY, $newPassword); + } /** * 初始化数据库表 @@ -89,7 +113,7 @@ class UserService public function verifyLogin(string $username, string $password) { // 验证管理员账号 - if ($username === self::ADMIN_USERNAME && $password === self::ADMIN_PASSWORD) { + if ($username === self::ADMIN_USERNAME && $password === $this->getAdminPassword()) { return [ 'username' => $username, 'is_admin' => true, @@ -190,4 +214,71 @@ class UserService { return $username === self::ADMIN_USERNAME; } + + /** + * 用户自助修改密码(需验证旧密码) + */ + public function changeUserPassword(string $username, string $oldPassword, string $newPassword): array + { + if (empty($newPassword)) { + return ['code' => 0, 'msg' => '新密码不能为空']; + } + + // 管理员走配置 + if ($username === self::ADMIN_USERNAME) { + $current = $this->getAdminPassword(); + if ($oldPassword !== $current) { + return ['code' => 0, 'msg' => '旧密码错误']; + } + return $this->setAdminPassword($newPassword); + } + + $this->initDatabase(); + $user = User::where('username', $username)->find(); + if (!$user) { + return ['code' => 0, 'msg' => '用户不存在']; + } + if ($user->password !== $oldPassword) { + return ['code' => 0, 'msg' => '旧密码错误']; + } + $user->password = $newPassword; + $user->save(); + return ['code' => 1, 'msg' => '密码修改成功']; + } + + /** + * 管理员重置普通用户密码(无需旧密码) + */ + public function resetUserPassword(string $username, string $newPassword): array + { + if ($username === self::ADMIN_USERNAME) { + return ['code' => 0, 'msg' => '不能在此重置管理员密码']; + } + if (empty($username) || empty($newPassword)) { + return ['code' => 0, 'msg' => '用户名和新密码不能为空']; + } + $this->initDatabase(); + $user = User::where('username', $username)->find(); + if (!$user) { + return ['code' => 0, 'msg' => '用户不存在']; + } + $user->password = $newPassword; + $user->save(); + return ['code' => 1, 'msg' => '重置成功']; + } + + /** + * 管理员修改管理员密码(需要旧密码) + */ + public function changeAdminPassword(string $oldPassword, string $newPassword): array + { + if (empty($newPassword)) { + return ['code' => 0, 'msg' => '新密码不能为空']; + } + $current = $this->getAdminPassword(); + if ($oldPassword !== $current) { + return ['code' => 0, 'msg' => '旧密码错误']; + } + return $this->setAdminPassword($newPassword); + } } diff --git a/route/app.php b/route/app.php index d02f8cb..224bb19 100644 --- a/route/app.php +++ b/route/app.php @@ -25,6 +25,7 @@ Route::get('auth/logout', 'auth/logout'); // 注意:API路由必须在index路由之前定义,确保优先匹配 Route::get('crawler/getUserConfig', 'crawler/getUserConfig'); Route::post('crawler/saveUserConfig', 'crawler/saveUserConfig'); +Route::post('crawler/changePassword', 'crawler/changePassword'); Route::post('crawler/getDsdmOptions', 'crawler/getDsdmOptions'); Route::post('crawler/getZwdmList', 'crawler/getZwdmList'); Route::post('crawler/getPositionInfo', 'crawler/getPositionInfo'); @@ -39,4 +40,5 @@ Route::get('admin/getBaseUrl', 'admin/getBaseUrl'); Route::post('admin/addUser', 'admin/addUser'); Route::post('admin/deleteUser', 'admin/deleteUser'); Route::post('admin/setBaseUrl', 'admin/setBaseUrl'); +Route::post('admin/resetUserPassword', 'admin/resetUserPassword'); Route::get('admin', 'admin/index'); \ No newline at end of file diff --git a/view/admin/index.html b/view/admin/index.html index 47f56a5..662d7ba 100644 --- a/view/admin/index.html +++ b/view/admin/index.html @@ -181,24 +181,12 @@ - -
| ${user.username || ''} | ${user.created_at || '-'} | + | `; @@ -334,13 +323,22 @@ } } - // 添加账号 - function addUser() { - const username = document.getElementById('new-username').value.trim(); - const password = document.getElementById('new-password').value.trim(); + // 打开添加账号弹窗 + function openAddUserModal() { + document.getElementById('add-modal-username').value = ''; + document.getElementById('add-modal-password').value = ''; + document.getElementById('add-modal-message').innerHTML = ''; + document.getElementById('add-user-modal').style.display = 'flex'; + } + function closeAddUserModal() { + document.getElementById('add-user-modal').style.display = 'none'; + } + function submitAddUserModal() { + const username = document.getElementById('add-modal-username').value.trim(); + const password = document.getElementById('add-modal-password').value.trim(); if (!username || !password) { - showMessage('add-message', '请输入用户名和密码', 'error'); + showMessage('add-modal-message', '请输入用户名和密码', 'error'); return; } @@ -355,19 +353,67 @@ .then(response => response.json()) .then(data => { if (data.code === 1) { - showMessage('add-message', data.msg || '添加成功', 'success'); - document.getElementById('new-username').value = ''; - document.getElementById('new-password').value = ''; - loadUsers(); + showMessage('add-modal-message', data.msg || '添加成功', 'success'); + setTimeout(() => { + closeAddUserModal(); + loadUsers(); + }, 600); } else { - showMessage('add-message', data.msg || '添加失败', 'error'); + showMessage('add-modal-message', data.msg || '添加失败', 'error'); } }) .catch(error => { - showMessage('add-message', '请求失败: ' + error.message, 'error'); + showMessage('add-modal-message', '请求失败: ' + error.message, 'error'); }); } + // 打开用户编辑弹窗(用户名+密码) + function openUserModal(username) { + document.getElementById('user-modal-username').value = username || ''; + document.getElementById('user-modal-password').value = ''; + document.getElementById('user-modal-message').innerHTML = ''; + document.getElementById('user-modal').style.display = 'flex'; + } + + function closeUserModal() { + document.getElementById('user-modal').style.display = 'none'; + } + + function submitUserModal() { + const username = document.getElementById('user-modal-username').value.trim(); + const password = document.getElementById('user-modal-password').value.trim(); + + if (!username || !password) { + showMessage('user-modal-message', '请输入用户名和新密码', 'error'); + return; + } + + fetch(API_BASE_URL + '/admin/resetUserPassword', { + method: 'POST', + headers: { + 'Content-Type': 'application/x-www-form-urlencoded', + 'X-Requested-With': 'XMLHttpRequest', + }, + body: `username=${encodeURIComponent(username)}&new_password=${encodeURIComponent(password)}` + }) + .then(response => response.json()) + .then(data => { + if (data.code === 1) { + showMessage('user-modal-message', data.msg || '修改成功', 'success'); + setTimeout(() => { + closeUserModal(); + loadUsers(); + }, 600); + } else { + showMessage('user-modal-message', data.msg || '修改失败', 'error'); + } + }) + .catch(error => { + showMessage('user-modal-message', '请求失败: ' + error.message, 'error'); + }); + } + + // 删除账号 function deleteUser(username) { if (!confirm('确定要删除账号 "' + username + '" 吗?')) { @@ -402,5 +448,46 @@ container.innerHTML = ``; } + + + + + + +