2022-02-11 21:21:41 +08:00
|
|
|
|
|
|
|
(rl => {
|
|
|
|
|
|
|
|
rl && addEventListener('rl-view-model', e => {
|
2022-02-11 21:52:04 +08:00
|
|
|
const id = e.detail.viewModelTemplateID;
|
|
|
|
if (e.detail && ('AdminLogin' === id || 'Login' === id)
|
2022-02-11 21:21:41 +08:00
|
|
|
&& rl.pluginSettingsGet('recaptcha', 'show_captcha_on_login')) {
|
|
|
|
let
|
|
|
|
nId = null,
|
|
|
|
script;
|
|
|
|
|
|
|
|
const
|
2022-02-11 21:52:04 +08:00
|
|
|
mode = 'Login' === id ? 'user' : 'admin',
|
|
|
|
|
2022-02-11 21:21:41 +08:00
|
|
|
doc = document,
|
|
|
|
|
|
|
|
container = e.detail.viewModelDom.querySelector('#plugin-Login-BottomControlGroup'),
|
|
|
|
|
|
|
|
ShowRecaptcha = () => {
|
|
|
|
if (window.grecaptcha && null === nId && container) {
|
|
|
|
const oEl = doc.createElement('div');
|
|
|
|
oEl.className = 'controls';
|
|
|
|
|
|
|
|
container.after(oEl);
|
|
|
|
|
|
|
|
nId = window.grecaptcha.render(oEl, {
|
|
|
|
'sitekey': rl.pluginSettingsGet('recaptcha', 'public_key'),
|
|
|
|
'theme': rl.pluginSettingsGet('recaptcha', 'theme')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
StartRecaptcha = () => {
|
|
|
|
if (window.grecaptcha) {
|
|
|
|
ShowRecaptcha();
|
|
|
|
} else if (!script) {
|
|
|
|
script = doc.createElement('script');
|
|
|
|
// script.onload = ShowRecaptcha;
|
|
|
|
script.src = 'https://www.google.com/recaptcha/api.js?onload=ShowRecaptcha&render=explicit&hl=' + doc.documentElement.lang;
|
|
|
|
doc.head.append(script);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
window.ShowRecaptcha = ShowRecaptcha;
|
|
|
|
|
|
|
|
StartRecaptcha();
|
|
|
|
|
2022-02-11 21:52:04 +08:00
|
|
|
addEventListener(`sm-${mode}-login`, e => {
|
2022-02-11 21:21:41 +08:00
|
|
|
if (null !== nId && window.grecaptcha) {
|
|
|
|
e.detail.set('RecaptchaResponse', window.grecaptcha.getResponse(nId));
|
|
|
|
} else {
|
2022-02-11 21:52:04 +08:00
|
|
|
e.preventDefault();
|
2022-02-11 21:21:41 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-02-11 21:52:04 +08:00
|
|
|
addEventListener(`sm-${mode}-login-response`, e => {
|
2022-02-11 21:21:41 +08:00
|
|
|
if (e.detail.error) {
|
|
|
|
if (null !== nId && window.grecaptcha) {
|
|
|
|
window.grecaptcha.reset(nId);
|
|
|
|
} else if (e.detail.data && e.detail.data.Captcha) {
|
|
|
|
StartRecaptcha();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
})(window.rl);
|