1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- 'use strict';
- const Controller = require('egg').Controller;
- const filePath = require('../../config/filespath');
- class WanController extends Controller {
- // 修改wan信息
- async wanupdate() {
- const wanpath = filePath.wan;
- const configJson = require(filePath.configJson);
- const { address, netmask, gateway, type, dns, staticip } = this.ctx.request.body;
- const form = { address, netmask, gateway, type, dns, staticip };
- console.log(address, netmask, gateway, type, dns, staticip);
- if (type === 1) form.staticip = 0;
- try {
- if (staticip === 1) {
- const resolvstring = await this.service.fileshandler.fileread({ filePath: filePath.resolv });
- if (resolvstring.errcode === 0) {
- resolvstring.data.trim().split('\n').forEach(function(v) {
- if (v.includes('nameserver')) {
- form.dns = v.replace('nameserver', '');
- }
- });
- }
- }
- // 数据文件赋值
- configJson.wan = form;
- // 数据文件对象变字符串
- const jsonstr = JSON.stringify(configJson);
- // 写入数据文件
- await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
- // 写入字符串模板返回字符串
- const wanstr = await this.ctx.renderView('wan.nj', form);
- console.log(wanstr);
- if (wanstr) {
- // 写入wan文件
- await this.service.fileshandler.write({ filePath: wanpath, str: wanstr });
- this.ctx.body = { errcode: 0, errmsg: '' };
- }
- } catch (error) {
- const body = { errcode: -1002, errmsg: '设置失败', error };
- throw new Error(JSON.stringify(body));
- }
- }
- // 查询wan信息
- async wanquery() {
- const { ctx } = this;
- const configJson = require(filePath.configJson);
- const data = configJson.wan;
- ctx.body = { errcode: 0, errmsg: '', data };
- }
- }
- module.exports = WanController;
|