source: firmaeventos/static/js/funciones.js @ aef49cb

Last change on this file since aef49cb was a1de3f1, checked in by rudmanmrrod <rudman22@…>, 7 years ago

Agregado decorador para permitir post sin el csrf token

  • Property mode set to 100644
File size: 6.9 KB
Line 
1/**
2 * Funcion para refrescar el captcha
3 * @param element Recibe el parametro
4*/
5
6function refresh_captcha(element) {
7    $form = $(element).parents('form');
8    var url = location.protocol + "//" + window.location.hostname + ":" + location.port + "/captcha/refresh/";
9
10    $.getJSON(url, {}, function(json) {
11        $form.find('input[name="captcha_0"]').val(json.key);
12        $form.find('img.captcha').attr('src', json.image_url);
13    });
14
15    return false;
16}
17
18/**
19 * Función para obtener los usuarios de un evento
20*/
21function get_event_user() {
22    var modal = false;
23    var pasaporte = $('#id_pasaporte').val();
24    if (pasaporte!='') {
25        var routes = $(location).attr('pathname').split('/');
26        var pk = routes[routes.length-1];
27        var url = URL_USUARIO_EVENTO+pk+"/"+pasaporte;
28        $.getJSON(url, function(data){
29            if (Object.keys(data).length > 0) {
30                construir_datos(data);   
31            }
32            else{
33                simple_modal('Lo sentimos, no esta registrado para firmar');
34            }
35        }).fail(function(jqxhr, textStatus, error) {
36            simple_modal('Petición fállida' + textStatus + ", " + error);
37        })
38    }
39    else{
40        simple_modal('Debe ingresar un pasaporte');
41    }   
42}
43
44/**
45 * Función para crear un modal sencillo
46*/
47function simple_modal(mensaje) {
48    MaterialDialog.alert(
49        mensaje,
50        {
51            title:'Alerta',
52            buttons:{
53                close:{
54                    text:'cerrar',
55                    className:'blue',
56                }
57            }
58        }
59    );
60}
61
62/**
63 * Función para construir la data del participante
64 * @param data Recibe los datos para crear la lista
65*/
66function construir_datos(data) {
67    firma = data.firmo;
68    data = data.datos;
69    $('#datos_paricipante').html('');
70    html = '<ul class="collection">'
71    html += '<li class="collection-item"><b>Nombre: </b>'+data.nombres+'</li>';
72    html += '<li class="collection-item"><b>Apellido: </b>'+data.apellidos+'</li>';
73    html += '<li class="collection-item"><b>Pasaporte: </b>'+data.pasaporte+'</li>';
74    html += '<li class="collection-item"><b>Correo: </b>'+data.correo+'</li>';
75    html += '</ul>';
76    html += '<iframe width="700px" height="600px" src="https://192.168.12.154:8443/Murachi/0.1/archivos/listadopdf/'+data.documento+'">';
77    html += '</iframe><br/>';
78    if (firma==true) {
79        html += '<h4 class="red-text center">Ya firmó este documento</h4>'
80    }
81    else{
82        html += '<a type="button" id="firmar" class="btn waves-effect blue darken-1" onclick="ObtenerCertificadoFirmanteMultiples(\''+data.documento+'\')">';
83        html += '<i class="material-icons left">mode_edit</i> Firmar</a>';   
84    }
85    $('#datos_paricipante').html(html);
86}
87
88/**
89 * Función para obtener el certificado del participante
90 * @param fileId Recibe el id del documento
91*/
92function ObtenerCertificadoFirmanteMultiples(fileId){
93
94        // identificador del archivo en el servidor
95        window.hwcrypto.getCertificate({lang: "en"}).then(
96                function(response) {
97                        var cert = response;
98                        var parameters = "";
99                        parameters = JSON.stringify({
100                                "fileId":fileId,
101                                "certificate":cert.hex,
102                                "reason":"Certificado",
103                                "location":"RedGealc",
104                                "contact":"RedGealc",
105                                "signatureVisible":"false",
106                                "signaturePage": "",
107                                "xPos": "",
108                                "yPos": ""
109                                });                             
110
111                        // ahora llamar al ajax de obtener la resena del pdf
112                        ObtenerHashPDFServerMultiples(parameters, cert);       
113
114                }, 
115                function(err) {
116            var error;
117            if(err == "Error: user_cancel") {
118                error = "El usuario cancelo la operación"; 
119             }     
120             else if(err == "Error: no_certificates") {
121                 error = "No hay certificado disponible";
122             }
123             else if(err == "Error: no_implementation") {
124                 error = "No hay soporte para el manejo del certificado";
125                        }
126            simple_modal(error);
127                }
128
129        );
130}
131
132
133/**
134 * Función para obtener el hash y procesar la informacion
135 * @param parameters Recibe los parametros
136 * @param cert Recibe los certificados
137*/
138function ObtenerHashPDFServerMultiples(parameters,cert){
139
140        $.ajax({
141                type: 'POST',
142                contentType: 'application/json',                               
143                url:"https://192.168.12.154:8443/Murachi/0.1/archivos/pdfs2",
144                //url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfs",
145        //url: "https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/firmados/pdfs",
146                dataType: "json",
147                data: parameters,               
148                xhrFields: {withCredentials: true},
149                headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
150                success: function(data, textStatus, jqXHR){
151                        var json_x = data;
152                        var hash = json_x['hash'];             
153                        var hashtype = "SHA-256";
154                        var lang = "eng";
155                       
156                        //Procesa la información
157                        window.hwcrypto.sign(cert, {type: hashtype, hex: hash}, {lang: lang}).then(
158                                function(signature) {
159                                        FinalizarFirmaMultiples(signature.hex);
160                        }, 
161                        function(err) {
162                                        var error;
163                    if(err == "Error: user_cancel") {
164                        error = "El usuario cancelo la operación"; 
165                     }     
166                     else if(err == "Error: no_certificates") {
167                         error = "No hay certificado disponible";
168                     }
169                     else if(err == "Error: no_implementation") {
170                         error = "No hay soporte para el manejo del certificado";
171                     }
172                    simple_modal(error);
173                });
174                       
175                },                                                             
176                error: function(jqXHR, textStatus, errorThrown){
177                        console.log('ajax error function: ' + jqXHR.responseText);
178                }
179               
180        });     
181}
182
183/**
184 * Función para enviar la firma al servidor
185 * @param signature Recibe la firma
186*/
187function FinalizarFirmaMultiples(signature){
188
189        $.ajax({
190                type: 'POST',
191                contentType: 'application/json',
192                url:"https://192.168.12.154:8443/Murachi/0.1/archivos/pdfs/resenas",
193                //url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfs/resenas",
194                dataType: 'json',
195                data: JSON.stringify({"signature":signature}),
196                xhrFields: {withCredentials: true},
197                headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
198                success: function(data, textStatus, jqXHR){
199            actualizar_participante(data['signedFileId']);
200                },
201                error: function(jqXHR, textStatus, errorThrown){
202                        console.log('error en pdfs/resenas: ' + textStatus);
203                }
204        });
205
206}
207
208/**
209 * Función para actualizar los datos del participante
210 * @param id_documento Recibe el id del documento
211*/
212function actualizar_participante(id_documento) {
213    var pasaporte = $('#id_pasaporte').val();
214    var routes = $(location).attr('pathname').split('/');
215    var pk = routes[routes.length-1];
216   
217    $.post(URL_ACTUALIZAR_PARTICIPACION,{'event_id':pk,'pasaporte':pasaporte,'serial':id_documento})
218    .done(function(data){
219        if (data.validate==true) {
220            simple_modal(data.mensaje);
221            $('#firmar').remove();
222        }
223        else{
224            simple_modal(data.mensaje);
225        }
226    })
227    .fail(function(err){
228        console.log(err);
229    });
230}
Note: See TracBrowser for help on using the repository browser.