LoginViewModel.cs 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266
  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 = "123456";
  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. var password = EncryptHelper.SHA256(loginId + TradePassword.Trim()).ToLower();
  570. _loginService.TradeAccountLogin((LoginType)CurrentLoginType.Value, Convert.ToUInt64(loginId),
  571. loginId, password, LoginCallBack, LoginErrorFunc);
  572. //LoginCallBack(new TradeAccount() { LoginID = 310020000 });
  573. }), null);
  574. //var password = EncryptHelper.SHA256(TradeAccount.Trim() + TradePassword.Trim()).ToLower();
  575. //_loginService.TradeAccountLogin((LoginType)CurrentLoginType.Value, Convert.ToUInt64(TradeAccount.Trim()),
  576. // TradeAccount.Trim(), password, LoginCallBack, LoginErrorFunc);
  577. }
  578. else
  579. {
  580. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  581. {
  582. ErrorManager.ShowReturnError(ExceptionManager.ConnectTradeServerTimeOut,
  583. Client_Resource.UI2014_LoginTips);
  584. RestStoryboard();
  585. }));
  586. }
  587. });
  588. }
  589. catch (Exception ex)
  590. {
  591. RestStoryboard();
  592. _loginCount++;
  593. if (_loginCount >= c_MaxLoginError) //登录出错超出限制次数
  594. {
  595. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  596. {
  597. ErrorManager.ShowReturnError(ExceptionManager.LoginTimesError,
  598. Client_Resource.UI2014_LoginTips);
  599. WindowHelper.RerunApplication(false);
  600. }));
  601. return;
  602. }
  603. IsEnabled = true;
  604. IsReadOnly = false;
  605. LogHelper.WriteError(typeof(LoginViewModel), ex.ToString());
  606. Application.Current.Dispatcher.BeginInvoke(
  607. new Action(() => MessageBoxHelper.ShowInfo(ex.Message, Client_Resource.UI2014_LoginTips, true)));
  608. }
  609. }, (view) => IsOKCommand);
  610. }
  611. }
  612. /// <summary>
  613. ///
  614. /// </summary>
  615. public RelayCommand<LoginWindowBase> GuestCommand
  616. {
  617. get
  618. {
  619. return new RelayCommand<LoginWindowBase>((view) =>
  620. {
  621. IsEnabled = false;
  622. _view = view;
  623. MessengerHelper.DefaultUnregister<bool>(this,MessengerTokens.QuoteCheckTokenSuccess);
  624. MessengerHelper.DefaultRegister<bool>(this, MessengerTokens.QuoteCheckTokenSuccess, (e) =>
  625. {
  626. IsEnabled = true;
  627. if (e)
  628. {
  629. var dataQuote = SimpleIoc.Default.GetInstance<IQuoteDataService>();
  630. dataQuote.QueryQuoteGoodsInfo(QueryGoodsSuccess, QueryGoodsError);
  631. }
  632. else
  633. {
  634. LinkManager.Instance.Dispose();
  635. LinkManager.Instance.GuestQuoteTcpLinkProxy = null; ///清除链路
  636. Application.Current.Dispatcher.BeginInvoke(
  637. new Action(() => MessageBoxHelper.ShowInfo(Client_Resource.GuestQuoteConnectError, Client_Resource.UI2014_LoginTips, true)));
  638. }
  639. });
  640. ////ShowMainWindow();
  641. LinkManager.Instance.CreateGuestQuoteTcpLink();
  642. });
  643. }
  644. }
  645. /// <summary>
  646. /// 打开注册地址
  647. /// </summary>
  648. public RelayCommand RegAccountCommand
  649. {
  650. get
  651. {
  652. return new RelayCommand(() =>
  653. {
  654. try
  655. {
  656. if (!string.IsNullOrWhiteSpace(ApplicationParameter.RegAccountAddress))
  657. {
  658. IPHelper.OpenWebAddress(ApplicationParameter.RegAccountAddress);
  659. }
  660. }
  661. catch (Exception ex)
  662. {
  663. MessageBoxHelper.ShowInfo(Client_Resource.IsNotSupportOpenBrowner,
  664. Client_Resource.UI2014_Tips, true);
  665. new MsgAlter(ApplicationParameter.RegAccountAddress).ShowDialog();
  666. LogHelper.WriteError(typeof (LoginViewModel), ex.ToString());
  667. }
  668. });
  669. }
  670. }
  671. /// <summary>
  672. /// 是否可游客登录
  673. /// </summary>
  674. public bool IsGuestLogin
  675. {
  676. get
  677. {
  678. ////未配置游客登录行情地址
  679. if (string.IsNullOrWhiteSpace(ApplicationParameter.GuestQuoteAddress))
  680. {
  681. return false;
  682. }
  683. string[] addArrs = ApplicationParameter.GuestQuoteAddress.Split(':');
  684. if (addArrs.Length != 2)
  685. {
  686. return false;
  687. }
  688. //todo:校验IP,端口
  689. return true;
  690. }
  691. }
  692. /// <summary>
  693. /// 是否显示注册连接
  694. /// </summary>
  695. public bool IsRegAccount
  696. {
  697. get
  698. {
  699. return !string.IsNullOrWhiteSpace(ApplicationParameter.RegAccountAddress);
  700. }
  701. }
  702. /// <summary>
  703. /// 登录回调
  704. /// </summary>
  705. /// <param name="loginEntity"></param>
  706. private void LoginCallBack(TradeAccount loginEntity)
  707. {
  708. LogInfoHelper.WriteInfo("登陆成功,AccountId: " + loginEntity.AccountId);
  709. LinkManager.Instance.TradeTcpLinkProxy.StartSendBeat();
  710. // 等待1s,以免行情接入TOKEN检验失败
  711. Thread.Sleep(1000);
  712. //创建行情链路, 链路成功后订阅商品
  713. LinkManager.Instance.CreateQuoteTcpLink();
  714. ApplicationParameter.SetSystemTime(loginEntity.SystemTime);
  715. SetQueryTips(3);
  716. //仅获取更新时间戳
  717. //_loginService.LoginQuerySearch(UserManager.CurrentTradeAccount.LoginID, LoginQueryType.ParamValues, QueryLastUpdateTimeSuccess, QueryError);
  718. //查询所有数据
  719. _loginService.LoginQuerySearch(UserManager.CurrentTradeAccount.LoginID, LoginQueryType.All, QueryLastUpdateTimeSuccess, QueryError);
  720. //todo: 开发期间注释升级
  721. //todo:是否有会员资源升级
  722. var lastLoginUser = UserManager.GetLastLoginUser();
  723. if ((!string.IsNullOrWhiteSpace(ApplicationParameter.UpdateAddress)) && (lastLoginUser == null || string.IsNullOrEmpty(lastLoginUser.MemberId) ||
  724. !lastLoginUser.MemberId.Equals(loginEntity.MemberAreaId + string.Empty))) ////升级会员个性资源
  725. {
  726. KillUpate(); ////
  727. string fileName = string.Format("{0}{1}", AppDomain.CurrentDomain.BaseDirectory,
  728. "Client.Update.exe");
  729. var arguments = string.Format("{0},{1}", ApplicationParameter.UpdateAddress,
  730. lastLoginUser == null ? string.Empty : lastLoginUser.MemberId);
  731. Process.Start(fileName, arguments);
  732. }
  733. }
  734. /// <summary>
  735. /// 查询更新时间列表
  736. /// </summary>
  737. /// <param name="LastUpdateTimeModel">更新时间列表</param>
  738. private void QueryLastUpdateTimeSuccess(AccountBaseInfoModel itemInfo)
  739. {
  740. SetQueryTips(2);
  741. //查询系统基本参数,登录成功第一时间需要获取系统必要的参数来构建UI界面
  742. //如果系统基本参数有更新,就重新调用接口去更新系统参数,否则直接使用本地文件的系统参数
  743. ulong timeUTC = CacheManager.GetLastUpdateTimeBy(Muchinfo.MTPClient.Data.Enums.LastUpdateTimeType.ConfigUTime);// 1498122159671;
  744. if (ConfigParamResxManager.ConfigParamResx.ResourceHashtable == null || ConfigParamResxManager.ConfigParamResx.Version != timeUTC)
  745. {
  746. _loginService.ConfigQuerySearch(timeUTC, QueryConfigInfoSuccess, QueryError);
  747. }
  748. else
  749. {
  750. ConfigInfoModel[] configArr = Newtonsoft.Json.JsonConvert.DeserializeObject<ConfigInfoModel[]>(Newtonsoft.Json.JsonConvert.SerializeObject(ConfigParamResxManager.ConfigParamResx.ResourceHashtable.Values));
  751. //foreach(DictionaryEntry de in ConfigParamResxManager.ConfigParamResx.ResourceHashtable)
  752. //{
  753. // configLst.Add(Newtonsoft.Json.JsonConvert.DeserializeObject<ConfigInfoModel>(de.Value.ToString()));
  754. //}
  755. List<ConfigInfoModel> configLst = configArr.ToList();
  756. QueryConfigInfoSuccess(configLst, 0, false);
  757. SetApplicationParameters(configLst);
  758. }
  759. }
  760. /// <summary>
  761. /// 设置程序参数
  762. /// </summary>
  763. /// <param name="parameterConfigs">查询的参数</param>
  764. private void SetApplicationParameters(List<ConfigInfoModel> parameterConfigs)
  765. {
  766. try
  767. {
  768. if (parameterConfigs != null && parameterConfigs.Count > 0)
  769. {
  770. Type t = typeof(ApplicationParameter);
  771. var properties = t.GetProperties().Where((item) => item.CanWrite);
  772. foreach (var propertyInfo in properties)
  773. {
  774. var parameter =
  775. parameterConfigs.Where(u => !string.IsNullOrEmpty(u.ParamName) && u.ParamName.Trim().ToLower().Equals(propertyInfo.Name.Trim().ToLower())).ToList();
  776. if (parameter != null && parameter.Count > 0)
  777. {
  778. var finacingModel = parameter[0];
  779. if (propertyInfo.PropertyType == typeof(String))
  780. {
  781. propertyInfo.SetValue(t, finacingModel.ParamValue, null);
  782. }
  783. else if (propertyInfo.PropertyType == typeof(Int32))
  784. {
  785. propertyInfo.SetValue(t, Convert.ToInt32(finacingModel.ParamValue), null);
  786. }
  787. else if (propertyInfo.PropertyType == typeof(DateTime))
  788. {
  789. propertyInfo.SetValue(t, Convert.ToDateTime(finacingModel.ParamValue), null);
  790. }
  791. else if (propertyInfo.PropertyType == typeof(Decimal))
  792. {
  793. propertyInfo.SetValue(t, Convert.ToDecimal(finacingModel.ParamValue), null);
  794. }
  795. else if (propertyInfo.PropertyType == typeof(Boolean))
  796. {
  797. propertyInfo.SetValue(t, Convert.ToBoolean(finacingModel.ParamValue), null);
  798. }
  799. }
  800. }
  801. }
  802. }
  803. catch (Exception ex)
  804. {
  805. ////写错误日志
  806. LogHelper.WriteError(typeof(Exception), ex.ToString());
  807. }
  808. }
  809. /// <summary>
  810. /// 查询系统参
  811. /// </summary>
  812. /// <param name="LastUpdateTimeModel">更新时间列表</param>
  813. private void QueryConfigInfoSuccess(List<ConfigInfoModel> itemInfo,ulong configstamp=0,bool bupdate=true)
  814. {
  815. if (bupdate)
  816. {
  817. //如果是调用接口更新的系统参数,需要更新对应的本地文件
  818. Task.Factory.TryStartNew(() =>
  819. {
  820. var hashtable = new Hashtable();
  821. foreach (var config in itemInfo)
  822. {
  823. hashtable.Add(config.ParamCode, config);
  824. }
  825. ConfigParamResxManager.SaveConfigParamResx(hashtable, configstamp, UserManager.UserDataFolder);
  826. });
  827. }
  828. //获取系统参数成功后进入主界面
  829. UserManager.IsAccountLogin = true;
  830. ShowMainWindow();
  831. }
  832. private void QueryError(ErrorEntity error)
  833. {
  834. LogInfoHelper.WriteInfo("登陆成功,系统参数查询失败...");
  835. RestStoryboard();
  836. ////清除链路
  837. LinkManager.Instance.Dispose();
  838. }
  839. //private void SystemClientParamterConfigError(ErrorEntity error)
  840. //{
  841. // LogInfoHelper.WriteInfo("登陆成功,系统参数查询失败...");
  842. // RestStoryboard();
  843. // ////清除链路
  844. // LinkManager.Instance.Dispose();
  845. // var format = "交易端系统参数查询:" + string.Format(ErrorManager.FormatErrorMsg(error));
  846. // LogInfoHelper.WriteInfo(format);
  847. //}
  848. private void RestStoryboard()
  849. {
  850. ProgressText = string.Empty;
  851. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  852. {
  853. IsEnabled = true;
  854. IsReadOnly = false;
  855. //出错播放回复动画
  856. BeginStoryboard("ShowLoginButton");
  857. }));
  858. }
  859. public void FeeRuleCallBack(Dictionary<int, TradeGoodsFeeRule> GoodsFeeRules)
  860. {
  861. ///todo:保存在缓存中
  862. //CacheManager.TradeGoodsFeeRules = GoodsFeeRules.ToDictionary((item) => item.GoodsID);
  863. }
  864. /// <summary>
  865. /// 更新程序是否正在运行
  866. /// </summary>
  867. /// <returns></returns>
  868. private void KillUpate()
  869. {
  870. try
  871. {
  872. var processes = Process.GetProcessesByName("Client.Update.exe");
  873. foreach (var process in processes)
  874. {
  875. process.Kill();
  876. }
  877. }
  878. catch (Exception)
  879. {
  880. }
  881. }
  882. /// <summary>
  883. /// 错误处理
  884. /// </summary>
  885. /// <param name="error">错误内容</param>
  886. private void LoginErrorFunc(ErrorEntity error)
  887. {
  888. ProgressText = string.Empty;
  889. IsEnabled = true;
  890. IsReadOnly = false;
  891. ////清除链路
  892. LinkManager.Instance.Dispose();
  893. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  894. {
  895. ErrorManager.ShowReturnError(error, Client_Resource.UI2014_Tips, true);
  896. IsEnabled = true;
  897. IsReadOnly = false;
  898. //出错播放回复动画
  899. BeginStoryboard("ShowLoginButton");
  900. }));
  901. IdentifyingCodeImg = BitmapFrame.Create(vc.CreateCheckCodeImage());
  902. RaisePropertyChanged(() => IdentifyingCode);
  903. if (ApplicationParameter.IsShowIdentifyingCode == ((int)eShowIdentifyingCode.ErrorShow).ToString())
  904. {
  905. IsVisibilityIdentifyingCode = Visibility.Visible;
  906. }
  907. }
  908. #endregion
  909. #region 私有方法
  910. private void HiddenCotrl()
  911. {
  912. if (string.IsNullOrWhiteSpace(ApplicationParameter.GuestQuoteAddress) && string.IsNullOrWhiteSpace(ApplicationParameter.RegAccountAddress))
  913. {
  914. MarginValue = "85,0,0,0";
  915. IsVisibilityControlHidden = Visibility.Collapsed;
  916. ColumnSpan3 = 3;
  917. }
  918. }
  919. /// <summary>
  920. /// 播放LoginNewView里Storyboard
  921. /// </summary>
  922. /// <param name="storyboardName">Name of the storyboard.</param>
  923. private void BeginStoryboard(string storyboardName)
  924. {
  925. if (_view == null) return;
  926. var storyboard = (Storyboard)_view.FindResource(storyboardName);
  927. if (storyboard != null) storyboard.Begin(_view);
  928. }
  929. /// <summary>
  930. /// 初始化列表
  931. /// </summary>
  932. private void InitDataAccount()
  933. {
  934. //获取记住账号内容
  935. try
  936. {
  937. lastLoginUser = UserManager.GetLastLoginUser();
  938. List<UserInfo> loginUserInfo = UserManager.GetTradeAccounts();
  939. if (loginUserInfo != null && loginUserInfo.Count > 0)
  940. {
  941. var list = loginUserInfo.FindAll(k => k.UserName != "" && k.RememberAccount);
  942. if (list != null)
  943. {
  944. LoginAccountList = list; //.Where(z => z.RememberAccount).ToList();
  945. }
  946. if (LoginAccountList != null && LoginAccountList.Count > 0)
  947. {
  948. TradeAccount = LoginAccountList[0].UserName;
  949. IsSaveAccount = LoginAccountList[0].RememberAccount;
  950. CurrentLoginType =
  951. LoginTypeList.FirstOrDefault(k => k.Value == (int)LoginAccountList[0].LoginType);
  952. return;
  953. }
  954. }
  955. CurrentLoginType = LoginTypeList.FirstOrDefault(k => k.Value == 0);
  956. }
  957. catch (Exception exception)
  958. {
  959. LogHelper.WriteError(typeof(LoginViewModel), exception.ToString());
  960. }
  961. }
  962. /// <summary>
  963. /// 登录信息验证
  964. /// </summary>
  965. /// <returns><c>true</c> if XXXX, <c>false</c> otherwise</returns>
  966. private bool ValidData()
  967. {
  968. if (string.IsNullOrWhiteSpace(TradeAccount.Trim()) || TradeAccount.Length < 3)
  969. {
  970. MessageBoxHelper.ShowInfo(Client_Resource.UI2014_AccountNotLessThan3Characters, Client_Resource.UI2014_Tips, true);
  971. //IsVisibilityIdentifyingCode = Visibility.Visible;
  972. return false;
  973. }
  974. if (string.IsNullOrWhiteSpace(TradePassword) || TradePassword.Length < 6)
  975. {
  976. MessageBoxHelper.ShowInfo(Client_Resource.UI2014_AccountNotLessThan6Characters, Client_Resource.UI2014_Tips, true);
  977. //IsVisibilityIdentifyingCode = Visibility.Visible;
  978. return false;
  979. }
  980. if ((ApplicationParameter.IsShowIdentifyingCode == ((int)eShowIdentifyingCode.Show).ToString() || ApplicationParameter.IsShowIdentifyingCode == ((int)eShowIdentifyingCode.ErrorShow).ToString()) && IsVisibilityIdentifyingCode == Visibility.Visible && string.IsNullOrWhiteSpace(IdentifyingCode))
  981. {
  982. //请输入验证码
  983. MessageBoxHelper.ShowInfo(Client_Resource.LoginViewModel_Cs_EnterVerificationCode, Client_Resource.UI2014_Tips, true);
  984. RaisePropertyChanged(() => IdentifyingCode);
  985. return false;
  986. }
  987. if ((ApplicationParameter.IsShowIdentifyingCode == ((int)eShowIdentifyingCode.Show).ToString() || ApplicationParameter.IsShowIdentifyingCode == ((int)eShowIdentifyingCode.ErrorShow).ToString()) && IsVisibilityIdentifyingCode == Visibility.Visible && string.Compare(IdentifyingCode, vc.CheckCode, true) != 0)
  988. {
  989. //验证码错误
  990. MessageBoxHelper.ShowInfo(Client_Resource.LoginViewModel_Cs_VerificationCodeError, Client_Resource.UI2014_Tips, true);
  991. IdentifyingCodeImg = BitmapFrame.Create(vc.CreateCheckCodeImage());
  992. RaisePropertyChanged(() => IdentifyingCode);
  993. return false;
  994. }
  995. return true;
  996. }
  997. private void ShowWarningTips()
  998. {
  999. bool isShowWarnTips = false;
  1000. //0 —不显示(即登录后不需要阅读风险告知书)1 —每次显示(每次登录后都弹出)2 —每天显示(每天登录多次仅第一次显示)3 —显示一次(登录后弹出一次,用户阅读后便不再显示)
  1001. var tipsType = (RateTipsType) ApplicationParameter.IsShowRateTips;
  1002. var info = UserManager.GetTradeAccount(TradeAccount.Trim());
  1003. if (info == null)
  1004. {
  1005. info = new UserInfo() { UserName = TradeAccount.Trim() };
  1006. }
  1007. switch (tipsType)
  1008. {
  1009. case RateTipsType.NotRate:
  1010. isShowWarnTips = false;
  1011. break;
  1012. case RateTipsType.EveryTimes:
  1013. info.IsShowWarnTips = true;
  1014. isShowWarnTips = true;
  1015. break;
  1016. case RateTipsType.ShowEveryDay:
  1017. isShowWarnTips = info.LoginTime.Date.CompareTo(DateTime.Now.Date) < 0 ||
  1018. (info.LoginTime.Date.CompareTo(DateTime.Now.Date) == 0 && !info.IsShowWarnTips);
  1019. break;
  1020. case RateTipsType.ShowOneTimes:
  1021. isShowWarnTips = !info.IsShowWarnTips;
  1022. break;
  1023. }
  1024. ////是否显示风险提示信息
  1025. if ( isShowWarnTips)
  1026. {
  1027. var rateNotices = new RateNotices();
  1028. var dialogResult = rateNotices.ShowDialog();
  1029. if (dialogResult == false)
  1030. {
  1031. WindowHelper.RerunApplication(false);
  1032. }
  1033. else
  1034. {
  1035. var noticesViewModel = rateNotices.DataContext as RateNoticeViewModel;
  1036. if (noticesViewModel != null)
  1037. {
  1038. info.IsShowWarnTips = noticesViewModel.IsNotice;
  1039. }
  1040. }
  1041. }
  1042. UserManager.LoginedSave(info, IsSaveAccount);
  1043. }
  1044. /// <summary>
  1045. /// 查询参数配置
  1046. /// </summary>
  1047. /// <param name="configs">参数列表</param>
  1048. //private void QueryConfigSuccess(List<ParameterConfig> configs)
  1049. //{
  1050. // //todo:查询系统参数成功
  1051. // UserManager.IsAccountLogin = true;
  1052. // ShowMainWindow();
  1053. //}
  1054. /// <summary>
  1055. /// 显示主窗口
  1056. /// </summary>
  1057. private void ShowMainWindow()
  1058. {
  1059. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  1060. {
  1061. var loginView = _view;
  1062. loginView.Hide();
  1063. ShowWarningTips();
  1064. var homePage = ViewModelLocator.MainPage;
  1065. Application.Current.MainWindow = homePage;
  1066. homePage.Show();
  1067. loginView.Close();
  1068. }));
  1069. }
  1070. private void SetQueryTips(int type)
  1071. {
  1072. var sBuilder = new StringBuilder();
  1073. sBuilder.Append(Client_Resource.Query_Title_String);
  1074. switch (type)
  1075. {
  1076. case 0:
  1077. sBuilder.Append(Client_Resource.Query_Base_info);
  1078. sBuilder.Append("、");
  1079. sBuilder.Append(Client_Resource.Query_system_info);
  1080. break;
  1081. case 1: //提示基本查询未完成
  1082. sBuilder.Append(Client_Resource.Query_Base_info);
  1083. break;;
  1084. case 2: //提示参数查询未完成
  1085. break;
  1086. case 3: //提示参数查询未完成
  1087. // sBuilder.Append("查询时间戳");
  1088. // sBuilder.Append("交易规则和费用");
  1089. sBuilder.Append(Client_Resource.Resources_Service_QueryAccountGoodsParams);
  1090. break;
  1091. default:
  1092. break;
  1093. }
  1094. ProgressText = sBuilder.ToString();
  1095. }
  1096. #region 行情连接
  1097. /// <summary>
  1098. /// 查询行情商品成功回应
  1099. /// </summary>
  1100. /// <param name="quoteGoodses"></param>
  1101. private void QueryGoodsSuccess(List<QuoteGoods> quoteGoodses )
  1102. {
  1103. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  1104. {
  1105. var loginView = _view;
  1106. loginView.Hide();
  1107. // ShowWarmingTips();
  1108. var homePage = ViewModelLocator.MainPage;
  1109. Application.Current.MainWindow = homePage;
  1110. ViewModelLocator.Home.InitializeThemes();
  1111. homePage.Show();
  1112. loginView.Close();
  1113. }));
  1114. ////查询商品成功订阅行情
  1115. QuoteProxyHelper.QuoteSubscribe(GoodsFromScr.Brown);
  1116. }
  1117. private void QueryGoodsError(ErrorEntity errorEntity)
  1118. {
  1119. LinkManager.Instance.GuestQuoteTcpLinkProxy.Dispose();
  1120. Application.Current.Dispatcher.BeginInvoke(new Action(() =>
  1121. {
  1122. ErrorManager.ShowReturnError(errorEntity, Client_Resource.UI2014_Tips, true);
  1123. IsEnabled = true;
  1124. IsReadOnly = false;
  1125. }));
  1126. }
  1127. #endregion
  1128. #endregion
  1129. }
  1130. }