where('user_id', (int)$uid)->lock(true)->find(); if ((int)$row[$lockedField] > time()) { throw new \RuntimeException('密码错误次数过多,请15分钟后重试'); } if ($kind === 'login') { $user = Db::name('user')->field('password,salt')->where('id', (int)$uid)->find(); $correct = $user && $user['password'] !== '' && hash_equals((string)$user['password'], md5(md5($password) . $user['salt'])); } elseif ($kind === 'payment') { if ($row['payment_password_hash'] === '') { throw new \RuntimeException('请先在账号安全中设置支付密码'); } $correct = password_verify($password, $row['payment_password_hash']); } $failures = $correct ? 0 : (int)$row[$attemptsField] + 1; Db::name('user_security')->where('user_id', (int)$uid)->update([ $attemptsField => $failures >= 5 ? 0 : $failures, $lockedField => $failures >= 5 ? time() + 900 : 0, ]); Db::commit(); } catch (\Throwable $e) { Db::rollback(); throw $e; } if (!$correct) { throw new \RuntimeException($failures >= 5 ? '密码错误次数过多,请15分钟后重试' : '密码不正确'); } return true; } }