card.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. class CertController extends Controller {
  4. // wan添加
  5. async wanadd() {
  6. const login = await this.service.files.login();
  7. if (login.errcode !== 0) {
  8. this.ctx.body = login;
  9. return false;
  10. }
  11. try {
  12. const { ctx } = this;
  13. const { address, netmask, gateway, type } = ctx.request.body;
  14. const form = { type, address: address || '192.168.0.10', netmask: netmask || '255.255.255.0', gateway: gateway || '192.168.0.1' };
  15. const filePath = this.app.config.filePath.configJson;
  16. const person = require(this.app.config.filePath.configJson);
  17. person.wan = form;
  18. const jsonstr = JSON.stringify(person);
  19. const paths = this.app.config.filePath.wan;
  20. // 写入自己管理的文件
  21. await this.service.files.write({ filePath, str: jsonstr });
  22. const wanstr = await ctx.renderView('wan.nj', form);
  23. console.log(wanstr);
  24. // 写入配置文件
  25. await this.service.files.write({ filePath: paths, str: wanstr });
  26. ctx.body = { errcode: 0, errmsg: '' };
  27. } catch (error) {
  28. // this.ctx.body = { errcode: -2, errmsg: error };
  29. throw error;
  30. }
  31. }
  32. // lan添加
  33. async lanadd() {
  34. const login = await this.service.files.login();
  35. if (login.errcode !== 0) {
  36. this.ctx.body = login;
  37. return false;
  38. }
  39. const { ctx } = this;
  40. try {
  41. const form = { ...ctx.request.body };
  42. const filePath = this.app.config.filePath.configJson;
  43. const person = require(this.app.config.filePath.configJson);
  44. person.lan = { address: form.address || '192.168.3.1', type: form.type };
  45. person.dhcp = { start: form.start || '120', end: form.end || '200', address: form.address || '192.168.3.1' };
  46. const jsonstr = JSON.stringify(person);
  47. await this.service.files.write({ filePath, str: jsonstr });
  48. const items = [];
  49. if (!form.address) {
  50. form.address = '192.168.3.1';
  51. }
  52. for (let index = 0; index < form.address.length; index++) {
  53. if (form.address[index] === '.') {
  54. items.push(index);
  55. }
  56. }
  57. form.addressTow = form.address.slice(0, items[2]);
  58. const lanstr = await ctx.renderView('lan.nj', form);
  59. const paths = this.app.config.filePath.lan;
  60. await this.service.files.write({ filePath: paths, str: lanstr });
  61. if (form.type === 0) {
  62. form.start = form.start || '120';
  63. form.end = form.end || '200';
  64. const dhcpstr = await ctx.renderView('dhcp.nj', form);
  65. // 写入dhcp
  66. const dhcppath = this.app.config.filePath.dhcpd;
  67. const dhcpres = await this.service.files.write({ filePath: dhcppath, str: dhcpstr });
  68. // 启动dhcp
  69. if (dhcpres.errcode === 0) {
  70. await this.service.files.dhcp({ type: 'enable' });
  71. }
  72. } else {
  73. // 停止dhcp服务
  74. await this.service.files.dhcp({ type: 'disable' });
  75. }
  76. ctx.body = { errcode: 0, errmsg: '' };
  77. } catch (error) {
  78. // console.log(error);
  79. // this.ctx.body = { errcode: -2, errmsg: error };
  80. throw error;
  81. }
  82. }
  83. // wan查询
  84. async wanquery() {
  85. try {
  86. const { ctx } = this;
  87. const person = require(this.app.config.filePath.configJson);
  88. const data = person.wan;
  89. ctx.body = { errcode: 0, errmsg: '', data };
  90. } catch (error) {
  91. // this.ctx.body = { errcode: -2, errmsg: error };
  92. throw error;
  93. }
  94. }
  95. // lan查询
  96. async lanquery() {
  97. try {
  98. const { ctx } = this;
  99. const person = require(`${this.app.config.filePath.configJson}`);
  100. const data = person.lan;
  101. ctx.body = { errcode: 0, errmsg: '', data };
  102. } catch (error) {
  103. // this.ctx.body = { errcode: -2, errmsg: error };
  104. throw error;
  105. }
  106. }
  107. // dhcp查询
  108. async dhcpquery() {
  109. try {
  110. const { ctx } = this;
  111. const person = require(this.app.config.filePath.configJson);
  112. const data = person.dhcp;
  113. ctx.body = { errcode: 0, errmsg: '', data };
  114. } catch (error) {
  115. // this.ctx.body = { errcode: -2, errmsg: error };
  116. throw error;
  117. }
  118. }
  119. }
  120. module.exports = CertController;