You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

233 lines
10 KiB
C#

using Newtonsoft.Json;
using Prism.Commands;
using Prism.Ioc;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using StartServerWPF.Models;
using StartServerWPF.Modules.Main.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace StartServerWPF.Modules.Main.ViewModels
{
public class SetParamViewModel : BindableBase
{
public SetParamViewModel(SystemConfigModel configModel, WorkareaModelArray workareaModelArray)
{
_systemConfig = configModel;
this._workareaModelArray = workareaModelArray;
MainModel = _systemConfig.vpnInfo;
WorkareaNameSource = new ObservableCollection<string>();
_workareaModelArray.workarea.Select(n=>n.workareaname).ToList().ForEach(a=> WorkareaNameSource.Add(a));
WareaSelectedIndex = _workareaModelArray.selectIndex;
WareaModel = _workareaModelArray.workarea[WareaSelectedIndex];
}
private string _title = "应用列表";
public string Title => _title;
string apmStationFile = string.Empty;
string apmttime2dFile = string.Empty;
private SystemConfigModel _systemConfig;
private readonly WorkareaModelArray _workareaModelArray;
private ObservableCollection<string> _workareaNameSource;
public ObservableCollection<string> WorkareaNameSource
{
get => _workareaNameSource;
set { SetProperty(ref _workareaNameSource, value); }
}
private int _wareaSelectedIndex;
public int WareaSelectedIndex
{
get => _wareaSelectedIndex;
set {
if (value >= 0)
{
WareaModel = _workareaModelArray.workarea[value];
}
SetProperty(ref _wareaSelectedIndex, value);
}
}
private WorkareaModel _wareaModel = new WorkareaModel();
public WorkareaModel WareaModel
{
get => _wareaModel;
set { SetProperty(ref _wareaModel, value); }
}
private string _addWorkareaName;
public string AddWorkareaName
{
get => _addWorkareaName;
set { SetProperty(ref _addWorkareaName, value); }
}
private VpnInfo _mainModel = new VpnInfo();
public VpnInfo MainModel
{
get => _mainModel;
set { SetProperty(ref _mainModel, value); }
}
public DelegateCommand LoadedCommand => new(() =>
{
apmStationFile = string.Empty;
apmttime2dFile = string.Empty;
});
public DelegateCommand AddItemCommand => new(() =>
{
string strJson= JsonConvert.SerializeObject(WareaModel, Formatting.Indented);
WorkareaModel workarea = JsonConvert.DeserializeObject<WorkareaModel>(strJson);
workarea.workareaname = AddWorkareaName;
var mod = _workareaModelArray.workarea.Where(w => w.workareaname == _addWorkareaName).FirstOrDefault();
if (mod != null)
{
MessageBox.Show($"名称:{AddWorkareaName}已存在");
return;
}
workarea.chartsavepath = "EVT2014";
workarea.apmsavepath = "NET2014";
workarea.toolsavepath = "post";
workarea.StationConfig.Stations.ForEach(w => { w.IsEnable= true; });
_workareaModelArray.workarea.Add(workarea);
WorkareaNameSource.Add(AddWorkareaName);
});
public DelegateCommand<object> SelectFileCommand => new((obj) =>
{
string para = obj.ToString();
System.Windows.Forms.OpenFileDialog fbd = new System.Windows.Forms.OpenFileDialog();
fbd.Multiselect = false;
// fbd.InitialDirectory = Path.GetFullPath(_wareaModel.savepath);
if (fbd.ShowDialog()== System.Windows.Forms.DialogResult.OK)
{
switch (para)
{
case "apmsStation":
WareaModel.apmModel.savepath = fbd.SafeFileName;
apmStationFile = fbd.FileName;
break;
case "apmsttime2d":
WareaModel.apmModel.ttime[0].ttime2d = fbd.SafeFileName;
apmttime2dFile = fbd.FileName;
break;
default:
break;
}
}
});
public DelegateCommand<object> FilePathSaveCommand=> new((obj)=>
{
string para= obj.ToString();
System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
// fbd.SelectedPath = MainModel.DataSavePath;
fbd.ShowNewFolderButton = true;
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
string path= fbd.SelectedPath.Replace("\\", "/");
if (para == "savepath")
{
WareaModel.savepath = path;
}
}
});
public DelegateCommand LogPathSaveCommand=> new(() =>
{
System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
fbd.SelectedPath = System.Environment.CurrentDirectory + "\\" + MainModel.SystemLogPath;
fbd.ShowNewFolderButton = true;
if (fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
MainModel.SystemLogPath = fbd.SelectedPath;
}
});
public ICommand ConfirmCommand
{
get => new DelegateCommand(() =>
{
// 数据校验关键字段不能为空、年龄做数字区间的校验、做UserName的唯一检查自定义特性检查
if (string.IsNullOrEmpty(MainModel.VpnName) || string.IsNullOrEmpty(MainModel.VpnIP))
{
MessageBox.Show("内容不能为空", "提示", MessageBoxButton.OK);
return;
}
if (MessageBox.Show("确认修改?", "参数设置", MessageBoxButton.OKCancel,
MessageBoxImage.Exclamation) == MessageBoxResult.OK)
{
//1 Workarea配置更新
string wmJson = JsonConvert.SerializeObject(_workareaModelArray, Formatting.Indented);
_wareaModel.apmModel.channels = new List<StationChannelsModel>
{
new StationChannelsModel()
{
location = _wareaModel.StationConfig.Location,
network = _wareaModel.StationConfig.Network,
channels = _wareaModel.StationConfig.Channels,
station = string.Join(",", _wareaModel.StationConfig.Stations.Where(p => p.IsEnable).Select(n => n.Name))
}
};
File.WriteAllText(Path.Combine(JsonParser.workareaPath, "Workarea.json"), wmJson);
if (!string.IsNullOrEmpty(apmStationFile))
{
string destFileName = Path.GetFullPath(Path.Combine(JsonParser.workareaPath, WareaModel.apmModel.savepath));
File.Copy(apmStationFile, destFileName, true);
apmStationFile = null;
}
if(!string.IsNullOrEmpty(apmttime2dFile))
{
string destFileName = Path.GetFullPath(Path.Combine(JsonParser.workareaPath, WareaModel.apmModel.ttime[0].ttime2d));
File.Copy(apmttime2dFile, destFileName, true);
apmttime2dFile = null;
}
//2 gw.apms.exe配置更新
{
ApmsModel apms = _wareaModel.apmModel;
apms.station = Path.GetFullPath(Path.Combine(JsonParser.workareaPath, WareaModel.apmModel.station)).Replace("\\","/");
apms.ttime[0].ttime2d= Path.GetFullPath(Path.Combine(JsonParser.workareaPath, WareaModel.apmModel.ttime[0].ttime2d)).Replace("\\", "/"); ;
apms.savepath = _wareaModel.savepath + "/" + _wareaModel.apmsavepath;
apms.push2wx = _wareaModel.apmpush2wx ? "YES" : "NO";
JsonParser.WriteSystemConfigFile(Path.Combine(_systemConfig.proApms.ProPath, "apms.json"), apms);
}
//3 gw.tools.exe配置更新
{
ApmsModel tools = _wareaModel.apmModel;
tools.station = Path.GetFullPath(Path.Combine(JsonParser.workareaPath, WareaModel.apmModel.station)).Replace("\\", "/");
tools.ttime[0].ttime2d = Path.GetFullPath(Path.Combine(JsonParser.workareaPath, WareaModel.apmModel.ttime[0].ttime2d)).Replace("\\", "/"); ;
tools.savepath = _wareaModel.savepath+"/"+ _wareaModel.toolsavepath;
tools.push2wx = _wareaModel.toolpush2wx ? "YES" : "NO"; ;
JsonParser.WriteSystemConfigFile(Path.Combine(_systemConfig.proTools.ProPath, "apms.json"), tools);
//4 SystemConfig.json配置更新
int index = _systemConfig.proTools.ProParams.IndexOf("-savepath");
string str = _systemConfig.proTools.ProParams.Substring(0, index) + "-savepath";
_systemConfig.proTools.ProParams = $"{str} {tools.savepath} -delay {_wareaModel.delayTime * 60}";
JsonParser.WriteSystemConfigFile(JsonParser.systemConfigPath, _systemConfig);
}
//5 gw.recvftp.exe配置更新
JsonParser.UpdateRecvJson(Path.Combine(_systemConfig.proRecv.ProPath, _systemConfig.proRecv.JsonPath), _wareaModel.savepath + "/" + _wareaModel.chartsavepath);
}
});
}
}
}