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

571 lines
22 KiB

using BBWY.Client.APIServices;
using BBWY.Client.Helpers;
using BBWY.Client.Models;
using BBWY.Client.Views;
using BBWY.Common.Extensions;
using BBWY.Common.Http;
using BBWY.Common.Models;
using GalaSoft.MvvmLight.Command;
using Microsoft.Web.WebView2.Core;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net.Http;
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
private IHttpClientFactory httpClientFactory;
private MdsApiService mdsApiService;
private LogisticsService logisticsService;
private ShopService shopService;
private MenuModel selectedMenuModel;
private bool showShopChoosePanel;
private bool showWB2RuntimeDownloadPanel;
private double wb2DownloadProgress;
private IList<string> managerDepartment;
private IList<string> packDepartment;
private WebView2Manager w2m;
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); } }
/// <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); } }
#endregion
#region Commands
public ICommand ClosingCommand { get; set; }
//public ICommand ChooseShopCommand { get; set; }
public ICommand OpenSelectShopCommand { get; set; }
#endregion
#region Methods
public MainViewModel(GlobalContext globalContext,
MdsApiService mdsApiService,
LogisticsService logisticsService,
ShopService shopService,
IHttpClientFactory httpClientFactory,
WebView2Manager w2m,
DongDongHelper dongdongHelper)
{
this.w2m = w2m;
this.managerDepartment = new List<string>() { "董事办", "财务部", "技术部", "总经办" };
this.packDepartment = new List<string>() { "打包组", "发货组", "仓储部", "供应链部" };
this.httpClientFactory = httpClientFactory;
this.mdsApiService = mdsApiService;
this.logisticsService = logisticsService;
this.shopService = shopService;
ClosingCommand = new RelayCommand<System.ComponentModel.CancelEventArgs>(Exit);
//ChooseShopCommand = new RelayCommand<Shop>((s) => ChooseShop(s));
OpenSelectShopCommand = new RelayCommand(OpenSelectShop);
this.GlobalContext = globalContext;
ShopList = new ObservableCollection<Shop>();
MenuList = new ObservableCollection<MenuModel>()
{
};
Task.Factory.StartNew(Login);
if (!CheckWebview2Runtime())
{
//下载webview2 runtime
Task.Factory.StartNew(DownloadWebview2Runtime);
}
dongdongHelper.StartInJection();
}
/// <summary>
/// 动态创建菜单
/// </summary>
private void CreateMenu()
{
if (this.packDepartment.Contains(GlobalContext.User.TeamName) || packDepartment.Any(m => GlobalContext.User.SonDepartmentNames.Contains(m)))
{
App.Current.Dispatcher.Invoke(() =>
{
MenuList.Add(new MenuModel()
{
Name = "齐库仓库端",
ChildList = new List<MenuModel>()
{
new MenuModel(){ Name="包装任务",Url="/Views/PackTask/WareHouseList.xaml" },
new MenuModel(){ Name="耗材管理",Url="/Views/PackTask/Consumable.xaml" },
new MenuModel(){ Name="账单管理",Url="/Views/PackTask/PackTaskTotal.xaml" },
new MenuModel(){ Name="收益账单",Url="/Views/TotalPackTask/PackUserSalaryList.xaml" }
}
});
});
}
else if (GlobalContext.User.TeamName == "供应链部")
{
App.Current.Dispatcher.Invoke(() =>
{
MenuList.Add(new MenuModel()
{
Name = "采购管理",
ChildList = new List<MenuModel>()
{
new MenuModel(){ Name="采购列表(Web)",Url="/Views/Web/Web.xaml" }
}
});
MenuList.Add(new MenuModel()
{
Name = "商品管理",
ChildList = new List<MenuModel>()
{
new MenuModel(){ Name="货源管理",Url="/Views/Ware/WareManager.xaml" }
}
});
MenuList.Add(new MenuModel()
{
Name = "齐库仓库端",
ChildList = new List<MenuModel>()
{
new MenuModel(){ Name="包装任务",Url="/Views/PackTask/WareHouseList.xaml" }
}
});
});
}
else
{
if (GlobalContext.User.TeamName == "刷单组")
{
App.Current.Dispatcher.Invoke(() => MenuList.Add(new MenuModel()
{
Name = "订单管理",
ChildList = new List<MenuModel>()
{
new MenuModel(){ Name="刷单组",Url="/Views/Order/SDGroup.xaml" }
}
}));
return;
}
App.Current.Dispatcher.Invoke(() =>
{
MenuList.Add(new MenuModel()
{
Name = "订单管理",
ChildList = new List<MenuModel>()
{
new MenuModel(){ Name="最近订单",Url="/Views/Order/OrderList.xaml" },
new MenuModel(){ Name="服务单管理",Url="/Views/ServiceOrder/ServiceOrderList.xaml" }
}
});
MenuList.Add(new MenuModel()
{
Name = "采购管理",
ChildList = new List<MenuModel>()
{
//new MenuModel(){ Name="采购列表",Url="/Views/BatchPurchase/BatchPurchaseOrderList.xaml" },
new MenuModel(){ Name="采购列表(Web)",Url="/Views/Web/Web.xaml" }
}
});
MenuList.Add(new MenuModel()
{
Name = "商品管理",
ChildList = new List<MenuModel>()
{
new MenuModel(){ Name="货源管理",Url="/Views/Ware/WareManager.xaml" },
new MenuModel(){ Name="产品库存",Url="/Views/Ware/WareStock.xaml" }
}
});
MenuList.Add(new MenuModel()
{
Name = "齐库",
ChildList = new List<MenuModel>()
{
new MenuModel(){ Name="发布任务",Url="/Views/PackTask/TaskList.xaml" },
new MenuModel(){ Name="包装账单",Url="/Views/TotalPackTask/ShopPackTaskTotal.xaml" },
}
});
});
if (managerDepartment.Contains(GlobalContext.User.TeamName) || managerDepartment.Any(m => GlobalContext.User.SonDepartmentNames.Contains(m)))
{
App.Current.Dispatcher.Invoke(() =>
{
MenuList.Add(new MenuModel()
{
Name = "齐库仓库端",
ChildList = new List<MenuModel>()
{
new MenuModel(){ Name="包装任务",Url="/Views/PackTask/WareHouseList.xaml" },
new MenuModel(){ Name="耗材管理",Url="/Views/PackTask/Consumable.xaml" },
new MenuModel(){ Name="账单管理",Url="/Views/PackTask/PackTaskTotal.xaml" },
new MenuModel(){ Name="收益账单",Url="/Views/TotalPackTask/PackUserSalaryList.xaml" }
}
});
});
App.Current.Dispatcher.Invoke(() => MenuList.Add(new MenuModel()
{
Name = "财务端",
ChildList = new List<MenuModel>()
{
new MenuModel(){ Name="采购审计",Url="/Views/FinancialTerminal/ProcurementAudit.xaml" },
new MenuModel(){ Name="费用矫正",Url="/Views/BillCorrection/BillCorrectionView.xaml" },
new MenuModel(){ Name="订单导出",Url="/Views/Order/ExportOrderSkuView.xaml" }
}
}));
}
App.Current.Dispatcher.Invoke(() =>
{
MenuList.Add(new MenuModel()
{
Name = "设置",
ChildList = new List<MenuModel>()
{
new MenuModel(){ Name="店铺设置",Url="/Views/Setting/ShopSetting.xaml" },
new MenuModel(){ Name="团队配置",Url="/Views/Setting/TeamSetting.xaml" }
}
});
});
}
}
private void Exit(System.ComponentModel.CancelEventArgs e)
{
App.Current.Shutdown();
//Environment.Exit(Environment.ExitCode);
}
private void Login()
{
try
{
Thread.Sleep(1000);
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));
CreateMenu();
IList<Department> departmentList = null;
var response1 = shopService.GetDepartmentList();
if (!response1.Success)
throw new Exception(response1.Msg);
departmentList = response1.Data.Map<IList<Department>>();
//if (GlobalContext.User.TeamName == "刷单组")
//{
var shopList = new List<Shop>();
foreach (var d in departmentList)
shopList.AddRange(d.ShopList);
GlobalContext.User.ShopList = shopList;
if (GlobalContext.User.TeamName == "刷单组" || this.packDepartment.Contains(GlobalContext.User.TeamName) || packDepartment.Any(m => GlobalContext.User.SonDepartmentNames.Contains(m)) ||
managerDepartment.Contains(GlobalContext.User.TeamName) ||
managerDepartment.Any(m => GlobalContext.User.SonDepartmentNames.Contains(m)))
{
ShowShopChoosePanel = true;
//}
}
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<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());
}
}
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;
}
}
}
}
if (departmentList.Count == 1 && departmentList[0].ShopList.Count == 1)
{
ShowShopChoosePanel = false;
ChooseShop(departmentList[0].ShopList[0], true);
return;
}
else
ShowShopChoosePanel = true;
}
GlobalContext.User.DepartmentList = departmentList;
if (this.packDepartment.Contains(GlobalContext.User.TeamName))
return;
if (GlobalContext.User.TeamName == "刷单组")
return;
App.Current.Dispatcher.Invoke(() =>
{
var selectShop = new SelectShopW(departmentList);
if (selectShop.ShowDialog() == true)
{
ChooseShop(selectShop.Shop, true);
}
else
{
Environment.Exit(Environment.ExitCode);
}
});
}
catch (Exception ex)
{
App.Current.Dispatcher.Invoke(() =>
{
MessageBox.Show(ex.Message, "登录失败");
});
Environment.Exit(Environment.ExitCode);
}
}
private void OpenSelectShop()
{
try
{
var selectShop = new SelectShopW(GlobalContext.User.DepartmentList);
if (selectShop.ShowDialog() == true)
{
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();
if (vm.IsCreateBatchPurchaseOrderList)
vm.BatchPurchaseOrderListVM.Refresh();
try
{
w2m.Close();
}
catch { }
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "切换失败");
}
}
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} 店铺数据不完整";
if (_throw)
throw new Exception(error);
else
{
MessageBox.Show(error, "选择店铺");
return;
}
}
GlobalContext.User.Shop = shop;
//ShowShopChoosePanel = false;
Task.Factory.StartNew(GetLogisticsList);
if (shop.Platform == Platform.)
Task.Factory.StartNew(GetJDServiceGroup);
}
private void GetLogisticsList()
{
var response = logisticsService.GetLogisticsList();
if (!response.Success)
{
App.Current.Dispatcher.Invoke(() => MessageBox.Show(response.Msg, "获取物流公司"));
return;
}
GlobalContext.LogisticsResponseList = response.Data;
}
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;
var url = "https://msedge.sf.dl.delivery.mp.microsoft.com/filestreamingservice/files/238fc310-c6c1-4a3e-a806-4a7c3c17b377/MicrosoftEdgeWebView2RuntimeInstallerX64.exe";
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;
}
private void GetJDServiceGroup()
{
var apiResponse = shopService.GetServiceGroupList();
if (!apiResponse.Success)
{
App.Current.Dispatcher.Invoke(() => MessageBox.Show(apiResponse.Msg, "获取客服组"));
if (apiResponse.Msg.Contains("订购链接"))
{
var url = "https://fw.jd.com/main/detail/FW_GOODS-187201";
try
{
ShellExecuteHelper.ShellExecute(IntPtr.Zero, "open", url, string.Empty, string.Empty, ShellExecuteHelper.ShowCommands.SW_SHOWNORMAL);
}
catch (Exception ex)
{
Clipboard.SetText(url);
App.Current.Dispatcher.Invoke(() => MessageBox.Show($"{ex.Message}\r\n打开订购页面失败,订购链接已复制到系统剪切板", "提示"));
}
}
//Environment.Exit(Environment.ExitCode);
}
GlobalContext.ShopServiceGroupList.Clear();
GlobalContext.ShopServiceGroupLowerList.Clear();
if (apiResponse.Data != null && apiResponse.Data.Count() > 0)
{
foreach (var waiter in apiResponse.Data)
{
GlobalContext.ShopServiceGroupList.Add(waiter.Name);
GlobalContext.ShopServiceGroupLowerList.Add(waiter.Name.ToLower());
}
}
}
#endregion
}
}