SMPay.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. using Newtonsoft.Json.Linq;
  2. using System;
  3. using System.Windows.Forms;
  4. using HttpServices;
  5. using Ini;
  6. using System.Drawing;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace UppPay.Forms
  10. {
  11. public partial class SMPay : Form
  12. {
  13. public JObject InjObject { get; set; }
  14. public JObject inParamsobj { get; set; }
  15. public JObject OutjObject { get; set; }
  16. public string authcode { get; set; }
  17. IniFile ini = new IniFile();
  18. private Form loadingForm;
  19. private bool isButtonClose = false;
  20. public SMPay()
  21. {
  22. InitializeComponent();
  23. authcodeBox.Text = "";
  24. OutjObject = new JObject();
  25. authcode = "";
  26. // 设置窗体在屏幕上居中显示
  27. this.StartPosition = FormStartPosition.CenterScreen;
  28. // 设置窗体始终显示在其他窗口的最前面
  29. this.TopMost = true;
  30. authcodeBox.Focus();
  31. // 为 TextBox 的 KeyDown 事件添加处理程序
  32. authcodeBox.KeyDown += authcodeBox_KeyDown;
  33. // 处理窗体关闭事件,阻止关闭操作
  34. this.FormClosing += MainForm_FormClosing;
  35. authcodeBox.Focus();
  36. }
  37. private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
  38. {
  39. if (!isButtonClose)
  40. {
  41. OutjObject = new JObject();
  42. OutjObject["errorCode"] = "-1";
  43. OutjObject["errorMessage"] = "收费员已取消操作";
  44. //this.DialogResult = DialogResult.OK;
  45. //this.Close();
  46. }
  47. }
  48. private void authcodeBox_KeyDown(object sender, KeyEventArgs e)
  49. {
  50. // 检查是否按下回车键
  51. if (e.KeyCode == Keys.Enter)
  52. {
  53. // 处理回车键按下事件
  54. SMPayLoading(authcodeBox.Text);
  55. // 阻止回车键的默认行为,避免在多行文本框中换行
  56. e.SuppressKeyPress = true;
  57. }
  58. }
  59. private void button2_Click(object sender, EventArgs e)
  60. {
  61. SMPayLoading(authcodeBox.Text);
  62. if (OutjObject["errorCode"].ToString() == "0")
  63. {
  64. isButtonClose = true;
  65. this.DialogResult = DialogResult.OK;
  66. this.Close();
  67. }
  68. else {
  69. MessageBox.Show(this, OutjObject["errorMessage"].ToString(), "注意错误提示!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  70. authcodeBox.Focus();
  71. }
  72. }
  73. private async void SMPayLoading(string authcode)
  74. {
  75. // 显示加载遮罩层
  76. //ShowLoadingForm();
  77. //SMPayHelper(authcodeBox.Text);
  78. //StopLoading();
  79. DisableControls(this);
  80. try
  81. {
  82. Tlabel.Text = "正在支付。。。。。。请勿操作!!!请耐心等待!!";
  83. await Task.Run(() => SMPayHelper(authcode));
  84. }
  85. catch (Exception ex)
  86. {
  87. // 处理异常
  88. MessageBox.Show($"发生错误: {ex.Message}", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  89. }
  90. finally
  91. {
  92. // 启用主窗体上的所有控件
  93. EnableControls(this);
  94. }
  95. if (OutjObject["errorCode"].ToString() == "0")
  96. {
  97. isButtonClose = true;
  98. this.DialogResult = DialogResult.OK;
  99. this.Close();
  100. }
  101. else
  102. {
  103. Tlabel.Text = OutjObject["errorMessage"].ToString();
  104. MessageBox.Show(this, OutjObject["errorMessage"].ToString(), "注意错误提示!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
  105. authcodeBox.Focus();
  106. }
  107. }
  108. public async Task<string> SMPayHelper(string authcode)
  109. {
  110. //System.Diagnostics.Debugger.Launch();
  111. string errorCode = "-1";
  112. string errorMessage = "";
  113. string readData = "";
  114. try
  115. {
  116. if (string.IsNullOrWhiteSpace(authcode))
  117. {
  118. Tlabel.Text = "请输入支付码内容....";
  119. }
  120. InjObject["authcode"] = authcode;
  121. //{ "totalFee":"0.01","authcode":"134734650042765256","hisTradeNo":"1","body":"1111","businessType":"WXSM","hospitalCode":"44030001","userCode":"xys","patID":"000000001", "patName":"namwe", "idCardNo":"idCardNo", "mobile":""}
  122. string PayCode = ini.IniReadValue("SMPay", "PayCode");
  123. // 创建一个 JObject 对象
  124. JObject hisParamsobj = new JObject();
  125. // 创建一个 JArray 对象
  126. JArray paramsArray = new JArray();
  127. paramsArray.Add(InjObject);
  128. hisParamsobj["params"] = paramsArray;
  129. hisParamsobj["code"] = PayCode;
  130. hisParamsobj["session"] = inParamsobj["session"];
  131. //调用his进行数据保存
  132. RestClient restClient = new RestClient();
  133. string result = await restClient.HISHTTPServices(hisParamsobj.ToString()); //调用HIS回调
  134. OutjObject = JObject.Parse(result);
  135. return OutjObject.ToString();
  136. }
  137. catch (Exception ex2)
  138. {
  139. errorCode = "-1";
  140. errorMessage = ex2.Message;
  141. }
  142. OutjObject["errorCode"] = errorCode;
  143. OutjObject["errorMessage"] = errorMessage;
  144. OutjObject["posData"] = readData;
  145. return OutjObject.ToString();
  146. }
  147. private void ShowLoadingForm()
  148. {
  149. DisableControls(this);
  150. loadingForm = new Form();
  151. loadingForm.FormBorderStyle = FormBorderStyle.None;
  152. loadingForm.BackColor = Color.Black;
  153. loadingForm.Opacity = 0.5;
  154. loadingForm.Size = this.ClientSize;
  155. loadingForm.StartPosition = FormStartPosition.Manual;
  156. loadingForm.Location = this.PointToScreen(Point.Empty);
  157. loadingForm.TopMost = true;
  158. /*
  159. // 创建加载提示标签
  160. Label label = new Label
  161. {
  162. Text = "正在交易,请稍候...",
  163. //ForeColor = Color.Red, // 设置提示文字为红色
  164. Font = new Font("Arial", 24),
  165. AutoSize = true
  166. };
  167. // 将标签添加到遮罩层
  168. loadingForm.Controls.Add(label);
  169. // 计算标签的居中位置
  170. Size labelSize = label.PreferredSize;
  171. label.Location = new Point((loadingForm.Width - labelSize.Width) / 2, (loadingForm.Height - labelSize.Height) / 2);
  172. */
  173. loadingForm.Show();
  174. }
  175. private void DisableControls(Control container)
  176. {
  177. foreach (Control control in container.Controls)
  178. {
  179. control.Enabled = false;
  180. if (control.HasChildren)
  181. {
  182. DisableControls(control);
  183. }
  184. }
  185. }
  186. private void StopLoading()
  187. {
  188. if (loadingForm != null)
  189. {
  190. loadingForm.Close();
  191. loadingForm.Dispose();
  192. loadingForm = null;
  193. }
  194. // 启用主窗体上的所有控件
  195. EnableControls(this);
  196. }
  197. private void EnableControls(Control container)
  198. {
  199. foreach (Control control in container.Controls)
  200. {
  201. control.Enabled = true;
  202. if (control.HasChildren)
  203. {
  204. EnableControls(control);
  205. }
  206. }
  207. }
  208. }
  209. }