/** * Файл отвечает за работу получения и отображения скрытой страницы сделанной на tilda */ /* exported tma__resources__initPage */ /** * Функция делает запрос на получение страницы и отображает ее в iframe */ function tma__resources__initPage() { var objProfile = tma__getProfileObjFromLS(); if (!tma__resources__isAuthorized(objProfile)) { const allRecords = document.getElementById('allrecords'); const projectId = (objProfile && objProfile.projectid) || (allRecords && allRecords.getAttribute('data-tilda-project-id')); tma__resources__clearProfile(projectId); document.location.replace(tma__getMembersLoginUrl()); return; } if (tma__resources__hasProfileData(objProfile)) { tma__resources__loadPageHtml(objProfile); return; } tma__getObjProfile(() => { tma__resources__loadPageHtml(tma__getProfileObjFromLS()); }); } /** * Проверяет, что в профиле есть данные авторизации * * @param {object | boolean} profile - профиль участника из localStorage * @returns {boolean} */ function tma__resources__isAuthorized(profile) { return Boolean(profile && profile.token && profile.projectid); } /** * Удаляет профиль участника из localStorage * * @param {string} projectId - идентификатор проекта */ function tma__resources__clearProfile(projectId) { if (!projectId) return; const profileName = 'tilda_members_profile' + projectId; localStorage.removeItem(profileName); localStorage.removeItem(profileName + '_timestamp'); } /** * Отличает полный профиль от записи с token и projectid, которую сохраняет корзина. * Пустые имя и контакты допустимы, поэтому проверяем наличие полей, а не их значения. * * @param {object | boolean} profile - профиль участника из localStorage * @returns {boolean} */ function tma__resources__hasProfileData(profile) { if (!profile || typeof profile !== 'object') return false; return 'name' in profile && ('login' in profile || 'phone' in profile); } /** * Загружает HTML закрытой страницы (protected page) после подготовки полного профиля. * * @param {object} objProfile - профиль участника с данными авторизации */ function tma__resources__loadPageHtml(objProfile) { var allRecords = document.getElementById('allrecords'); var tildaMembersData = window.tildaMembers; var indexPage = ''; var formData = { projectid: objProfile.projectid, token: objProfile.token, tzoffset: new Date().getTimezoneOffset(), pageurl: window.location.href, }; if (allRecords) { indexPage = allRecords.getAttribute('data-tilda-page-id'); if (indexPage) { formData.pageid = indexPage; } else { indexPage = allRecords.getAttribute('data-doc-id'); formData.docid = indexPage; } var feeduid = allRecords.getAttribute('data-tilda-feeduid'); if (feeduid) { formData.feeduid = feeduid; } } tma__request('/api/getpage/', JSON.stringify(formData), '', function (response) { if (response.status === 'ok' && typeof response.data === 'object') { var title = response.data.html.slice( response.data.html.indexOf('') + 7, response.data.html.indexOf('') ); if (response.data.html.indexOf('tilda-members-init.min.js') > 0) { tma__resources__showErrorPageNotСorrectly(); return; } document.open(); document.write(response.data.html); document.close(); document.title = title; var counter = 0; var timerId = setInterval(function () { counter += 1; // 300 время через которое необходимо остановить таймер // 300 * 100 = 30000 мс -> 30 сек if (counter === 300) clearInterval(timerId); if (document.getElementById('allrecords')) { tma__resources__loadPage(tildaMembersData); clearInterval(timerId); } }, 100); } else { if (response.code === 'unauthorized') { tma__resources__clearProfile(objProfile.projectid); document.location.replace('/members/login'); } else { document.location.replace('/members/'); } } }); } /** * Отрисовывает компоненты после загрузки страницы * * @param {object} tildaMembersData - данные ЛК */ function tma__resources__loadPage(tildaMembersData) { window.tildaMembers = tildaMembersData; tma__getObjStyle(function () { tma__onFuncLoad('tma__userbar__drawWidget', function () { tma__userbar__drawWidget(); }); }); } // Если полученная страница содержит скрипт tilda-members-init.min.js, то такая страница не корректная для отображения function tma__resources__showErrorPageNotСorrectly() { alert('Sorry, this page does not work correctly'); tma__loadFiles(['userbar']); tma__getObjStyle(function () { tma__onFuncLoad('tma__userbar__drawWidget', function () { tma__userbar__drawWidget(); tma__showDocumentReady(); }); }); }