refactor(forgot_pw): bootstrap5

This commit is contained in:
Baoshuo Ren 2023-02-01 20:02:32 +08:00
parent 88e8f1de62
commit f2f4ae9b4e
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A
3 changed files with 47 additions and 31 deletions

View File

@ -1,37 +1,38 @@
<?php <?php
requireLib('bootstrap5');
requirePHPLib('form'); requirePHPLib('form');
use Gregwar\Captcha\PhraseBuilder; use Gregwar\Captcha\PhraseBuilder;
$forgot_form = new UOJBs4Form('forgot'); $forgot_form = new UOJForm('forgot');
$forgot_form->addInput( $forgot_form->addInput('username', [
'username', 'div_class' => '',
'text', 'label' => '用户名',
'用户名', 'validator_php' => function ($username, &$vdata) {
'',
function ($username, &$vdata) {
if (!validateUsername($username)) { if (!validateUsername($username)) {
return '用户名不合法'; return '用户名不合法';
} }
$vdata['user'] = UOJUser::query($username); $vdata['user'] = UOJUser::query($username);
if (!$vdata['user']) { if (!$vdata['user']) {
return '该用户不存在'; return '该用户不存在';
} }
return ''; return '';
}, },
null ]);
); $enter_verification_code_text = UOJLocale::get('enter verification code');
$forgot_form->appendHTML(<<<EOD $forgot_form->appendHTML(<<<EOD
<div id="div-captcha" class="form-group"> <div id="div-captcha" class="form-group mt-3">
<label for="input-captcha" class="col-sm-2 control-label">验证码</label> <label for="input-captcha" class="col-sm-2 control-label">验证码</label>
<div class="col-sm-3" style="max-width: 60%"> <div class="input-group">
<input type="text" class="form-control" id="input-captcha" name="captcha" placeholder="请输入验证码" maxlength="20" style="display: inline-block; width: 12em;" /> <input type="text" class="form-control" id="input-captcha" name="captcha" placeholder="{$enter_verification_code_text}" maxlength="20" />
<div style="display: inline-block; margin-left: 8px; position: relative; top: -2px; cursor: pointer;"> <span class="input-group-text p-0">
<img id="captcha" src="" /> <img id="captcha" class="col w-100 h-100" src="/captcha">
</span>
</div> </div>
<span class="help-block" id="help-captcha" style="display: block"></span>
</div> </div>
</div>
EOD); EOD);
$forgot_form->handle = function (&$vdata) { $forgot_form->handle = function (&$vdata) {
$user = $vdata['user']; $user = $vdata['user'];
@ -53,7 +54,7 @@ $forgot_form->handle = function (&$vdata) {
$oj_name_short = UOJConfig::$data['profile']['oj-name-short']; $oj_name_short = UOJConfig::$data['profile']['oj-name-short'];
$check_code = md5($user['username'] . "+" . $password . '+' . UOJTime::$time_now_str); $check_code = md5($user['username'] . "+" . $password . '+' . UOJTime::$time_now_str);
$sufs = base64url_encode($user['username'] . "." . $check_code); $sufs = base64url_encode($user['username'] . "." . $check_code);
$url = HTML::url("/reset-password", ['params' => ['p' => $sufs]]); $url = HTML::url("/reset_password", ['params' => ['p' => $sufs]]);
$oj_url = HTML::url('/'); $oj_url = HTML::url('/');
$name = $user['username']; $name = $user['username'];
$remote_addr = UOJContext::remoteAddr(); $remote_addr = UOJContext::remoteAddr();
@ -85,9 +86,19 @@ EOD;
$mailer->addAddress($user['email'], $user['username']); $mailer->addAddress($user['email'], $user['username']);
$mailer->Subject = $oj_name_short . " 密码找回"; $mailer->Subject = $oj_name_short . " 密码找回";
$mailer->msgHTML($html); $mailer->msgHTML($html);
if (!$mailer->send()) {
error_log($mailer->ErrorInfo); $res = retry_loop(function () use (&$mailer) {
becomeMsgPage('<div class="text-center"><h2>邮件发送失败,请重试!</h2></div>'); $res = $mailer->send();
if ($res) return true;
UOJLog::error($mailer->ErrorInfo);
return false;
});
if (!$res) {
becomeMsgPage('<div class="text-center"><h2>邮件发送失败,请重试!</h2><a href="">返回</a></div>');
} else { } else {
DB::update([ DB::update([
"update user_info", "update user_info",
@ -102,13 +113,19 @@ EOD;
becomeMsgPage('<div class="text-center"><h2>邮件发送成功,请检查收件箱!</h2><span>如果邮件未出现在收件箱中,请检查垃圾箱。</span></div>'); becomeMsgPage('<div class="text-center"><h2>邮件发送成功,请检查收件箱!</h2><span>如果邮件未出现在收件箱中,请检查垃圾箱。</span></div>');
} }
}; };
$forgot_form->submit_button_config['align'] = 'offset';
$forgot_form->runAtServer(); $forgot_form->runAtServer();
?> ?>
<?php echoUOJPageHeader('找回密码') ?> <?php echoUOJPageHeader('找回密码') ?>
<h2 class="page-header">找回密码</h2>
<h4>请输入需要找回密码的用户名:</h4> <div class="card mw-100 mx-auto" style="width:600px">
<?php $forgot_form->printHTML(); ?> <div class="card-body">
<h1 class="text-center mb-3">找回密码</h1>
<?php $forgot_form->printHTML() ?>
</div>
</div>
<script> <script>
function refreshCaptcha() { function refreshCaptcha() {
var timestamp = new Date().getTime(); var timestamp = new Date().getTime();
@ -116,11 +133,10 @@ $forgot_form->runAtServer();
} }
$(document).ready(function() { $(document).ready(function() {
refreshCaptcha();
$("#captcha").click(function(e) { $("#captcha").click(function(e) {
refreshCaptcha(); refreshCaptcha();
}); });
}); });
</script> </script>
<?php echoUOJPageFooter() ?> <?php echoUOJPageFooter() ?>

View File

@ -136,7 +136,7 @@ if (isset($_POST['login'])) {
if (msg == 'ok') { if (msg == 'ok') {
var prevUrl = document.referrer; var prevUrl = document.referrer;
if (prevUrl == '' || /.*\/login.*/.test(prevUrl) || /.*\/logout.*/.test(prevUrl) || /.*\/register.*/.test(prevUrl) || /.*\/reset-password.*/.test(prevUrl)) { if (prevUrl == '' || /.*\/login.*/.test(prevUrl) || /.*\/logout.*/.test(prevUrl) || /.*\/register.*/.test(prevUrl) || /.*\/reset_password.*/.test(prevUrl)) {
prevUrl = '/'; prevUrl = '/';
}; };
window.location.href = prevUrl; window.location.href = prevUrl;
@ -165,7 +165,7 @@ if (isset($_POST['login'])) {
$('#div-username > .form-floating, #input-username').addClass('is-invalid'); $('#div-username > .form-floating, #input-username').addClass('is-invalid');
$('#div-password').addClass('has-validation'); $('#div-password').addClass('has-validation');
$('#div-password > .form-floating, #input-password').addClass('is-invalid'); $('#div-password > .form-floating, #input-password').addClass('is-invalid');
$('#help-password').html('用户名或密码错误。<a href="/forgot-password">忘记密码?</a>'); $('#help-password').html('用户名或密码错误。<a href="/forgot_password">忘记密码?</a>');
refreshCaptcha(); refreshCaptcha();
} }
}); });

View File

@ -80,8 +80,8 @@ Route::group(
Route::any('/login', '/login.php'); Route::any('/login', '/login.php');
Route::any('/logout', '/logout.php'); Route::any('/logout', '/logout.php');
Route::any('/register', '/register.php'); Route::any('/register', '/register.php');
Route::any('/forgot-password', '/forgot_pw.php'); Route::any('/forgot_password', '/forgot_pw.php');
Route::any('/reset-password', '/reset_pw.php'); Route::any('/reset_password', '/reset_pw.php');
Route::any('/user/{username}', '/user_info.php'); Route::any('/user/{username}', '/user_info.php');
Route::any('/user/{username}/edit(?:/{tab})?', '/user_info_edit.php'); Route::any('/user/{username}/edit(?:/{tab})?', '/user_info_edit.php');