Skip to content

Commit

Permalink
✨ 完善前台注册/找回密码
Browse files Browse the repository at this point in the history
  • Loading branch information
Licoy committed Nov 13, 2022
1 parent a007151 commit b0cd63e
Show file tree
Hide file tree
Showing 7 changed files with 259 additions and 38 deletions.
20 changes: 10 additions & 10 deletions assets/dist/libs.min.css

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion assets/dist/puock.min.js

Large diffs are not rendered by default.

46 changes: 32 additions & 14 deletions assets/js/puock.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,17 @@ class Puock {
const el = $(this.ct(e));
const target = $(el.attr("data-target"));
const self = $(el.attr("data-self"));
const modalTitle = el.attr("data-modal-title");
if (target.hasClass("d-none")) {
self.addClass("d-none");
target.removeClass("d-none");
} else {
self.removeClass("d-none");
target.addClass("d-none");
}
if (modalTitle) {
el.closest(".modal").find(".modal-title").text(modalTitle);
}
});
// form ajax submit
$(document).on("submit", ".ajax-form", (e) => {
Expand Down Expand Up @@ -150,18 +154,20 @@ class Puock {
setTimeout(() => {
switch (resData.action) {
case 'reload':
window.location.reload();
this.goUrl(window.location.href)
break
}
}, 500)
}
}
} else {
this.toast(res.msg || errorTip, TYPE_DANGER)
this.loadCommentCaptchaImage(form, true)
}
},
error: (e) => {
this.toast(`请求错误:${e.statusText}`, TYPE_DANGER)
this.loadCommentCaptchaImage(form, true)
}
})
})
Expand All @@ -175,9 +181,15 @@ class Puock {
}
}

loadCommentCaptchaImage(el) {
const url = el.attr("src") + '&t=' + (new Date()).getTime()
el.attr("src", url)
loadCommentCaptchaImage(el, parent = false) {
if (parent) {
el.find(".captcha").each((_, item) => {
this.loadCommentCaptchaImage($(item))
})
} else {
const url = el.attr("src") + '&t=' + (new Date()).getTime()
el.attr("src", url)
}
}

searchInit() {
Expand All @@ -199,15 +211,20 @@ class Puock {
toggle();
})
$(document).on("submit", ".global-search-form", (e) => {
if (this.data.params.is_pjax) {
const el = $(e.currentTarget)
InstantClick.go(el.attr("action") + "/?" + el.serialize())
return false;
}
return true;
e.preventDefault();
const el = $(this.ct(e));
this.goUrl(el.attr("action") + "/?" + el.serialize())
})
}

goUrl(url) {
if (this.data.params.is_pjax) {
InstantClick.go(url)
} else {
window.location.href = url
}
}

