Files

250 lines
11 KiB
C#

using _29WxOrderWpf.BaseWindows;
using AllBase;
using CefSharp;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Work;
using Work.BaseForms;
namespace _29WxOrderWpf
{
/// <summary>
/// App.xaml 的交互逻辑
/// </summary>
public partial class App : Application
{
public static int ProcessCount = 0;
public static string[] args { get; set; } = new List<string>().ToArray();
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
if (e.Args.Length > 0)
{
if (e.Args.Length != 2 && e.Args.Any(a => a.Contains("--browser-subprocess-path")))
{
//MessageBox.Show(string.Join(",", args));
Process.GetCurrentProcess().Kill();
return;
}
args = e.Args;
}
try
{
//设置应用程序处理异常方式:ThreadException处理
//Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
//处理UI线程异常
//Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
//处理非UI线程异常
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
StartUp.Init();
new Initialization().Get();
#region 贴牌
//本地缓存文件
string IgPakFile = "locales/ig.pak";//贴牌客服端码
string OemVersionFile = "locales/igv.pak";//贴牌资源配置版本
string IaPakFile = "locales/ia.pak";//logo
//string IbPakFile = "locales/ib.pak";//ico
string IcPakFile = "locales/ic.pak";//功能配置
if (File.Exists("locales/io.pak"))
{
var ioTxt = FileBase.ReadAllText("locales/io.pak");
if (ioTxt.StartsWith("MThd") || !ioTxt.Contains("MThd"))
return;
string agentNameFile = "locales/na.pak";
if (File.Exists(agentNameFile))
{
var config = FileBase.ReadAllText(agentNameFile, Encoding.UTF8);
var configArr = config.Split(new string[] { "\r\n", "MThd" }, StringSplitOptions.RemoveEmptyEntries);
if (configArr.Length >= 2)
{
BaseClass.ProgramName = configArr[1];
BaseClass.ToolName = configArr[1];
BaseClass.AgentName = configArr[0];
}
}
string LogoPakFile = "locales/lo.pak"; // ico
if (File.Exists(LogoPakFile))
{
BitmapImage bitmap = new BitmapImage(new Uri(LogoPakFile, UriKind.Relative));
Application.Current.Resources["main29WeixinIcon"] = bitmap;
// winform部分,直接指定路径,从bitmap转换的会丢失颜色
BaseClass.Ico = new Icon(LogoPakFile); //BitmapImageToIcon(Application.Current.Resources["main29WeixinIcon"] as BitmapImage);
}
//BaseClass.ProgromLogo = Environment.CurrentDirectory + "\\locales\\ia.pak";//logo
//BaseClass.ProgromLogoIco = Environment.CurrentDirectory + "\\locales\\ib.pak";//ico
string ImgPakFile = "locales/im.pak";
if (File.Exists(ImgPakFile))
{
BaseClass.LogoImage = Image.FromFile(ImgPakFile);
}
// 待后续完善
OemMechanism(IgPakFile, OemVersionFile, IaPakFile, LogoPakFile, IcPakFile);
}
#endregion
}
catch (Exception ex)
{
string str = GetExceptionMsg(ex, string.Empty);
NaiBa.Util.Helper.LogHelper.WriteWithTime("SystemError:" + str);
//BaseMsgForm.ShowError(str, "系统错误");
BaseMsgWindow.ShowError(str, "系统错误", null, MsgWindowLevel.LARGE);
}
#region 初始化cef
if (!args.Any(a => a.Contains("--browser-subprocess-path")))
{
CefSharpSettings.WcfEnabled = false;
CefSharp.Wpf.CefSettings settings = new CefSharp.Wpf.CefSettings();
settings.UserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36";
settings.CefCommandLineArgs.Add("disable-gpu", "1"); // 禁止显卡加速
settings.IgnoreCertificateErrors = true;
settings.Locale = "zh-CN";
settings.LogSeverity = LogSeverity.Disable;
var pName = Process.GetCurrentProcess().ProcessName;
ProcessCount = Process.GetProcesses().Count(p => p.ProcessName == pName);
//138版cef自己不会分缓存,会因占用导致报错,手动分缓存
settings.CachePath = Path.Combine(Environment.CurrentDirectory, "Cache\\Order" + ProcessCount);
//settings.CachePath = null; //禁止缓存
//settings.CefCommandLineArgs.Add("enable-javascript", "1");
//settings.CefCommandLineArgs.Add("disable-features", "NetworkService,NetworkServiceInProcess");
settings.CefCommandLineArgs.Add("--disable-web-security", "1");
//settings.CefCommandLineArgs.Add("--disable-request-handling-for-testing", "1");
#if DEBUG
settings.LogFile = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cef_log_order.txt");
// 设置日志级别为详细
settings.LogSeverity = LogSeverity.Verbose;
// 捕获JavaScript异常堆栈(最多32帧)
settings.UncaughtExceptionStackSize = 32;
#endif
Cef.Initialize(settings);
}
#endregion
new MainWindow().Show();
}
//static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
//{
// string str = GetExceptionMsg(e.Exception, e.ToString());
// NaiBa.Util.Helper.LogHelper.WriteWithTime("SystemError:" + str);
// BaseMsgWindow.ShowError(str, "系统错误");
//}
private void Current_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
{
string str = GetExceptionMsg(e.Exception, e.ToString());
NaiBa.Util.Helper.LogHelper.WriteWithTime("SystemError:" + str);
BaseMsgWindow.ShowError(str, "系统错误", null, MsgWindowLevel.LARGE);
}
static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
{
string str = GetExceptionMsg(e.ExceptionObject as Exception, e.ToString());
NaiBa.Util.Helper.LogHelper.WriteWithTime("SystemError:" + str);
BaseMsgWindow.ShowError(str, "系统错误", null, MsgWindowLevel.LARGE);
}
/// <summary>
/// 生成自定义异常消息
/// </summary>
/// <param name="ex">异常对象</param>
/// <param name="backStr">备用异常消息:当ex为null时有效</param>
/// <returns>异常字符串文本</returns>
static string GetExceptionMsg(Exception ex, string backStr)
{
StringBuilder sb = new StringBuilder();
sb.AppendLine("****************************异常文本****************************");
sb.AppendLine("【出现时间】:" + DateTime.Now.ToString());
if (ex != null)
{
sb.AppendLine("【异常类型】:" + ex.GetType().Name);
sb.AppendLine("【异常信息】:" + ex.Message);
sb.AppendLine("【堆栈调用】:" + ex.StackTrace);
}
else
{
sb.AppendLine("【未处理异常】:" + backStr);
}
sb.AppendLine("***************************************************************");
NaiBa.Util.Helper.LogHelper.WriteWithTime("Error:" + sb);
BaseMsgWindow.ShowError(sb.ToString(), "系统错误", null, MsgWindowLevel.LARGE);
return sb.ToString();
}
/// <summary>
/// Oem贴牌机制
/// </summary>
/// <param name="sIgPakFile">贴牌商客户端码文件</param>
/// <param name="sOenVersionFile">贴牌配置版本号文件</param>
/// <param name="sIaPakFile">ico本地缓存文件</param>
/// <param name="sIbPakFile">logo本地缓存文件</param>
/// <param name="sIcPakFile">贴牌配置本地缓存文件</param>
private void OemMechanism(string IgPakFile, string OemVersionFile, string IaPakFile, string logoBitmapFile, string IcPakFile)
{
return;
#region 贴牌修改logo
//// 替换字典程序logo等
//Uri imageUri = new Uri("pack://application:,,,/Resources/WindowBoxes/close.hover.png"); // 新的图片路径
//BitmapImage newBitmapImage = new BitmapImage(imageUri);
//// 获取资源字典
//ResourceDictionary dict = Application.Current.Resources;
//// 添加或更新资源
//dict["main29WeixinIcon"] = newBitmapImage;
if (File.Exists(logoBitmapFile))
{
BitmapImage bitmap = new BitmapImage(new Uri(logoBitmapFile, UriKind.Relative));
Application.Current.Resources["main29WeixinIcon"] = bitmap;
}
BaseClass.Ico = BitmapImageToIcon(Application.Current.Resources["main29WeixinIcon"] as BitmapImage);
#endregion
}
public Icon BitmapImageToIcon(BitmapImage bitmapImage)
{
// 将BitmapImage转换为BitmapSource
BitmapSource bitmapSource = BitmapFrame.Create(bitmapImage);
// 创建一个MemoryStream来存储BitmapSource的像素数据
using (MemoryStream memoryStream = new MemoryStream())
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Frames.Add(BitmapFrame.Create(bitmapSource));
encoder.Save(memoryStream);
memoryStream.Seek(0, SeekOrigin.Begin);
// 从MemoryStream中创建一个System.Drawing.Bitmap对象
using (var bitmap = new Bitmap(memoryStream))
{
// 现在你可以将System.Drawing.Bitmap转换为System.Drawing.Icon了
return Icon.FromHandle(bitmap.GetHicon());
}
}
}
}
}