login.html
8.88 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Inicie Sesión - Ecosistema SIGEM</title>
<!-- Google Font: Source Sans Pro -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,400i,700&display=fallback">
<!-- Font Awesome -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
<!-- Theme style AdminLTE 3 -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/admin-lte@3.2/dist/css/adminlte.min.css">
<style>
.login-page {
background: linear-gradient(135deg, #e0eafc 0%, #cfdef3 100%);
}
.card-primary.card-outline {
border-top: 3px solid #0056b3;
}
.btn-primary {
background-color: #0056b3;
border-color: #0056b3;
}
.btn-primary:hover {
background-color: #004494;
border-color: #004494;
}
.login-logo a {
color: #333 !important;
}
#municipioSearch::placeholder {
font-size: 0.85rem;
}
</style>
</head>
<body class="hold-transition login-page">
<div class="login-box" style="width: 400px;">
<div class="login-logo">
<a href="#"><b>SIGEM</b>WEB</a>
</div>
<div class="card card-outline card-primary shadow-lg">
<div class="card-body login-card-body rounded">
<p class="login-box-msg font-weight-bold" style="color: #555;">Visor Georreferenciado Multi-Tenant</p>
<form id="loginForm">
<label for="municipioSearch" class="text-xs text-muted mb-1" style="font-size: 0.8rem; text-transform: uppercase;">1. Entidad (Municipalidad)</label>
<div class="input-group mb-2">
<input type="text" id="municipioSearch" class="form-control" placeholder="Digite código o nombre del municipio..." autocomplete="off">
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-university"></span>
</div>
</div>
</div>
<div class="form-group mb-3 position-relative">
<select id="entidad" class="form-control" required size="4" style="height: 110px; cursor: pointer; font-size: 0.9rem;">
<option value="" disabled>Cargando municipios...</option>
</select>
<div id="loader-entidades" class="text-center p-2" style="position: absolute; top:0; left:0; width:100%; height:100%; background:white; display:none;">
<div class="spinner-border spinner-border-sm text-primary"></div>
</div>
</div>
<label class="text-xs text-muted mb-1" style="font-size: 0.8rem; text-transform: uppercase;">2. Credenciales</label>
<div class="input-group mb-3">
<input type="text" id="username" class="form-control" placeholder="Usuario (ej. operador)" required autocomplete="username">
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-user"></span>
</div>
</div>
</div>
<div class="input-group mb-3">
<input type="password" id="password" class="form-control" placeholder="Contraseña SIGEM" required autocomplete="current-password">
<div class="input-group-append">
<div class="input-group-text">
<span class="fas fa-lock"></span>
</div>
</div>
</div>
<div id="error-msg" class="alert alert-danger p-2 mb-3 text-center" style="display:none; font-size: 13px;"></div>
<div class="row mt-4">
<div class="col-12">
<button type="submit" class="btn btn-primary btn-block font-weight-bold" id="btn-login" style="padding: 10px;">
<span id="btn-text">INICIAR SESIÓN</span>
<div class="spinner-border spinner-border-sm text-light" id="spinner" role="status" style="display:none; margin: auto;"></div>
</button>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.2/dist/js/bootstrap.bundle.min.js"></script>
<script>
const searchInput = document.getElementById('municipioSearch');
const selectEntidad = document.getElementById('entidad');
const loader = document.getElementById('loader-entidades');
let allEntidades = [];
// Cargar Entidades desde la Base de Datos Remota (.254)
async function loadEntidades() {
loader.style.display = 'block';
try {
const response = await fetch('/gis-geoserver/api/auth/entidades');
if (!response.ok) throw new Error("Error al obtener entidades");
allEntidades = await response.json();
renderOptions(allEntidades);
} catch (error) {
console.error(error);
selectEntidad.innerHTML = '<option value="" disabled>Error al cargar municipios</option>';
} finally {
loader.style.display = 'none';
}
}
function renderOptions(data) {
selectEntidad.innerHTML = '';
data.forEach(ent => {
const opt = document.createElement('option');
opt.value = ent.entidad;
opt.textContent = `${ent.entidad} - ${ent.nombre}`;
selectEntidad.appendChild(opt);
});
if (data.length > 0) selectEntidad.selectedIndex = 0;
}
searchInput.addEventListener('input', (e) => {
const text = e.target.value.toLowerCase();
const filtered = allEntidades.filter(ent =>
ent.entidad.toString().includes(text) ||
ent.nombre.toLowerCase().includes(text)
);
renderOptions(filtered);
// Si el usuario escribió un código exacto que existe, seleccionarlo
const exactMatch = allEntidades.find(ent => ent.entidad.toString() === text);
if (exactMatch) {
selectEntidad.value = exactMatch.entidad;
}
});
// Carga inicial
loadEntidades();
localStorage.removeItem('jwt');
document.getElementById('loginForm').addEventListener('submit', function(e) {
e.preventDefault();
const username = document.getElementById('username').value;
const password = document.getElementById('password').value;
const searchVal = searchInput.value.trim();
const errorMsg = document.getElementById('error-msg');
// Regla: Validar si la entidad seleccionada coincide con lo digitado o si hay una seleccionada
let entidadId = selectEntidad.value;
// Si el buscador tiene algo pero no hay selección válida, intentamos buscar el código exacto
if (!entidadId && searchVal) {
const match = allEntidades.find(ent => ent.entidad.toString() === searchVal);
if (match) entidadId = match.entidad;
}
if (!entidadId) {
errorMsg.innerText = "La MUNICIPALIDAD con código " + (searchVal || "seleccionado") + " no existe.";
errorMsg.style.display = 'block';
return;
}
const btnText = document.getElementById('btn-text');
const spinner = document.getElementById('spinner');
errorMsg.style.display = 'none';
btnText.style.display = 'none';
spinner.style.display = 'block';
document.getElementById('btn-login').disabled = true;
fetch('/gis-geoserver/api/auth/login', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password, entidad: entidadId })
})
.then(async response => {
const data = await response.json();
if (!response.ok) throw new Error(data.message || "Credenciales incorrectas.");
return data;
})
.then(data => {
localStorage.setItem('jwt', data.token);
localStorage.setItem('user_name', data.nombre);
localStorage.setItem('entidad', entidadId);
localStorage.setItem('entidad_nombre', data.entidadNombre);
localStorage.setItem('eslogan', data.eslogan);
localStorage.setItem('responsable', data.responsable);
localStorage.setItem('entidad_logo', data.entidadLogo);
localStorage.setItem('map_lat', data.lat);
localStorage.setItem('map_lng', data.lng);
localStorage.setItem('map_zoom', data.zoom);
window.location.href = "/gis-geoserver/landing";
})
.catch(error => {
errorMsg.innerText = error.message;
errorMsg.style.display = 'block';
btnText.style.display = 'block';
spinner.style.display = 'none';
document.getElementById('btn-login').disabled = false;
});
});
</script>
</body>
</html>