eventShareStart() {
$(document).on("click", ".share-to>div", (e) => {
const id = $(this.ct(e)).attr("data-id");
Expand Down Expand Up @@ -297,9 +314,10 @@ class Puock {
}
}

lazyLoadInit(el = '.lazyload') {
lazyLoadInit(parent = null, el = '.lazyload') {
if (window.LazyLoad !== undefined) {
new window.LazyLoad(document.querySelectorAll([el, "[data-lazy=true]"]), {
const elList = parent ? parent.find(el) : document.querySelectorAll([el, "[data-lazy=true]"]);
new window.LazyLoad(elList, {
root: null,
rootMargin: "0px",
threshold: 0
Expand Down Expand Up @@ -855,6 +873,7 @@ class Puock {
const bodyEl = target.find(".modal-body")
$.get(url, (res) => {
bodyEl.html(res);
this.lazyLoadInit(bodyEl);
}).error((e) => {
console.error(e)
bodyEl.text("加载失败:" + (e.responseText || e.message || '未知错误'));
Expand Down Expand Up @@ -988,8 +1007,7 @@ class Puock {
$(document).on("click", ".swiper-slide a", (e) => {
if (this.data.params.is_pjax) {
e.preventDefault();
console.log(e.currentTarget.href)
InstantClick.go(e.currentTarget.href)
this.goUrl(e.currentTarget.href)
}
});
}
Expand Down
210 changes: 200 additions & 10 deletions inc/ajax/page-front-login.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,24 @@ function pk_front_login_exec()
'username' => ['name' => '用户名', 'required' => true],
'password' => ['name' => '密码', 'required' => true],
'vd' => ['name' => '验证码', 'required' => true],
'remember' => ['name' => '记住我', 'required' => false],
])) === true) {
echo pk_ajax_resp_error($data);
wp_die();
}
if (!pk_captcha_validate('login', $data['vd'])) {
echo pk_ajax_resp_error('验证码错误');
wp_die();
}
$user = wp_signon([
'user_login' => $data['username'],
'user_password' => $data['password'],
'remember' => true
],is_ssl());
'remember' => $data['remember'] === 'on',
], is_ssl());
if ($user instanceof WP_User) {
wp_set_auth_cookie($user->ID, true, is_ssl());
echo pk_ajax_resp([
'action' => 'reload'
'action' => 'reload',
], '登录成功');
} else {
echo pk_ajax_resp_error('账号或密码错误');
Expand All @@ -28,13 +33,126 @@ function pk_front_login_exec()

pk_ajax_register('pk_front_login_exec', 'pk_front_login_exec', true);

function pk_front_register_exec()
{
if (is_string($data = pk_get_req_data([
'username' => ['name' => '用户名', 'required' => true],
'email' => ['email' => '邮箱', 'required' => true],
'password' => ['name' => '密码', 'required' => true],
'vd' => ['name' => '验证码', 'required' => true],
])) === true) {
echo pk_ajax_resp_error($data);
wp_die();
}
if (strlen($data['username']) < 5 || strlen($data['username']) > 10) {
echo pk_ajax_resp_error('用户名不合法');
wp_die();
}
if (strlen($data['password']) < 6 || strlen($data['password']) > 18) {
echo pk_ajax_resp_error('密码不合法');
wp_die();
}
if (!is_email($data['email'])) {
echo pk_ajax_resp_error('邮箱不合法');
wp_die();
}
if (!pk_captcha_validate('register', $data['vd'])) {
echo pk_ajax_resp_error('验证码错误');
wp_die();
}
$user_id = wp_create_user($data['username'], $data['password'], $data['email']);
if ($user_id instanceof WP_Error) {
echo pk_ajax_resp_error($user_id->get_error_message());
} else {
wp_set_auth_cookie($user_id, true, is_ssl());
echo pk_ajax_resp([
'action' => 'reload',
], '注册成功,已自动登录');
}
wp_die();
}

if (get_option('users_can_register') == 1) {
pk_ajax_register('pk_front_register_exec', 'pk_front_register_exec', true);
}

function pk_front_forget_password_exec()
{
if (is_string($data = pk_get_req_data([
'email' => ['email' => '邮箱', 'required' => true],
're-password' => ['email' => '重复密码', 'required' => true],
'password' => ['name' => '密码', 'required' => true],
'vd' => ['name' => '验证码', 'required' => true],
])) === true) {
echo pk_ajax_resp_error($data);
wp_die();
}
if (strlen($data['password']) < 6 || strlen($data['password']) > 18) {
echo pk_ajax_resp_error('密码不合法');
wp_die();
}
if ($data['password'] !== $data['re-password']) {
echo pk_ajax_resp_error('两次密码不一致');
wp_die();
}
if (!is_email($data['email'])) {
echo pk_ajax_resp_error('邮箱不合法');
wp_die();
}
if (!pk_captcha_validate('forget-password', $data['vd'])) {
echo pk_ajax_resp_error('验证码错误');
wp_die();
}
$user = get_user_by('email', $data['email']);
if (empty($user)) {
echo pk_ajax_resp_error('不存在该邮箱的用户');
wp_die();
}
$code = md5($data['email'] . wp_generate_password(20, false));
set_transient('pk_forget_password_' . $code, ['password' => $data['password'], 'email' => $data['email']], 60 * 5);
$url = pk_ajax_url('pk_front_forget_password_reset_exec', [
'code' => $code,
]);
if (wp_mail($data['email'], '密码重置 - ' . pk_get_web_title(), "您的密码重置链接为:{$url},请在5分钟内点击链接重置密码")) {
echo pk_ajax_resp(null, '重置密码链接已发送至邮箱');
} else {
echo pk_ajax_resp_error('重置密码链接邮件发送失败');
}
wp_die();
}

pk_ajax_register('pk_front_forget_password_exec', 'pk_front_forget_password_exec', true);

function pk_front_forget_password_reset_exec()
{
$code = $_REQUEST['code'] ?? '';
if (empty($code)) {
pk_ajax_result_page(false, '密码重置失败:密码重置链接无效');
}
$info = get_transient('pk_forget_password_' . $code);
if (empty($info)) {
pk_ajax_result_page(false, '密码重置失败:密码重置链接无效');
}
$user = get_user_by('email', $info['email']);
if (empty($user)) {
pk_ajax_result_page(false, '密码重置失败:用户不存在');
}
delete_transient('pk_forget_password_' . $code);
wp_set_password($info['password'], $user->ID);
pk_ajax_result_page(true, '密码重置成功,请返回登录');
}

pk_ajax_register('pk_front_forget_password_reset_exec', 'pk_front_forget_password_reset_exec', true);


pk_ajax_register('pk_font_login_page', 'pk_front_login_page_callback', true);


function pk_front_login_page_callback()
{
$redirect = $_GET['redirect'] ?? get_edit_profile_url();
$forget_password_url = pk_ajax_url('pk_font_login_page', ['redirect' => $_SERVER['REQUEST_URI']]);
$open_register = get_option('users_can_register') == 1;
?>

<form id="front-login-form" action="<?php echo pk_ajax_url('pk_front_login_exec'); ?>" method="post"
Expand All @@ -61,23 +179,38 @@ class="form-control form-control-sm t-sm" name="vd"
id="_front_login_vd">
</div>
<div class="col-4 col-sm-5 pr15">
<img class="captcha" src="<?php echo pk_captcha_url('login', 100, 28) ?>" alt="验证码">
<img class="captcha lazyload" data-src="<?php echo pk_captcha_url('login', 100, 28) ?>" alt="验证码">
</div>
</div>
</div>
<div class="mb15">
<label><input type="checkbox" name="remember"> 记住我</label>
</div>
<div class="mb15 d-flex justify-content-center wh100">
<button class="btn btn-ssm btn-primary mr5" type="submit"><i class="fa fa-right-to-bracket"></i> 立即登录
</button>
</div>
<div class="mb15 d-flex justify-content-between align-content-center fs12">
<a class="c-sub t-hover-primary toggle-el-show-hide" data-target="#front-register-form"
data-self="#front-login-form" href="javascript:void(0)">还没有账号?立即注册</a>
<a class="c-sub t-hover-primary" href="javascript:void(0)">忘记密码?立即找回密码</a>
<?php if ($open_register): ?>
<a class="c-sub t-hover-primary toggle-el-show-hide" data-target="#front-register-form"
data-modal-title="注册"
data-self="#front-login-form" href="javascript:void(0)">还没有账号?立即注册</a>
<?php endif; ?>
<a class="c-sub t-hover-primary toggle-el-show-hide" data-target="#front-forget-password-form"
data-modal-title="找回密码"
data-self="#front-login-form" href="javascript:void(0)">忘记密码?立即找回密码</a>
</div>
</form>


<?php if ($open_register): ?>
<form id="front-register-form" action="<?php echo pk_ajax_url('pk_front_register_exec'); ?>" method="post"
class="d-none ajax-form">
<div class="mb15">
<label for="_front_register_username" class="form-label">用户名</label>
<input type="text" name="username" class="form-control form-control-sm" data-required
id="_front_register_username" placeholder="请输入最少5~10位的用户名">
</div>
<div class="mb15">
<label for="_front_register_email" class="form-label">邮箱</label>
<input type="email" name="email" class="form-control form-control-sm" data-required
Expand All @@ -86,15 +219,72 @@ class="d-none ajax-form">
<div class="mb15">
<label for="_front_register_password" class="form-label">密码</label>
<input type="password" name="password" class="form-control form-control-sm" data-required
id="_front_register_password" placeholder="请输入密码">
id="_front_register_password" placeholder="请输入6~18位字符的密码">
</div>
<div class="mb15">
<label for="_front_register_vd" class="form-label">验证码</label>
<div class="row flex-row justify-content-end">
<div class="col-8 col-sm-7 text-right pl15">
<input type="text" data-required placeholder="请输入验证码" maxlength="4"
class="form-control form-control-sm t-sm" name="vd"
autocomplete="off"
id="_front_register_vd">
</div>
<div class="col-4 col-sm-5 pr15">
<img class="captcha lazyload" data-src="<?php echo pk_captcha_url('register', 100, 28) ?>" alt="验证码">
</div>
</div>
</div>
<div class="mb15 d-flex justify-content-center wh100">
<button class="btn btn-ssm btn-primary mr5" type="submit"><i class="fa fa-right-to-bracket"></i> 立即注册
</button>
</div>
<div class="mb15 d-flex justify-content-end fs12">
<a class="c-sub t-hover-primary toggle-el-show-hide" href="javascript:void(0)"
data-self="#front-register-form" data-target="#front-login-form" data-modal-title="登入">已有账号?立即登录</a>
</div>
</form>
<?php endif; ?>

<form id="front-forget-password-form" action="<?php echo pk_ajax_url('pk_front_forget_password_exec'); ?>"
method="post" class="d-none ajax-form">
<div class="mb15">
<label for="_front_forget_password_email" class="form-label">邮箱</label>
<input type="email" name="email" class="form-control form-control-sm" data-required
id="_front_forget_password_email" placeholder="请输入邮箱">
</div>
<div class="mb15">
<label for="_front_forget_password_password" class="form-label">新密码</label>
<input type="password" name="password" class="form-control form-control-sm" data-required
id="_front_forget_password_password" placeholder="请输入6~18位字符的新密码">
</div>
<div class="mb15">
<label for="_front_forget_password_password_re" class="form-label">重复新密码</label>
<input type="password" name="re-password" class="form-control form-control-sm" data-required
id="_front_forget_password_password_re" placeholder="请重复输入6~18位字符的新密码">
</div>
<div class="mb15">
<label for="_front_forget_password_vd" class="form-label">验证码</label>
<div class="row flex-row justify-content-end">
<div class="col-8 col-sm-7 text-right pl15">
<input type="text" data-required placeholder="请输入验证码" maxlength="4"
class="form-control form-control-sm t-sm" name="vd"
autocomplete="off"
id="_front_forget_password_vd">
</div>
<div class="col-4 col-sm-5 pr15">
<img class="captcha lazyload" data-src="<?php echo pk_captcha_url('forget-password', 100, 28) ?>" alt="验证码">
</div>
</div>
</div>
<div class="mb15 d-flex justify-content-center wh100">
<button class="btn btn-ssm btn-info mr5" type="submit"><i class="fa fa-right-to-bracket"></i> 立即注册
<button class="btn btn-ssm btn-primary mr5" type="submit"><i class="fa fa-paper-plane"></i> 发送邮件
</button>
</div>
<div class="mb15 d-flex justify-content-end fs12">
<a class="c-sub t-hover-primary toggle-el-show-hide" href="javascript:void(0)"
data-self="#front-register-form" data-target="#front-login-form">已有账号?立即登录</a>
data-self="#front-forget-password-form" data-target="#front-login-form"
data-modal-title="登入">想起密码?立即登录</a>
</div>
</form>

Expand Down
Loading

0 comments on commit b0cd63e

Please sign in to comment.