sigcert.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. // 签名证书证书
  2. 'use strict';
  3. const Controller = require('egg').Controller;
  4. const UUID = require('uuid');
  5. const filePath = require('../../config/filespath');
  6. class SigcertController extends Controller {
  7. // 添加申请书
  8. async sigcertreq() {
  9. const { ctx } = this;
  10. const uuid = UUID.v1();
  11. const { dn, pwatype, name } = ctx.request.body;
  12. const state = 0;
  13. try {
  14. // 创建key
  15. const reskey = await this.service.shell.applykey({ ...ctx.request.body, filePath: `${filePath.keys}${uuid}.key` });
  16. if (reskey.errcode === 0) {
  17. // 创建req
  18. const resreq = await this.service.shell.applyreq({ ...ctx.request.body, keyPath: `${filePath.keys}${uuid}.key`, filePath: `${filePath.req}${uuid}.pem` });
  19. if (resreq.errcode === 0) {
  20. const form = { state, dn, pwatype, name, uuid };
  21. const person = require(filePath.configJson);
  22. person.cert.push(form);
  23. const jsonstr = JSON.stringify(person);
  24. // 添加到数据文件
  25. await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
  26. this.ctx.body = { errcode: 0, errmsg: '' };
  27. }
  28. }
  29. } catch (error) {
  30. const body = { errcode: -1002, errmsg: '创建申请书失败', error };
  31. throw new Error(JSON.stringify(body));
  32. }
  33. }
  34. // 签名证书查询
  35. async sigcacertquery() {
  36. const { ctx } = this;
  37. const person = require(filePath.configJson);
  38. const data = person.cert;
  39. const total = data.length || 0;
  40. ctx.body = { errcode: 0, errmsg: '', data, total };
  41. }
  42. // 删除签名证书
  43. async sigcertdelete() {
  44. const uuid = this.ctx.query.uuid;
  45. const configJson = require(filePath.configJson);
  46. // 过滤掉当前uuid数据
  47. const data = configJson.cert.filter(p => p.uuid !== uuid);
  48. const isdata = configJson.cert.filter(p => p.uuid === uuid);
  49. configJson.cert = data;
  50. const jsonstr = JSON.stringify(configJson);
  51. try {
  52. // 存储到数据文件
  53. await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
  54. const files = [];
  55. if (isdata[0].state === 1) {
  56. files.push({ path: `${filePath.cert}${uuid}.cer` }, { path: `${filePath.cert}${uuid}.key` });
  57. } else {
  58. files.push({ path: `${filePath.req}${uuid}.pem` });
  59. }
  60. // 删除证书文件
  61. const res = await Promise.all(files.filter(async p => {
  62. await this.service.fileshandler.filesDel(p.path);
  63. }));
  64. if (res.length > 0) {
  65. this.ctx.body = { errcode: 0, errmsg: '' };
  66. } else {
  67. this.ctx.body = { errcode: -1, errmsg: '证书删除失败' };
  68. }
  69. } catch (error) {
  70. const body = { errcode: -1002, errmsg: '证书删除失败', error };
  71. throw new Error(JSON.stringify(body));
  72. }
  73. }
  74. // 签名证书上传
  75. async sigcacertupload() {
  76. const { ctx } = this;
  77. const person = require(filePath.configJson);
  78. const stream = await this.ctx.getFileStream();
  79. const uuid = stream.fields.uuid;
  80. // 路径 + 文件名
  81. const name = `${filePath.cert}${uuid}.cer`;
  82. try {
  83. const res = await this.service.fileshandler.upload({ stream, name });
  84. if (res.errcode === 0) {
  85. person.cert.map(p => {
  86. if (p.uuid === uuid) {
  87. p.state = 1;
  88. }
  89. return p;
  90. });
  91. const jsonstr = JSON.stringify(person);
  92. await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
  93. ctx.body = { errcode: 0, errmsg: '' };
  94. }
  95. } catch (error) {
  96. const body = { errcode: -1002, errmsg: '证书上传失败', error };
  97. throw new Error(JSON.stringify(body));
  98. }
  99. }
  100. // 申请书下载
  101. async reqdownload() {
  102. try {
  103. const uuid = this.ctx.query.uuid;
  104. const reqfilePath = `${filePath.req}${uuid}.pem`;
  105. const res = await this.service.fileshandler.download({ filePath: reqfilePath });
  106. this.ctx.body = res;
  107. } catch (error) {
  108. const body = { errcode: -1002, errmsg: '申请书下载失败', error };
  109. throw new Error(JSON.stringify(body));
  110. }
  111. }
  112. // 签名证书下载
  113. async sigcertdownload() {
  114. try {
  115. const uuid = this.ctx.query.uuid;
  116. const sigfilePath = `${filePath.cert}${uuid}.cer`;
  117. const res = await this.service.fileshandler.download({ filePath: sigfilePath });
  118. this.ctx.body = res;
  119. } catch (error) {
  120. const body = { errcode: -1002, errmsg: '证书下载失败', error };
  121. throw new Error(JSON.stringify(body));
  122. }
  123. }
  124. }
  125. module.exports = SigcertController;