Public apply/status shell on aksrt_user_verify_* meta, AJAX submit with materials upload, admin approve promotes apply payload to auth info.
65 lines
1.7 KiB
JavaScript
65 lines
1.7 KiB
JavaScript
/**
|
|
* User auth apply form AJAX.
|
|
*
|
|
* @package AKSRT
|
|
*/
|
|
(function () {
|
|
'use strict';
|
|
|
|
function ajaxUrl() {
|
|
if (window.aksrtData && window.aksrtData.ajaxUrl) return window.aksrtData.ajaxUrl;
|
|
return '/wp-admin/admin-ajax.php';
|
|
}
|
|
|
|
function toast(msg, type) {
|
|
if (window.aksrt && typeof window.aksrt.toast === 'function') {
|
|
window.aksrt.toast(msg, type || 'info');
|
|
return;
|
|
}
|
|
// eslint-disable-next-line no-alert
|
|
window.alert(msg);
|
|
}
|
|
|
|
function init() {
|
|
var form = document.querySelector('.aksrt-user-auth-form');
|
|
if (!form) return;
|
|
|
|
form.addEventListener('submit', function (e) {
|
|
e.preventDefault();
|
|
var btn = form.querySelector('button[type="submit"]');
|
|
if (btn) btn.disabled = true;
|
|
var fd = new FormData(form);
|
|
if (!fd.get('action')) fd.set('action', 'aksrt_user_auth_apply');
|
|
|
|
fetch(ajaxUrl(), {
|
|
method: 'POST',
|
|
body: fd,
|
|
credentials: 'same-origin',
|
|
})
|
|
.then(function (r) { return r.json(); })
|
|
.then(function (data) {
|
|
if (!data || data.error) {
|
|
toast((data && data.msg) || '提交失败', 'danger');
|
|
return;
|
|
}
|
|
toast(data.msg || '提交成功', 'success');
|
|
if (data.reload) {
|
|
window.setTimeout(function () { window.location.reload(); }, 500);
|
|
}
|
|
})
|
|
.catch(function () {
|
|
toast('网络错误', 'danger');
|
|
})
|
|
.finally(function () {
|
|
if (btn) btn.disabled = false;
|
|
});
|
|
});
|
|
}
|
|
|
|
if (document.readyState === 'loading') {
|
|
document.addEventListener('DOMContentLoaded', init);
|
|
} else {
|
|
init();
|
|
}
|
|
})();
|