using Microsoft.Extensions.DependencyInjection; using Microsoft.Web.WebView2.Core; using Microsoft.Web.WebView2.Wpf; using QYMessageCenter.Client.APIServices; using QYMessageCenter.Client.Models; using QYMessageCenter.Common.Extensions; using System.IO; using System.Reflection; using System.Text; using System.Transactions; using System.Windows; using io = System.IO; namespace QYMessageCenter.Client { /// /// Interaction logic for MainWindow.xaml /// public partial class MainWindow : Window { private WebView2 wb2; private MdsApiService mdsApiService; private ShopService shopService; private GlobalContext globalContext; public MainWindow() { InitializeComponent(); var sp = (App.Current as App).ServiceProvider; using (var s = sp.CreateScope()) { this.mdsApiService = s.ServiceProvider.GetRequiredService(); this.shopService = s.ServiceProvider.GetRequiredService(); this.globalContext = s.ServiceProvider.GetRequiredService(); } this.Loaded += MainWindow_Loaded; } private async void MainWindow_Loaded(object sender, RoutedEventArgs e) { this.Title = $"齐越消息中心 {globalContext.Version}"; wb2 = new WebView2(); grid.Children.Add(wb2); var wb2Setting = CoreWebView2Environment.CreateAsync(userDataFolder: io.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "QYMessageCenter.Client")).Result; wb2.CoreWebView2InitializationCompleted += Wb2_CoreWebView2InitializationCompleted; await wb2.EnsureCoreWebView2Async(wb2Setting); wb2.CoreWebView2.PermissionRequested += CoreWebView2_PermissionRequested; //wb2.CoreWebView2.NavigateToString(html); } private void CoreWebView2_PermissionRequested(object? sender, CoreWebView2PermissionRequestedEventArgs e) { e.State = CoreWebView2PermissionState.Allow; } private void Wb2_CoreWebView2InitializationCompleted(object? sender, CoreWebView2InitializationCompletedEventArgs e) { Login(); wb2.CoreWebView2.AddHostObjectToScript("qymsgcenter", this.globalContext); //var html = File.ReadAllText(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "static", "index.html"), Encoding.UTF8); //wb2.CoreWebView2.NavigateToString(html); var htmlPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "static", "index.html"); wb2.CoreWebView2.Navigate(htmlPath); this.Visibility = Visibility.Collapsed; //globalContext.Test(); } private void Login() { try { var mdsUserResponse = mdsApiService.GetUserInfo(globalContext.UserToken); if (!mdsUserResponse.Success) throw new Exception($"获取磨刀石用户信息失败 {mdsUserResponse.Msg}"); globalContext.User = mdsUserResponse.Data.Map(); globalContext.User.Token = globalContext.UserToken; globalContext.User.SonDepartmentNames = string.Empty; //if (mdsUserResponse.Data.SonDepartmentList != null && mdsUserResponse.Data.SonDepartmentList.Count > 0) // globalContext.User.SonDepartmentNames = string.Join(',', mdsUserResponse.Data.SonDepartmentList.Select(sd => sd.DepartmentName)); var response = mdsApiService.GetShopDetailList(); if (!response.Success) throw new Exception(response.Msg); var departmentList = response.Data; if (departmentList.Count == 0) throw new Exception("缺少有效的部门数据"); globalContext.User.DepartmentList = departmentList; } catch (Exception ex) { App.Current.Dispatcher.Invoke(() => { MessageBox.Show(ex.Message, "登录失败"); }); Environment.Exit(Environment.ExitCode); } } } }