步步为盈
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
5.8 KiB

3 years ago
using BBWY.Client.Models;
using BBWY.Client.ViewModels;
3 years ago
using BBWY.Common.Extensions;
using BBWY.Common.Http;
using BBWY.Common.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
3 years ago
using System.IO.MemoryMappedFiles;
3 years ago
using System.Linq;
3 years ago
using System.Net.Http;
3 years ago
using System.Reflection;
3 years ago
using System.Text;
3 years ago
using System.Threading.Tasks;
using System.Windows;
namespace BBWY.Client
{
/// <summary>
/// Interaction logic for App.xaml
/// </summary>
public partial class App : Application
{
public IServiceProvider ServiceProvider { get; private set; }
public IConfiguration Configuration { get; private set; }
protected override void OnStartup(StartupEventArgs e)
{
3 years ago
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
3 years ago
var gl = new GlobalContext();
3 years ago
string userToken = string.Empty;
3 years ago
#if DEBUG
userToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxNDE1OTMxMjU4NjEzMDEwNDMyIiwidGVhbUlkIjoiMTQxNDkzNTcwNDQ2MjQzMDIwOCIsInNvblRlYW1JZHMiOiIiLCJleHAiOjE2OTQxMDY1MjV9.XeMruS0YkwI7B6nODGe-vbdUqA_PSAt4EacfRAHIjV0"; //齐越文魁
//userToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxNDA1MTUxNjE5NTk0NTg4MTYwIiwidGVhbUlkIjoiMTQxMDk1NTYwNDYxMjA5MTkwNCIsImV4cCI6MTY4NjQ4MjU0M30.emnQNDcbqnuO2jH0Kgfk2Ui7crrwzRb986Sa511iS-U"; //齐越小一
//userToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxNDM5OTA1OTcyOTk3ODUzMTg0IiwidGVhbUlkIjoiMTQzOTkwNDA1MDc3NzY5MDExMiIsImV4cCI6MTY4NjQxODcyOH0.K7-MXjt_zfNK5v_08VjmCDnhAGF6SI7sEQr5sgDn7Jk"; //齐越珍珍
3 years ago
3 years ago
//userToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxNDM5OTE0NjczMzA5NzQ5MjQ4IiwidGVhbUlkIjoiMTQ0ODY4MTU0NDE4ODE3MDI0MCIsImV4cCI6MTY4NjQ1OTY0Mn0.4yCbZxOtogD0N868-YgySR5A-16VNnrp8fC13VCRs20"; //齐越耐克
3 years ago
3 years ago
#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(uid);
if (tokenResult.isOk)
userToken = tokenResult.content;
else
{
MessageBox.Show($"读取内存数据失败\r\n{tokenResult.content}", "提示");
Environment.Exit(0);
}
#endif
3 years ago
gl.UserToken = userToken;
3 years ago
#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;
IServiceCollection serviceCollection = new ServiceCollection();
serviceCollection.AddHttpClient();
3 years ago
serviceCollection.AddHttpClient("gzip").ConfigurePrimaryHttpMessageHandler(handler => new HttpClientHandler()
{
AutomaticDecompression = System.Net.DecompressionMethods.GZip
});
serviceCollection.AddMemoryCache();
3 years ago
serviceCollection.AddSingleton<RestApiService>();
serviceCollection.AddSingleton(gl);
serviceCollection.BatchRegisterServices(new Assembly[] { Assembly.Load("BBWY.Client") }, typeof(IDenpendency));
serviceCollection.AddMapper(new MappingProfile());
serviceCollection.AddTransient<_1688PreviewPurchaseViewModel>();
3 years ago
ServiceProvider = serviceCollection.BuildServiceProvider();
base.OnStartup(e);
}
3 years ago
protected override void OnExit(ExitEventArgs e)
{
using (var s = ServiceProvider.CreateScope())
{
var wb2m = s.ServiceProvider.GetRequiredService<WebView2Manager>();
wb2m.Close();
}
base.OnExit(e);
}
3 years ago
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);
}
3 years ago
3 years ago
public (bool isOk, string content) ReadMMF(string mapname)
{
try
{
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);
}
}
3 years ago
}
}