lan.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // lan
  2. 'use strict';
  3. const Controller = require('egg').Controller;
  4. const filePath = require('../../config/filespath');
  5. const shells = require('../../config/shells');
  6. class LanController extends Controller {
  7. // 修改lan信息
  8. async lanupdate() {
  9. const lanpath = filePath.lan;
  10. const configJson = require(filePath.configJson);
  11. const { address, start, end, type } = this.ctx.request.body;
  12. const form = { address, start, end, type };
  13. form.addressTow = form.address.substring(0, form.address.lastIndexOf('.'));
  14. // 数据文件赋值
  15. configJson.lan = form;
  16. // 数据文件对象变字符串
  17. const jsonstr = JSON.stringify(configJson);
  18. try {
  19. // 写入数据文件
  20. await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
  21. // 写入字符串模板
  22. const lanstr = await this.ctx.renderView('lan.nj', form);
  23. // 写入lan文件
  24. await this.service.fileshandler.write({ filePath: lanpath, str: lanstr });
  25. // dhcp部分
  26. if (type === '0') {
  27. // 写入dhcp字符串模板
  28. const dhcpstr = await this.ctx.renderView('dhcp.nj', form);
  29. const dhcpres = await this.service.fileshandler.write({ filePath: filePath.dhcpd, str: dhcpstr });
  30. // 启动dhcp
  31. if (dhcpres.errcode === 0) {
  32. await this.service.shell.shell(shells.dhcpRestart);
  33. }
  34. } else {
  35. // 停止dhcp服务
  36. await this.service.shell.shell(shells.dhcpStop);
  37. }
  38. this.ctx.body = { errcode: 0, errmsg: '' };
  39. } catch (error) {
  40. const body = { errcode: -1002, errmsg: '设置失败', error };
  41. throw new Error(JSON.stringify(body));
  42. }
  43. }
  44. // 查询lan信息
  45. async lanquery() {
  46. const { ctx } = this;
  47. const configJson = require(filePath.configJson);
  48. const data = configJson.lan;
  49. ctx.body = { errcode: 0, errmsg: '', data };
  50. }
  51. }
  52. module.exports = LanController;