source: firmaeventos/static/js/funciones.js @ 3ae7c21

Last change on this file since 3ae7c21 was 3ae7c21, checked in by lhernandez <lhernandez@…>, 6 years ago

Merge branch 'master' of https://github.com/cenditel-desarrollo/FirmaEventos

  • Property mode set to 100755
File size: 7.8 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://murachi.cenditel.gob.ve/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    var xPos = yPos= signaturePage = "";
94    var lastSignature = false;
95    var routes = $(location).attr('pathname').split('/');
96    var pk = routes[routes.length-1];
97   
98    $.ajax({
99                type: 'GET',
100        async: false,
101                url:URL_ULTIMO_FIRMANTE+pk,
102                success: function(datos){
103            if (datos.valid==true) {
104                xPos = datos.data.posX;
105                yPos = datos.data.posY;
106                signaturePage = datos.data.page;
107                lastSignature = true;
108            }
109            window.hwcrypto.getCertificate({lang: "en"}).then(
110                function(response) {
111                    var cert = response;
112                    var parameters = "";
113                    parameters = JSON.stringify({
114                        "fileId":fileId,
115                        "certificate":cert.hex,
116                        "reason":"Certificado",
117                        "location":"RedGealc",
118                        "contact":"RedGealc",
119                        "signatureVisible":"false",
120                        "signaturePage": signaturePage,
121                        "xPos": xPos,
122                        "yPos": yPos,
123                        "lastSignature":lastSignature
124                        });                             
125       
126                    // ahora llamar al ajax de obtener la resena del pdf
127                    ObtenerHashPDFServerMultiples(parameters, cert);   
128       
129                }, 
130                function(err) {
131                    var error;
132                    if(err == "Error: user_cancel") {
133                        error = "El usuario cancelo la operación"; 
134                     }     
135                     else if(err == "Error: no_certificates") {
136                         error = "No hay certificado disponible";
137                     }
138                     else if(err == "Error: no_implementation") {
139                         error = "No hay soporte para el manejo del certificado";
140                    }
141                    simple_modal(error);
142                }
143       
144            );
145                },
146                error: function(jqXHR, textStatus, errorThrown){
147                        console.log('error: ' + textStatus);
148                }
149        });
150}
151
152
153/**
154 * Función para obtener el hash y procesar la informacion
155 * @param parameters Recibe los parametros
156 * @param cert Recibe los certificados
157*/
158function ObtenerHashPDFServerMultiples(parameters,cert){
159
160        $.ajax({
161                type: 'POST',
162                contentType: 'application/json',                               
163                url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfsqr",
164                dataType: "json",
165                data: parameters,               
166                xhrFields: {withCredentials: true},
167                headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
168                success: function(data, textStatus, jqXHR){
169                        var json_x = data;
170                        var hash = json_x['hash'];             
171                        var hashtype = "SHA-256";
172                        var lang = "eng";
173                       
174                        //Procesa la información
175                        window.hwcrypto.sign(cert, {type: hashtype, hex: hash}, {lang: lang}).then(
176                                function(signature) {
177                                        FinalizarFirmaMultiples(signature.hex);
178                        }, 
179                        function(err) {
180                                        var error;
181                    if(err == "Error: user_cancel") {
182                        error = "El usuario cancelo la operación"; 
183                     }     
184                     else if(err == "Error: no_certificates") {
185                         error = "No hay certificado disponible";
186                     }
187                     else if(err == "Error: no_implementation") {
188                         error = "No hay soporte para el manejo del certificado";
189                     }
190                    simple_modal(error);
191                });
192                       
193                },                                                             
194                error: function(jqXHR, textStatus, errorThrown){
195                        console.log('ajax error function: ' + jqXHR.responseText);
196                }
197               
198        });     
199}
200
201/**
202 * Función para enviar la firma al servidor
203 * @param signature Recibe la firma
204*/
205function FinalizarFirmaMultiples(signature){
206
207        $.ajax({
208                type: 'POST',
209                contentType: 'application/json',
210                url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfs/resenas",
211                dataType: 'json',
212                data: JSON.stringify({"signature":signature}),
213                xhrFields: {withCredentials: true},
214                headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
215                success: function(data, textStatus, jqXHR){
216            actualizar_participante(data['signedFileId']);
217                },
218                error: function(jqXHR, textStatus, errorThrown){
219                        console.log('error en pdfs/resenas: ' + textStatus);
220                }
221        });
222
223}
224
225/**
226 * Función para actualizar los datos del participante
227 * @param id_documento Recibe el id del documento
228*/
229function actualizar_participante(id_documento) {
230    var pasaporte = $('#id_pasaporte').val();
231    var routes = $(location).attr('pathname').split('/');
232    var pk = routes[routes.length-1];
233   
234    $.post(URL_ACTUALIZAR_PARTICIPACION,{'event_id':pk,'pasaporte':pasaporte,'serial':id_documento})
235    .done(function(data){
236        if (data.validate==true) {
237            simple_modal(data.mensaje);
238            $('#firmar').remove();
239        }
240        else{
241            simple_modal(data.mensaje);
242        }
243    })
244    .fail(function(err){
245        console.log(err);
246    });
247}
Note: See TracBrowser for help on using the repository browser.