using Common;
using Common.system;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using System.Security.Principal;
using System.Threading;
using System.Windows;
using System.Windows.Threading;
using XHZB.Model;
namespace XHZB.Desktop
{
public partial class APP : Application
{
#region 全局变量
public static int testdata=0;
///
/// 是否为测试版
///
public static bool isDebug = FileToolsCommon.GetConfigValue("IsDebug") != "0";
///
/// RTMP服务器地址
///
public static string RTMPServerPath = FileToolsCommon.GetConfigValue("ServerPath");
///
/// AES账号加密字符串
///
public static string AESAccountStr = null;
///
/// AES密码加密字符串
///
public static string AESPassWordStr = 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");
///
/// 数据存放目录
///
public static string dataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\XHZB\\";
public static string basePath = AppDomain.CurrentDomain.BaseDirectory;
//缓存图片地址
public static string tempPath = basePath + "temp\\";
///
/// 签名
///
public static string Signature = "";
public static string secretKey = "nanhuakaizhangjianwangni";
///
/// 录屏工具
///
public static FFMpeg FFmpeg = new FFMpeg();
///
/// 登陆状态 false 未登录 true已登陆
///
public static bool IsLoginType = false;
///
/// 防止图片地址重复
///
public static long num = 9999;
///
/// 导入图片路径
///
public static string[] Paths = new string[] { };
///
/// 截图图片路径
///
public static string[] JPaths = new string[999];
public static string ImgPath = string.Empty;
///
/// 页码IP
///
public static string[] OutPut = new string[] { };
public static string CameraPosition = string.Empty;
public static string fileStorageAddress = "";
///
/// 数据存放目录
///
public static string DataPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\XHZB\\";
///
/// 是否正在
///
public static bool IsUpLoad = false;
///
/// 后台线程帮助类
///
public static BackgroundWorkerHelper BackgroundWorkerHelper => BackgroundWorkerHelper.GetInstance();
///
/// 提示信息
///
public static string ErrorMessage = string.Empty;
///
/// 服务调用成功代码
///
public const int ServerScuessCode = 0;
public static int classid;
public static long roomid;
public static long askid;
public static int lsbid;
///
/// 登陆数据
///
public static LoginModel LoginUser;
private static List _ClassList;
///
/// 教师任教班级出参
///
public static List ClassList
{
get
{
if (_ClassList == null)
{
_ClassList = new List();
}
return _ClassList;
}
set => _ClassList = value;
}
private static List _TsubjectbookList;
///
/// 教师教材列表
///
public static List TsubjectbookList
{
get
{
if (_TsubjectbookList == null)
{
_TsubjectbookList = new List();
}
return _TsubjectbookList;
}
set => _TsubjectbookList = value;
}
private static List _DirectorList;
///
/// 章节列表 出参
///
public static List DirectorList
{
get
{
if (_DirectorList == null)
{
_DirectorList = new List();
}
return _DirectorList;
}
set => _DirectorList = value;
}
private static REQStartClassModel _REQStartClass;
///
/// 开始课堂入参
///
public static REQStartClassModel REQStartClass
{
get
{
if (_REQStartClass == null)
{
_REQStartClass = new REQStartClassModel();
}
return _REQStartClass;
}
set => _REQStartClass = value;
}
///
/// 抢答
///
public static List RollCallList = new List();
///
/// 在线用户
///
public static List OnlineUserList = new List();
private static List _ClassStudentList;
///
/// 班级学生--列表 出参
///
public static List ClassStudentList
{
get
{
if (_ClassStudentList == null)
{
_ClassStudentList = new List();
}
return _ClassStudentList;
}
set => _ClassStudentList = value;
}
private static ResourceMyListModel _ResourceMyList;
///
/// 我的备课列表 出参
///
public static ResourceMyListModel ResourceMyList
{
get
{
if (_ResourceMyList == null)
{
_ResourceMyList = new ResourceMyListModel();
}
return _ResourceMyList;
}
set => _ResourceMyList = value;
}
#region 页面
public static LoadDialog myloading;
///
/// 主页面
///
public static LoginWindow W_LoginWindow = null;
///
/// 工具栏
///
public static ToolbarWindow W_ToolbarWindow = null;
///
/// 个人空间
///
public static UserCenterWindow W_UserCenterWindow = null;
///
/// 抢答点名
///
public static RollCallWindow W_RollCallWindow = null;
///
/// 考勤
///
public static AttendanceWindow W_AttendanceWindow = null;
///
/// 黑板
///
public static ZBlackboardWindow W_ZBlackboardWindow = null;
///
/// 批注
///
public static PracticeWindow W_PracticeWindow = null;
///
/// 摄像头
///
public static CameraWindow W_CameraWindow = null;
#endregion
#endregion
///
/// 启动入口
///
/// 参数1:账号加密字符串 参数1:密码加密字符串
[STAThread]
private static void Main(string[] args)
{
myloading = new LoadDialog();
StopSameProcess();
Killffmpeg();
UnZipFFmpeg();
try
{
if (args != null&&args.Length>0)
{
AESAccountStr = args[0];
AESPassWordStr = args[1];
////解密
//string Account= AESHelper.AESDecrypt(AESAccountStr);
//string PassWord = AESHelper.AESDecrypt(AESPassWordStr);
}
}
catch (Exception ex)
{
LogHelper.WriteErrLog("参数接收失败:" + ex.Message, ex);
}
try
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
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();
//app.Run(wins);
W_LoginWindow = new LoginWindow();
Application app = new Application();
app.Run(W_LoginWindow);
}
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();
//app.Run(wins);
W_LoginWindow = new LoginWindow();
Application app = new Application();
app.Run(W_LoginWindow);
}
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 检测解压ffmpeg
public static void UnZipFFmpeg()
{
new Thread(o =>
{
if (!FileToolsCommon.IsExistDirectory(FileToolsCommon.GetFileAbsolutePath("/ffmpeg/")))
{
try
{
//根据系统解压ffmpeg
if (Environment.Is64BitOperatingSystem)
{
//64
ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx64.zip"), FileToolsCommon.GetFileAbsolutePath());
}
else
{
//32
ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx32.zip"), FileToolsCommon.GetFileAbsolutePath());
}
}
catch (Exception)
{
ZipHelper.UnZip(FileToolsCommon.GetFileAbsolutePath("/ffmpegx64.zip"), FileToolsCommon.GetFileAbsolutePath());
}
}
}).Start();
}
#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
#region 内存处理 -创建人:赵耀 -创建时间:2020年8月12日
///
/// 内存释放压缩
///
///
protected override void OnStartup(StartupEventArgs e)
{
//private LierdaCracker cracker = new LierdaCracker();
//cracker.Cracker(5000);//间隔时间
//base.OnStartup(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日
#region 测试
public static void AddTestData()
{
testdata = 1;
}
///
/// 图片真实路径
///
///
public static string classRoomImagePath()
{
//string path = $"{nginxHtml}school\\{ZCommonData.loginUser.schoolid}\\class\\{ZCommonData.roomid}\\";
string path = AppDomain.CurrentDomain.BaseDirectory + "temp\\"; ;
return path;
}
///
/// 本地服务器存储路径
///
///
public static string dbImagePath()
{
//string path = $"school\\{ZCommonData.loginUser.schoolid}\\class\\{ZCommonData.roomid}\\";
string path = AppDomain.CurrentDomain.BaseDirectory + "temp\\";
return path;
}
///
/// 获取图片保存路径 设置服务器存储路径 创建人:赵耀 创建时间:2020年8月11日
///
///
public static string GetImagePath(out string ServerSavePath)
{
string imagepath = APP.classRoomImagePath();
if (!Directory.Exists(imagepath))
{
Directory.CreateDirectory(imagepath);
}
TimeSpan ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
string timestr = Convert.ToInt64(ts.TotalMilliseconds).ToString();
string filepath = Path.Combine(imagepath, timestr + ".jpg");
ServerSavePath = APP.dbImagePath() + timestr + ".jpg";
return filepath;
}
#endregion
#region 写入注册表
///
/// 注册表
///
public static RegistryHelper reg = new RegistryHelper("XHZB\\", RegDomain.ClassesRoot);
public static void WriteRegistry()
{
if(IsSubKeyExist())
{
//存在时对比位置
string regStr = ReadRegeditKey();
string pathStr = FileToolsCommon.GetFileAbsolutePath("/星火直播.exe").Replace("/","\\\\");
if(regStr!= pathStr)
{
}
}
else
{
}
}
///
/// 判断注册表是否存在
///
public static bool IsSubKeyExist()
{
return reg.IsSubKeyExist();
}
///
/// 读取注册表键值
///
///
public static string ReadRegeditKey()
{
if(reg.IsRegeditKeyExist("URL Protocol"))
{
return reg.ReadRegeditKey("URL Protocol").ToString();
}
else
{
return "";
}
}
public static void CreateRegeditKey()
{
reg.CreateSubKey();
string pathStr = FileToolsCommon.GetFileAbsolutePath("/星火直播.exe").Replace("/", "\\\\");
reg.WriteRegeditKey("@","星火直播");
reg.WriteRegeditKey("URL Protocol", pathStr);
RegistryHelper reg1 = new RegistryHelper("XHZB\\DefaultIcon\\", RegDomain.ClassesRoot);
reg1.CreateSubKey();
reg1.WriteRegeditKey("@", pathStr+",1");
//reg1.CreateSubKey("XHZB\\DefaultIcon\\");
RegistryHelper reg2 = new RegistryHelper("XHZB\\shell\\open\\command\\", RegDomain.ClassesRoot);
reg2.CreateSubKey();
reg2.WriteRegeditKey("@", "\""+pathStr + "\"\"%1\"");
//reg.CreateSubKey("XHZB\\shell\\open\\command\\");
//[HKEY_CLASSES_ROOT\XHZB]
//"URL Protocol" = "E:\\项目\\星火直播\\XHZB\\XHZB.Desktop\\bin\\Debug\\星火直播.exe"
//@= "XHZBProtocol"
//[HKEY_CLASSES_ROOT\XHZB\DefaultIcon]
//@= "E:\\项目\\星火直播\\XHZB\\XHZB.Desktop\\bin\\Debug\\星火直播.exe,1"
//[HKEY_CLASSES_ROOT\XHZB\shell]
//[HKEY_CLASSES_ROOT\XHZB\shell\open]
//[HKEY_CLASSES_ROOT\XHZB\shell\open\command]
//@= "\"E:\\项目\\星火直播\\XHZB\\XHZB.Desktop\\bin\\Debug\\星火直播.exe\"\"%1\""
}
#endregion
}
}