160 lines
4.3 KiB
C#
160 lines
4.3 KiB
C#
|
|
using GalaSoft.MvvmLight;
|
|
using GalaSoft.MvvmLight.Command;
|
|
using StandardDesign.IDataAccess;
|
|
using StandardDesign.Models;
|
|
using System.CodeDom;
|
|
using System.IO;
|
|
using System.Net.Sockets;
|
|
using System.Text.RegularExpressions;
|
|
using System.Windows;
|
|
|
|
namespace StandardDesign.ViewModels
|
|
{
|
|
public class LoginViewModel : ViewModelBase
|
|
{
|
|
ILoacalDataAccess _localDataAccess;
|
|
|
|
|
|
//ip
|
|
public string ip;
|
|
public string IP
|
|
{
|
|
get { return ip; }
|
|
set { Set(ref ip, value); }
|
|
}
|
|
//Port
|
|
public string port;
|
|
public string Port
|
|
{
|
|
get { return port; }
|
|
set { Set(ref port, value); }
|
|
}
|
|
|
|
//授权码
|
|
public string _authorizeCode;
|
|
public string AuthorizeCode
|
|
{
|
|
get { return _authorizeCode; }
|
|
set { Set(ref _authorizeCode, value); }
|
|
}
|
|
|
|
//异常信息
|
|
public string _failedMsg;
|
|
public string FailedMsg
|
|
{
|
|
get { return _failedMsg; }
|
|
set { Set(ref _failedMsg, value); }
|
|
}
|
|
|
|
//是否保存记录
|
|
private bool _isRecord;
|
|
|
|
public bool IsRecord
|
|
{
|
|
get { return _isRecord; }
|
|
set { Set<bool>(ref _isRecord, value); }
|
|
}
|
|
|
|
|
|
//登录按钮
|
|
public RelayCommand<object> LoginCommand { get; set; }
|
|
public RelayCommand<object> SkipCommand { get; set; }
|
|
|
|
|
|
public LoginViewModel(ILoacalDataAccess localDataAccess)
|
|
{
|
|
_localDataAccess = localDataAccess;
|
|
|
|
if (File.Exists("temp.txt"))
|
|
{
|
|
string info = File.ReadAllText("temp.txt");
|
|
this.IP = info.Split("|")[0];
|
|
this.Port = info.Split("|")[1];
|
|
}
|
|
if (!IsInDesignMode)
|
|
{
|
|
LoginCommand = new RelayCommand<object>(DoLogin);
|
|
SkipCommand = new RelayCommand<object>(SkipLogin);
|
|
}
|
|
}
|
|
private void DoLogin(object obj)
|
|
{
|
|
try
|
|
{
|
|
// 根据选项进行信息记录
|
|
if (IsRecord)
|
|
{
|
|
string info = $"{this.IP}|{this.Port}";
|
|
File.WriteAllText("temp.txt", info);
|
|
}
|
|
else
|
|
{
|
|
if (File.Exists("temp.txt"))
|
|
File.Delete("temp.text");
|
|
}
|
|
|
|
if (string.IsNullOrEmpty(IP))
|
|
{
|
|
throw new Exception("IP地址为空!");
|
|
}
|
|
else if (!ValidateIPAddress(IP))
|
|
{
|
|
throw new Exception("IP输入有误!");
|
|
}
|
|
else if (string.IsNullOrEmpty(Port))
|
|
{
|
|
throw new Exception("端口号为空!");
|
|
}
|
|
else if (!ValidatePortAddress(Port))
|
|
{
|
|
throw new Exception("端口号输入有误!");
|
|
}
|
|
else
|
|
{
|
|
(obj as Window).DialogResult = true;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
FailedMsg = ex.Message;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 跳过连接
|
|
/// </summary>
|
|
/// <param name="obj"></param>
|
|
|
|
private void SkipLogin(object obj)
|
|
{
|
|
(obj as Window).DialogResult = true;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 校验Ip
|
|
/// </summary>
|
|
/// <param name="ipAddress"></param>
|
|
/// <returns></returns>
|
|
public static bool ValidateIPAddress(string ipAddress)
|
|
{
|
|
Regex validipregex = new Regex(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
|
|
return (ipAddress != "" && validipregex.IsMatch(ipAddress.Trim())) ? true : false;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 校验端口号
|
|
/// </summary>
|
|
/// <param name="prtAddress"></param>
|
|
/// <returns></returns>
|
|
public static bool ValidatePortAddress(string prtAddress)
|
|
{
|
|
Regex validipregex = new Regex(@"^(\d|[1-9]\d{1,3}|[1-5]\d{4}|6[0-4]\d{3}|65[0-4]\d{2}|655[0-2]\d|6553[0-5])$");
|
|
return (prtAddress != "" && validipregex.IsMatch(prtAddress.Trim())) ? true : false;
|
|
}
|
|
|
|
}
|
|
|
|
}
|