账号问题新增

This commit is contained in:
杨志
2026-01-21 11:44:35 +08:00
parent bc622c6c34
commit 05d383e010
6 changed files with 368 additions and 33 deletions

View File

@@ -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);
}
}

View File

@@ -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()]);
}
}
/**
* 保存用户配置
*/

View File

@@ -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);
}
}