import React from "react"; import Link from "next/link"; import { SpinnerCircular } from "spinners-react"; import moment from "moment"; import fileDownload from 'js-file-download'; import { getCertificates, isPluginCryptoProInstalled, generateSignature } from "../../../utils/digital_signature"; import { downloadQuestionnaire, uploadSignedFile } from "../../../actions"; export default class DigitalSignaturesList extends React.Component { constructor(props) { super(props); this.state = { loading: true, certificates_error: null, certificate_selected: undefined, }; } componentDidMount() { const script = document.createElement("script"); script.src = `${ process.env.NEXT_PUBLIC_SELF_API_HOST }/assets/js/cadesplugin_api.js`; script.async = true; document.body.appendChild(script); setTimeout(() => { isPluginCryptoProInstalled() .then(setTimeout(() => { getCertificates() .then(certificates => { const certificates_list = []; for(let i in certificates) { const certificate = certificates[i]; let today = moment(); if(today < moment(certificate.info.validToDate)) { certificate.info.validToDate = moment(certificate.info.validToDate).format('DD.MM.YYYY'); certificates_list.push(certificate); } } this.setState({ certificates: certificates_list, certificates_error: certificates_list.length > 0 ? null : "ISSUED", loading: false }); }) .catch((error_get_certificates) => { console.error("error_get_certificates"); console.error(error_get_certificates); console.log("certificates", 'OTHER'); this.setState({ certificates: [], certificates_error: "OTHER", loading: false }); }) }, 500) ) .catch((error_plugin_installed) => { console.error("error_plugin_installed"); console.error(error_plugin_installed); console.log("certificates", 'NOT_INSTALLED'); this.setState({ certificates: [], certificates_error: "NOT_INSTALLED", loading: false }) }); }, 1000); } componentDidUpdate(prevProps, prevState) { } _sign = () => { const { company, main, onSignDigital } = this.props; const filename = `${ main.inn }_questionnaire_${ moment().format("DDMMYYYY_HHmmss") }.pdf`; downloadQuestionnaire({ filename, download: false }) .then(({ file }) => { //let created_at = moment().format('DD-MM-yyyy'); //let filename = `Коммерческое предложение №${this.props.proposal.proposalId} от ${createAt}.docx` let file_to_sign = new File([ file ], filename); console.log("this.state", this.state); console.log("file"); console.log(file); console.log("file_to_sign"); console.log(file_to_sign); const now = moment().format("DDMMYYYY_HHmmss"); generateSignature(file_to_sign, this.state.certificate_selected.certificate) .then(signature => { console.log("signature"); console.log(signature); uploadSignedFile(signature, company.questionnaire_id) .then(() => { onSignDigital(); }) .catch(() => { }); //fileDownload(signature, `${ now }_${ filename }.sig`); //fileDownload(file, `${ now }_${ filename }`); //alert("Подписание ЭЦП прошло успешно."); }); }) .catch((e) => { console.error("exception on sign"); console.error(e); let errorsText = { 'The action was cancelled by the user. (0x8010006E)': 'Подписание было отменено', 'Key does not exist. (0x8009000D)': 'Сертификата не существует', 'Keyset does not exist (0x80090016)': 'Набор ключей не существует', 'File upload error': 'Ошибка загрузки файла', 'Invalid algorithm specified. (0x80090008)': 'На вашем компьютере не установлена программа «КриптоПро CSP»', 'The card cannot be accessed because the wrong PIN was presented. (0x8010006B)': 'Указанный пароль неверный' } if(e !== undefined) { let errorText = errorsText[e.message] || 'Ошибка подписания файла'; alert(errorText); } }); } render() { const { certificates, certificates_error, certificate_selected, loading } = this.state; console.log(certificates); if(loading) { return (
Выберите подписанта
Ошибка Плагин КриптоПРО не установлен, посмотрите инструкцию как установить плагин КриптоПРО.
) } { certificates_error === "OTHER" && (Ошибка Плагин КриптоПРО не активирован, пожалуйста, обновите страницу и подтвердите разрешение для сайта на доступ к списку сертификатов.
) } { certificates_error === "ISSUED" && (Ошибка Отсутствуют действующие сертификаты.
) } { certificates_error === "MISMATCH" && (Ошибка Подписант не соответствует указанному подписанту в анкете.
) }