$(document).ready(function () { const Toast = Swal.mixin({ toast: true, position: "bottom", showConfirmButton: false, timer: 3000, timerProgressBar: true, didOpen: (toast) => { toast.onmouseenter = Swal.stopTimer; toast.onmouseleave = Swal.resumeTimer; } }); const LK_BASE = 'https://invictica.ru/lk'; // === Работа с параметром back === const urlParams = new URLSearchParams(window.location.search); const isBackAllowed = urlParams.get('back') === '1'; if (isBackAllowed) { localStorage.setItem('back_allowed', '1'); } else { localStorage.removeItem('back_allowed'); } const input = $('#phone'); // или другой ID/селектор для твоего поля телефона const label = $('._Label_141fu_74'); input.on('input', function () { if (input.val().trim()) { label.addClass('_Label_hasValue_141fu_1'); } else { label.removeClass('_Label_hasValue_141fu_1'); } }); const phoneElement = document.getElementById('phone'); if (phoneElement && typeof IMask === 'function') { IMask(phoneElement, { mask: [ { mask: '+{0}(000)000-00-00', lazy: true }, { mask: '+0000000000000000' } ] }); } // === Отправка телефона для проверки === $('#phoneForm').on('submit', function (e) { e.preventDefault(); const phone = $('#phone').val().replace(/[^0-9]/g, ''); if (phone.length !== 11) { Toast.fire('Ошибка', 'Некорректный номер телефона', 'error'); return; } // Блокируем форму $('#phoneForm input, #phoneForm button').attr('disabled', 'disabled'); $('#phoneForm').addClass('processing'); $.ajax({ url: LK_BASE + '/ajax/check-phone.php', method: 'POST', data: { phone: phone }, dataType: 'json', success: function (response) { if (response.exists) { $.post(LK_BASE + '/ajax/send-sms.php', { phone: phone }, function (smsResponse) { // Разблокируем форму $('#phoneForm input, #phoneForm button').removeAttr('disabled'); $('#phoneForm').removeClass('processing'); if (smsResponse.success) { $('#phoneForm').hide(); $('#codeBlock').show().addClass('active'); startResendTimer(60); localStorage.setItem('lastPhone', phone); } else if (smsResponse.redirect_to_register) { window.location.href = smsResponse.register_url; } else { Toast.fire('Ошибка', smsResponse.error || 'Ошибка при отправке кода', 'error'); } }, 'json'); } else if (response.redirect_to_register) { window.location.href = response.register_url; } else { Toast.fire('Ошибка', response.error || 'Пользователь не найден', 'error'); } }, error: function () { $('#phoneForm input, #phoneForm button').removeAttr('disabled'); $('#phoneForm').removeClass('processing'); Toast.fire('Ошибка', 'Ошибка сервера', 'error'); }, complete: function () { // Всегда разблокируем форму после завершения запроса $('#phoneForm input, #phoneForm button').removeAttr('disabled'); $('#phoneForm').removeClass('processing'); } }); }); // === Ввод кода === $('#codeForm').on('submit', function (e) { e.preventDefault(); const code = $('#smsCode').val(); const phone = $('#phone').val().replace(/[^0-9]/g, ''); // Блокируем форму $('#codeForm input, #codeForm button').attr('disabled', 'disabled'); $('#codeBlock').addClass('processing'); $.post(LK_BASE + '/ajax/check-code.php', { phone: phone, code: code }, function (response) { if (response.authenticated) { // Добавляем класс успешной авторизации $('#codeBlock').removeClass('processing').addClass('authenticated'); // Показываем успех через Toast Toast.fire('Авторизация успешна', '', 'success'); // Через 1 секунду обновляем страницу или редиректим setTimeout(function () { location.href = LK_BASE; }, 2000); } else { $('#codeForm input, #codeForm button').removeAttr('disabled'); Toast.fire('Ошибка', response.error || 'Неверный код', 'error'); } }, 'json') .fail(function () { Toast.fire('Ошибка', 'Ошибка сервера', 'error'); }) .always(function () { // Убираем класс processing по завершении $('#codeBlock').removeClass('processing'); }); }); // === Таймер из сессии === function startResendTimer(seconds) { let timeLeft = seconds; $('#resendTimer').text(timeLeft); $('#timerText').show(); $('#manualResendBtn').hide(); const interval = setInterval(() => { timeLeft--; $('#resendTimer').text(timeLeft); if (timeLeft <= 0) { clearInterval(interval); $('#timerText').hide(); $('#manualResendBtn').removeClass('uk-hidden').show(); } }, 1000); } // === Кнопка "Отправить повторно" === $(document).on('click', '#manualResendBtn', function () { const phone = $('#phone').val().replace(/[^0-9]/g, ''); $('#manualResendBtn').attr('disabled', 'disabled').addClass('processing'); $.post(SMS_URL, { phone: phone }, function (response) { if (response.success) { $('#manualResendBtn').removeAttr('disabled').removeClass('processing'); $('#manualResendBtn').addClass('uk-hidden'); $('#timerText').show(); startResendTimer(60); } else { Toast.fire('Ошибка', response.error || 'Не удалось отправить код', 'error'); } }, 'json') .always(function () { $('#manualResendBtn').removeAttr('disabled').removeClass('processing'); }); }); });