7 changed files with 245 additions and 69 deletions
@ -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<User>(); |
||||
|
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<IList<Department>>(); |
||||
|
|
||||
|
|
||||
|
var shopList = new List<Shop>(); |
||||
|
foreach (var d in allDepartmentList) |
||||
|
shopList.AddRange(d.ShopList); |
||||
|
GlobalContext.User.ShopList = shopList; |
||||
|
|
||||
|
|
||||
|
IList<Department> 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<string>(); |
||||
|
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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,25 @@ |
|||||
|
<c:BWindow x:Class="BBWYB.Client.Views.Web" |
||||
|
xmlns:c="clr-namespace:SJ.Controls;assembly=SJ.Controls" |
||||
|
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" |
||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" |
||||
|
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" |
||||
|
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" |
||||
|
xmlns:local="clr-namespace:BBWYB.Client.Views" |
||||
|
mc:Ignorable="d" |
||||
|
Style="{StaticResource bwstyle}" |
||||
|
DataContext="{Binding WebVM,Source={StaticResource Locator}}" |
||||
|
Title="Web" Height="450" Width="800"> |
||||
|
<Grid x:Name="grid"> |
||||
|
<Grid.RowDefinitions> |
||||
|
<RowDefinition Height="30"/> |
||||
|
<RowDefinition/> |
||||
|
</Grid.RowDefinitions> |
||||
|
<Border BorderThickness="0,0,0,1" BorderBrush="{StaticResource MainMenu.BorderBrush}"> |
||||
|
<StackPanel Orientation="Horizontal" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="10,0,0,0"> |
||||
|
<TextBlock Text="{Binding GlobalContext.User.Name}"/> |
||||
|
<TextBlock Text="{Binding GlobalContext.BBWYBApiVersion}" Margin="5,0,0,0"/> |
||||
|
</StackPanel> |
||||
|
</Border> |
||||
|
<c:RoundWaitProgress Play="{Binding IsLoading}" Panel.ZIndex="999" Grid.RowSpan="2"/> |
||||
|
</Grid> |
||||
|
</c:BWindow> |
@ -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 |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// Web.xaml 的交互逻辑
|
||||
|
/// </summary>
|
||||
|
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<WebView2Manager>(); |
||||
|
globalContext = s.ServiceProvider.GetRequiredService<GlobalContext>(); |
||||
|
} |
||||
|
|
||||
|
WeakReferenceMessenger.Default.Register<Message_WebB_LoginCompleted>(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<object> |
||||
|
{ |
||||
|
public Message_WebB_LoginCompleted(object value) : base(value) |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
|
} |
Loading…
Reference in new issue