LoginViewModel.cs 44 KB

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