source: prototipo_portal_2018/prototipo/static/js/functions/firmarDocumentoPdf.js @ 9be8a48

Last change on this file since 9be8a48 was 9be8a48, checked in by José Sulbarán <jsulbaran@…>, 7 years ago

Se agrego la firma bdoc y se mejoro la firma pdf

  • Property mode set to 100644
File size: 8.5 KB
Line 
1
2function True_False(data){
3
4    if (data == "true" ){
5      return "Verdadero";
6    }
7    if (data ==  "false"){
8      return "Falso";
9    }
10
11}
12
13function SerealizeMyJson(data){
14  for (var i = 0; i < data.signatures.length; i++) {
15      data.signatures[i].integrityCheck = True_False(data.signatures[i].integrityCheck);
16      data.signatures[i].signerCertificateStillValid = True_False(data.signatures[i].signerCertificateStillValid);
17      data.signatures[i].signerCertificateValidAtTimeOfSigning = True_False(data.signatures[i].signerCertificateValidAtTimeOfSigning);
18      data.signatures[i].signatureCoversWholeDocument = True_False(data.signatures[i].signatureCoversWholeDocument);
19      data.signatures[i].certificatesVerifiedAgainstTheKeyStore = True_False(data.signatures[i].certificatesVerifiedAgainstTheKeyStore);         
20  }
21  return data;
22}
23
24
25//Obtenemos la  informacion de documento
26function InfoDocumentDataTablePDF(signedFileId){
27
28    $.ajax({
29        url: "https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/"+signedFileId,
30        type: "get",
31        dataType: "json",       
32        headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
33        success: function(response) {
34           
35                        if (FIRMA_VISIBLE == true){             
36
37                        INFO_DATATABLE = SerealizeMyJson(response);
38                               
39                                CONT += 1;
40                                if (CONT == 1){
41                                        DATATABLE_SIGN_VISIBLE = DataTablePDF(response);       
42
43                                }
44                                if (CONT > 1){
45                                        DATATABLE_SIGN_VISIBLE.destroy();
46                                        DATATABLE_SIGN_VISIBLE = DataTablePDF(response);       
47                                }       
48
49                                CONTAINER = false;
50                                $("#firmar-documento").hide();
51                                $("#pdf-main-container").hide();
52                                $("#myJson_wrapper").show();                                   
53                        }
54                        else{
55
56
57                        INFO_DATATABLE_NO_VISIBLE = SerealizeMyJson(response);
58                               
59                                CONT_NO_VISIBLE += 1;
60                                if (CONT_NO_VISIBLE == 1){
61                                        DATATABLE_SIGN_NO_VISIBLE = DataTablePDFNoVisible(response);   
62
63                                }
64                                if (CONT_NO_VISIBLE > 1){
65                                        DATATABLE_SIGN_NO_VISIBLE.destroy();
66                                        DATATABLE_SIGN_NO_VISIBLE = DataTablePDFNoVisible(response);   
67                                }       
68
69                                CONTAINER2 = false;                             
70                                $("#firmar-doc-noVisible").hide();
71                                $("#pdf-main-container2").hide();
72                                $("#myJsonPDF_NO_VISIBLE_wrapper").show();
73
74
75                        }
76                       
77        },
78        error: function(jqXHR, textStatus, errorThrown){
79                alert(textStatus+", "+ errorThrown+" el documento PDF para mostrar la info de la sign");     
80        }
81        });
82}
83
84
85// Cuarto paso (Se envia la información del token para terminar la firma)
86function FinalizarFirma(signature){
87
88        $.ajax({
89                type: 'POST',
90                contentType: 'application/json',
91                //url:"https://192.168.12.125:8443/Murachi/0.1/archivos/pdfs/resenas",
92                url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfs/resenas",
93                dataType: 'json',
94                data: JSON.stringify({"signature":signature}),
95                xhrFields: {withCredentials: true},
96                headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
97                success: function(data, textStatus, jqXHR){
98
99                        var linkToDownload = "<a href=\"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/descargas/" + data['signedFileId'] +"\">descargar archivo firmado</a>";
100                        document.getElementById("respuesta").innerHTML = 'Archivo firmado correctamente: ' + data['signedFileId']+ '<br>' +linkToDownload;     
101                       
102                        InfoDocumentDataTablePDF(data['signedFileId']);         
103
104                },
105                error: function(jqXHR, textStatus, errorThrown){
106                        alert('error en pdfs/resenas: ' + textStatus);
107                        $("#respuesta").html("error en pdfs/resenas: " + textStatus);
108                }
109        });
110
111}
112
113
114//Tercer paso (Obtenemos el hash de pdf enviado por el servidor y luego procesa la información en el token)
115function ObtenerHashPDFServer(parameters,cert){
116
117        $.ajax({
118                type: 'POST',
119                contentType: 'application/json',                               
120                //url:"https://192.168.12.125:8443/Murachi/0.1/archivos/pdfs",
121                url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfs",
122                dataType: "json",
123                data: parameters,               
124                xhrFields: {withCredentials: true},
125                headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
126                success: function(data, textStatus, jqXHR){
127                        var json_x = data;
128                        var hash = json_x['hash']; 
129                        //alert("hash recibido del servidor "+hash);           
130                        var hashtype = "SHA-256";
131                        var lang = "eng";
132                       
133                        //Procesa la información
134                        window.hwcrypto.sign(cert, {type: hashtype, hex: hash}, {lang: lang}).then(
135                                function(signature) {
136                                        FinalizarFirma(signature.hex);
137                        }, 
138                        function(err) {
139                                log_text("sign() failed: " + err);
140                                        var error;
141                    log_text("sign() failed: " + err);
142                    if(err == "Error: user_cancel") {
143                        alert("sign() failed: El usuario cancelo la operación");
144                        error = "El usuario cancelo la operación"; 
145                     }     
146                     else if(err == "Error: no_certificates") {
147                         alert("sign() failed: No hay certificado disponible");
148                         error = "No hay certificado disponible";
149                     }
150                     else if(err == "Error: no_implementation") {
151                         alert("sign() failed: No hay soporte para el manejo del certificado");
152                         error = "No hay soporte para el manejo del certificado";
153                     }
154
155                                //alert("sign() failed: " + err);
156                                $("#respuesta").html("sign() failed: " + error);
157                });
158                       
159                },                                                             
160                error: function(jqXHR, textStatus, errorThrown){
161                        //alert('error: ' + textStatus);
162                        //var responseText = jQuery.parseJSON(jqXHR.responseText);
163                        alert('ajax error function: ' + jqXHR.responseText);
164                        $("#respuesta").html("error function: " + jqXHR.responseText);
165                }
166               
167        });
168               
169}
170
171
172//Segundo paso (Seleccionamos el Certificado Firmante)
173function ObtenerCertificadoFirmante(response){
174
175        // identificador del archivo en el servidor
176        var fileId = response.fileId.toString();
177        var cert;
178
179        window.hwcrypto.getCertificate({lang: "en"}).then(
180                function(response) {
181                        var cert = response;
182                        var parameters = "";
183
184                        if (FIRMA_VISIBLE == true){             
185                                parameters = JSON.stringify({
186                                        "fileId":fileId,
187                                        "certificate":cert.hex,
188                                        "reason":"Certificado",
189                                        "location":"CENDITEL",
190                                        "contact":"582746574336",
191                                        "signatureVisible":"true"
192                                        "page": VALOR_PAGE,
193                                        "coord_x": VALOR_X,
194                                        "coord_y": VALOR_Y
195                                        });
196                        }
197                        else{
198                                parameters = JSON.stringify({
199                                        "fileId":fileId,
200                                        "certificate":cert.hex,
201                                        "reason":"Certificado",
202                                        "location":"CENDITEL",
203                                        "contact":"582746574336",
204                                        "signatureVisible":"true"
205                                        });                             
206                        }
207                        // ahora llamar al ajax de obtener la resena del pdf
208                        ObtenerHashPDFServer(parameters, cert); 
209
210                }, 
211                function(err) {
212                        log_text("getCertificate() failed: " + err);
213            var error;
214            if(err == "Error: user_cancel") {
215                alert("getCertificate() failed: El usuario cancelo la operación"    );
216                error = "El usuario cancelo la operación"; 
217             }     
218             else if(err == "Error: no_certificates") {
219                 alert("getCertificate() failed: No hay certificado disponible")    ;
220                 error = "No hay certificado disponible";
221             }
222             else if(err == "Error: no_implementation") {
223                 alert("getCertificate() failed: No hay soporte para el manejo del certificado");
224                 error = "No hay soporte para el manejo del certificado";
225                        }
226                        //alert("getCertificate() failed: " + err);
227                        $("#respuesta").html("getCertificate() failed: " + error);
228                }
229
230        );
231}
232
233
234
235
236// Primer paso (Subir el documento al servidor)
237function SubirDocumentServer(formData){
238
239    $.ajax({
240        //url: "https://192.168.12.125:8443/Murachi/0.1/archivos",               
241        url: "https://murachi.cenditel.gob.ve/Murachi/0.1/archivos",
242        type: "post",
243        dataType: "json",
244        data: formData,
245        cache: false,
246        contentType: false,
247                processData: false,
248                xhrFields: {withCredentials: true},
249                headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
250                success: function(response) {
251
252                        document.getElementById("respuesta").innerHTML = manejoJsonPDF(JSON.stringify(response));       
253                        ObtenerCertificadoFirmante(response);
254        },
255        error: function(response) {
256                //Que se ejecuta cuando finalice la petición de con error
257                        $("#respuesta").html('Error, al subir el archivo al servidor...!!!');
258                alert("ocurrio un error, al subir el archivo al servidor")
259                }
260    });
261
262}
263
264
265$("#Form-Format-Visible").on('submit', function(event) {
266        event.preventDefault();
267
268        var formData = new FormData(); 
269    formData.append("upload", $("#file-sign-ft_Vble")[0].files[0]);     
270       
271        $('#myJson_wrapper').hide();
272        FIRMA_VISIBLE = true;
273        SubirDocumentServer(formData);
274       
275});
Note: See TracBrowser for help on using the repository browser.