更新
This commit is contained in:
@@ -21,6 +21,9 @@ use app\common\service\FileService;
|
||||
use think\facade\Config;
|
||||
use think\facade\Cache;
|
||||
use think\facade\Log;
|
||||
use think\facade\Db;
|
||||
use app\adminapi\service\AdminAccessSyncService;
|
||||
use app\common\service\JsonService;
|
||||
|
||||
/**
|
||||
* 登录逻辑
|
||||
@@ -118,8 +121,17 @@ class LoginLogic extends BaseLogic
|
||||
*/
|
||||
public function login($params)
|
||||
{
|
||||
return Db::transaction(static function () use ($params) {
|
||||
$time = time();
|
||||
$admin = Admin::where('account', '=', $params['account'])->find();
|
||||
$admin = Admin::where('account', '=', $params['account'])->lock(true)->findOrEmpty();
|
||||
// 原网页调试开关只在原入口双门禁同时满足时保留;集团接口在外层始终复核真实密码。
|
||||
$debug = in_array(strtolower((string) env('app_debug', '0')), ['1', 'true', 'yes', 'on'], true)
|
||||
&& in_array(strtolower((string) env('local_debug.login_skip_password', '')), ['1', 'true', 'yes', 'on'], true);
|
||||
if ($admin->isEmpty() || (int) $admin['disable'] !== 0 || (!$debug
|
||||
&& !hash_equals((string) $admin['password'], create_password((string) ($params['password'] ?? ''),
|
||||
(string) Config::get('project.unique_identification'))))) {
|
||||
JsonService::throw('账号或密码错误,或账号已停用', [], -1);
|
||||
}
|
||||
|
||||
//用户表登录信息更新
|
||||
$admin->login_time = $time;
|
||||
@@ -145,6 +157,7 @@ class LoginLogic extends BaseLogic
|
||||
]);
|
||||
|
||||
return $row;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -213,6 +226,11 @@ class LoginLogic extends BaseLogic
|
||||
return false;
|
||||
}
|
||||
|
||||
return Db::transaction(static function () use ($admin, $terminal, $wxUserId) {
|
||||
$admin = Admin::where('id', $admin->id)->where('work_wechat_userid', $wxUserId)->lock(true)->findOrEmpty();
|
||||
if ($admin->isEmpty() || (int) $admin['disable'] !== 0) {
|
||||
JsonService::throw('账号已停用或绑定已变更', [], -1);
|
||||
}
|
||||
// 更新登录信息
|
||||
$time = time();
|
||||
$admin->login_time = $time;
|
||||
@@ -234,6 +252,7 @@ class LoginLogic extends BaseLogic
|
||||
// 企微扫码登录即已绑定 userid
|
||||
'need_bind_work_wechat' => false,
|
||||
];
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -321,38 +340,31 @@ class LoginLogic extends BaseLogic
|
||||
public function changeFirstPassword($token, $password)
|
||||
{
|
||||
try {
|
||||
// 通过 token 获取管理员信息
|
||||
$adminSession = \app\common\model\auth\AdminSession::where('token', '=', $token)
|
||||
->where('expire_time', '>', time())
|
||||
->find();
|
||||
|
||||
if (!$adminSession) {
|
||||
self::setError('登录已过期,请重新登录');
|
||||
return false;
|
||||
if (!is_string($password) || strlen($password) < 6 || strlen($password) > 32 || str_contains($password, "\0")) {
|
||||
throw new \RuntimeException('密码长度须在6-32字节');
|
||||
}
|
||||
|
||||
$admin = Admin::find($adminSession->admin_id);
|
||||
if (!$admin) {
|
||||
self::setError('管理员不存在');
|
||||
return false;
|
||||
}
|
||||
|
||||
// 使用系统的密码加密方式
|
||||
$passwordSalt = Config::get('project.unique_identification');
|
||||
$encryptedPassword = create_password($password, $passwordSalt);
|
||||
|
||||
// 更新密码和 is_paw 标记
|
||||
$admin->password = $encryptedPassword;
|
||||
$admin->is_paw = 1;
|
||||
$admin->save();
|
||||
|
||||
// 使当前 token 过期,要求重新登录
|
||||
AdminTokenService::expireToken($token);
|
||||
|
||||
$adminId = 0;
|
||||
$tokens = Db::transaction(static function () use ($token, $password, &$adminId): array {
|
||||
$candidate = \app\common\model\auth\AdminSession::where('token', $token)->findOrEmpty();
|
||||
if ($candidate->isEmpty()) { throw new \RuntimeException('登录已过期,请重新登录'); }
|
||||
$adminId = (int) $candidate['admin_id'];
|
||||
$admin = Admin::where('id', $adminId)->lock(true)->findOrEmpty();
|
||||
$active = \app\common\model\auth\AdminSession::where('token', $token)->where('admin_id', $adminId)
|
||||
->where('expire_time', '>', time())->lock(true)->count();
|
||||
if ($admin->isEmpty() || (int) $admin['disable'] !== 0 || !$active) {
|
||||
throw new \RuntimeException('登录已过期,请重新登录');
|
||||
}
|
||||
if ((int) $admin['is_paw'] !== 0) { throw new \RuntimeException('首次密码已设置,请使用个人改密'); }
|
||||
$admin->password = create_password($password, (string) Config::get('project.unique_identification'));
|
||||
$admin->is_paw = 1;
|
||||
$admin->save();
|
||||
return AdminAccessSyncService::revoke([$adminId]);
|
||||
});
|
||||
AdminAccessSyncService::clearCommitted([$adminId], $tokens);
|
||||
return true;
|
||||
} catch (\Exception $e) {
|
||||
self::setError($e->getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user