步步为盈
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.

350 lines
13 KiB

3 years ago
using BBWY.Client.APIServices;
using BBWY.Client.Models;
3 years ago
using BBWY.Client.Views;
3 years ago
using BBWY.Common.Extensions;
3 years ago
using BBWY.Common.Http;
3 years ago
using BBWY.Common.Models;
using GalaSoft.MvvmLight.Command;
3 years ago
using Microsoft.Web.WebView2.Core;
3 years ago
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
3 years ago
using System.Net.Http;
3 years ago
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
namespace BBWY.Client.ViewModels
{
public class MainViewModel : BaseVM, IDenpendency
{
#region Properties
3 years ago
private IHttpClientFactory httpClientFactory;
3 years ago
private MdsApiService mdsApiService;
3 years ago
private LogisticsService logisticsService;
private ShopService shopService;
3 years ago
private MenuModel selectedMenuModel;
private bool showShopChoosePanel;
3 years ago
private bool showWB2RuntimeDownloadPanel;
private double wb2DownloadProgress;
private IList<string> managerDepartment;
private WebView2Manager w2m;
3 years ago
public GlobalContext GlobalContext { get; set; }
public IList<MenuModel> MenuList { get; set; }
public IList<Shop> ShopList { get; set; }
public MenuModel SelectedMenuModel
{
get => selectedMenuModel;
set
{
if (value == null)
return;
Set(ref selectedMenuModel, value);
foreach (var menu in MenuList)
{
foreach (var cmenu in menu.ChildList)
{
if (!ReferenceEquals(value, cmenu))
cmenu.IsSelected = false;
}
}
}
}
/// <summary>
/// 是否显示店铺选择列表
/// </summary>
public bool ShowShopChoosePanel { get => showShopChoosePanel; set { Set(ref showShopChoosePanel, value); } }
3 years ago
/// <summary>
/// 是否显示webview2 runtime下载面板
/// </summary>
public bool ShowWB2RuntimeDownloadPanel { get => showWB2RuntimeDownloadPanel; set { Set(ref showWB2RuntimeDownloadPanel, value); } }
/// <summary>
/// webview2 runtime下载进度
/// </summary>
public double Wb2DownloadProgress { get => wb2DownloadProgress; set { Set(ref wb2DownloadProgress, value); } }
3 years ago
#endregion
#region Commands
public ICommand ClosingCommand { get; set; }
3 years ago
//public ICommand ChooseShopCommand { get; set; }
public ICommand OpenSelectShopCommand { get; set; }
3 years ago
#endregion
#region Methods
public MainViewModel(GlobalContext globalContext,
MdsApiService mdsApiService,
LogisticsService logisticsService,
ShopService shopService,
IHttpClientFactory httpClientFactory,
WebView2Manager w2m)
3 years ago
{
this.w2m = w2m;
3 years ago
this.managerDepartment = new List<string>() { "董事办", "财务部", "技术部", "总经办" };
3 years ago
this.httpClientFactory = httpClientFactory;
3 years ago
this.mdsApiService = mdsApiService;
3 years ago
this.logisticsService = logisticsService;
this.shopService = shopService;
3 years ago
ClosingCommand = new RelayCommand<System.ComponentModel.CancelEventArgs>(Exit);
3 years ago
//ChooseShopCommand = new RelayCommand<Shop>((s) => ChooseShop(s));
OpenSelectShopCommand = new RelayCommand(OpenSelectShop);
3 years ago
this.GlobalContext = globalContext;
ShopList = new ObservableCollection<Shop>();
MenuList = new ObservableCollection<MenuModel>()
3 years ago
{
new MenuModel()
{
Name="订单管理",ChildList=new List<MenuModel>()
{
new MenuModel(){ Name="最近订单",Url="/Views/Order/OrderList.xaml" },
//new MenuModel(){ Name="售后管理",Url="/Views/Order/OrderList.xaml" }
3 years ago
}
},
new MenuModel()
{
Name="商品管理",ChildList=new List<MenuModel>()
{
new MenuModel(){ Name="货源管理",Url="/Views/Ware/WareManager.xaml" },
new MenuModel(){ Name="产品库存",Url="/Views/Ware/WareStock.xaml" }
}
3 years ago
},
new MenuModel()
{
Name="设置",ChildList=new List<MenuModel>()
{
new MenuModel(){ Name="店铺设置",Url="/Views/Setting/ShopSetting.xaml" },
new MenuModel(){ Name="团队配置",Url="/Views/Setting/TeamSetting.xaml" }
}
3 years ago
},
new MenuModel()
{
Name="财务端",ChildList=new List<MenuModel>()
{
new MenuModel(){ Name="采购审计",Url="/Views/FinancialTerminal/ProcurementAudit.xaml" }
//new MenuModel(){ Name="补单审计",Url="/Views/FinancialTerminal/ShopSetting.xaml" },
//new MenuModel(){ Name="利润表",Url="/Views/FinancialTerminal/ShopSetting.xaml" }
}
3 years ago
}
};
Task.Factory.StartNew(Login);
3 years ago
if (!CheckWebview2Runtime())
{
//下载webview2 runtime
Task.Factory.StartNew(DownloadWebview2Runtime);
}
3 years ago
}
private void Exit(System.ComponentModel.CancelEventArgs e)
{
App.Current.Shutdown();
//Environment.Exit(Environment.ExitCode);
}
private void Login()
{
try
{
Thread.Sleep(1000);
3 years ago
var mdsUserResponse = mdsApiService.GetUserInfo(GlobalContext.UserToken);
3 years ago
if (!mdsUserResponse.Success)
throw new Exception($"获取磨刀石用户信息失败 {mdsUserResponse.Msg}");
GlobalContext.User = mdsUserResponse.Data.Map<User>();
#if RELEASE
if (!managerDepartment.Contains(GlobalContext.User.TeamName)) //非管理账号,屏蔽财务端
{
App.Current.Dispatcher.Invoke(() =>
{
MenuList.RemoveAt(MenuList.Count() - 1);
});
}
#endif
IList<Department> departmentList = null;
3 years ago
if (!managerDepartment.Contains(GlobalContext.User.TeamName))
3 years ago
{
var shopListResponse = mdsApiService.GetShopsByUserTeam(GlobalContext.User.Id);
if (!shopListResponse.Success)
throw new Exception(shopListResponse.Msg);
if (shopListResponse.Data == null || shopListResponse.Data.Count == 0)
throw new Exception("未绑定店铺");
var shopList = shopListResponse.Data.Map<IList<Shop>>();
if (shopList.Count == 1)
3 years ago
{
3 years ago
ShowShopChoosePanel = false;
ChooseShop(shopList[0], true);
return;
}
else
{
3 years ago
ShowShopChoosePanel = true;
departmentList = new List<Department>();
3 years ago
foreach (var shop in shopList)
{
var department = departmentList.FirstOrDefault(d => d.Id == shop.TeamId);
3 years ago
if (department == null)
{
department = new Department() { Id = shop.TeamId, Name = shop.TeamName };
departmentList.Add(department);
3 years ago
}
department.ShopList.Add(shop);
}
}
3 years ago
}
else
{
3 years ago
ShowShopChoosePanel = true;
var response = shopService.GetDepartmentList();
if (!response.Success)
throw new Exception(response.Msg);
departmentList = response.Data.Map<IList<Department>>();
}
GlobalContext.User.DepartmentList = departmentList;
App.Current.Dispatcher.Invoke(() =>
{
var selectShop = new SelectShop(departmentList);
if (selectShop.ShowDialog() == true)
{
ChooseShop(selectShop.Shop, true);
}
else
{
Environment.Exit(Environment.ExitCode);
}
});
3 years ago
}
catch (Exception ex)
{
App.Current.Dispatcher.Invoke(() =>
{
MessageBox.Show(ex.Message, "登录失败");
});
Environment.Exit(Environment.ExitCode);
}
}
3 years ago
private void OpenSelectShop()
{
3 years ago
try
3 years ago
{
3 years ago
var selectShop = new SelectShop(GlobalContext.User.DepartmentList);
if (selectShop.ShowDialog() == true)
{
3 years ago
ChooseShop(selectShop.Shop, true);
var vm = App.Current.Resources["Locator"] as ViewModelLocator;
if (vm.IsCreateOrderList)
vm.OrderList.Refresh();
if (vm.IsCreateWareManager)
vm.WareManager.Refresh();
if (vm.IsCreateWareStock)
vm.WareStock.Refresh();
try
{
w2m.Close();
}
catch { }
}
3 years ago
}
3 years ago
catch (Exception ex)
{
MessageBox.Show(ex.Message, "切换失败");
}
3 years ago
}
3 years ago
private void ChooseShop(Shop shop, bool _throw = false)
{
if (shop.ShopId == 0 ||
string.IsNullOrEmpty(shop.AppKey) ||
string.IsNullOrEmpty(shop.AppSecret) ||
string.IsNullOrEmpty(shop.AppToken) ||
(shop.Platform == Platform. && string.IsNullOrEmpty(shop.VenderType)))
{
var error = $"{shop.ShopName} 店铺数据不完整";
3 years ago
if (_throw)
throw new Exception(error);
else
{
MessageBox.Show(error, "选择店铺");
return;
}
}
GlobalContext.User.Shop = shop;
3 years ago
//ShowShopChoosePanel = false;
3 years ago
Task.Factory.StartNew(GetLogisticsList);
}
private void GetLogisticsList()
{
var response = logisticsService.GetLogisticsList();
if (!response.Success)
{
App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "获取物流公司"));
return;
}
GlobalContext.LogisticsResponseList = response.Data;
3 years ago
}
3 years ago
private bool CheckWebview2Runtime()
{
bool isInstall = false;
try
{
isInstall = !string.IsNullOrEmpty(CoreWebView2Environment.GetAvailableBrowserVersionString());
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
return isInstall;
}
private void DownloadWebview2Runtime()
{
ShowWB2RuntimeDownloadPanel = true;
var dw = new HttpDownloader(httpClientFactory, null);
dw.OnDownloadProgressChanged += Dw_OnDownloadProgressChanged;
dw.OnDownloadComplated += Dw_OnDownloadComplated;
3 years ago
var url = "https://msedge.sf.dl.delivery.mp.microsoft.com/filestreamingservice/files/f72be533-7a03-499e-be21-bfc3e89e9cd5/MicrosoftEdgeWebView2RuntimeInstallerX64.exe";
3 years ago
dw.DownloadFile(url, System.IO.Path.GetTempPath(), "MicrosoftEdgeWebView2RuntimeInstallerX64.exe", null);
dw.OnDownloadProgressChanged -= Dw_OnDownloadProgressChanged;
dw.OnDownloadComplated -= Dw_OnDownloadComplated;
}
private void Dw_OnDownloadComplated(object sender, DownloadCompletedEventArgs e)
{
try
{
if (e.Error == null)
{
System.Diagnostics.Process.Start(System.IO.Path.Combine(System.IO.Path.GetTempPath(), "MicrosoftEdgeWebView2RuntimeInstallerX64.exe"));
ShowWB2RuntimeDownloadPanel = false;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Dw_OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Wb2DownloadProgress = e.ProgressPercentage;
}
#endregion
3 years ago
}
}