Files
wcs/货架标准上位机/ViewModels/MainViewModel.cs
hehaibing-1996 e89b64ea3a !提交代码
2024-04-15 18:43:28 +08:00

228 lines
7.2 KiB
C#

using HandyControl.Controls;
using Newtonsoft.Json;
using Ping9719.WpfEx;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Input;
using System.Windows.Markup;
namespace .ViewModel
{
public class MainViewModel : BindableBase
{
Mutex mutex;
private string title = string.Empty;
/// <summary>
/// 标题
/// </summary>
public string Title { get => title; set { SetProperty(ref title, value); } }
#region _MainWindow2
//隐藏式选项卡的高度
public double TabItemHeight { get; set; } = 0;
private string SelectedValue_ = "主页";
public string SelectedValue { get => SelectedValue_; set { SetProperty(ref SelectedValue_, value); } }
#endregion
private DateTime time;
/// <summary>
/// 时间
/// </summary>
public DateTime Time { get => time; set { SetProperty(ref time, value); } }
/// <summary>
/// 在初始化前的相关检查
/// </summary>
public bool InitAgo()
{
//只允许打开一个
mutex = new Mutex(true, string.Concat("MengXun货架标准上位机", Path.GetFileNameWithoutExtension(LocalFile.AppName)), out bool createdNew);
if (!createdNew)
{
MessageBox.Warning("已有实列在运行!", "提示");
return false;
}
//初始化
Tuple<string, string, Action>[] tuples = new Tuple<string, string, Action>[]
{
new Tuple<string, string, Action>("检测文件..", "缺少文件,尝试将项目[data/]中的所有文件复制到[bin/data/]中", () =>
{
if(!System.IO.File.Exists(LocalFile.ConfigPath))
throw new Exception("缺少文件,请尝试将项目[data/]中的所有文件复制到[bin/data/]中");
}),
new Tuple<string, string, Action>("检测文件..", "配置文件中没有内容,请联系管理员", () =>
{
if(string.IsNullOrWhiteSpace(System.IO.File.ReadAllText(LocalFile.ConfigPath, Encoding.UTF8)))
throw new Exception($"配置文件[{LocalFile.ConfigPath}]中没有内容");
}),
new Tuple<string, string, Action>("初始化数据中..", "初始化数据库失败,请联系管理员", () =>
{
}),
new Tuple<string, string, Action>("初始化扫码枪连接..", "初始化扫码枪连接失败,请联系管理员", () =>
{
ScannerManager.InitScanners();
}),
};
MainLoadWindow.TaskSleepTime = 200;
if (!MainLoadWindow.Show(tuples))
return false;
//清理日志
Task.Run(() =>
{
if (LocalFile.Config.Sys.SaveLogDay < 0)
return;
Logs.Clear(TimeSpan.FromDays(LocalFile.Config.Sys.SaveLogDay));
});
////不登录,直接使用最高权限
//{
// UserLoginView.NotLogin();
// return true;
//}
//登录
{
var userLoginView = new UserLoginView();
var isok = userLoginView.ShowDialog() == true;
userLoginView = null;
return isok;
}
}
/// <summary>
/// 初始化并运行时间
/// </summary>
public async void Init(System.Windows.Window window)
{
//加载 程序名称
var versionInfo = FileVersionInfo.GetVersionInfo(LocalFile.AppPath);
Title = versionInfo.ProductName;
//加载 窗体模式
if (LocalFile.Config.Sys.StartupFull)
window.WindowState = System.Windows.WindowState.Maximized;
//注册 设备
IotDevice.UserChange = ((iot) =>
{
if (string.IsNullOrEmpty(iot.Funct))
Growl.Info($"你点击了[{iot.Name}],尝试改为[{iot.Val}]");
else
Growl.Info($"你点击了[{iot.Name}][{iot.Funct}],尝试改为[{iot.Val}]");
});
//注册 信息
TextBoxLog.TextBoxLogAdd = ((info) =>
{
Logs.Write(info.Text, LogsType.Info, info.Time);
});
//加载 时钟
while (true)
{
Time = DateTime.Now;
await Task.Delay(1);
}
}
public ICommand OpenUserCommand { get => new DelegateCommand(OpenUser); }
/// <summary>
/// 打开用户
/// </summary>
public void OpenUser()
{
if (UserInfoView.viewModel.IsLogin)
{
var userInfoView = new UserInfoView();
userInfoView.ShowDialog();
if (userInfoView.IsExitLogin)
{
Growl.Info("您已退出登录。");
userInfoView = null;
}
}
else
{
var userLoginView = new UserLoginView();
//登录成功
if (userLoginView.ShowDialog() == true)
{
Growl.Success($"欢迎您:{UserInfoView.viewModel.LoginName}");
userLoginView = null;
}
}
}
public ICommand OpenLogCommand { get => new DelegateCommand(OpenLog); }
/// <summary>
/// 打开日记
/// </summary>
public void OpenLog()
{
if (!System.IO.Directory.Exists(LocalFile.LogDir))
{
Growl.Info("暂时没有日志。");
return;
}
try
{
Folder.OpenFolder(LocalFile.LogDir);
}
catch (Exception)
{
Growl.Error("打开日志目录失败。");
}
}
public ICommand OpenHelpCommand { get => new DelegateCommand(OpenHelp); }
/// <summary>
/// 打开帮助
/// </summary>
public void OpenHelp()
{
if (!System.IO.File.Exists(LocalFile.DocPath))
{
Growl.Info("帮助文档正在书写中。");
return;
}
try
{
Folder.OpenFolderAndSelectedFile(LocalFile.DocPath);
}
catch (Exception)
{
Growl.Error("打开帮助文档失败。");
}
}
public ICommand OpenWeCommand { get => new DelegateCommand(OpenWe); }
/// <summary>
/// 打开关于
/// </summary>
public void OpenWe()
{
var aboutView = new AboutView();
aboutView.ShowDialog();
aboutView = null;
}
}
}