diff --git a/modules/siteData/index.js b/modules/siteData/index.js index 174031b..0514df1 100644 --- a/modules/siteData/index.js +++ b/modules/siteData/index.js @@ -12,63 +12,65 @@ function loadEquipmentGallery(data) { }); } +function loadSiteData() { + const data = {}; + + function loadTable(table, key) { + return new Promise((resolve) => { + db.all(`SELECT * FROM ${table} ORDER BY sort_order`, (err, rows) => { + if (err) { data[key] = []; resolve(); return; } + if (table === 'equipment' || table === 'partners') { + rows = rows.filter(r => r.is_active == 1); + } + if (table === 'services' || table === 'work_schemes') { + rows = rows.filter(r => r.is_active == 1); + } + data[key] = rows; + resolve(); + }); + }); + } + + function loadSettings(table, key, transformFn) { + return new Promise((resolve) => { + db.all(`SELECT * FROM ${table} ORDER BY id`, (err, rows) => { + if (err) { data[key] = {}; resolve(); return; } + if (transformFn) { + data[key] = transformFn(rows); + } else { + const obj = {}; + rows.forEach(r => { obj[r.key] = r.value; }); + data[key] = obj; + } + resolve(); + }); + }); + } + + return Promise.all([ + loadSettings('hero_settings', 'hero'), + loadTable('hero_stats', 'hero_stats'), + loadSettings('about_settings', 'about'), + loadTable('about_features', 'about_features'), + loadTable('services', 'services'), + loadTable('work_schemes', 'work_schemes'), + loadTable('process_steps', 'process_steps'), + loadTable('equipment', 'equipment'), + loadEquipmentGallery(data), + loadTable('geo_regions', 'geo_regions'), + loadSettings('geo_settings', 'geo'), + loadTable('licenses', 'licenses'), + loadTable('license_gallery', 'license_gallery'), + loadTable('public_activities', 'public_activities'), + loadTable('activity_gallery', 'activity_gallery'), + loadTable('partners', 'partners'), + loadSettings('settings', 'settings') + ]).then(() => data); +} + function setupRoutes(app) { app.get('/api/site-data', (req, res) => { - const data = {}; - - function loadTable(table, key) { - return new Promise((resolve) => { - db.all(`SELECT * FROM ${table} ORDER BY sort_order`, (err, rows) => { - if (err) { data[key] = []; resolve(); return; } - if (table === 'equipment' || table === 'partners') { - rows = rows.filter(r => r.is_active == 1); - } - if (table === 'services' || table === 'work_schemes') { - rows = rows.filter(r => r.is_active == 1); - } - data[key] = rows; - resolve(); - }); - }); - } - - function loadSettings(table, key, transformFn) { - return new Promise((resolve) => { - db.all(`SELECT * FROM ${table} ORDER BY id`, (err, rows) => { - if (err) { data[key] = {}; resolve(); return; } - if (transformFn) { - data[key] = transformFn(rows); - } else { - const obj = {}; - rows.forEach(r => { obj[r.key] = r.value; }); - data[key] = obj; - } - resolve(); - }); - }); - } - - Promise.all([ - loadSettings('hero_settings', 'hero'), - loadTable('hero_stats', 'hero_stats'), - loadSettings('about_settings', 'about'), - loadTable('about_features', 'about_features'), - loadTable('services', 'services'), - loadTable('work_schemes', 'work_schemes'), - loadTable('process_steps', 'process_steps'), - loadTable('equipment', 'equipment'), - loadEquipmentGallery(data), - loadTable('geo_regions', 'geo_regions'), - loadSettings('geo_settings', 'geo'), - loadTable('licenses', 'licenses'), - loadTable('license_gallery', 'license_gallery'), - loadTable('public_activities', 'public_activities'), - loadTable('activity_gallery', 'activity_gallery'), - loadTable('partners', 'partners'), - loadSettings('settings', 'settings') - ]).then(() => { - res.json(data); - }); + loadSiteData().then(data => res.json(data)); }); app.post('/api/leads', (req, res) => { @@ -83,4 +85,4 @@ function setupRoutes(app) { }); } -module.exports = { init, setupRoutes }; +module.exports = { init, setupRoutes, loadSiteData }; diff --git a/modules/ssrRenderer.js b/modules/ssrRenderer.js new file mode 100644 index 0000000..3a5359f --- /dev/null +++ b/modules/ssrRenderer.js @@ -0,0 +1,250 @@ +const yearsSince2011 = new Date().getFullYear() - 2011; + +function esc(s) { return String(s||'').replace(/&/g,'&').replace(/"/g,'"').replace(//g,'>'); } + +function imgSrc(path) { + if (!path) return './img/hero-bg.webp'; + if (path.startsWith('./')) return path; + if (path.startsWith('http')) return path; + if (/^(equipment|partner|license|activity|hero)_images\//.test(path)) return '/data/' + path; + return './img/' + path; +} + +function renderHero(data) { + var h = data.hero || {}; + var stats = data.hero_stats || []; + var phone800 = h.phone_800 || '8 (800) 555-29-31'; + var phoneEkb = h.phone_ekb || '8 (343) 237-25-66'; + var email = h.email || 'sts_zakaz@mail.ru'; + var address = h.address || 'г. Екатеринбург, ул. Московская, 11, оф. 702'; + + var contactsHtml = ''; + if (phone800 && phoneEkb) { + contactsHtml += '
'; + } + contactsHtml += ''; + contactsHtml += '' + (h.subtitle || '').replace('{years}', yearsSince2011) + '
' + + '' + (a.section_subtitle || '') + '
' + a.paragraph_1 + '
' : '') + + (a.paragraph_2 ? '' + a.paragraph_2 + '
' : '') + + (a.paragraph_3 ? '' + a.paragraph_3 + '
' : '') + + '' + (s.description || '') + '
' + + (items.length ? '' + (st.section_services_subtitle || '') + '
' + (s.description || '') + '
' + + (items.length ? '' + (st.section_schemes_subtitle || '') + '
' + (s.description || '') + '
' + (st.section_process_subtitle || '') + '
' + esc(st.section_equipment_subtitle || '') + '
' + (geo.section_subtitle || st.section_services_subtitle || '') + '
' + (geo.difficult_desc || '') + '
' + (l.description || '') + '
' + (g.description || '') + '
' + (st.section_licenses_subtitle || '') + '
' + (a.description || '') + '
' + (g.description || '') + '
' + (st.section_activity_subtitle || '') + '
' + (st.section_partners_subtitle || '') + '
' + (s.section_contacts_subtitle || '') + '
' + address + '
' + workHours + '
' + (s.form_subtitle || 'Заполните форму и мы свяжемся с вами') + '
' + + '' + + 'Мы свяжемся с вами в ближайшее время