import React from "react"; import Link from "next/link"; import { SpinnerCircular } from "spinners-react"; import moment from "moment"; import dynamic from 'next/dynamic'; import { concatSeries } from 'async'; import fileDownload from 'js-file-download'; import FormMessage from "./FormMessage"; import { downloadQuestionnaire, uploadSignedFile } from "../../../actions"; //import { generateSignature } from "../../../utils/digital_signature"; export default class DigitalCertificates extends React.Component { constructor(props) { super(props); this.state = { loading: true, certificates: [], certificates_error: null, certificate_selected: undefined, signing: false, }; this.cryptopro = null; } async componentDidMount() { const { company, main, head_person } = this.props; //console.log({ company, main }); //console.log("DC", "CDM"); const { CryptoPro } = await import('ruscryptojs'); this.cryptopro = new CryptoPro(); this.cryptopro.init() .then((info) => { //console.log('Initialized', info); this.cryptopro.listCertificates() .then((certificates_list) => { //console.log({ certificates_list }); concatSeries(certificates_list, (certificate, callback) => { //console.log(certificate.id, certificate.name); this.cryptopro.certificateInfo(certificate.id) .then((cert) => { //console.log({ cert }); if(cert.IsValid) { if(cert.Subject['OGRNIP'] !== undefined && cert.Subject['OGRNIP'] !== null && cert.Subject['OGRNIP'] !== "") { if(cert.Subject['INN'] !== undefined && cert.Subject['INN'] !== null && cert.Subject['INN'] !== "") { if(company.inn === cert.Subject['INN']) { callback(null, [ { id: certificate.id, fields: cert.Subject, valid_to_date: moment(cert.ValidToDate).format("DD.MM.YYYY") } ]); } else { callback(null, []); } } else { callback(null, []); } } else { if(cert.Subject['INNLE'] !== undefined && cert.Subject['INNLE'] !== null && cert.Subject['INNLE'] !== "") { let owner_valid = true; const fields = [ "lastname", "firstname", "middlename", ]; const cert_owner = `${ cert.Subject['SN']} ${ cert.Subject['G']}`.toUpperCase(); for(let i in fields) { if(head_person[fields[i]] !== null && head_person[fields[i]] !== "") { if(cert_owner.indexOf(head_person[fields[i]].toUpperCase().trim()) < 0) { owner_valid = false; } } } if(owner_valid) { callback(null, [ { id: certificate.id, fields: cert.Subject, valid_to_date: moment(cert.ValidToDate).format("DD.MM.YYYY") } ]); } else { callback(null, []); } } else { callback(null, []); } } } else { callback(null, []); } }) .catch((error_certificates_info) => { console.error({ error_certificates_info }); this.setState({ loading: false, certificates_error: "CERTIFICATES" }); }); }, (error, certificates) => { if(certificates.length === 0) { this.setState({ loading: false, certificates_error: "ISSUED" }); } else { this.setState({ loading: false, certificates }); } }); }) .catch((error_certificates_list) => { console.error({ error_certificates_list }); this.setState({ loading: false, certificates_error: "CERTIFICATES" }); }); }) .catch((error_init) => { console.error({ error_init }); this.setState({ loading: false, certificates_error: "NOT_INSTALLED" }); }); } _sign = () => { this.setState({ signing: true }, () => { const { company, main, onSignDigital } = this.props; const filename = `${ main.inn }_questionnaire_${ moment().format("DDMMYYYY_HHmmss") }.sig`; downloadQuestionnaire({ filename, download: false, base64: true }) .then(({ file }) => { let file_to_sign = file; const now = moment().format("DDMMYYYY_HHmmss"); this.cryptopro.signData(file_to_sign, this.state.certificate_selected.id) .then(signature => { //console.log({ signature }); let signature_file = new File([ signature ], filename); uploadSignedFile(signature_file, company.questionnaire_id, true) .then(() => { onSignDigital(); }) .catch(() => { this.setState({ signing: false }); }); }) .catch((error_sign) => { console.error({ error_sign }); this.setState({ signing: false }); }) }) .catch((e) => { console.error("exception on sign"); console.error(e); }); }); } render() { const { company, main, head_person } = this.props; const { loading, certificates, certificates_error, certificate_selected, signing } = this.state; //console.log("render()", { certificates }); if(loading) { return (
Выберите подписанта