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

185 lines
6.8 KiB

3 years ago
using BBWY.Client.APIServices;
using BBWY.Client.Models;
using BBWY.Common.Extensions;
using BBWY.Common.Models;
using GalaSoft.MvvmLight.Command;
using Jd.Api;
using Jd.Api.Request;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
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 MdsApiService mdsApiService;
private MenuModel selectedMenuModel;
private bool showShopChoosePanel;
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); } }
#endregion
#region Commands
public ICommand ClosingCommand { get; set; }
public ICommand ChooseShopCommand { get; set; }
#endregion
#region Methods
public MainViewModel(GlobalContext globalContext, MdsApiService mdsApiService)
{
this.mdsApiService = mdsApiService;
ClosingCommand = new RelayCommand<System.ComponentModel.CancelEventArgs>(Exit);
ChooseShopCommand = new RelayCommand<Shop>((s) => ChooseShop(s));
this.GlobalContext = globalContext;
ShopList = new ObservableCollection<Shop>();
MenuList = new List<MenuModel>()
{
new MenuModel()
{
Name="订单管理",ChildList=new List<MenuModel>()
{
new MenuModel(){ Name="最近订单",Url="/Views/Order/OrderList.xaml" },
//new MenuModel(){ Name="等待采购",Url="/Views/Order/OrderList.xaml" },
//new MenuModel(){ Name="待出库",Url="/Views/Order/OrderList.xaml" },
new MenuModel(){ Name="售后管理",Url="/Views/Order/OrderList.xaml" }
}
},
new MenuModel()
{
Name="商品管理",ChildList=new List<MenuModel>()
{
new MenuModel(){ Name="货源管理",Url="/Views/Ware/WareManager.xaml" },
new MenuModel(){ Name="产品库存",Url="/Views/Ware/WareStock.xaml" }
}
}
};
Task.Factory.StartNew(Login);
}
private void Exit(System.ComponentModel.CancelEventArgs e)
{
App.Current.Shutdown();
//Environment.Exit(Environment.ExitCode);
}
private void Login()
{
try
{
Thread.Sleep(1000);
//从磨刀石获取用户Token
var userToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxNDA1MTUxNjE5NTk0NTg4MTYwIiwidGVhbUlkIjoiMTQzOTg5OTEyMzk1NTI3MzcyOCIsImV4cCI6MTY3MTkwMTU1NH0.UaUubqP442qxVc6ppQt7FO0jcFs3w6KR6q1OeBuL1i8"; //齐越小一
//var userToken = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VySWQiOiIxNDE1OTMxMjU4NjEzMDEwNDMyIiwidGVhbUlkIjoiMTQxNDkzNTcwNDQ2MjQzMDIwOCIsImV4cCI6MTY3MjgzMTE3N30.gOlJav6J1bSrLvIzUCmc3zfTYcW_8hAlUpJxc02kyos"; //齐越文魁
var mdsUserResponse = mdsApiService.GetUserInfo(userToken);
if (!mdsUserResponse.Success)
throw new Exception($"获取磨刀石用户信息失败 {mdsUserResponse.Msg}");
GlobalContext.User = mdsUserResponse.Data.Map<User>();
GlobalContext.User.Token = userToken;
//请求用户信息接口
//GlobalContext.User = new User()
//{
// Id = userId,
// Name = "ShanJ",
// Store = new Store()
// {
// Platform = Platform.京东,
// AppKey = "120EA9EC65AB017567D78CC1139EEEA5",
// AppSecret = "866a9877f5f24b03b537483b4defe75d",
// AppToken = "d8433fb2a4994484b5d9e5a5896a6dfdmyjj" //10388155
3 years ago
// }
//};
var shopListResponse = mdsApiService.GetShopsByUserId(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)
{
ChooseShop(shopList[0], true);
}
else
{
App.Current.Dispatcher.Invoke(() =>
{
foreach (var s in shopList)
ShopList.Add(s);
});
ShowShopChoosePanel = true;
}
}
catch (Exception ex)
{
App.Current.Dispatcher.Invoke(() =>
{
MessageBox.Show(ex.Message, "登录失败");
});
Environment.Exit(Environment.ExitCode);
}
}
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.Name} 店铺数据不完整";
if (_throw)
throw new Exception(error);
else
{
MessageBox.Show(error, "选择店铺");
return;
}
}
GlobalContext.User.Shop = shop;
ShowShopChoosePanel = false;
}
#endregion
}
}