// lan 'use strict'; const Controller = require('egg').Controller; const filePath = require('../../config/filespath'); const shells = require('../../config/shells'); class LanController extends Controller { // 修改lan信息 async lanupdate() { const lanpath = filePath.lan; const configJson = require(filePath.configJson); const { address, start, end, type } = this.ctx.request.body; const form = { address, start, end, type }; form.addressTow = form.address.substring(0, form.address.lastIndexOf('.')); // 数据文件赋值 configJson.lan = form; // 数据文件对象变字符串 const jsonstr = JSON.stringify(configJson); try { // 写入数据文件 await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr }); // 写入字符串模板 const lanstr = await this.ctx.renderView('lan.nj', form); // 写入lan文件 await this.service.fileshandler.write({ filePath: lanpath, str: lanstr }); // dhcp部分 if (type === '0') { // 写入dhcp字符串模板 const dhcpstr = await this.ctx.renderView('dhcp.nj', form); const dhcpres = await this.service.fileshandler.write({ filePath: filePath.dhcpd, str: dhcpstr }); // 启动dhcp if (dhcpres.errcode === 0) { await this.service.shell.shell(shells.dhcpRestart); } } else { // 停止dhcp服务 await this.service.shell.shell(shells.dhcpStop); } this.ctx.body = { errcode: 0, errmsg: '' }; } catch (error) { const body = { errcode: -1004, errmsg: '设置失败', error }; throw new Error(JSON.stringify(body)); } } // 查询lan信息 async lanquery() { const { ctx } = this; const configJson = require(filePath.configJson); const data = configJson.lan; ctx.body = { errcode: 0, errmsg: '', data }; } } module.exports = LanController;