|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253 |
- using BPASmartClient.IoT;
- using BPASmartClient.Message;
- using Microsoft.Toolkit.Mvvm.ComponentModel;
- using Microsoft.Toolkit.Mvvm.Input;
- using Microsoft.Win32;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Media;
- using System.Windows.Threading;
-
- namespace BPASmartClient.ViewModel
- {
- /// <summary>
- /// 日志界面
- /// </summary>
- public class LogViewModel :ObservableObject
- {
- //public DispatcherTimer dispatcherTimer;
- public string ClientId= System.Configuration.ConfigurationManager.AppSettings["ClientId"].ToString();
- private ObservableCollection<LogModel> _LogModels;
- public ObservableCollection<LogModel> LogDataGrid
- {
- get
- {
- return _LogModels;
- }
- set
- {
- if (_LogModels == value)
- return;
- _LogModels = value;
- OnPropertyChanged("LogDataGrid");
- }
- }
- private volatile static LogViewModel _Instance;
- public static LogViewModel GetInstance() => _Instance ?? (_Instance = new LogViewModel());
- private LogViewModel()
- {
- if(LogDataGrid==null)
- LogDataGrid = new ObservableCollection<LogModel>();
- MessageLog.GetInstance.InfoNotify = new Action<string>((s) =>
- {
- System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() =>
- {
- LogDataGrid.Insert(0,new LogModel { message = s,type = "Info" });
- }));
- });
- MessageLog.GetInstance.ExInfoNotify = new Action<string>((s) =>
- {
- System.Windows.Application.Current?.Dispatcher.Invoke((Action)(() =>
- {
- LogDataGrid.Insert(0,new LogModel { message = s,type = "Error" });
- DataVClient.GetInstance().HttpAddLog(new LogTable
- {
- ClientId = ClientId,
- LogTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
- LogType = "严重",
- LogMessage = s,
- LogVla = "错误日志"
- });
- }));
- });
- ExcelCommand = new RelayCommand(() =>
- {
- ExcellOrder();
- });
- }
-
- /// <summary>
- /// 导出数据
- /// </summary>
- public void ExcellOrder()
- {
- if (LogDataGrid.Count > 0)
- {
- string text = "时间 类型 日志内容\n";
- LogDataGrid?.ToList().ForEach(temp =>
- {
- text = text + temp.time + " " + temp.type + " " + temp.message+ "\n";
- });
- SaveFileDialog openfile = new SaveFileDialog();
- openfile.Filter = "Txt文件(*.txt)|*.txt";
- if (openfile.ShowDialog() == false)
- {
- return;
- }
- string path = openfile.FileName;
- if (!System.IO.File.Exists(path))
- {
- //没有则创建这个文件
- FileStream fs1 = new FileStream(path,FileMode.Create,FileAccess.Write);//创建写入文件
- StreamWriter sw = new StreamWriter(fs1);
- sw.WriteLine(text);//开始写入值
- sw.Close();
- fs1.Close();
- }
- else
- {
- FileStream fs = new FileStream(path,FileMode.Open,FileAccess.Write);
- StreamWriter sr = new StreamWriter(fs);
- sr.WriteLine(text);//开始写入值
- sr.Close();
- fs.Close();
- }
- string msg = string.Format("记录导出完成,共导出记录{0}条,是否打开!",LogDataGrid.Count);
- if (System.Windows.MessageBox.Show(msg,"提示",MessageBoxButton.OKCancel) == MessageBoxResult.OK)
- {
- OpenFile(openfile.FileName);
- }
- }
- else
- System.Windows.MessageBox.Show("无数据!");
-
- }
-
- /// <summary>
- /// 打开指定路径下文件,比如:Word、Excel、Dll、图片等都可以(前提是你已经安装打开程序的对应软件)
- /// </summary>
- /// <param name="NewFileName">eg:D:\Test\模版8.doc</param>
- /// <param name="NewFileName">eg:D:\Test\模版8.doc</param>
- private void OpenFile(string NewFileName)
- {
- Process process = new Process();
- ProcessStartInfo processStartInfo = new ProcessStartInfo(NewFileName);
- process.StartInfo = processStartInfo;
- #region 下面这段被注释掉代码(可以用来全屏打开代码)
- //建立新的系统进程
- ////System.Diagnostics.Process process = new System.Diagnostics.Process();
- //设置文件名,此处为图片的真实路径 + 文件名(需要有后缀)
- ////process.StartInfo.FileName = NewFileName;
- //此为关键部分。设置进程运行参数,此时为最大化窗口显示图片。
- ////process.StartInfo.Arguments = "rundll32.exe C://WINDOWS//system32//shimgvw.dll,ImageView_Fullscreen";
- // 此项为是否使用Shell执行程序,因系统默认为true,此项也可不设,但若设置必须为true
- process.StartInfo.UseShellExecute = true;
- #endregion
- try
- {
- process.Start();
- try
- {
- // process.WaitForExit();
- }
- catch (Exception ex)
- {
- throw ex;
- }
- }
- catch (Exception ex)
- {
- throw ex;
- }
- finally
- {
- try
- {
- if (process != null)
- {
- process.Close();
- process = null;
- }
- }
- catch { }
- }
- }
-
- private bool _RealTimeModel = true;
- public bool RealTimeModel
- {
- get
- {
- return _RealTimeModel;
- }
- set
- {
- if (_RealTimeModel == value)
- return;
- _RealTimeModel = value;
- OnPropertyChanged("RealTimeModel");
- }
- }
-
- private bool _TimedClear = true;
- public bool TimedClear
- {
- get
- {
- return _TimedClear;
- }
- set
- {
- if (_TimedClear == value)
- return;
- _TimedClear = value;
- OnPropertyChanged("TimedClear");
- }
- }
-
- public RelayCommand ExcelCommand { get; set; }
-
- }
-
- public class LogModel :ObservableObject
- {
- public string time { get; set; }
-
- private string _type;
- public string type
- {
- get
- {
- return _type;
- }
- set
- {
- if (_type == value)
- return;
- _type = value;
- if(_type== "Error") foreground = new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#ed0032"));
-
- OnPropertyChanged("type");
- }
- }
- public string message { get; set; }
-
- private Brush _foreground;
- public Brush foreground
- {
- get
- {
- return _foreground;
- }
- set
- {
- if (_foreground == value)
- return;
- _foreground = value;
- OnPropertyChanged("foreground");
- }
- }
- public LogModel()
- {
- foreground=new SolidColorBrush((System.Windows.Media.Color)System.Windows.Media.ColorConverter.ConvertFromString("#21bb2e"));
- time =DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
- }
- }
- }
|