// 签名证书证书 'use strict'; const Controller = require('egg').Controller; const UUID = require('uuid'); const filePath = require('../../config/filespath'); class SigcertController extends Controller { // 添加申请书 async sigcertreq() { const { ctx } = this; const uuid = UUID.v1(); const { dn, pwatype, name } = ctx.request.body; const state = 0; try { // 创建key const reskey = await this.service.shell.applykey({ ...ctx.request.body, filePath: `${filePath.keys}${uuid}.key` }); if (reskey.errcode === 0) { // 创建req const resreq = await this.service.shell.applyreq({ ...ctx.request.body, keyPath: `${filePath.keys}${uuid}.key`, filePath: `${filePath.req}${uuid}.pem` }); if (resreq.errcode === 0) { const form = { state, dn, pwatype, name, uuid }; const person = require(filePath.configJson); person.cert.push(form); const jsonstr = JSON.stringify(person); // 添加到数据文件 await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr }); this.ctx.body = { errcode: 0, errmsg: '' }; } } } catch (error) { const body = { errcode: -1002, errmsg: '创建申请书失败', error }; throw new Error(JSON.stringify(body)); } } // 签名证书查询 async sigcacertquery() { const { ctx } = this; const person = require(filePath.configJson); const data = person.cert; const total = data.length || 0; ctx.body = { errcode: 0, errmsg: '', data, total }; } // 删除签名证书 async sigcertdelete() { const uuid = this.ctx.query.uuid; const configJson = require(filePath.configJson); // 过滤掉当前uuid数据 const data = configJson.cert.filter(p => p.uuid !== uuid); const isdata = configJson.cert.filter(p => p.uuid === uuid); configJson.cert = data; const jsonstr = JSON.stringify(configJson); try { // 存储到数据文件 await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr }); const files = []; if (isdata[0].state === 1) { files.push({ path: `${filePath.cert}${uuid}.cer` }, { path: `${filePath.cert}${uuid}.key` }); } else { files.push({ path: `${filePath.req}${uuid}.pem` }); } // 删除证书文件 const res = await Promise.all(files.filter(async p => { await this.service.fileshandler.filesDel(p.path); })); if (res.length > 0) { this.ctx.body = { errcode: 0, errmsg: '' }; } else { this.ctx.body = { errcode: -1, errmsg: '证书删除失败' }; } } catch (error) { const body = { errcode: -1002, errmsg: '证书删除失败', error }; throw new Error(JSON.stringify(body)); } } // 签名证书上传 async sigcacertupload() { const { ctx } = this; const person = require(filePath.configJson); const stream = await this.ctx.getFileStream(); const uuid = stream.fields.uuid; // 路径 + 文件名 const name = `${filePath.cert}${uuid}.cer`; try { const res = await this.service.fileshandler.upload({ stream, name }); if (res.errcode === 0) { person.cert.map(p => { if (p.uuid === uuid) { p.state = 1; } return p; }); const jsonstr = JSON.stringify(person); await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr }); ctx.body = { errcode: 0, errmsg: '' }; } } catch (error) { const body = { errcode: -1002, errmsg: '证书上传失败', error }; throw new Error(JSON.stringify(body)); } } // 申请书下载 async reqdownload() { try { const uuid = this.ctx.query.uuid; const reqfilePath = `${filePath.req}${uuid}.pem`; const res = await this.service.fileshandler.download({ filePath: reqfilePath }); this.ctx.body = res; } catch (error) { const body = { errcode: -1002, errmsg: '申请书下载失败', error }; throw new Error(JSON.stringify(body)); } } // 签名证书下载 async sigcertdownload() { try { const uuid = this.ctx.query.uuid; const sigfilePath = `${filePath.cert}${uuid}.cer`; const res = await this.service.fileshandler.download({ filePath: sigfilePath }); this.ctx.body = res; } catch (error) { const body = { errcode: -1002, errmsg: '证书下载失败', error }; throw new Error(JSON.stringify(body)); } } } module.exports = SigcertController;