using BBWY.Client.Models; using BBWY.Client.ViewModels; using BBWY.Client.ViewModels.PackTask; using BBWY.Client.ViewModels.SealBox; using BBWY.Common.Extensions; using BBWY.Common.Http; using BBWY.Common.Models; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Newtonsoft.Json; using QuanTan.SDK.Client; using System; using System.Collections.Generic; using System.Diagnostics; using System.IO; using System.IO.MemoryMappedFiles; using System.Linq; using System.Net.Http; using System.Reflection; using System.Security.Principal; using System.Text; using System.Threading.Tasks; using System.Windows; using Utils; namespace BBWY.Client { /// /// Interaction logic for App.xaml /// public partial class App : Application { public IServiceProvider ServiceProvider { get; private set; } public IConfiguration Configuration { get; private set; } public bool IsAdministrator() { WindowsIdentity current = WindowsIdentity.GetCurrent(); WindowsPrincipal windowsPrincipal = new WindowsPrincipal(current); return windowsPrincipal.IsInRole(WindowsBuiltInRole.Administrator); } protected override void OnStartup(StartupEventArgs e) { var isAdmin = IsAdministrator(); Console.WriteLine($"run as admin : {isAdmin}"); //System.Diagnostics.Debug.WriteLine($"run as admin : {isAdmin}"); Encoding.RegisterProvider(CodePagesEncodingProvider.Instance); var gl = new GlobalContext(); string userToken = string.Empty; #if DEBUG //仓库组测试 //userToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxNjM1OTMyMDAyMjcwMDU2NDQ4IiwidGVhbUlkIjoiMTU0MDg4NTU3MDYyNzA0NzQyNCIsInNvblRlYW1JZHMiOiIxNTQwODg1NTcwNjI3MDQ3NDI0IiwiZXhwIjoxNzEwNDk2NTQ1fQ.Sx2vGJcYGf98x99slQLU3vt7yWdjspptSpbUCcZgWek"; //齐越梧桐 userToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxNjY1ODk1OTgzMzc4MDc1NjQ4IiwidGVhbUlkIjoiMTUxNjcxNTM3MjQ0NTcwMDA5NiIsInNvblRlYW1JZHMiOiIxNTQwODg1N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TQxMDk1NTYwNDYxMjA5MTkwNCIsImV4cCI6MTcyNDkxNDM5M30.tIgo70tzRxZyUQkX1Jsh9I4ySciX0VC9HMMOoTnwVtM"; #else //var uid = e.Args.Count() > 0 ? e.Args.LastOrDefault(args => args.StartsWith("uid:")) : string.Empty; //if (string.IsNullOrEmpty(uid)) //{ // MessageBox.Show("缺少启动参数", "提示"); // Environment.Exit(0); //} var tokenResult = ReadMMF(); if (tokenResult.isOk) userToken = tokenResult.content; else { MessageBox.Show($"读取内存数据失败\r\n{tokenResult.content}", "提示"); Environment.Exit(0); } #endif gl.UserToken = userToken; #region 注册全局异常 this.DispatcherUnhandledException += App_DispatcherUnhandledException; AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException; TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException; #endregion var applicationPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); var builder = new ConfigurationBuilder().SetBasePath(applicationPath).AddJsonFile("BBWYAppSettings.json", false, true); Configuration = builder.Build(); gl.BBYWApiHost = Configuration.GetSection("BBWYApiHost").Value; gl.MDSApiHost = Configuration.GetSection("MDSApiHost").Value; gl.JOSApiHost = Configuration.GetSection("JOSApiHost").Value; gl._1688ApiHost = Configuration.GetSection("1688ApiHost").Value; gl.QKApiHost = Configuration.GetSection("QKApiHost").Value; IServiceCollection serviceCollection = new ServiceCollection(); serviceCollection.AddHttpClient(); // serviceCollection.AddTransient(); serviceCollection.AddHttpClient("gzip").ConfigurePrimaryHttpMessageHandler(handler => new HttpClientHandler() { AutomaticDecompression = System.Net.DecompressionMethods.GZip }); serviceCollection.AddMemoryCache(); serviceCollection.AddSingleton(); serviceCollection.AddSingleton(gl); serviceCollection.BatchRegisterServices(new Assembly[] { Assembly.Load("BBWY.Client") }, typeof(IDenpendency)); serviceCollection.AddMapper(new MappingProfile()); serviceCollection.AddTransient<_1688PreviewPurchaseViewModel>(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); serviceCollection.AddTransient(); #region 注册拳探SDK相关类 serviceCollection.AddSingleton(); #endregion ServiceProvider = serviceCollection.BuildServiceProvider(); #region 启动本机WebSocket服务 { var wssExeList = Process.GetProcessesByName("BBWY.WebSocket.Server"); if (wssExeList == null || wssExeList.Count() == 0) { try { ProcessStartInfo p = new ProcessStartInfo(); p.CreateNoWindow = true; p.FileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "BBWY.WebSocket.Server.exe"); p.Verb = "runas"; Process.Start(p); } catch (Exception ex) { MessageBox.Show(ex.Message, "启动bbwy websocket服务"); } } } #endregion //设置一个值,此值指示数据绑定 TextBox 是否应显示与源的 Text 属性值一致的字符串 为false System.Windows.FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false; base.OnStartup(e); } protected override void OnExit(ExitEventArgs e) { using (var s = ServiceProvider.CreateScope()) { var wb2m = s.ServiceProvider.GetRequiredService(); wb2m.Close(); } base.OnExit(e); } private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e) { Console.WriteLine(e.Exception); e.SetObserved(); } private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e) { throw new NotImplementedException(); } private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e) { Console.WriteLine(e.Exception); } public (bool isOk, string content) ReadMMF() { try { var token = MemoryHelper.GetMemoryToken(); if (string.IsNullOrEmpty(token)) return (false, "token为空"); else return (true, token); //using (MemoryMappedFile mmf = MemoryMappedFile.OpenExisting(mapname)) //{ // using (var mmfStream = mmf.CreateViewStream(0, 1000, MemoryMappedFileAccess.ReadWrite)) // { // StreamReader reader = new StreamReader(mmfStream); // string jwt = reader.ReadToEnd().Replace("\0", "").TrimEnd(); // return (true, jwt); // } //} } catch (Exception ex) { return (false, ex.Message); } } } }