source: prototipo_portal_2018/prototipo/static/js/functions/firmarDocumentoPdf.js @ 8ba421e

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

Se los log

  • Property mode set to 100644
File size: 8.6 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'] +"\"><h4>Descargar archivo firmado</h4></a>";
100                        document.getElementById("log").innerHTML = '';   
101                        document.getElementById("respuesta").innerHTML = '<center><h2>Archivo firmado correctamente:</h2> <br>'+linkToDownload+'</center>';                     
102
103                        InfoDocumentDataTablePDF(data['signedFileId']);         
104
105                },
106                error: function(jqXHR, textStatus, errorThrown){
107                        alert('error en pdfs/resenas: ' + textStatus);
108                        $("#respuesta").html("error en pdfs/resenas: " + textStatus);
109                }
110        });
111
112}
113
114
115//Tercer paso (Obtenemos el hash de pdf enviado por el servidor y luego procesa la información en el token)
116function ObtenerHashPDFServer(parameters,cert){
117
118        $.ajax({
119                type: 'POST',
120                contentType: 'application/json',                               
121                //url:"https://192.168.12.125:8443/Murachi/0.1/archivos/pdfs",
122                url:"https://murachi.cenditel.gob.ve/Murachi/0.1/archivos/pdfs",
123                dataType: "json",
124                data: parameters,               
125                xhrFields: {withCredentials: true},
126                headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
127                success: function(data, textStatus, jqXHR){
128                        var json_x = data;
129                        var hash = json_x['hash']; 
130                        //alert("hash recibido del servidor "+hash);           
131                        var hashtype = "SHA-256";
132                        var lang = "eng";
133                       
134                        //Procesa la información
135                        window.hwcrypto.sign(cert, {type: hashtype, hex: hash}, {lang: lang}).then(
136                                function(signature) {
137                                        FinalizarFirma(signature.hex);
138                        }, 
139                        function(err) {
140                                log_text("sign() failed: " + err);
141                                        var error;
142                    log_text("sign() failed: " + err);
143                    if(err == "Error: user_cancel") {
144                        alert("sign() failed: El usuario cancelo la operación");
145                        error = "El usuario cancelo la operación"; 
146                     }     
147                     else if(err == "Error: no_certificates") {
148                         alert("sign() failed: No hay certificado disponible");
149                         error = "No hay certificado disponible";
150                     }
151                     else if(err == "Error: no_implementation") {
152                         alert("sign() failed: No hay soporte para el manejo del certificado");
153                         error = "No hay soporte para el manejo del certificado";
154                     }
155
156                                //alert("sign() failed: " + err);
157                                $("#respuesta").html("sign() failed: " + error);
158                });
159                       
160                },                                                             
161                error: function(jqXHR, textStatus, errorThrown){
162                        //alert('error: ' + textStatus);
163                        //var responseText = jQuery.parseJSON(jqXHR.responseText);
164                        alert('ajax error function: ' + jqXHR.responseText);
165                        $("#respuesta").html("error function: " + jqXHR.responseText);
166                }
167               
168        });
169               
170}
171
172
173//Segundo paso (Seleccionamos el Certificado Firmante)
174function ObtenerCertificadoFirmante(response){
175
176        // identificador del archivo en el servidor
177        var fileId = response.fileId.toString();
178        var cert;
179
180        window.hwcrypto.getCertificate({lang: "en"}).then(
181                function(response) {
182                        var cert = response;
183                        var parameters = "";
184
185                        if (FIRMA_VISIBLE == true){             
186                                parameters = JSON.stringify({
187                                        "fileId":fileId,
188                                        "certificate":cert.hex,
189                                        "reason":"Certificado",
190                                        "location":"CENDITEL",
191                                        "contact":"582746574336",
192                                        "signatureVisible":"true"
193                                        //"page": VALOR_PAGE,
194                                        //"coord_x": VALOR_X,
195                                        //"coord_y": VALOR_Y
196                                        });
197                        }
198                        else{
199                                parameters = JSON.stringify({
200                                        "fileId":fileId,
201                                        "certificate":cert.hex,
202                                        "reason":"Certificado",
203                                        "location":"CENDITEL",
204                                        "contact":"582746574336",
205                                        "signatureVisible":"true"
206                                        });                             
207                        }
208                        // ahora llamar al ajax de obtener la resena del pdf
209                        ObtenerHashPDFServer(parameters, cert); 
210
211                }, 
212                function(err) {
213                        log_text("getCertificate() failed: " + err);
214            var error;
215            if(err == "Error: user_cancel") {
216                alert("getCertificate() failed: El usuario cancelo la operación"    );
217                error = "El usuario cancelo la operación"; 
218             }     
219             else if(err == "Error: no_certificates") {
220                 alert("getCertificate() failed: No hay certificado disponible")    ;
221                 error = "No hay certificado disponible";
222             }
223             else if(err == "Error: no_implementation") {
224                 alert("getCertificate() failed: No hay soporte para el manejo del certificado");
225                 error = "No hay soporte para el manejo del certificado";
226                        }
227                        //alert("getCertificate() failed: " + err);
228                        $("#respuesta").html("getCertificate() failed: " + error);
229                }
230
231        );
232}
233
234
235
236
237// Primer paso (Subir el documento al servidor)
238function SubirDocumentServer(formData){
239
240    $.ajax({
241        //url: "https://192.168.12.125:8443/Murachi/0.1/archivos",               
242        url: "https://murachi.cenditel.gob.ve/Murachi/0.1/archivos",
243        type: "post",
244        dataType: "json",
245        data: formData,
246        cache: false,
247        contentType: false,
248                processData: false,
249                xhrFields: {withCredentials: true},
250                headers: {"Authorization":"Basic YWRtaW46YWRtaW4="},
251                success: function(response) {
252
253                        document.getElementById("respuesta").innerHTML = manejoJsonPDF(JSON.stringify(response));       
254                        ObtenerCertificadoFirmante(response);
255        },
256        error: function(response) {
257                //Que se ejecuta cuando finalice la petición de con error
258                        $("#respuesta").html('Error, al subir el archivo al servidor...!!!');
259                alert("ocurrio un error, al subir el archivo al servidor")
260                }
261    });
262
263}
264
265
266$("#Form-Format-Visible").on('submit', function(event) {
267        event.preventDefault();
268
269        var formData = new FormData(); 
270    formData.append("upload", $("#file-sign-ft_Vble")[0].files[0]);     
271       
272        $('#myJson_wrapper').hide();
273        FIRMA_VISIBLE = true;
274        SubirDocumentServer(formData);
275       
276});
Note: See TracBrowser for help on using the repository browser.