using ComeCapture;
using Common.system;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Principal;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using TStudyDigitalPen.HID;
using XHWK.Model;
using XHWK.WKTool.Helpers;
using XHWK.WKTool.Skin;
namespace XHWK.WKTool
{
public partial class APP : Application
{
#region 全局变量
#region 更新需改动
///
/// 是否为测试版
///
public static bool isDebug = FileToolsCommon.GetConfigValue("IsDebug") != "0";
///
/// 参数是否加密
///
public static bool IsParameterEncryption = FileToolsCommon.GetConfigValue("IsParameterEncryption") != "0";
#endregion 更新需改动
#region 接口地址
///
/// 是否为校外接口
///
public static bool IsOutsideSchool = FileToolsCommon.GetConfigValue("IsOutsideSchool") == "1";
///
/// 服务地址
///
public static Model_ServiceAddress ServiceAddress = null;
///
/// 接口地址
///
public static string apiUrl = isDebug ? "http://schoolapitest.xhkjedu.com" : FileToolsCommon.GetConfigValue("APIRequestAddress");
///
/// 图片地址
///
public static string uploadUrl = isDebug ? "http://schoolfiletest.xhkjedu.com/" : FileToolsCommon.GetConfigValue("FileRequestAddress");
///
/// 展示文件
///
public static string showImageUrl = isDebug ? "http://schoolfiletest.xhkjedu.com/static/" : FileToolsCommon.GetConfigValue("SchoolfileRequestAddress") + "/static/";
///
/// 认证接口地址
///
public static string certapiUrl = isDebug ? "http://certapitest.xhkjedu.com" : FileToolsCommon.GetConfigValue("CertapiRequestAddress");
#endregion 接口地址
#region 配置项
///
/// 是否输出测试记录日志
///
public static bool IsOutputInfoLog = FileToolsCommon.GetConfigValue("IsOutputInfoLog") != "0";
///
/// 是否隐藏录屏工具栏
///
public static bool IsHideSRTool = FileToolsCommon.GetConfigValue("IsHideSRTool") != "0";
///
/// 皮肤样式 0白 1蓝 2黑色
///
public static string SkinStyle = FileToolsCommon.GetConfigValue("SkinStyle");
///
/// 数据存放目录
///
public static string DataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\XHMicroLesson\\";
///
/// 摄像头位置
///
public static string CameraPosition = string.Empty;
///
/// 摄像头名
///
public static string CameraName = "";
///
/// 麦克风名
///
public static string MicrophoneName = "";
#endregion 配置项
#region 记录数据
///
/// 全局页面数据变量
///
public static readonly Model_Page PageContextData = new Model.Model_Page();
///
/// 录屏工具
///
public static FFMpeg FFmpeg = new FFMpeg();
///
/// 后台线程帮助类
///
public static BackgroundWorkerHelper BackgroundWorkerHelper => BackgroundWorkerHelper.GetInstance();
///
/// 签名
///
public static string Signature = "";
public static string secretKey = "nanhuakaizhangjianwangni";
///
/// 用户信息
///
public static Model_UserInfo UserInfo = null;
///
/// 接口返回消息
///
public static string ServerMsg = string.Empty;
///
/// 登录状态 false 未登录 true已登录
///
public static bool IsLoginType = false;
///
/// 截图地址
///
public static string ImgPath = string.Empty;
///
/// 教材列表
///
public static List TsubjectbookList = null;
///
/// 章节列表
///
public static List DirectorList = null;
///
/// 上传个人空间 入参
///
public static Model_ResourceAddTwo ResourceAddTwo = null;
///
/// 是否正在上传
///
public static bool IsUpLoad = false;
#endregion 记录数据
#region 微课数据记录
///
/// 用户微课列表模型
///
public static List WKDataList = null;
///
/// 微课模型
///
public static Model_WKData WKData = null;
///
/// 微课视频列表
///
public static List VideoList = null;
///
/// 画板模型
///
public static List PageDrawList = null;
#endregion 微课数据记录
#region 点阵笔
///
/// 点阵笔
///
public static DigitalPenHID digitalPen;
///
/// 笔序号
///
public static string PenSerial;
///
/// 笔状态,是否已连接
///
public static bool PenStatus = false;
///
/// 手写板状态,是否已连接
///
public static bool BoardStatus = false;
#endregion 点阵笔
#region 腾千里手写笔
/////
///// 铺码服务请求
/////
//public static DAL_TmatrixCode dAL_TmatrixCode;
///
/// 腾千里手写笔事件
///
public static PenEvents TQLPenevents;
///
/// 腾千里手写笔是否已连接
///
public static bool TQLPenStatus = false;
#region 打印
public static bool gbGenerateBGWithVImage = false;
public static bool gbGenerateVImage = false;
public static bool gbGenerateBGWithoutVImage = false;
public static bool gbGenerateBGWithImage = false;
public static bool gbGenPageSet = false;
public static int[] gPointType = new int[5];
public static int[] gPointDPI = new int[5];
public static string gTMXElementFileName;
public static string gNewProjectDirectory;
public static bool gbElementFileExist { get; set; }
#endregion 打印
#endregion 腾千里手写笔
#region 页面
///
/// 主页面
///
public static XHMicroLessonSystemWindow W_XHMicroLessonSystemWindow = null;
///
/// 创建微课页面
///
public static CreateAMicroLessonWindow W_CreateAMicroLessonWindow = null;
///
/// 开始录像
///
public static CountdownWindow W_CountdownWindow = null;
///
/// 录像工具栏
///
public static ScreenRecordingToolbarWindow W_ScreenRecordingToolbarWindow = null;
///
/// 登录
///
public static LoginWindow W_LoginWindow = null;
///
/// 截图
///
public static JieTuWindow W_JieTuWindow = null;
///
/// 批注
///
public static PracticeWindow W_PracticeWindow = null;
///
/// 提示窗口
///
public static PromptWindow W_PromptWindow = null;
///
/// loding页面
///
public static LoadDialog myloading;
///
/// 上传页面
///
public static UploadWindow W_UploadWindow = null;
///
/// 最小化工具栏
///
public static MinToolbar W_MinToolbar = null;
///
/// 视频剪辑
///
public static VideoClipWindow W_VideoClipWindow = null;
///
/// 设备检测
///
public static DeviceWindow W_DeviceWindow = null;
#endregion 页面
#endregion 全局变量
[STAThread]
private static void Main()
{
myloading = new LoadDialog();
StopSameProcess();
Killffmpeg();
//拓思德打印配置生成
//LatticeFileHelper.RunPrintConfig();
try
{
UserInfo = new Model_UserInfo();
WKDataList = new List();
VideoList = new List();
#region 强制以管理员方式运行 修改人:赵耀 修改时间:2020年9月7日
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
//if (principal.IsInRole(WindowsBuiltInRole.Administrator))
//{
// //如果是管理员,则直接运行
// W_CreateAMicroLessonWindow = new CreateAMicroLessonWindow();
// // 定义Application对象作为整个应用程序入口
// Application app = new Application();
// app.Run(W_CreateAMicroLessonWindow);
// //Application.Run(W_CreateAMicroLessonWindow);
//}
//else
//{
// //创建启动对象
// System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
// {
// UseShellExecute = true,
// WorkingDirectory = Environment.CurrentDirectory,
// FileName = Assembly.GetExecutingAssembly().Location,
// //设置启动动作,确保以管理员身份运行
// Verb = "runas"
// };
// try
// {
// System.Diagnostics.Process.Start(startInfo);
// }
// catch
// {
// return;
// }
// //退出
// //Current.Shutdown();
// Environment.Exit(0);
//}
#endregion 强制以管理员方式运行 修改人:赵耀 修改时间:2020年9月7日
try
{
if (Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "Temp"))//清除临时文件
{
Directory.Delete(AppDomain.CurrentDomain.BaseDirectory + "Temp", true);
}
}
catch (Exception)
{
}
//如果本地文件为空,弹出弹窗 输密匙
//本地文件不为空, 解析
if (!File.Exists(APP.DataPath + "signature.txt"))//上次登录时间为空 或者签名为空 或者上次登录时间不是今天 都需要输入密匙验证 ,否则只验证本地的签名
{
if (principal.IsInRole(WindowsBuiltInRole.Administrator))
{
//如果是管理员,则直接运行
KeyVerification wins = new KeyVerification();
// 定义Application对象作为整个应用程序入口
Application app = new Application();
SkinConfig.InitSkin();
app.Run(wins);
}
else
{
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
{
UseShellExecute = true,
WorkingDirectory = Environment.CurrentDirectory,
FileName = Assembly.GetExecutingAssembly().Location,
//设置启动动作,确保以管理员身份运行
Verb = "runas"
};
try
{
System.Diagnostics.Process.Start(startInfo);
}
catch
{
return;
}
//退出
//Current.Shutdown();
Environment.Exit(0);
}
}
else
{
if (principal.IsInRole(WindowsBuiltInRole.Administrator))
{
ProductVerification wins = new ProductVerification();
Application app = new Application();
SkinConfig.InitSkin();
app.Run(wins);
}
else
{
//创建启动对象
System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo
{
UseShellExecute = true,
WorkingDirectory = Environment.CurrentDirectory,
FileName = Assembly.GetExecutingAssembly().Location,
//设置启动动作,确保以管理员身份运行
Verb = "runas"
};
try
{
System.Diagnostics.Process.Start(startInfo);
}
catch
{
return;
}
//退出
//Current.Shutdown();
Environment.Exit(0);
}
}
}
catch (Exception ex)
{
string ErrMessage = "【进程】(Main):进程意外关闭。 " + ex.Message;
LogHelper.WriteErrLog(ErrMessage, ex);
}
}
#region 杀死已存在的进程
///
/// 结束已运行的程序
///
private static void StopSameProcess()
{
Process current = Process.GetCurrentProcess();
Process[] processList = Process.GetProcesses();
foreach (Process process in processList)
{
if (process.ProcessName.ToUpper() == "星火微课")
{
if (process.Id != current.Id) //忽略当前进程
{
try
{
process.Kill(); //结束进程
}
catch (Exception)
{
}
}
}
}
}
///
/// 杀死正在运行的ffmpeg
///
public static void Killffmpeg()
{
Process[] KillProcessArray = Process.GetProcessesByName("ffmpeg");
foreach (Process KillProcess in KillProcessArray)
{
KillProcess.Kill();
}
}
#endregion 杀死已存在的进程
///
/// 错误处理
///
///
///
private void MyApp_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
LogHelper.WriteErrLog("未标示错误:" + e.Exception.Message, e.Exception);
MessageWindow.Show("程序异常." + Environment.NewLine + e.Exception.Message);
e.Handled = true;
}
#region 数据保存和读取
///
/// 保存微课信息
///
public static void SaveWkData()
{
try
{
VideoList = VideoList.Where((x, i) => VideoList.FindIndex(z => z.FileGuid == x.FileGuid) == i).ToList();
WKData.VideoList = VideoList;
if (WKDataList != null)
{
WKDataList.RemoveAll(x => x.WkPath == WKData.WkPath);
}
else
{
WKDataList = new List();
}
WKDataList.Add(WKData);
//string WkDateXmlStr = XmlUtilHelper.Serializer(typeof(Model_WKData), WKData);
string WkDateXmlStr = XmlUtilHelper.XmlSerialize(WKDataList);
string SavePath = FileToolsCommon.GetFileAbsolutePath("/Data/");
FileToolsCommon.CreateDirectory(SavePath);
string SaveName = SavePath + "WkDate.d";
FileToolsCommon.DeleteFile(SaveName);
FileToolsCommon.WriteText(SaveName, WkDateXmlStr);
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【微课数据保存】(SaveWkData)保存失败:" + ex.Message, ex);
}
}
///
/// 读取微课信息
///
public static void ReadWkData(string WkPath)
{
try
{
string SavePath = FileToolsCommon.GetFileAbsolutePath("/Data/");
FileToolsCommon.CreateDirectory(SavePath);
string SaveName = SavePath + "WkDate.d";
//文件若存在则读取
if (FileToolsCommon.IsExistFile(SaveName))
{
string WkDateXmlStr = FileToolsCommon.FileToString(SaveName);
WKDataList = XmlUtilHelper.DESerializer>(WkDateXmlStr);
foreach (Model_WKData wklist in WKDataList)
{
//移除找不到视频的微课数据
wklist.VideoList.RemoveAll(x => !FileToolsCommon.IsExistFile(x.VideoPath));
}
if (WKDataList.Exists(x => x.WkPath == WkPath))
{
WKData = WKDataList.Find(x => x.WkPath == WkPath);
VideoList = new List();
WKData.VideoList = WKData.VideoList.Where((x, i) => WKData.VideoList.FindIndex(z => z.FileGuid == x.FileGuid) == i).ToList();
VideoList = WKData.VideoList;
//List companyList = WKData.VideoList;
//VideoList = companyList.Where((x, i) => companyList.FindIndex(z => z.FileGuid == x.FileGuid) == i).ToList();
//VideoList = companyList.Distinct().ToList();
}
}
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【微课数据读取】(ReadWkData)读取失败:" + ex.Message, ex);
}
}
///
/// 保存画板数据
///
public static void SaveDraw()
{
try
{
if (PageDrawList == null)
{
return;
}
if (PageDrawList.Count < 1)
{
return;
}
FileToolsCommon.CreateDirectory(WKData.WkPath);
string SavePath = WKData.WkPath + "PageData.d";
FileToolsCommon.DeleteFile(SavePath);
string PageDataXmlStr = XmlUtilHelper.XmlSerialize(PageDrawList);
FileToolsCommon.WriteText(SavePath, PageDataXmlStr);
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【画板数据保存】(SaveDraw)保存失败:" + ex.Message, ex);
}
}
///
/// 读取文件
///
public static void ReadDrawData()
{
try
{
PageDrawList = new List();
string SavePath = WKData.WkPath + "PageData.d";
if (FileToolsCommon.IsExistFile(SavePath))
{
string PageDataXmlStr = FileToolsCommon.FileToString(SavePath);
PageDrawList = XmlUtilHelper.DESerializer>(PageDataXmlStr);
}
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【画板数据读取】(ReadDraw)读取失败:" + ex.Message, ex);
}
}
#endregion 数据保存和读取
#region 服务地址数据
///
/// 服务地址存储到本地
///
public static void SaveServiceAddressData()
{
try
{
if (ServiceAddress == null)
{
return;
}
string DateXmlStr = XmlUtilHelper.XmlSerialize(ServiceAddress);
string SavePath = DataPath;
FileToolsCommon.CreateDirectory(SavePath);
string SaveName = SavePath + "ServiceAddress.d";
FileToolsCommon.DeleteFile(SaveName);
FileToolsCommon.WriteText(SaveName, DateXmlStr);
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【服务地址存储】(SaveServiceAddressData)保存失败:" + ex.Message, ex);
}
}
///
/// 从本地读取服务地址
///
public static void ReadServiceAddressData()
{
try
{
string SavePath = DataPath;
FileToolsCommon.CreateDirectory(SavePath);
string SaveName = SavePath + "ServiceAddress.d";
//文件若存在则读取
if (FileToolsCommon.IsExistFile(SaveName))
{
string DateXmlStr = FileToolsCommon.FileToString(SaveName);
ServiceAddress = XmlUtilHelper.DESerializer(DateXmlStr);
SwitchAddress();
}
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【服务地址读取】(ReadServiceAddressData)读取失败:" + ex.Message, ex);
}
}
///
/// 切换校内外网
///
public static void SwitchAddress()
{
try
{
apiUrl = IsOutsideSchool ? ServiceAddress.wapi : ServiceAddress.napi;
uploadUrl = (IsOutsideSchool ? ServiceAddress.wfile : ServiceAddress.nfile) + "/";
showImageUrl = (IsOutsideSchool ? ServiceAddress.wdown : ServiceAddress.ndown) + "/static/";
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【服务地址切换】(SwitchAddress)服务复制切换失败,请求地址为空!", ex);
}
}
#endregion 服务地址数据
#region 检测安装Screen Capturer Recorder
///
/// 检测是否安装了Screen Capturer Recorder
///
///
public static bool CheckScreenCapturerRecorder()
{
if (FileToolsCommon.CheckSoftWartInstallState("Screen Capturer Recorder"))
{
//已经安装
return true;
}
else
{
return false;
}
}
///
/// 安装Screen Capturer Recorder
///
///
public static bool InstallScreenCapturerRecorder()
{
Process InstallProcess = new Process();
string StandardError = "";
string StandardOutput = "";
try
{
InstallProcess.StartInfo.FileName = FileToolsCommon.GetFileAbsolutePath(@"/SSCR/Setup Screen Capturer Recorder v0.12.10.exe"); //绝对路径
InstallProcess.StartInfo.UseShellExecute = false; //不使用操作系统外壳程序启动
InstallProcess.StartInfo.RedirectStandardError = true; //重定向标准错误输出
InstallProcess.StartInfo.RedirectStandardOutput = true;
InstallProcess.StartInfo.CreateNoWindow = true; //不显示程序窗口
InstallProcess.StartInfo.RedirectStandardInput = true; //用于模拟该进程控制台的输入
//外部程序(这里是FFMPEG)输出流时候产生的事件,这里是把流的处理过程转移到下面的方法中,详细请查阅MSDN
InstallProcess.ErrorDataReceived += new DataReceivedEventHandler(InstallProcess_ErrorDataReceived);
InstallProcess.Start(); //启动线程
InstallProcess.WaitForExit(); //阻塞等待进程结束
StandardError = InstallProcess.StandardError.ReadToEnd();
StandardOutput = InstallProcess.StandardOutput.ReadToEnd();
int ExitCode = InstallProcess.ExitCode;
InstallProcess.Close(); //关闭进程
InstallProcess.Dispose(); //释放资源
if (ExitCode == 0) //非0则为安装失败 1为取消安装
{
return true;
}
else
{
MessageBoxResult br = MessageWindow.Show("不安装环境将无法使用微课录制系统,是否现在安装?", "安装", MessageBoxButton.OKCancel);
if (br == MessageBoxResult.OK)
{
return InstallScreenCapturerRecorder();
}
else
{
return false;
}
}
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + ex.Message, ex);
if (string.IsNullOrWhiteSpace(StandardError))
{
LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + StandardError, null);
}
if (string.IsNullOrWhiteSpace(StandardOutput))
{
LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装返回信息:" + StandardOutput, null);
}
return false;
}
}
///
/// 安装失败信息
///
///
///
private static void InstallProcess_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
LogHelper.WriteErrLog("【ScreenCapturerRecorder安装】(installScreenCapturerRecorder)安装失败:" + e.Data, null);
//MessageWindow.Show(e.Data);
}
#endregion 检测安装Screen Capturer Recorder
#region 内存处理 -创建人:赵耀 -创建时间:2020年8月12日
///
/// 内存释放压缩
///
///
protected override void OnStartup(StartupEventArgs e)
{
new Thread(o =>
{
while (true)
{
try
{
//FreeMemoryHelper.SetDate();
FreeMemoryHelper.ReallocateMemory();
Thread.Sleep(20000);//暂定每20秒释放一次
}
catch (Exception ex)
{
LogHelper.WriteErrLog("【释放内存(MyApp)" + ex.Message, ex);
}
}
}).Start();
}
#endregion 内存处理 -创建人:赵耀 -创建时间:2020年8月12日
}
}