Авто-синхронизация каждые 45 сек + обновление активной вкладки

This commit is contained in:
Dauren777 2026-06-11 04:58:00 +00:00
parent 657b8a0359
commit 7c1c56b292

View File

@ -103,7 +103,8 @@ function saveA(d){localStorage.setItem("pab_audits",JSON.stringify(d))}
function saveU(d){localStorage.setItem("pab_users",JSON.stringify(d))} function saveU(d){localStorage.setItem("pab_users",JSON.stringify(d))}
// Supabase — фоновая синхронизация, не трогает вкладки // Supabase — фоновая синхронизация, не трогает вкладки
function sbSync(){sbPullUsers();sbPullAudits()} function sbSync(){sbPullUsers();sbPullAudits().then(function(){refreshCurrentTab()})}
function refreshCurrentTab(){var panels=["pnNA","pnMS","pnDB","pnVL","pnHS"];for(var i=0;i<panels.length;i++){if(document.getElementById(panels[i]).classList.contains("ac")){var n=panels[i].replace("pn","");if(n==="MS")rMS();if(n==="DB")rDB();if(n==="VL")rVL();if(n==="HS")rHS();break}}}
function sbPullUsers(){fetch(SBU+"/rest/v1/users?select=*",{headers:{"apikey":SBK,"Authorization":"Bearer "+SBK}}).then(function(r){return r.json()}).then(function(d){var um=getU();d.forEach(function(x){if(!um[x.login])um[x.login]={pass:x.pass,name:x.name,email:x.email,role:x.role,freq:x.freq,branch:x.branch,dept:x.dept,region:x.region,oblast:x.oblast,city:x.city}});saveU(um)}).catch(function(){})} function sbPullUsers(){fetch(SBU+"/rest/v1/users?select=*",{headers:{"apikey":SBK,"Authorization":"Bearer "+SBK}}).then(function(r){return r.json()}).then(function(d){var um=getU();d.forEach(function(x){if(!um[x.login])um[x.login]={pass:x.pass,name:x.name,email:x.email,role:x.role,freq:x.freq,branch:x.branch,dept:x.dept,region:x.region,oblast:x.oblast,city:x.city}});saveU(um)}).catch(function(){})}
function sbPullAudits(){fetch(SBU+"/rest/v1/audits?select=*&order=created_at.desc",{headers:{"apikey":SBK,"Authorization":"Bearer "+SBK}}).then(function(r){return r.json()}).then(function(d){var am=[],ca=getA();d.forEach(function(x){am.push({id:x.id,number:x.number,date:x.date,location:x.location,region:x.region,workType:x.work_type,workerCount:x.worker_count,observer:x.observer,observerRole:x.observer_role,overallSafe:x.overall_safe,categories:x.categories,totalViolations:x.total_violations,dialogue:x.dialogue,photos:x.photos,docs:x.docs,createdBy:x.created_by,createdAt:x.created_at})});ca.forEach(function(a){var found=false;am.forEach(function(b){if(b.id===a.id)found=true});if(!found)am.push(a)});saveA(am)}).catch(function(){})} function sbPullAudits(){fetch(SBU+"/rest/v1/audits?select=*&order=created_at.desc",{headers:{"apikey":SBK,"Authorization":"Bearer "+SBK}}).then(function(r){return r.json()}).then(function(d){var am=[],ca=getA();d.forEach(function(x){am.push({id:x.id,number:x.number,date:x.date,location:x.location,region:x.region,workType:x.work_type,workerCount:x.worker_count,observer:x.observer,observerRole:x.observer_role,overallSafe:x.overall_safe,categories:x.categories,totalViolations:x.total_violations,dialogue:x.dialogue,photos:x.photos,docs:x.docs,createdBy:x.created_by,createdAt:x.created_at})});ca.forEach(function(a){var found=false;am.forEach(function(b){if(b.id===a.id)found=true});if(!found)am.push(a)});saveA(am)}).catch(function(){})}
function sbPushAudit(e){fetch(SBU+"/rest/v1/audits",{method:"POST",headers:{"apikey":SBK,"Authorization":"Bearer "+SBK,"Content-Type":"application/json","Prefer":"resolution=merge-duplicates"},body:JSON.stringify({id:e.id,number:e.number,date:e.date,location:e.location,region:e.region,work_type:e.workType,worker_count:e.workerCount,observer:e.observer,observer_role:e.observerRole,overall_safe:e.overallSafe,categories:e.categories,total_violations:e.totalViolations,dialogue:e.dialogue,photos:e.photos,docs:e.docs,created_by:e.createdBy,created_at:e.createdAt})}).catch(function(){})} function sbPushAudit(e){fetch(SBU+"/rest/v1/audits",{method:"POST",headers:{"apikey":SBK,"Authorization":"Bearer "+SBK,"Content-Type":"application/json","Prefer":"resolution=merge-duplicates"},body:JSON.stringify({id:e.id,number:e.number,date:e.date,location:e.location,region:e.region,work_type:e.workType,worker_count:e.workerCount,observer:e.observer,observer_role:e.observerRole,overall_safe:e.overallSafe,categories:e.categories,total_violations:e.totalViolations,dialogue:e.dialogue,photos:e.photos,docs:e.docs,created_by:e.createdBy,created_at:e.createdAt})}).catch(function(){})}
@ -252,7 +253,7 @@ function importData(){
}; };
input.click(); input.click();
} }
rHS();sbSync();checkSA(); rHS();sbSync();checkSA();setInterval(function(){sbSync()},45000);
</script> </script>
</body> </body>
</html> </html>