-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCode.gs
More file actions
96 lines (84 loc) · 3.22 KB
/
Code.gs
File metadata and controls
96 lines (84 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* Code.gs
* Ponto de entrada do SaneAgent — GET, template helper e sessão.
* O dispatcher POST e os controladores estão em Router.gs.
*/
// ============================================================================
// PONTO DE ENTRADA - REQUISIÇÕES GET
// ============================================================================
/**
* Função chamada quando o script é acessado via GET
*/
function doGet(e) {
const pagina = e.parameter.page || 'login';
const token = e.parameter.token || '';
try {
if (token) {
if (!validarToken(token)) {
return HtmlService.createHtmlOutput('<h1>Sessão expirada</h1><p><a href="' + ScriptApp.getService().getUrl() + '">Voltar ao login</a></p>');
}
}
switch (pagina) {
case 'login':
return carregarTemplate('Login.html');
case 'dashboard':
return carregarTemplate('Dashboard.html', { token: token });
case 'plantas':
return carregarTemplate('Plantas.html', { token: token });
case 'lotes':
return carregarTemplate('Lotes.html', { token: token });
case 'manejos':
return carregarTemplate('Manejos.html', { token: token });
case 'vendas':
return carregarTemplate('Vendas.html', { token: token });
case 'clientes':
return carregarTemplate('Clientes.html', { token: token });
case 'financeiro':
return carregarTemplate('Financeiro.html', { token: token });
case 'configuracoes':
return carregarTemplate('Configuracoes.html', { token: token });
default:
return carregarTemplate('Login.html');
}
} catch (erro) {
tratarErro(erro, 'CRÍTICO - doGet');
return HtmlService.createHtmlOutput('<h1>Erro ao carregar página</h1><p>' + erro.message + '</p>');
}
}
// ============================================================================
// FUNÇÕES AUXILIARES
// ============================================================================
/**
* Carrega e avalia um template HTML, passando dados opcionais.
*/
function carregarTemplate(nomeTemplate, dados = {}) {
try {
const html = HtmlService.createTemplateFromFile(nomeTemplate);
for (const [chave, valor] of Object.entries(dados)) {
html[chave] = valor;
}
return html.evaluate()
.setXFrameOptionsMode(HtmlService.XFrameOptionsMode.ALLOWALL)
.addMetaTag('viewport', 'width=device-width, initial-scale=1')
.setFaviconUrl('https://www.gstatic.com/images/branding/product/1x/sheets_48dp.png');
} catch (erro) {
tratarErro(erro, 'carregarTemplate');
return HtmlService.createHtmlOutput('<h1>Erro ao carregar template: ' + nomeTemplate + '</h1>');
}
}
/**
* Helper para incluir outros arquivos HTML em templates GAS.
* Uso: <?!= include('CSSGlobal') ?> em qualquer template.
*/
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename).getContent();
}
/**
* Verifica se uma sessão está ativa.
*/
function verificarSessaoAtiva(token) {
if (!token) return { ativo: false };
const sessao = obterSessao(token);
if (!sessao) return { ativo: false };
return { ativo: true, usuarioId: sessao.usuario_id };
}