LoginViewModel.cs 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252
  1. using System.Security.AccessControl;
  2. using System.Text;
  3. using System.Threading;
  4. using System.Windows.Input;
  5. using GalaSoft.MvvmLight;
  6. using GalaSoft.MvvmLight.Command;
  7. using GalaSoft.MvvmLight.Ioc;
  8. using Muchinfo.MTPClient.Account.Views;
  9. using Muchinfo.MTPClient.CustomException;
  10. using Muchinfo.MTPClient.Data;
  11. using Muchinfo.MTPClient.Data.Enums;
  12. using Muchinfo.MTPClient.Data.Helper;
  13. using Muchinfo.MTPClient.Data.Model;
  14. using Muchinfo.MTPClient.Data.Model.Account;
  15. using Muchinfo.MTPClient.Data.Model.GoodRules;
  16. using Muchinfo.MTPClient.Infrastructure.Cache;
  17. using Muchinfo.MTPClient.Infrastructure.Helpers;
  18. using Muchinfo.MTPClient.Infrastructure.LinkProxy;
  19. using Muchinfo.MTPClient.Infrastructure.MessageBox;
  20. using Muchinfo.MTPClient.Infrastructure.Users;
  21. using Muchinfo.MTPClient.Infrastructure.Utilities;
  22. using Muchinfo.MTPClient.IService;
  23. using Muchinfo.MTPClient.Resources;
  24. using Muchinfo.MTPClient.Service.Utilities;
  25. using Muchinfo.MTPClient.UI.Utilities;
  26. using Muchinfo.MTPClient.UI.Views;
  27. using Muchinfo.PC.Common.Extensions;
  28. using Muchinfo.PC.Common.Helpers;
  29. using Muchinfo.WPF.Controls.Keyboard;
  30. using System;
  31. using System.Collections.Generic;
  32. using System.Diagnostics;
  33. using System.IO;
  34. using System.Linq;
  35. using System.Threading.Tasks;
  36. using System.Windows;
  37. using System.Windows.Controls;
  38. using System.Windows.Media;
  39. using System.Windows.Media.Animation;
  40. using System.Windows.Media.Imaging;
  41. using System.Collections;
  42. namespace Muchinfo.MTPClient.UI.ViewModels
  43. {
  44. public class LoginWindowBase : Window
  45. {
  46. public LoginWindowBase() { }
  47. }
  48. /// <summary>
  49. /// 用户登录
  50. /// </summary>
  51. public class LoginViewModel : ViewModelBase
  52. {
  53. #region Fields
  54. private const int c_MaxLoginError = 6;
  55. private const int c_QueryCount = 2; //登录总共需查询数量
  56. private int _loginCount = 0;
  57. //最近一次登录会员
  58. private LastLoginUser lastLoginUser;
  59. private ISystemService _iSystemService;
  60. private IDBTableMsgService _IDBTableMsgService;
  61. private int _queryComplete ; ///查询完成数量
  62. public static object _lockObject = new object();
  63. private static AutoResetEvent _mutex = new AutoResetEvent(false);
  64. public ILoginService _loginService { get; set; }
  65. private LoginWindowBase _view;
  66. private ValidCode vc;//实例4位字符加数字的验证码
  67. private string _systemName;
  68. private string _progressText;
  69. private bool _connectResult; ////连接是否成功
  70. #endregion
  71. #region 绑定属性
  72. /// <summary>
  73. /// 客户端版本
  74. /// </summary>
  75. public string Version
  76. {
  77. get
  78. {
  79. return "v"+ ApplicationParameter.Version;
  80. }
  81. }
  82. #region 隐藏控件当指定条件
  83. private Visibility _isVisibilityControlHidden = Visibility.Visible;
  84. /// <summary>
  85. /// 隐藏控件当指定条件
  86. /// </summary>
  87. public Visibility IsVisibilityControlHidden
  88. {
  89. get
  90. {
  91. return _isVisibilityControlHidden;
  92. }
  93. set
  94. {
  95. Set(() => IsVisibilityControlHidden, ref _isVisibilityControlHidden, value);
  96. }
  97. }
  98. #endregion
  99. #region 变更集合列数
  100. private decimal _columnSpan3 = 1;
  101. /// <summary>
  102. /// 变更集合列数
  103. /// </summary>
  104. public decimal ColumnSpan3
  105. {
  106. get
  107. {
  108. return _columnSpan3;
  109. }
  110. set
  111. {
  112. Set(() => ColumnSpan3, ref _columnSpan3, value);
  113. }
  114. }
  115. #endregion
  116. #region 验证码容器偏移量
  117. private string _marginValue = "35,0,0,0";
  118. /// <summary>
  119. /// 验证码容器偏移量
  120. /// </summary>
  121. public string MarginValue
  122. {
  123. get
  124. {
  125. return _marginValue;
  126. }
  127. set
  128. {
  129. Set(() => MarginValue, ref _marginValue, value);
  130. }
  131. }
  132. #endregion
  133. #region 是否显示验证码
  134. private Visibility _isVisibilityIdentifyingCode = Visibility.Collapsed;
  135. /// <summary>
  136. /// 是否显示验证码
  137. /// </summary>
  138. public Visibility IsVisibilityIdentifyingCode
  139. {
  140. get
  141. {
  142. return _isVisibilityIdentifyingCode;
  143. }
  144. set
  145. {
  146. Set(() => IsVisibilityIdentifyingCode, ref _isVisibilityIdentifyingCode, value);
  147. }
  148. }
  149. #endregion
  150. /// <summary>
  151. /// 登录方式集合
  152. /// </summary>
  153. private List<DropDataTemplate> _loginTypeList;
  154. public List<DropDataTemplate> LoginTypeList
  155. {
  156. get { return _loginTypeList; }
  157. set
  158. {
  159. Set(() => LoginTypeList, ref _loginTypeList, value);
  160. }
  161. }
  162. /// <summary>
  163. /// 登录方式
  164. /// </summary>
  165. private DropDataTemplate _currentLoginType;
  166. public DropDataTemplate CurrentLoginType
  167. {
  168. get { return _currentLoginType; }
  169. set
  170. {
  171. Set(() => CurrentLoginType, ref _currentLoginType, value);
  172. if (CurrentLoginType.Value == 0)
  173. {
  174. TradeLoginVisibility = Visibility.Visible;
  175. PhoneLoginVisibility = Visibility.Collapsed;
  176. }
  177. else if (CurrentLoginType.Value == 1)
  178. {
  179. TradeLoginVisibility = Visibility.Collapsed;
  180. PhoneLoginVisibility = Visibility.Visible;
  181. }
  182. }
  183. }
  184. /// <summary>
  185. /// 交易登录是否显示,隐藏
  186. /// </summary>
  187. private Visibility _tradeLoginVisibility;
  188. public Visibility TradeLoginVisibility
  189. {
  190. get
  191. {
  192. return _tradeLoginVisibility;
  193. }
  194. set
  195. {
  196. Set(() => TradeLoginVisibility, ref _tradeLoginVisibility, value);
  197. }
  198. }
  199. /// <summary>
  200. /// 手机登录是否显示,隐藏
  201. /// </summary>
  202. private Visibility _phoneLoginVisibility;
  203. public Visibility PhoneLoginVisibility
  204. {
  205. get
  206. {
  207. return _phoneLoginVisibility;
  208. }
  209. set
  210. {
  211. Set(() => PhoneLoginVisibility, ref _phoneLoginVisibility, value);
  212. }
  213. }
  214. private List<UserInfo> _loginAccountList = new List<UserInfo>();
  215. /// <summary>
  216. /// 记住账号列表
  217. /// </summary>
  218. public List<UserInfo> LoginAccountList
  219. {
  220. get { return _loginAccountList; }
  221. set
  222. {
  223. Set(() => LoginAccountList, ref _loginAccountList, value);
  224. }
  225. }
  226. private string _tradeAccount = string.Empty;
  227. /// <summary>
  228. /// 交易账号
  229. /// </summary>
  230. public string TradeAccount
  231. {
  232. get
  233. {
  234. return _tradeAccount;
  235. }
  236. set
  237. {
  238. if (_tradeAccount == value) return;
  239. _tradeAccount = value;
  240. RaisePropertyChanged(() => TradeAccount);
  241. TradePassword = string.Empty;
  242. #if DEBUG
  243. TradePassword = "123456";
  244. #endif
  245. if (LoginAccountList == null) return;
  246. var frirstUser = LoginAccountList.FirstOrDefault(user => _tradeAccount.Equals(user.UserName));
  247. if (frirstUser != null)
  248. {
  249. IsSaveAccount = frirstUser.RememberAccount;
  250. }
  251. else
  252. {
  253. IsSaveAccount = true;
  254. }
  255. }
  256. }
  257. private string _tradePassword = string.Empty;
  258. /// <summary>
  259. /// 交易密码
  260. /// </summary>
  261. public string TradePassword
  262. {
  263. get
  264. {
  265. return _tradePassword;
  266. }
  267. set
  268. {
  269. Set(() => TradePassword, ref _tradePassword, value);
  270. }
  271. }
  272. private bool _isSaveAccount = true;
  273. /// <summary>
  274. /// 是否保存账号
  275. /// </summary>
  276. public bool IsSaveAccount
  277. {
  278. get { return _isSaveAccount; }
  279. set
  280. {
  281. Set(() => IsSaveAccount, ref _isSaveAccount, value);
  282. }
  283. }
  284. private double _processRate = 1;
  285. /// <summary>
  286. /// 处理进度
  287. /// </summary>
  288. public double ProcessRate
  289. {
  290. set
  291. {
  292. Set(() => ProcessRate, ref _processRate, value);
  293. }
  294. get
  295. {
  296. return _processRate;
  297. }
  298. }
  299. private bool _isEnabled = true;
  300. /// <summary>
  301. /// 用于控制登录按钮是否可用
  302. /// </summary>
  303. public bool IsEnabled
  304. {
  305. get { return _isEnabled; }
  306. set
  307. {
  308. Set(() => IsEnabled, ref _isEnabled, value);
  309. }
  310. }
  311. private bool _isMainEnabled = false;
  312. /// <summary>
  313. /// 是否完成加载数据
  314. /// </summary>
  315. public bool IsMainEnabled
  316. {
  317. get { return _isMainEnabled; }
  318. set { Set(() => IsMainEnabled, ref _isMainEnabled, value); }
  319. }
  320. private bool _isReadOnly = false;
  321. /// <summary>
  322. /// 用于控制文本框只读
  323. /// </summary>
  324. public bool IsReadOnly
  325. {
  326. get { return _isReadOnly; }
  327. set
  328. {
  329. Set(() => IsReadOnly, ref _isReadOnly, value);
  330. }
  331. }
  332. private bool _isOKCommand = true;
  333. /// <summary>
  334. /// 是否可登录操作
  335. /// </summary>
  336. public bool IsOKCommand
  337. {
  338. get { return _isOKCommand; }
  339. set { _isOKCommand = value; }
  340. }
  341. public string _imagePath;
  342. public string ImagePath
  343. {
  344. get
  345. {
  346. return _imagePath;
  347. }
  348. set
  349. {
  350. _imagePath = value;
  351. RaisePropertyChanged(() => ImageSource);
  352. }
  353. }
  354. public BitmapImage ImageSource
  355. {
  356. get
  357. {
  358. if (File.Exists(ImagePath))
  359. {
  360. return new BitmapImage(new Uri(ImagePath));
  361. }
  362. else
  363. {
  364. return null;
  365. }
  366. }
  367. }
  368. private ImageSource _identifyingCodeImg;
  369. public ImageSource IdentifyingCodeImg
  370. {
  371. get
  372. {
  373. return _identifyingCodeImg;
  374. }
  375. set
  376. {
  377. Set(() => IdentifyingCodeImg, ref _identifyingCodeImg, value);
  378. }
  379. }
  380. private string _identifyingCode = string.Empty;
  381. public string IdentifyingCode
  382. {
  383. get
  384. {
  385. if (_identifyingCode.Length == 4 && string.Compare(_identifyingCode, vc.CheckCode, true) == 0)
  386. {
  387. IdentifyingCodeRightOrErrorImg = new BitmapImage(new Uri("../../Images/RightIcon.png", UriKind.Relative));
  388. }
  389. else
  390. {
  391. IdentifyingCodeRightOrErrorImg = new BitmapImage(new Uri("../../Images/ErrorIcon.png", UriKind.Relative));
  392. }
  393. return _identifyingCode;
  394. }
  395. set
  396. {
  397. Set(() => IdentifyingCode, ref _identifyingCode, value);
  398. }
  399. }
  400. #region 验证码实时验证对错图标
  401. //private BitmapImage RightIcon = new BitmapImage(new Uri("../../Images/RightIcon.png", UriKind.Relative));
  402. //private BitmapImage ErrorIcon = new BitmapImage(new Uri("../../Images/ErrorIcon.png", UriKind.Relative));
  403. private ImageSource _identifyingCodeRightOrErrorImg = new BitmapImage(new Uri("../../Images/ErrorIcon.png", UriKind.Relative));
  404. public ImageSource IdentifyingCodeRightOrErrorImg
  405. {
  406. get
  407. {
  408. return _identifyingCodeRightOrErrorImg;
  409. }
  410. set
  411. {
  412. Set(() => IdentifyingCodeRightOrErrorImg, ref _identifyingCodeRightOrErrorImg, value);
  413. }
  414. }
  415. #endregion
  416. /// <summary>
  417. /// 交易系统名称
  418. /// </summary>
  419. /// <value>The name of the system.</value>
  420. public string SystemName
  421. {
  422. get
  423. {
  424. return _systemName;
  425. }
  426. set
  427. {
  428. Set(() => SystemName, ref _systemName, value);
  429. }
  430. }
  431. /// <summary>
  432. /// 进度条的名称
  433. /// </summary>
  434. public string ProgressText
  435. {
  436. get
  437. {
  438. return _progressText;
  439. }
  440. set
  441. {
  442. Set(() => ProgressText, ref _progressText, value);
  443. }
  444. }
  445. public Brush _progressTextForeground;
  446. /// <summary>
  447. /// 进度条错误提示
  448. /// </summary>
  449. public Brush ProgressTextForeground
  450. {
  451. get
  452. {
  453. return _progressTextForeground;
  454. }
  455. set
  456. {
  457. Set(() => ProgressTextForeground, ref _progressTextForeground, value);
  458. }
  459. }
  460. #endregion
  461. private int QueryComplete
  462. {
  463. get { return _queryComplete; }
  464. set
  465. {
  466. lock (_lockObject)
  467. {
  468. _queryComplete = value;
  469. }
  470. }
  471. }
  472. #region 构造函数
  473. /// <summary>
  474. /// Initializes a new instance of the <see cref="LoginViewModel"/> class.
  475. /// </summary>
  476. public LoginViewModel()
  477. {
  478. _loginService = SimpleIoc.Default.GetInstance<ILoginService>();
  479. _IDBTableMsgService = SimpleIoc.Default.GetInstance<IDBTableMsgService>();
  480. _iSystemService = SimpleIoc.Default.GetInstance<ISystemService>();
  481. LoginTypeList = EnumHelper.EnumToDictionary(typeof (LoginType));
  482. CurrentLoginType = LoginTypeList.FirstOrDefault(k => k.Value == 0);
  483. SystemName = ApplicationParameter.TradeSystemName;
  484. MessengerHelper.DefaultRegister<bool>(this, MessengerTokens.TradeCreateConnect, ((result) =>
  485. {
  486. _connectResult = result;
  487. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  488. {
  489. _mutex.Set();// IsEnabled = true;
  490. }));
  491. }));
  492. ////实例4位字符加数字的验证码
  493. vc = new ValidCode(4, ValidCode.CodeType.Words);
  494. Task.Factory.TryStartNew(() => InitDataAccount(), () => IsMainEnabled = true);
  495. IdentifyingCodeImg = BitmapFrame.Create(vc.CreateCheckCodeImage());
  496. HiddenCotrl();
  497. if (ApplicationParameter.IsShowIdentifyingCode == ((int)eShowIdentifyingCode.Show).ToString())
  498. {
  499. IsVisibilityIdentifyingCode = Visibility.Visible;
  500. }
  501. }
  502. #endregion
  503. #region 绑定命令
  504. /// <summary>
  505. /// 更新验证码IsSaveAccount
  506. /// </summary>
  507. public RelayCommand IdentifyingCodeCommand
  508. {
  509. get
  510. {
  511. return new RelayCommand(() =>
  512. {
  513. IdentifyingCodeImg = BitmapFrame.Create(vc.CreateCheckCodeImage());
  514. RaisePropertyChanged(() => IdentifyingCode);
  515. });
  516. }
  517. }
  518. /// <summary>
  519. /// 打开键盘命令
  520. /// </summary>
  521. public RelayCommand<Control> OpenKeyboardCommand
  522. {
  523. get
  524. {
  525. return new RelayCommand<Control>((control) =>
  526. {
  527. PassWordKeyBoard.SetTouchScreenKeyboard(control, true);
  528. control.Focus();
  529. });
  530. }
  531. }
  532. public RelayCommand<LoginWindowBase> LoginNewCommand
  533. {
  534. get
  535. {
  536. return new RelayCommand<LoginWindowBase>(
  537. (v) =>
  538. {
  539. //验证用户输入基本信息
  540. if (!ValidData()) return;
  541. ProcessRate = 1;
  542. QueryComplete = c_QueryCount; //需查询数量
  543. try
  544. {
  545. //============================
  546. if (IsEnabled == false) return;
  547. IsEnabled = false;
  548. IsReadOnly = true;
  549. ProgressTextForeground = Application.Current.FindResource("MuchinfoBrush124") as Brush;
  550. ProgressText = Client_Resource.Login_Progress_Logining;
  551. _view = v;
  552. //播放加载动画
  553. BeginStoryboard("ShowProgress");
  554. ProgressText = Client_Resource.Login_Progress_Title;
  555. //创建交易链路
  556. LinkManager.Instance.CreateTradeLink();
  557. Task.Factory.TryStartNew(() =>
  558. {
  559. _mutex.WaitOne();
  560. if (_connectResult)
  561. {
  562. LogInfoHelper.WriteInfo("登陆开始...账号:" + TradeAccount.Trim());
  563. var password = EncryptHelper.SHA256(TradeAccount.Trim() + TradePassword.Trim()).ToLower();
  564. _loginService.TradeAccountLogin((LoginType)CurrentLoginType.Value, Convert.ToUInt64(TradeAccount.Trim()),
  565. TradeAccount.Trim(), password, LoginCallBack, LoginErrorFunc);
  566. //LoginCallBack(new TradeAccount() { LoginID = 310020000 });
  567. }
  568. else
  569. {
  570. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  571. {
  572. ErrorManager.ShowReturnError(ExceptionManager.ConnectTradeServerTimeOut,
  573. Client_Resource.UI2014_LoginTips);
  574. RestStoryboard();
  575. }));
  576. }
  577. });
  578. }
  579. catch (Exception ex)
  580. {
  581. RestStoryboard();
  582. _loginCount++;
  583. if (_loginCount >= c_MaxLoginError) //登录出错超出限制次数
  584. {
  585. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  586. {
  587. ErrorManager.ShowReturnError(ExceptionManager.LoginTimesError,
  588. Client_Resource.UI2014_LoginTips);
  589. WindowHelper.RerunApplication(false);
  590. }));
  591. return;
  592. }
  593. IsEnabled = true;
  594. IsReadOnly = false;
  595. LogHelper.WriteError(typeof(LoginViewModel), ex.ToString());
  596. Application.Current.Dispatcher.BeginInvoke(
  597. new Action(() => MessageBoxHelper.ShowInfo(ex.Message, Client_Resource.UI2014_LoginTips, true)));
  598. }
  599. }, (view) => IsOKCommand);
  600. }
  601. }
  602. /// <summary>
  603. ///
  604. /// </summary>
  605. public RelayCommand<LoginWindowBase> GuestCommand
  606. {
  607. get
  608. {
  609. return new RelayCommand<LoginWindowBase>((view) =>
  610. {
  611. IsEnabled = false;
  612. _view = view;
  613. MessengerHelper.DefaultUnregister<bool>(this,MessengerTokens.QuoteCheckTokenSuccess);
  614. MessengerHelper.DefaultRegister<bool>(this, MessengerTokens.QuoteCheckTokenSuccess, (e) =>
  615. {
  616. IsEnabled = true;
  617. if (e)
  618. {
  619. var dataQuote = SimpleIoc.Default.GetInstance<IQuoteDataService>();
  620. dataQuote.QueryQuoteGoodsInfo(QueryGoodsSuccess, QueryGoodsError);
  621. }
  622. else
  623. {
  624. LinkManager.Instance.Dispose();
  625. LinkManager.Instance.GuestQuoteTcpLinkProxy = null; ///清除链路
  626. Application.Current.Dispatcher.BeginInvoke(
  627. new Action(() => MessageBoxHelper.ShowInfo(Client_Resource.GuestQuoteConnectError, Client_Resource.UI2014_LoginTips, true)));
  628. }
  629. });
  630. ////ShowMainWindow();
  631. LinkManager.Instance.CreateGuestQuoteTcpLink();
  632. });
  633. }
  634. }
  635. /// <summary>
  636. /// 打开注册地址
  637. /// </summary>
  638. public RelayCommand RegAccountCommand
  639. {
  640. get
  641. {
  642. return new RelayCommand(() =>
  643. {
  644. try
  645. {
  646. if (!string.IsNullOrWhiteSpace(ApplicationParameter.RegAccountAddress))
  647. {
  648. IPHelper.OpenWebAddress(ApplicationParameter.RegAccountAddress);
  649. }
  650. }
  651. catch (Exception ex)
  652. {
  653. MessageBoxHelper.ShowInfo(Client_Resource.IsNotSupportOpenBrowner,
  654. Client_Resource.UI2014_Tips, true);
  655. new MsgAlter(ApplicationParameter.RegAccountAddress).ShowDialog();
  656. LogHelper.WriteError(typeof (LoginViewModel), ex.ToString());
  657. }
  658. });
  659. }
  660. }
  661. /// <summary>
  662. /// 是否可游客登录
  663. /// </summary>
  664. public bool IsGuestLogin
  665. {
  666. get
  667. {
  668. ////未配置游客登录行情地址
  669. if (string.IsNullOrWhiteSpace(ApplicationParameter.GuestQuoteAddress))
  670. {
  671. return false;
  672. }
  673. string[] addArrs = ApplicationParameter.GuestQuoteAddress.Split(':');
  674. if (addArrs.Length != 2)
  675. {
  676. return false;
  677. }
  678. //todo:校验IP,端口
  679. return true;
  680. }
  681. }
  682. /// <summary>
  683. /// 是否显示注册连接
  684. /// </summary>
  685. public bool IsRegAccount
  686. {
  687. get
  688. {
  689. return !string.IsNullOrWhiteSpace(ApplicationParameter.RegAccountAddress);
  690. }
  691. }
  692. /// <summary>
  693. /// 登录回调
  694. /// </summary>
  695. /// <param name="loginEntity"></param>
  696. private void LoginCallBack(TradeAccount loginEntity)
  697. {
  698. LogInfoHelper.WriteInfo("登陆成功,AccountId: " + loginEntity.AccountId);
  699. LinkManager.Instance.TradeTcpLinkProxy.StartSendBeat();
  700. // 等待1s,以免行情接入TOKEN检验失败
  701. Thread.Sleep(1000);
  702. //创建行情链路, 链路成功后订阅商品
  703. LinkManager.Instance.CreateQuoteTcpLink();
  704. ApplicationParameter.SetSystemTime(loginEntity.SystemTime);
  705. SetQueryTips(3);
  706. //仅获取更新时间戳
  707. //_loginService.LoginQuerySearch(UserManager.CurrentTradeAccount.LoginID, LoginQueryType.ParamValues, QueryLastUpdateTimeSuccess, QueryError);
  708. //查询所有数据
  709. _loginService.LoginQuerySearch(UserManager.CurrentTradeAccount.LoginID, LoginQueryType.All, QueryLastUpdateTimeSuccess, QueryError);
  710. //todo: 开发期间注释升级
  711. //todo:是否有会员资源升级
  712. var lastLoginUser = UserManager.GetLastLoginUser();
  713. if ((!string.IsNullOrWhiteSpace(ApplicationParameter.UpdateAddress)) && (lastLoginUser == null || string.IsNullOrEmpty(lastLoginUser.MemberId) ||
  714. !lastLoginUser.MemberId.Equals(loginEntity.MemberAreaId + string.Empty))) ////升级会员个性资源
  715. {
  716. KillUpate(); ////
  717. string fileName = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory,
  718. "Client.Update.exe");
  719. var arguments = string.Format("{0},{1}", ApplicationParameter.UpdateAddress,
  720. lastLoginUser == null ? string.Empty : lastLoginUser.MemberId);
  721. Process.Start(fileName, arguments);
  722. }
  723. }
  724. /// <summary>
  725. /// 查询更新时间列表
  726. /// </summary>
  727. /// <param name="LastUpdateTimeModel">更新时间列表</param>
  728. private void QueryLastUpdateTimeSuccess(AccountBaseInfoModel itemInfo)
  729. {
  730. SetQueryTips(2);
  731. //查询系统基本参数,登录成功第一时间需要获取系统必要的参数来构建UI界面
  732. //如果系统基本参数有更新,就重新调用接口去更新系统参数,否则直接使用本地文件的系统参数
  733. ulong timeUTC = CacheManager.GetLastUpdateTimeBy(Muchinfo.MTPClient.Data.Enums.LastUpdateTimeType.ConfigUTime);// 1498122159671;
  734. if (ConfigParamResxManager.ConfigParamResx.ResourceHashtable == null || ConfigParamResxManager.ConfigParamResx.Version != timeUTC)
  735. {
  736. _loginService.ConfigQuerySearch(timeUTC, QueryConfigInfoSuccess, QueryError);
  737. }
  738. else
  739. {
  740. ConfigInfoModel[] configArr = Newtonsoft.Json.JsonConvert.DeserializeObject<ConfigInfoModel[]>(Newtonsoft.Json.JsonConvert.SerializeObject(ConfigParamResxManager.ConfigParamResx.ResourceHashtable.Values));
  741. //foreach(DictionaryEntry de in ConfigParamResxManager.ConfigParamResx.ResourceHashtable)
  742. //{
  743. // configLst.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<ConfigInfoModel>(de.Value.ToString()));
  744. //}
  745. List<ConfigInfoModel> configLst = configArr.ToList();
  746. QueryConfigInfoSuccess(configLst, 0, false);
  747. SetApplicationParameters(configLst);
  748. }
  749. }
  750. /// <summary>
  751. /// 设置程序参数
  752. /// </summary>
  753. /// <param name="parameterConfigs">查询的参数</param>
  754. private void SetApplicationParameters(List<ConfigInfoModel> parameterConfigs)
  755. {
  756. try
  757. {
  758. if (parameterConfigs != null && parameterConfigs.Count > 0)
  759. {
  760. Type t = typeof(ApplicationParameter);
  761. var properties = t.GetProperties().Where((item) => item.CanWrite);
  762. foreach (var propertyInfo in properties)
  763. {
  764. var parameter =
  765. parameterConfigs.Where(u => !string.IsNullOrEmpty(u.ParamName) && u.ParamName.Trim().ToLower().Equals(propertyInfo.Name.Trim().ToLower())).ToList();
  766. if (parameter != null && parameter.Count > 0)
  767. {
  768. var finacingModel = parameter[0];
  769. if (propertyInfo.PropertyType == typeof(String))
  770. {
  771. propertyInfo.SetValue(t, finacingModel.ParamValue, null);
  772. }
  773. else if (propertyInfo.PropertyType == typeof(Int32))
  774. {
  775. propertyInfo.SetValue(t, Convert.ToInt32(finacingModel.ParamValue), null);
  776. }
  777. else if (propertyInfo.PropertyType == typeof(DateTime))
  778. {
  779. propertyInfo.SetValue(t, Convert.ToDateTime(finacingModel.ParamValue), null);
  780. }
  781. else if (propertyInfo.PropertyType == typeof(Decimal))
  782. {
  783. propertyInfo.SetValue(t, Convert.ToDecimal(finacingModel.ParamValue), null);
  784. }
  785. else if (propertyInfo.PropertyType == typeof(Boolean))
  786. {
  787. propertyInfo.SetValue(t, Convert.ToBoolean(finacingModel.ParamValue), null);
  788. }
  789. }
  790. }
  791. }
  792. }
  793. catch (Exception ex)
  794. {
  795. ////写错误日志
  796. LogHelper.WriteError(typeof(Exception), ex.ToString());
  797. }
  798. }
  799. /// <summary>
  800. /// 查询系统参
  801. /// </summary>
  802. /// <param name="LastUpdateTimeModel">更新时间列表</param>
  803. private void QueryConfigInfoSuccess(List<ConfigInfoModel> itemInfo,ulong configstamp=0,bool bupdate=true)
  804. {
  805. if (bupdate)
  806. {
  807. //如果是调用接口更新的系统参数,需要更新对应的本地文件
  808. Task.Factory.TryStartNew(() =>
  809. {
  810. var hashtable = new Hashtable();
  811. foreach (var config in itemInfo)
  812. {
  813. hashtable.Add(config.ParamCode, config);
  814. }
  815. ConfigParamResxManager.SaveConfigParamResx(hashtable, configstamp, UserManager.UserDataFolder);
  816. });
  817. }
  818. //获取系统参数成功后进入主界面
  819. UserManager.IsAccountLogin = true;
  820. ShowMainWindow();
  821. }
  822. private void QueryError(ErrorEntity error)
  823. {
  824. LogInfoHelper.WriteInfo("登陆成功,系统参数查询失败...");
  825. RestStoryboard();
  826. ////清除链路
  827. LinkManager.Instance.Dispose();
  828. }
  829. //private void SystemClientParamterConfigError(ErrorEntity error)
  830. //{
  831. // LogInfoHelper.WriteInfo("登陆成功,系统参数查询失败...");
  832. // RestStoryboard();
  833. // ////清除链路
  834. // LinkManager.Instance.Dispose();
  835. // var format = "交易端系统参数查询:" + string.Format(ErrorManager.FormatErrorMsg(error));
  836. // LogInfoHelper.WriteInfo(format);
  837. //}
  838. private void RestStoryboard()
  839. {
  840. ProgressText = string.Empty;
  841. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  842. {
  843. IsEnabled = true;
  844. IsReadOnly = false;
  845. //出错播放回复动画
  846. BeginStoryboard("ShowLoginButton");
  847. }));
  848. }
  849. public void FeeRuleCallBack(Dictionary<int, TradeGoodsFeeRule> GoodsFeeRules)
  850. {
  851. ///todo:保存在缓存中
  852. //CacheManager.TradeGoodsFeeRules = GoodsFeeRules.ToDictionary((item) => item.GoodsID);
  853. }
  854. /// <summary>
  855. /// 更新程序是否正在运行
  856. /// </summary>
  857. /// <returns></returns>
  858. private void KillUpate()
  859. {
  860. try
  861. {
  862. var processes = Process.GetProcessesByName("Client.Update.exe");
  863. foreach (var process in processes)
  864. {
  865. process.Kill();
  866. }
  867. }
  868. catch (Exception)
  869. {
  870. }
  871. }
  872. /// <summary>
  873. /// 错误处理
  874. /// </summary>
  875. /// <param name="error">错误内容</param>
  876. private void LoginErrorFunc(ErrorEntity error)
  877. {
  878. ProgressText = string.Empty;
  879. IsEnabled = true;
  880. IsReadOnly = false;
  881. ////清除链路
  882. LinkManager.Instance.Dispose();
  883. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  884. {
  885. ErrorManager.ShowReturnError(error, Client_Resource.UI2014_Tips, true);
  886. IsEnabled = true;
  887. IsReadOnly = false;
  888. //出错播放回复动画
  889. BeginStoryboard("ShowLoginButton");
  890. }));
  891. IdentifyingCodeImg = BitmapFrame.Create(vc.CreateCheckCodeImage());
  892. RaisePropertyChanged(() => IdentifyingCode);
  893. if (ApplicationParameter.IsShowIdentifyingCode == ((int)eShowIdentifyingCode.ErrorShow).ToString())
  894. {
  895. IsVisibilityIdentifyingCode = Visibility.Visible;
  896. }
  897. }
  898. #endregion
  899. #region 私有方法
  900. private void HiddenCotrl()
  901. {
  902. if (string.IsNullOrWhiteSpace(ApplicationParameter.GuestQuoteAddress) && string.IsNullOrWhiteSpace(ApplicationParameter.RegAccountAddress))
  903. {
  904. MarginValue = "85,0,0,0";
  905. IsVisibilityControlHidden = Visibility.Collapsed;
  906. ColumnSpan3 = 3;
  907. }
  908. }
  909. /// <summary>
  910. /// 播放LoginNewView里Storyboard
  911. /// </summary>
  912. /// <param name="storyboardName">Name of the storyboard.</param>
  913. private void BeginStoryboard(string storyboardName)
  914. {
  915. if (_view == null) return;
  916. var storyboard = (Storyboard)_view.FindResource(storyboardName);
  917. if (storyboard != null) storyboard.Begin(_view);
  918. }
  919. /// <summary>
  920. /// 初始化列表
  921. /// </summary>
  922. private void InitDataAccount()
  923. {
  924. //获取记住账号内容
  925. try
  926. {
  927. lastLoginUser = UserManager.GetLastLoginUser();
  928. List<UserInfo> loginUserInfo = UserManager.GetTradeAccounts();
  929. if (loginUserInfo != null && loginUserInfo.Count > 0)
  930. {
  931. var list = loginUserInfo.FindAll(k => k.UserName != "" && k.RememberAccount);
  932. if (list != null)
  933. {
  934. LoginAccountList = list; //.Where(z => z.RememberAccount).ToList();
  935. }
  936. if (LoginAccountList != null && LoginAccountList.Count > 0)
  937. {
  938. TradeAccount = LoginAccountList[0].UserName;
  939. IsSaveAccount = LoginAccountList[0].RememberAccount;
  940. CurrentLoginType =
  941. LoginTypeList.FirstOrDefault(k => k.Value == (int)LoginAccountList[0].LoginType);
  942. return;
  943. }
  944. }
  945. CurrentLoginType = LoginTypeList.FirstOrDefault(k => k.Value == 0);
  946. }
  947. catch (Exception exception)
  948. {
  949. LogHelper.WriteError(typeof(LoginViewModel), exception.ToString());
  950. }
  951. }
  952. /// <summary>
  953. /// 登录信息验证
  954. /// </summary>
  955. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  956. private bool ValidData()
  957. {
  958. if (string.IsNullOrWhiteSpace(TradeAccount.Trim()) || TradeAccount.Length < 3)
  959. {
  960. MessageBoxHelper.ShowInfo(Client_Resource.UI2014_AccountNotLessThan3Characters, Client_Resource.UI2014_Tips, true);
  961. //IsVisibilityIdentifyingCode = Visibility.Visible;
  962. return false;
  963. }
  964. if (string.IsNullOrWhiteSpace(TradePassword) || TradePassword.Length < 6)
  965. {
  966. MessageBoxHelper.ShowInfo(Client_Resource.UI2014_AccountNotLessThan6Characters, Client_Resource.UI2014_Tips, true);
  967. //IsVisibilityIdentifyingCode = Visibility.Visible;
  968. return false;
  969. }
  970. if ((ApplicationParameter.IsShowIdentifyingCode == ((int)eShowIdentifyingCode.Show).ToString() || ApplicationParameter.IsShowIdentifyingCode == ((int)eShowIdentifyingCode.ErrorShow).ToString()) && IsVisibilityIdentifyingCode == Visibility.Visible && string.IsNullOrWhiteSpace(IdentifyingCode))
  971. {
  972. //请输入验证码
  973. MessageBoxHelper.ShowInfo(Client_Resource.LoginViewModel_Cs_EnterVerificationCode, Client_Resource.UI2014_Tips, true);
  974. RaisePropertyChanged(() => IdentifyingCode);
  975. return false;
  976. }
  977. if ((ApplicationParameter.IsShowIdentifyingCode == ((int)eShowIdentifyingCode.Show).ToString() || ApplicationParameter.IsShowIdentifyingCode == ((int)eShowIdentifyingCode.ErrorShow).ToString()) && IsVisibilityIdentifyingCode == Visibility.Visible && string.Compare(IdentifyingCode, vc.CheckCode, true) != 0)
  978. {
  979. //验证码错误
  980. MessageBoxHelper.ShowInfo(Client_Resource.LoginViewModel_Cs_VerificationCodeError, Client_Resource.UI2014_Tips, true);
  981. IdentifyingCodeImg = BitmapFrame.Create(vc.CreateCheckCodeImage());
  982. RaisePropertyChanged(() => IdentifyingCode);
  983. return false;
  984. }
  985. return true;
  986. }
  987. private void ShowWarningTips()
  988. {
  989. bool isShowWarnTips = false;
  990. //0 —不显示(即登录后不需要阅读风险告知书)1 —每次显示(每次登录后都弹出)2 —每天显示(每天登录多次仅第一次显示)3 —显示一次(登录后弹出一次,用户阅读后便不再显示)
  991. var tipsType = (RateTipsType) ApplicationParameter.IsShowRateTips;
  992. var info = UserManager.GetTradeAccount(TradeAccount.Trim());
  993. if (info == null)
  994. {
  995. info = new UserInfo() { UserName = TradeAccount.Trim() };
  996. }
  997. switch (tipsType)
  998. {
  999. case RateTipsType.NotRate:
  1000. isShowWarnTips = false;
  1001. break;
  1002. case RateTipsType.EveryTimes:
  1003. info.IsShowWarnTips = true;
  1004. isShowWarnTips = true;
  1005. break;
  1006. case RateTipsType.ShowEveryDay:
  1007. isShowWarnTips = info.LoginTime.Date.CompareTo(DateTime.Now.Date) < 0 ||
  1008. (info.LoginTime.Date.CompareTo(DateTime.Now.Date) == 0 && !info.IsShowWarnTips);
  1009. break;
  1010. case RateTipsType.ShowOneTimes:
  1011. isShowWarnTips = !info.IsShowWarnTips;
  1012. break;
  1013. }
  1014. ////是否显示风险提示信息
  1015. if ( isShowWarnTips)
  1016. {
  1017. var rateNotices = new RateNotices();
  1018. var dialogResult = rateNotices.ShowDialog();
  1019. if (dialogResult == false)
  1020. {
  1021. WindowHelper.RerunApplication(false);
  1022. }
  1023. else
  1024. {
  1025. var noticesViewModel = rateNotices.DataContext as RateNoticeViewModel;
  1026. if (noticesViewModel != null)
  1027. {
  1028. info.IsShowWarnTips = noticesViewModel.IsNotice;
  1029. }
  1030. }
  1031. }
  1032. UserManager.LoginedSave(info, IsSaveAccount);
  1033. }
  1034. /// <summary>
  1035. /// 查询参数配置
  1036. /// </summary>
  1037. /// <param name="configs">参数列表</param>
  1038. //private void QueryConfigSuccess(List<ParameterConfig> configs)
  1039. //{
  1040. // //todo:查询系统参数成功
  1041. // UserManager.IsAccountLogin = true;
  1042. // ShowMainWindow();
  1043. //}
  1044. /// <summary>
  1045. /// 显示主窗口
  1046. /// </summary>
  1047. private void ShowMainWindow()
  1048. {
  1049. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  1050. {
  1051. var loginView = _view;
  1052. loginView.Hide();
  1053. ShowWarningTips();
  1054. var homePage = ViewModelLocator.MainPage;
  1055. Application.Current.MainWindow = homePage;
  1056. homePage.Show();
  1057. loginView.Close();
  1058. }));
  1059. }
  1060. private void SetQueryTips(int type)
  1061. {
  1062. var sBuilder = new StringBuilder();
  1063. sBuilder.Append(Client_Resource.Query_Title_String);
  1064. switch (type)
  1065. {
  1066. case 0:
  1067. sBuilder.Append(Client_Resource.Query_Base_info);
  1068. sBuilder.Append("、");
  1069. sBuilder.Append(Client_Resource.Query_system_info);
  1070. break;
  1071. case 1: //提示基本查询未完成
  1072. sBuilder.Append(Client_Resource.Query_Base_info);
  1073. break;;
  1074. case 2: //提示参数查询未完成
  1075. break;
  1076. case 3: //提示参数查询未完成
  1077. // sBuilder.Append("查询时间戳");
  1078. // sBuilder.Append("交易规则和费用");
  1079. sBuilder.Append(Client_Resource.Resources_Service_QueryAccountGoodsParams);
  1080. break;
  1081. default:
  1082. break;
  1083. }
  1084. ProgressText = sBuilder.ToString();
  1085. }
  1086. #region 行情连接
  1087. /// <summary>
  1088. /// 查询行情商品成功回应
  1089. /// </summary>
  1090. /// <param name="quoteGoodses"></param>
  1091. private void QueryGoodsSuccess(List<QuoteGoods> quoteGoodses )
  1092. {
  1093. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  1094. {
  1095. var loginView = _view;
  1096. loginView.Hide();
  1097. // ShowWarmingTips();
  1098. var homePage = ViewModelLocator.MainPage;
  1099. Application.Current.MainWindow = homePage;
  1100. ViewModelLocator.Home.InitializeThemes();
  1101. homePage.Show();
  1102. loginView.Close();
  1103. }));
  1104. ////查询商品成功订阅行情
  1105. QuoteProxyHelper.QuoteSubscribe(GoodsFromScr.Brown);
  1106. }
  1107. private void QueryGoodsError(ErrorEntity errorEntity)
  1108. {
  1109. LinkManager.Instance.GuestQuoteTcpLinkProxy.Dispose();
  1110. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  1111. {
  1112. ErrorManager.ShowReturnError(errorEntity, Client_Resource.UI2014_Tips, true);
  1113. IsEnabled = true;
  1114. IsReadOnly = false;
  1115. }));
  1116. }
  1117. #endregion
  1118. #endregion
  1119. }
  1120. }