using System.Linq;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Ioc;
using Muchinfo.MTPClient.Account.Views;
using Muchinfo.MTPClient.Data;
using Muchinfo.MTPClient.Data.Enums;
using Muchinfo.MTPClient.Data.Model.Account;
using Muchinfo.MTPClient.Infrastructure.Helpers;
using Muchinfo.MTPClient.Infrastructure.MessageBox;
using Muchinfo.MTPClient.Infrastructure.Utilities;
using Muchinfo.MTPClient.IService;
using Muchinfo.WPF.Controls.Windows;
using System;
using System.Collections.Generic;
using System.Windows;
namespace Muchinfo.MTPClient.Account.ViewModels
{
///
/// 交易密码修改业务逻辑
///
public class TradePWDModifyViewModel : ViewModelBase
{
#region Constructors
public TradePWDModifyViewModel(bool isUnforceUpdate)
{
OldLoginPassword = string.Empty;
NewLoginPassword = string.Empty;
ConfLoginPassword = string.Empty;
OldFundsPassword = string.Empty;
NewFundsPassword = string.Empty;
ConfFundsPassword = string.Empty;
IsTradePanel = true;
LoginID = UserManager.CurrentTradeAccount != null ? UserManager.CurrentTradeAccount.LoginID : uint.MaxValue;
FundsLst = UserManager.CurrentTradeAccount != null ? UserManager.CurrentTradeAccount.FundsAccountIds : new List();
_loginService = SimpleIoc.Default.GetService(typeof(ILoginService)) as ILoginService;
IsUnforceUpdate = isUnforceUpdate;
}
#endregion Constructors
#region Properties
public ILoginService _loginService { get; set; }
private int ServerCount = 0;
private bool _isTradePanel;
///
/// 是否选中修改交易密码面板
///
public bool IsTradePanel
{
get { return _isTradePanel; }
set
{
Set(() => IsTradePanel, ref _isTradePanel, value);
ErrorMsg = string.Empty;
}
}
private bool _isPhonePanel;
///
/// 是否选中修改电话密码面板
///
public bool IsPhonePanel
{
get { return _isPhonePanel; }
set
{
Set(() => IsPhonePanel, ref _isPhonePanel, value);
ErrorMsg = string.Empty;
}
}
private bool _isCommunicationPanel;
///
/// 是否选中修改通讯密码面板
///
public bool IsCommunicationPanel
{
get { return _isCommunicationPanel; }
set
{
Set(() => IsCommunicationPanel, ref _isCommunicationPanel, value);
ErrorMsg = string.Empty;
}
}
private bool _isFundsPanel;
///
/// 是否选中修改通讯密码面板
///
public bool IsFundsPanel
{
get { return _isFundsPanel; }
set
{
Set(() => IsFundsPanel, ref _isFundsPanel, value);
ErrorMsg = string.Empty;
}
}
//错误提示
private string _errorMsg;
///
/// 修改不成功出现的错误提示
///
public string ErrorMsg
{
get
{
return _errorMsg;
}
set
{
Set(() => ErrorMsg, ref _errorMsg, value);
}
}
///
/// 是否显示忙
///
private bool _isBusy;
///
/// 是否忙,显示等待控件
///
public bool IsBusy
{
get { return _isBusy; }
set { Set(() => IsBusy, ref _isBusy, value); }
}
///
/// 登录账号
///
public ulong LoginID { get; set; }
///
/// 旧登录密码
///
public string OldLoginPassword { get; set; }
///
/// 新登录密码
///
public string NewLoginPassword { get; set; }
///
/// 确认登录密码
///
public string ConfLoginPassword { get; set; }
///
/// 资金账户ID列表
///
public List FundsLst { get; set; }
///
/// 旧资金帐号密码
///
public string OldFundsPassword { get; set; }
///
/// 新资金账号密码
///
public string NewFundsPassword { get; set; }
///
/// 确认资金帐号密码
///
public string ConfFundsPassword { get; set; }
private ulong _currentFundID = uint.MaxValue;
public ulong CurrentFundID
{
get { return _currentFundID; }
set
{
Set(() => CurrentFundID, ref _currentFundID, value);
}
}
private bool _isUnforceUpdate = true;
///
/// 是否非强制修改
///
/// true if this instance is unforce update; otherwise, false.
public bool IsUnforceUpdate
{
get { return _isUnforceUpdate; }
set
{
Set(() => IsUnforceUpdate, ref _isUnforceUpdate, value);
}
}
#endregion Properties
#region Command
public RelayCommand CancelCommand
{
get
{
return new RelayCommand((w) =>
{
w.DialogResult = false;
});
}
}
///
/// 修改密码
///
public RelayCommand OKCommand
{
get
{
return new RelayCommand((w) =>
{
if (!ValidData()) return;
var pwds = new AccountPwds() { ModifyPwdType = 1 };
if (IsTradePanel)
{
pwds.ModifyPwdType = 1;
pwds.ModifyPwdID = LoginID;
pwds.OldPwd = string.IsNullOrEmpty(OldLoginPassword) ? string.Empty : EncryptHelper.SHA256(LoginID.ToString().Trim() + OldLoginPassword.Trim()).ToLower();
pwds.NewPwd = string.IsNullOrEmpty(NewLoginPassword) ? string.Empty : EncryptHelper.SHA256(LoginID.ToString().Trim() + NewLoginPassword.Trim()).ToLower(); ;
}
else if (IsFundsPanel)
{
pwds.ModifyPwdType = 2;
pwds.ModifyPwdID = CurrentFundID;
pwds.OldPwd = string.IsNullOrEmpty(CurrentFundID.ToString()) ? string.Empty : EncryptHelper.SHA256(CurrentFundID.ToString().Trim() + OldFundsPassword.Trim()).ToLower();
pwds.NewPwd = string.IsNullOrEmpty(CurrentFundID.ToString()) ? string.Empty : EncryptHelper.SHA256(CurrentFundID.ToString().Trim() + NewFundsPassword.Trim()).ToLower();
}
ServerCount = 1;
_loginService.ModifyPasswords(pwds, ModifyPWDSuccess, ModifyPWdError);
IsBusy = true;
});
}
}
private void ModifyPWDSuccess(bool isOK)
{
ServerCount--;
if (ServerCount > 0)
{
return;
}
//修改成功清空密码
IsBusy = false;
ResetPWD();
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
//LogManager.WritePassWordModifyLog(UserManager.CurrentTradeAccount, 4);
MessageBoxHelper.ShowInfo("修改成功 "+Muchinfo.MTPClient.Resources.Client_Resource.Models_MakesureSafe_Relogin,
Muchinfo.MTPClient.Resources.Client_Resource.Models_Tips);
MessengerHelper.DefaultSend(null, MessengerTokens.RerunApplication);
}));
}
private void ModifyMemberAmountPWdError(ErrorEntity errorEntity)
{
IsBusy = false;
ResetPWD();
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
ErrorManager.ShowReturnError(errorEntity, Muchinfo.MTPClient.Resources.Client_Resource.ModelsMember_ErrorTips);
}));
}
private void ModifyPWdError(ErrorEntity errorEntity)
{
IsBusy = false;
ResetPWD();
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
ErrorManager.ShowReturnError(errorEntity, Muchinfo.MTPClient.Resources.Client_Resource.Models_ErrorTips);
}));
}
private void ResetPWD()
{
OldFundsPassword = "";
NewFundsPassword = "";
ConfFundsPassword = "";
RaisePropertyChanged(OldFundsPassword);
RaisePropertyChanged(NewFundsPassword);
RaisePropertyChanged(ConfFundsPassword);
// LogManager.WritePassWordModifyLog(UserManager.CurrentTradeAccount, 3);
OldLoginPassword = "";
NewLoginPassword = "";
ConfLoginPassword = "";
RaisePropertyChanged(OldLoginPassword);
RaisePropertyChanged(NewLoginPassword);
RaisePropertyChanged(ConfLoginPassword);
}
#endregion
#region Methods
///
/// 客户端验证登录数据
///
///
private bool ValidData()
{
ErrorMsg = string.Empty;
if (LoginID == uint.MaxValue)
{
ErrorMsg = Muchinfo.MTPClient.Resources.Client_Resource.Models_GetAccount_Failure;
return false;
}
if (string.IsNullOrWhiteSpace(OldLoginPassword))
{
ErrorMsg = Muchinfo.MTPClient.Resources.Client_Resource.Models_NotInputModifyPassword;
return false;
}
#region 验证登录密码
OldLoginPassword = OldLoginPassword.Trim();
if (!string.IsNullOrWhiteSpace(OldLoginPassword))
{
NewLoginPassword = NewLoginPassword.Trim();
ConfLoginPassword = ConfLoginPassword.Trim();
//验证交易密码
if (OldLoginPassword.Length < 6)
{
ErrorMsg = string.Format(Muchinfo.MTPClient.Resources.Client_Resource.Models_PasswordNotless6, Muchinfo.MTPClient.Resources.Client_Resource.Models_TradePassword);
}
else if (NewLoginPassword.Length < 6)
{
ErrorMsg = string.Format(Muchinfo.MTPClient.Resources.Client_Resource.Models_ClearPassword, Muchinfo.MTPClient.Resources.Client_Resource.Models_TradePassword);
}
else if (ConfLoginPassword.Length < 6)
{
ErrorMsg = string.Format(Muchinfo.MTPClient.Resources.Client_Resource.Models_MakesurePasswordNotless6);
}
else if (!NewLoginPassword.Equals(ConfLoginPassword))
{
ErrorMsg = string.Format(Muchinfo.MTPClient.Resources.Client_Resource.Models_MakesurePasswordUnmatchNewPassword);
}
else if (NewLoginPassword.Equals(OldLoginPassword))
{
ErrorMsg = string.Format(Muchinfo.MTPClient.Resources.Client_Resource.Models_NewPasswordUnmatchOriginalPassword);
}
if (!string.IsNullOrWhiteSpace(ErrorMsg))
{
var temp = ErrorMsg;
IsTradePanel = true;
ErrorMsg = temp;
return false;
}
}
#endregion
#region 验证资金密码
OldFundsPassword = OldFundsPassword.Trim();
if (!string.IsNullOrWhiteSpace(OldFundsPassword))
{
NewFundsPassword = NewFundsPassword.Trim();
ConfFundsPassword = ConfFundsPassword.Trim();
//验证交易密码
if (OldFundsPassword.Length < 6)
{
ErrorMsg = string.Format(Muchinfo.MTPClient.Resources.Client_Resource.Models_PasswordNotless6, Muchinfo.MTPClient.Resources.Client_Resource.Models_FundsPassword);
}
else if (NewFundsPassword.Length < 6)
{
ErrorMsg = string.Format(Muchinfo.MTPClient.Resources.Client_Resource.Models_ClearPassword, Muchinfo.MTPClient.Resources.Client_Resource.Models_FundsPassword);
}
else if (ConfFundsPassword.Length < 6)
{
ErrorMsg = string.Format(Muchinfo.MTPClient.Resources.Client_Resource.Models_MakesurePasswordNotless6);
}
else if (!NewFundsPassword.Equals(ConfFundsPassword))
{
ErrorMsg = string.Format(Muchinfo.MTPClient.Resources.Client_Resource.Models_MakesurePasswordUnmatchNewPassword);
}
else if (NewFundsPassword.Equals(OldFundsPassword))
{
ErrorMsg = string.Format(Muchinfo.MTPClient.Resources.Client_Resource.Models_NewPasswordUnmatchOriginalPassword);
}
if (!string.IsNullOrWhiteSpace(ErrorMsg))
{
var temp = ErrorMsg;
IsFundsPanel = true;
ErrorMsg = temp;
return false;
}
}
#endregion
return true;
}
#endregion Methods
}
}