From c97986fe5e44d9733d6528843ad441f7322176fc Mon Sep 17 00:00:00 2001 From: shanj <18996038927@163.com> Date: Thu, 31 Aug 2023 17:24:59 +0800 Subject: [PATCH] =?UTF-8?q?web=E9=A1=B5=E9=9D=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- BBWYB.Client/App.xaml | 2 +- BBWYB.Client/GlobalContext.cs | 3 +- BBWYB.Client/ViewModels/ViewModelLocator.cs | 9 ++ BBWYB.Client/ViewModels/WebVM.cs | 96 +++++++++++++++++++++ BBWYB.Client/Views/Web.xaml | 25 ++++++ BBWYB.Client/Views/Web.xaml.cs | 83 ++++++++++++++++++ WebTest/MainWindow.xaml.cs | 96 +++++++-------------- 7 files changed, 245 insertions(+), 69 deletions(-) create mode 100644 BBWYB.Client/ViewModels/WebVM.cs create mode 100644 BBWYB.Client/Views/Web.xaml create mode 100644 BBWYB.Client/Views/Web.xaml.cs diff --git a/BBWYB.Client/App.xaml b/BBWYB.Client/App.xaml index 5939b16..2048417 100644 --- a/BBWYB.Client/App.xaml +++ b/BBWYB.Client/App.xaml @@ -4,7 +4,7 @@ xmlns:local="clr-namespace:BBWYB.Client" xmlns:vm="clr-namespace:BBWYB.Client.ViewModels" xmlns:ctr="clr-namespace:BBWYB.Client.Converters" - StartupUri="/Views/MainWindow.xaml" + StartupUri="/Views/Web.xaml" ShutdownMode="OnExplicitShutdown"> diff --git a/BBWYB.Client/GlobalContext.cs b/BBWYB.Client/GlobalContext.cs index 19f8137..1471516 100644 --- a/BBWYB.Client/GlobalContext.cs +++ b/BBWYB.Client/GlobalContext.cs @@ -1,7 +1,6 @@ using BBWYB.Client.APIServices; using BBWYB.Client.Helpers; using BBWYB.Client.Models; -using BBWYB.Client.ViewModels; using BBWYB.Client.Views.PackPurchaseTaska; using BBWYB.Client.Views.WebB; using CommunityToolkit.Mvvm.ComponentModel; @@ -22,7 +21,7 @@ namespace BBWYB.Client { public GlobalContext() { - BBWYBApiVersion = "10037"; + BBWYBApiVersion = "10038"; } private User user; diff --git a/BBWYB.Client/ViewModels/ViewModelLocator.cs b/BBWYB.Client/ViewModels/ViewModelLocator.cs index fe4ff35..cbddf8a 100644 --- a/BBWYB.Client/ViewModels/ViewModelLocator.cs +++ b/BBWYB.Client/ViewModels/ViewModelLocator.cs @@ -118,5 +118,14 @@ namespace BBWYB.Client.ViewModels return s.ServiceProvider.GetRequiredService(); } } + + public WebVM WebVM + { + get + { + using var s = sp.CreateScope(); + return s.ServiceProvider.GetRequiredService(); + } + } } } diff --git a/BBWYB.Client/ViewModels/WebVM.cs b/BBWYB.Client/ViewModels/WebVM.cs new file mode 100644 index 0000000..46cce31 --- /dev/null +++ b/BBWYB.Client/ViewModels/WebVM.cs @@ -0,0 +1,96 @@ +using BBWYB.Client.APIServices; +using BBWYB.Client.Models; +using BBWYB.Client.Views; +using BBWYB.Common.Extensions; +using BBWYB.Common.Models; +using CommunityToolkit.Mvvm.Messaging; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; +using System.Windows; + +namespace BBWYB.Client.ViewModels +{ + public class WebVM : BaseVM, IDenpendency + { + private MdsApiService mdsApiService; + private MenuModel selectedMenuModel; + private bool isLoading; + ShopService shopService; + public GlobalContext GlobalContext { get; set; } + public bool IsLoading { get => isLoading; set { SetProperty(ref isLoading, value); } } + + public WebVM(GlobalContext globalContext, + MdsApiService mdsApiService, + ShopService shopService) + { + this.mdsApiService = mdsApiService; + this.GlobalContext = globalContext; + this.shopService = shopService; + Task.Factory.StartNew(Login); + } + + private void Login() + { + IsLoading = true; + 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 res = shopService.GetDepartmentList(); + if (!res.Success) + throw new Exception(res.Msg); + var allDepartmentList = res.Data.Map>(); + + + var shopList = new List(); + foreach (var d in allDepartmentList) + shopList.AddRange(d.ShopList); + GlobalContext.User.ShopList = shopList; + + + IList departmentList = null; + + var response = mdsApiService.GetShopDetailList(); + if (!response.Success) + throw new Exception(response.Msg); + departmentList = response.Data?.Where(d => d.Name.Contains("供应链")).ToList(); + if (departmentList.Count == 0) + throw new Exception("缺少有效的部门数据"); + + var shopIds = new List(); + foreach (var d in departmentList) + { + if (d.ShopList != null && d.ShopList.Count > 0) + { + foreach (var s in d.ShopList) + shopIds.Add(s.ShopId.ToString()); + } + } + + GlobalContext.User.DepartmentList = departmentList; + WeakReferenceMessenger.Default.Send(new Message_WebB_LoginCompleted(null)); + IsLoading = false; + } + catch (Exception ex) + { + IsLoading = false; + App.Current.Dispatcher.Invoke(() => + { + MessageBox.Show(ex.Message, "登录失败"); + }); + Environment.Exit(Environment.ExitCode); + } + } + } +} diff --git a/BBWYB.Client/Views/Web.xaml b/BBWYB.Client/Views/Web.xaml new file mode 100644 index 0000000..626056f --- /dev/null +++ b/BBWYB.Client/Views/Web.xaml @@ -0,0 +1,25 @@ + + + + + + + + + + + + + + + diff --git a/BBWYB.Client/Views/Web.xaml.cs b/BBWYB.Client/Views/Web.xaml.cs new file mode 100644 index 0000000..67462c0 --- /dev/null +++ b/BBWYB.Client/Views/Web.xaml.cs @@ -0,0 +1,83 @@ +using CommunityToolkit.Mvvm.Messaging; +using CommunityToolkit.Mvvm.Messaging.Messages; +using Microsoft.Extensions.DependencyInjection; +using SJ.Controls; +using System.Windows; +using System.Windows.Controls; + +namespace BBWYB.Client.Views +{ + /// + /// Web.xaml 的交互逻辑 + /// + public partial class Web : BWindow + { + private WebView2Manager w2m; + private bool isNavigated; + private GlobalContext globalContext; + + public Web() + { + InitializeComponent(); + this.Width = SystemParameters.WorkArea.Size.Width * 0.8; + this.Height = SystemParameters.WorkArea.Size.Height * 0.7; + var sp = (App.Current as App).ServiceProvider; + using (var s = sp.CreateScope()) + { + w2m = s.ServiceProvider.GetRequiredService(); + globalContext = s.ServiceProvider.GetRequiredService(); + } + + WeakReferenceMessenger.Default.Register(this, (o, x) => + { + this.Dispatcher.BeginInvoke(initWebView); + }); + } + + private void Web_Loaded(object sender, System.Windows.RoutedEventArgs e) + { + + + } + + private void initWebView() + { +#if DEBUG + var url = "http://qtbbwy.qiyue666.com";//"http://192.168.1.2:8080"; + var registerName = "webTestContext"; + //var url = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "s.html"); +#else + var url = "http://qtbbwy.qiyue666.com"; + var registerName = "webContext"; +#endif + //var url = "http://qtbbwy.qiyue666.com"; + w2m.CoreWebView2InitializationCompleted = (e) => + { + w2m.wb2.CoreWebView2.AddHostObjectToScript(registerName, this.globalContext); + isNavigated = true; + w2m.wb2.CoreWebView2.Navigate(url); + }; + + + w2m.Init("bbwyb_web"); + w2m.wb2.SetValue(Grid.RowProperty, 1); + w2m.wb2.Margin = new Thickness(1, 0, 1, 0); + //grid.Children.Clear(); + grid.Children.Add(w2m.wb2); + + if (w2m.IsInitializationCompleted && !isNavigated) + { + w2m.wb2.CoreWebView2.Navigate(url); + //w2m.wb2.CoreWebView2.NavigateToString(content); + isNavigated = true; + } + } + } + + public class Message_WebB_LoginCompleted : ValueChangedMessage + { + public Message_WebB_LoginCompleted(object value) : base(value) + { + } + } +} diff --git a/WebTest/MainWindow.xaml.cs b/WebTest/MainWindow.xaml.cs index 1653a5c..b096721 100644 --- a/WebTest/MainWindow.xaml.cs +++ b/WebTest/MainWindow.xaml.cs @@ -126,78 +126,42 @@ private string registerName = "webContext"; if (mdsUserResponse.Data.SonDepartmentList != null && mdsUserResponse.Data.SonDepartmentList.Count > 0) globalContext.User.SonDepartmentNames = string.Join(',', mdsUserResponse.Data.SonDepartmentList.Select(sd => sd.DepartmentName)); + + //if (GlobalContext.User.TeamName == "刷单组") + // return; + + var res = shopService.GetDepartmentList(); + if (!res.Success) + throw new Exception(res.Msg); + var allDepartmentList = res.Data.Map>(); + + //if (GlobalContext.User.TeamName == "刷单组") + //{ + //var shopList = new List(); + //foreach (var d in allDepartmentList) + // shopList.AddRange(d.ShopList); + //globalContext.User.ShopList = shopList; + + IList departmentList = null; - if (globalContext.User.TeamName == "刷单组" || - managerDepartment.Contains(globalContext.User.TeamName) || - managerDepartment.Any(m => globalContext.User.SonDepartmentNames.Contains(m))) - { - var response = shopService.GetDepartmentList(); - if (!response.Success) - throw new Exception(response.Msg); - departmentList = response.Data.Map>(); - } - else - { - var response = mdsApiService.GetShopDetailList(); - if (!response.Success) - throw new Exception(response.Msg); - departmentList = response.Data; - if (departmentList.Count == 0) - throw new Exception("缺少有效的部门数据"); - - var shopIds = new List(); - foreach (var d in departmentList) - { - if (d.ShopList != null && d.ShopList.Count > 0) - { - foreach (var s in d.ShopList) - shopIds.Add(s.ShopId.ToString()); - } - } - var shopList2Res = shopService.GetShopListByIds(shopIds); - if (shopList2Res.Success && shopList2Res.Data != null && shopList2Res.Data.Count() > 0) - { - foreach (var d in departmentList) - { - foreach (var shop in d.ShopList) - { - var s2 = shopList2Res.Data.FirstOrDefault(s => s.ShopId == shop.ShopId); - if (s2 != null) - { - shop.DingDingKey = s2.DingDingKey; - shop.DingDingWebHook = s2.DingDingWebHook; - shop.SkuSafeTurnoverDays = s2.SkuSafeTurnoverDays; - shop.SiNanPolicyLevel = s2.SiNanPolicyLevel; - shop.SiNanDingDingKey = s2.SiNanDingDingKey; - shop.SiNanDingDingWebHook = s2.SiNanDingDingWebHook; - shop.AppKey2 = s2.AppKey2; - shop.AppSecret2 = s2.AppSecret2; - shop.AppToken2 = s2.AppToken2; - } - } - } - } - } - for (var i = 0; i < departmentList.Count(); i++) + var response = mdsApiService.GetShopDetailList(); + if (!response.Success) + throw new Exception(response.Msg); + departmentList = response.Data?.Where(d => d.Name.Contains("供应链")).ToList(); + if (departmentList.Count == 0) + throw new Exception("缺少有效的部门数据"); + + var shopIds = new List(); + foreach (var d in departmentList) { - var d = departmentList[i]; - for (var j = 0; j < d.ShopList.Count(); j++) + if (d.ShopList != null && d.ShopList.Count > 0) { - var shop = d.ShopList[j]; - if (string.IsNullOrEmpty(shop.AppToken2)) - { - d.ShopList.RemoveAt(j); - j--; - } + foreach (var s in d.ShopList) + shopIds.Add(s.ShopId.ToString()); } - if (d.ShopList == null || d.ShopList.Count() == 0) - { - departmentList.RemoveAt(i); - i--; - } - } + globalContext.User.DepartmentList = departmentList; }