账号问题新增
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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()]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存用户配置
|
||||
*/
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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');
|
||||
@@ -181,24 +181,12 @@
|
||||
<button class="btn" onclick="saveBaseUrl()">保存配置</button>
|
||||
</div>
|
||||
|
||||
<!-- 添加账号 -->
|
||||
<div class="form-section">
|
||||
<h2>添加账号</h2>
|
||||
<div id="add-message"></div>
|
||||
<div class="form-group">
|
||||
<label for="new-username">用户名:</label>
|
||||
<input type="text" id="new-username" placeholder="请输入用户名">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="new-password">密码:</label>
|
||||
<input type="password" id="new-password" placeholder="请输入密码">
|
||||
</div>
|
||||
<button class="btn" onclick="addUser()">添加账号</button>
|
||||
</div>
|
||||
|
||||
<!-- 账号列表 -->
|
||||
<div class="form-section">
|
||||
<h2>账号列表</h2>
|
||||
<div style="display: flex; justify-content: space-between; align-items: center;">
|
||||
<h2 style="margin: 0;">账号列表</h2>
|
||||
<button class="btn" style="background:#2196F3;" onclick="openAddUserModal()">添加账号</button>
|
||||
</div>
|
||||
<div id="list-message"></div>
|
||||
<table id="users-table">
|
||||
<thead>
|
||||
@@ -326,6 +314,7 @@
|
||||
<td>${user.username || ''}</td>
|
||||
<td>${user.created_at || '-'}</td>
|
||||
<td>
|
||||
<button class="btn" style="background:#2196F3; margin-right:8px;" onclick="openUserModal('${user.username}')">修改</button>
|
||||
<button class="btn btn-danger" onclick="deleteUser('${user.username}')">删除</button>
|
||||
</td>
|
||||
`;
|
||||
@@ -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 = `<div class="message ${type}">${message}</div>`;
|
||||
}
|
||||
</script>
|
||||
|
||||
<!-- 添加账号弹窗 -->
|
||||
<div class="modal-overlay" id="add-user-modal" style="display:none; align-items:center; justify-content:center; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.45); z-index:999;">
|
||||
<div class="modal" style="background:#fff; padding:20px; border-radius:8px; width:360px; max-width:90%; box-shadow:0 4px 12px rgba(0,0,0,0.2);">
|
||||
<h3>添加账号</h3>
|
||||
<div id="add-modal-message"></div>
|
||||
<div class="form-group">
|
||||
<label for="add-modal-username">用户名:</label>
|
||||
<input type="text" id="add-modal-username" placeholder="请输入用户名">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="add-modal-password">密码:</label>
|
||||
<input type="password" id="add-modal-password" placeholder="请输入密码">
|
||||
</div>
|
||||
<div class="modal-actions">
|
||||
<button class="btn btn-secondary" style="background:#9e9e9e;" onclick="closeAddUserModal()">取消</button>
|
||||
<button class="btn" onclick="submitAddUserModal()">保存</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 用户编辑弹窗(用户名+密码) -->
|
||||
<div class="modal-overlay" id="user-modal" style="display:none; align-items:center; justify-content:center; position:fixed; top:0; left:0; width:100%; height:100%; background:rgba(0,0,0,0.45); z-index:999;">
|
||||
<div class="modal" style="background:#fff; padding:20px; border-radius:8px; width:360px; max-width:90%; box-shadow:0 4px 12px rgba(0,0,0,0.2);">
|
||||
<h3>修改用户</h3>
|
||||
<div id="user-modal-message"></div>
|
||||
<div class="form-group">
|
||||
<label for="user-modal-username">用户名:</label>
|
||||
<input type="text" id="user-modal-username" placeholder="请输入用户名">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="user-modal-password">新密码:</label>
|
||||
<input type="password" id="user-modal-password" placeholder="请输入新密码">
|
||||
</div>
|
||||
<div class="modal-actions">
|
||||
<button class="btn btn-secondary" style="background:#9e9e9e;" onclick="closeUserModal()">取消</button>
|
||||
<button class="btn" onclick="submitUserModal()">保存</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -29,6 +29,42 @@
|
||||
padding: 30px;
|
||||
}
|
||||
|
||||
/* 弹窗样式 */
|
||||
.modal-overlay {
|
||||
position: fixed;
|
||||
top: 0;
|
||||
left: 0;
|
||||
width: 100%;
|
||||
height: 100%;
|
||||
background: rgba(0,0,0,0.45);
|
||||
display: none;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
z-index: 999;
|
||||
}
|
||||
|
||||
.modal {
|
||||
background: #fff;
|
||||
padding: 20px;
|
||||
border-radius: 8px;
|
||||
box-shadow: 0 4px 12px rgba(0,0,0,0.2);
|
||||
width: 360px;
|
||||
max-width: 90%;
|
||||
}
|
||||
|
||||
.modal h3 {
|
||||
margin-bottom: 12px;
|
||||
font-size: 18px;
|
||||
color: #333;
|
||||
}
|
||||
|
||||
.modal-actions {
|
||||
display: flex;
|
||||
justify-content: flex-end;
|
||||
gap: 10px;
|
||||
margin-top: 15px;
|
||||
}
|
||||
|
||||
h1 {
|
||||
color: #333;
|
||||
margin-bottom: 30px;
|
||||
@@ -269,8 +305,9 @@
|
||||
<div class="container">
|
||||
<div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
|
||||
<h1 style="margin: 0;">职位信息爬虫工具</h1>
|
||||
<div style="display: flex; gap: 15px;">
|
||||
<a href="/auth/logout" style="color: #f44336; text-decoration: none; padding: 8px 15px; border-radius: 4px; background: #ffebee;">退出登录</a>
|
||||
<div style="display: flex; gap: 10px;">
|
||||
<button class="btn btn-secondary" style="background:#2196F3;" onclick="openPwdModal()">修改密码</button>
|
||||
<a href="/auth/logout" style="color: #f44336; text-decoration: none; padding: 8px 15px; border-radius: 4px; background: #ffebee;">退出登录</a>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -454,7 +491,7 @@
|
||||
showMessage('config-message', '请求失败: ' + error.message, 'error');
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// 获取地区选项
|
||||
function getDsdmOptions() {
|
||||
const examid = document.getElementById('examid').value.trim();
|
||||
@@ -934,6 +971,81 @@
|
||||
const container = document.getElementById(containerId);
|
||||
container.innerHTML = `<div class="message ${type}">${message}</div>`;
|
||||
}
|
||||
|
||||
// 弹窗:打开、关闭
|
||||
function openPwdModal() {
|
||||
document.getElementById('pwd-modal').style.display = 'flex';
|
||||
}
|
||||
function closePwdModal() {
|
||||
document.getElementById('pwd-modal').style.display = 'none';
|
||||
document.getElementById('modal-old-password').value = '';
|
||||
document.getElementById('modal-new-password').value = '';
|
||||
document.getElementById('modal-new-password-confirm').value = '';
|
||||
document.getElementById('modal-pwd-message').innerHTML = '';
|
||||
}
|
||||
|
||||
// 弹窗内修改密码
|
||||
function changePasswordModal() {
|
||||
const oldPassword = document.getElementById('modal-old-password').value.trim();
|
||||
const newPassword = document.getElementById('modal-new-password').value.trim();
|
||||
const newPasswordConfirm = document.getElementById('modal-new-password-confirm').value.trim();
|
||||
|
||||
if (!oldPassword || !newPassword || !newPasswordConfirm) {
|
||||
showMessage('modal-pwd-message', '请填写完整的旧密码和新密码', 'error');
|
||||
return;
|
||||
}
|
||||
if (newPassword !== newPasswordConfirm) {
|
||||
showMessage('modal-pwd-message', '两次输入的新密码不一致', 'error');
|
||||
return;
|
||||
}
|
||||
|
||||
showMessage('modal-pwd-message', '正在修改密码...', 'info');
|
||||
fetch(API_BASE_URL + '/crawler/changePassword', {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
},
|
||||
body: `old_password=${encodeURIComponent(oldPassword)}&new_password=${encodeURIComponent(newPassword)}`
|
||||
})
|
||||
.then(response => response.json())
|
||||
.then(data => {
|
||||
if (data.code === 1) {
|
||||
showMessage('modal-pwd-message', data.msg || '密码修改成功', 'success');
|
||||
setTimeout(() => {
|
||||
closePwdModal();
|
||||
}, 600);
|
||||
} else {
|
||||
showMessage('modal-pwd-message', data.msg || '密码修改失败', 'error');
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
showMessage('modal-pwd-message', '请求失败: ' + error.message, 'error');
|
||||
});
|
||||
}
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
<!-- 密码修改弹窗 -->
|
||||
<div class="modal-overlay" id="pwd-modal">
|
||||
<div class="modal">
|
||||
<h3>修改密码</h3>
|
||||
<div id="modal-pwd-message"></div>
|
||||
<div class="form-group">
|
||||
<label for="modal-old-password">旧密码:</label>
|
||||
<input type="password" id="modal-old-password" placeholder="请输入旧密码">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="modal-new-password">新密码:</label>
|
||||
<input type="password" id="modal-new-password" placeholder="请输入新密码">
|
||||
</div>
|
||||
<div class="form-group">
|
||||
<label for="modal-new-password-confirm">确认新密码:</label>
|
||||
<input type="password" id="modal-new-password-confirm" placeholder="请再次输入新密码">
|
||||
</div>
|
||||
<div class="modal-actions">
|
||||
<button class="btn btn-secondary" style="background:#9e9e9e;" onclick="closePwdModal()">取消</button>
|
||||
<button class="btn" onclick="changePasswordModal()">确定修改</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
Reference in New Issue
Block a user