账号问题新增

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

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