wan.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const filePath = require('../../config/filespath');
  4. class WanController extends Controller {
  5. // 修改wan信息
  6. async wanupdate() {
  7. const wanpath = filePath.wan;
  8. const configJson = require(filePath.configJson);
  9. const { address, netmask, gateway, type, dns, staticip } = this.ctx.request.body;
  10. const form = { address, netmask, gateway, type, dns, staticip };
  11. console.log(address, netmask, gateway, type, dns, staticip);
  12. if (type === 1) form.staticip = 0;
  13. try {
  14. if (staticip === 1) {
  15. const resolvstring = await this.service.fileshandler.fileread({ filePath: filePath.resolv });
  16. if (resolvstring.errcode === 0) {
  17. resolvstring.data.trim().split('\n').forEach(function(v) {
  18. if (v.includes('nameserver')) {
  19. form.dns = v.replace('nameserver', '');
  20. }
  21. });
  22. }
  23. }
  24. // 数据文件赋值
  25. configJson.wan = form;
  26. // 数据文件对象变字符串
  27. const jsonstr = JSON.stringify(configJson);
  28. // 写入数据文件
  29. await this.service.fileshandler.write({ filePath: filePath.configJson, str: jsonstr });
  30. // 写入字符串模板返回字符串
  31. const wanstr = await this.ctx.renderView('wan.nj', form);
  32. console.log(wanstr);
  33. if (wanstr) {
  34. // 写入wan文件
  35. await this.service.fileshandler.write({ filePath: wanpath, str: wanstr });
  36. this.ctx.body = { errcode: 0, errmsg: '' };
  37. }
  38. } catch (error) {
  39. const body = { errcode: -1002, errmsg: '设置失败', error };
  40. throw new Error(JSON.stringify(body));
  41. }
  42. }
  43. // 查询wan信息
  44. async wanquery() {
  45. const { ctx } = this;
  46. const configJson = require(filePath.configJson);
  47. const data = configJson.wan;
  48. ctx.body = { errcode: 0, errmsg: '', data };
  49. }
  50. }
  51. module.exports = WanController;