You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

183 lines
5.6 KiB

  1. using BPA.Helper;
  2. using HandyControl.Data;
  3. using HKCardIN.Helper;
  4. using HKCardIN.Logic;
  5. using HKCardIN.Logic.Model;
  6. using HKCardIN.Views;
  7. using HKLog;
  8. using Stylet;
  9. using System;
  10. using System.Text.RegularExpressions;
  11. using System.Threading;
  12. using System.Threading.Tasks;
  13. using System.Windows;
  14. using UHFHelper;
  15. namespace HKCardIN.ViewModels
  16. {
  17. public class RootViewModel : Conductor<IScreen>
  18. {
  19. bool IsLocker = false;
  20. public RootViewModel()
  21. {
  22. CodeVisible = Visibility.Collapsed;
  23. ContentVisible = Visibility.Visible;
  24. }
  25. #region 属性
  26. decimal _ShowMoney;
  27. public decimal ShowMoney
  28. {
  29. get => _ShowMoney;
  30. set => SetAndNotify(ref _ShowMoney, value);
  31. }
  32. Visibility _ContentVisible;
  33. public Visibility ContentVisible
  34. {
  35. get => _ContentVisible;
  36. set => SetAndNotify(ref _ContentVisible, value);
  37. }
  38. Visibility _CodeVisible;
  39. public Visibility CodeVisible
  40. {
  41. get => _CodeVisible;
  42. set => SetAndNotify(ref _CodeVisible, value);
  43. }
  44. string _InputMoney;
  45. public string InputMoney
  46. {
  47. get => _InputMoney;
  48. set
  49. {
  50. SetAndNotify(ref _InputMoney, value);
  51. decimal.TryParse(value, out decimal data);
  52. ShowMoney = data;
  53. }
  54. }
  55. string _CardNo;
  56. public string CardNo
  57. {
  58. get => _CardNo;
  59. set => SetAndNotify(ref _CardNo, value);
  60. }
  61. UserAndCardInfo _Info;
  62. public UserAndCardInfo Info
  63. {
  64. get => _Info;
  65. set => SetAndNotify(ref _Info, value);
  66. }
  67. #endregion
  68. #region 命令
  69. public void ReadAction()
  70. {
  71. if (!UHFCardHelper.GetInstance().ComOpen)
  72. {
  73. var res = UHFCardHelper.GetInstance().OpenPort();
  74. HKLogImport.WriteInfo(res.ResMes);
  75. }
  76. var RCardNo = UHFCardHelper.GetInstance().ReadCard();
  77. if (!Regex.IsMatch(RCardNo, "\\d{19}"))
  78. {
  79. var res = HandyControl.Controls.MessageBox.Show(new MessageBoxInfo
  80. {
  81. Button = MessageBoxButton.YesNo,
  82. IconKey = "InfoGeometry",
  83. IconBrushKey = "InfoBrush",
  84. YesContent = "是",
  85. NoContent = "否",
  86. Message = "此卡是新卡,前往制卡",
  87. Caption = "提示!"
  88. });
  89. if (res == MessageBoxResult.Yes)
  90. {
  91. CardView view = new CardView();
  92. if (view.ShowDialog().Value) HandyControl.Controls.Growl.InfoGlobal("制卡成功");
  93. else HandyControl.Controls.Growl.InfoGlobal("制卡失败");
  94. }
  95. Info = BaseLogic.GetInstance().PullUserAndCardInfo(CardNo);
  96. UHFCardHelper.GetInstance().ClosePort();
  97. }
  98. else
  99. {
  100. CardNo = RCardNo;
  101. Info = BaseLogic.GetInstance().PullUserAndCardInfo(CardNo);
  102. UHFCardHelper.GetInstance().ClosePort();
  103. }
  104. }
  105. public void InputAction(string input)
  106. {
  107. if (IsLocker) return;
  108. ShowMoney = decimal.Parse(input);
  109. }
  110. public void LockSreenAction()
  111. {
  112. IsLocker = true;
  113. ContentVisible = Visibility.Collapsed;
  114. CodeVisible = Visibility.Visible;
  115. }
  116. public void UnLockAction(HandyControl.Controls.PasswordBox input)
  117. {
  118. if (input.Password.Equals(DataBus.LockCode))
  119. {
  120. IsLocker = false;
  121. CodeVisible = Visibility.Collapsed;
  122. ContentVisible = Visibility.Visible;
  123. }
  124. }
  125. public void SaveAction()
  126. {
  127. if (!DataBus.NetWordState)
  128. {
  129. HandyControl.Controls.Growl.InfoGlobal("系统已离线,请连接网络!!!");
  130. return;
  131. }
  132. if (BaseLogic.GetInstance().PushMoneyToServer(CardNo, ShowMoney))
  133. {
  134. HandyControl.Controls.Growl.SuccessGlobal($"【{CardNo}】充值成功,充值金额【{ShowMoney}】");
  135. Task.Run(() =>
  136. {
  137. Thread.Sleep(5 * 1000);
  138. Info = null;
  139. });
  140. }
  141. else
  142. HandyControl.Controls.Growl.InfoGlobal($"【{CardNo}】充值失败");
  143. }
  144. #endregion
  145. #region 方法
  146. protected override void OnViewLoaded()
  147. {
  148. MainThread();
  149. }
  150. private void MainThread()
  151. {
  152. ThreadManage.GetInstance().StartLong(new Action(() =>
  153. {
  154. try
  155. {
  156. //1.检测网络上下线
  157. bool network = HKHelper.GetInstance().GetNetworkState();
  158. if (network != DataBus.NetWordState)
  159. {
  160. if (network) HandyControl.Controls.Growl.InfoGlobal("网络连接成功");
  161. else HandyControl.Controls.Growl.InfoGlobal("系统已离线,请连接网络!!!");
  162. DataBus.NetWordState = network;
  163. }
  164. Thread.Sleep(3000);
  165. }
  166. catch (Exception ex)
  167. {
  168. HandyControl.Controls.Growl.InfoGlobal(ex.Message);
  169. }
  170. }), "循环状态监测线程", false);
  171. }
  172. #endregion
  173. }
  174. }