| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217 |
- using Newtonsoft.Json.Linq;
- using System;
- using System.Windows.Forms;
- using HttpServices;
- using Ini;
- using System.Drawing;
- using System.Threading;
- using System.Threading.Tasks;
- namespace UppPay.Forms
- {
- public partial class SMPay : Form
- {
- public JObject InjObject { get; set; }
- public JObject inParamsobj { get; set; }
- public JObject OutjObject { get; set; }
- public string authcode { get; set; }
- IniFile ini = new IniFile();
- private Form loadingForm;
- private bool isButtonClose = false;
- public SMPay()
- {
- InitializeComponent();
- authcodeBox.Text = "";
- OutjObject = new JObject();
- authcode = "";
- // 设置窗体在屏幕上居中显示
- this.StartPosition = FormStartPosition.CenterScreen;
- // 设置窗体始终显示在其他窗口的最前面
- this.TopMost = true;
- authcodeBox.Focus();
- // 为 TextBox 的 KeyDown 事件添加处理程序
- authcodeBox.KeyDown += authcodeBox_KeyDown;
- // 处理窗体关闭事件,阻止关闭操作
- this.FormClosing += MainForm_FormClosing;
- authcodeBox.Focus();
- }
- private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- if (!isButtonClose)
- {
- OutjObject = new JObject();
- OutjObject["errorCode"] = "-1";
- OutjObject["errorMessage"] = "收费员已取消操作";
- //this.DialogResult = DialogResult.OK;
- //this.Close();
- }
- }
- private void authcodeBox_KeyDown(object sender, KeyEventArgs e)
- {
- // 检查是否按下回车键
- if (e.KeyCode == Keys.Enter)
- {
- // 处理回车键按下事件
- SMPayLoading(authcodeBox.Text);
- // 阻止回车键的默认行为,避免在多行文本框中换行
- e.SuppressKeyPress = true;
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- SMPayLoading(authcodeBox.Text);
- if (OutjObject["errorCode"].ToString() == "0")
- {
- isButtonClose = true;
- this.DialogResult = DialogResult.OK;
- this.Close();
- }
- else {
- MessageBox.Show(this, OutjObject["errorMessage"].ToString(), "注意错误提示!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
- authcodeBox.Focus();
- }
- }
- private async void SMPayLoading(string authcode)
- {
- // 显示加载遮罩层
- //ShowLoadingForm();
- //SMPayHelper(authcodeBox.Text);
- //StopLoading();
- DisableControls(this);
- try
- {
- Tlabel.Text = "正在支付。。。。。。请勿操作!!!请耐心等待!!";
- await Task.Run(() => SMPayHelper(authcode));
- }
- catch (Exception ex)
- {
- // 处理异常
- MessageBox.Show($"发生错误: {ex.Message}", "错误提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- finally
- {
- // 启用主窗体上的所有控件
- EnableControls(this);
- }
- if (OutjObject["errorCode"].ToString() == "0")
- {
- isButtonClose = true;
- this.DialogResult = DialogResult.OK;
- this.Close();
- }
- else
- {
- Tlabel.Text = OutjObject["errorMessage"].ToString();
- MessageBox.Show(this, OutjObject["errorMessage"].ToString(), "注意错误提示!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
- authcodeBox.Focus();
- }
- }
- public async Task<string> SMPayHelper(string authcode)
- {
- //System.Diagnostics.Debugger.Launch();
-
- string errorCode = "-1";
- string errorMessage = "";
- string readData = "";
- try
- {
- if (string.IsNullOrWhiteSpace(authcode))
- {
- Tlabel.Text = "请输入支付码内容....";
- }
- InjObject["authcode"] = authcode;
- //{ "totalFee":"0.01","authcode":"134734650042765256","hisTradeNo":"1","body":"1111","businessType":"WXSM","hospitalCode":"44030001","userCode":"xys","patID":"000000001", "patName":"namwe", "idCardNo":"idCardNo", "mobile":""}
- string PayCode = ini.IniReadValue("SMPay", "PayCode");
- // 创建一个 JObject 对象
- JObject hisParamsobj = new JObject();
- // 创建一个 JArray 对象
- JArray paramsArray = new JArray();
- paramsArray.Add(InjObject);
- hisParamsobj["params"] = paramsArray;
- hisParamsobj["code"] = PayCode;
- hisParamsobj["session"] = inParamsobj["session"];
- //调用his进行数据保存
- RestClient restClient = new RestClient();
- string result = await restClient.HISHTTPServices(hisParamsobj.ToString()); //调用HIS回调
- OutjObject = JObject.Parse(result);
- return OutjObject.ToString();
- }
- catch (Exception ex2)
- {
- errorCode = "-1";
- errorMessage = ex2.Message;
- }
-
- OutjObject["errorCode"] = errorCode;
- OutjObject["errorMessage"] = errorMessage;
- OutjObject["posData"] = readData;
- return OutjObject.ToString();
- }
- private void ShowLoadingForm()
- {
- DisableControls(this);
- loadingForm = new Form();
- loadingForm.FormBorderStyle = FormBorderStyle.None;
- loadingForm.BackColor = Color.Black;
- loadingForm.Opacity = 0.5;
- loadingForm.Size = this.ClientSize;
- loadingForm.StartPosition = FormStartPosition.Manual;
- loadingForm.Location = this.PointToScreen(Point.Empty);
- loadingForm.TopMost = true;
- /*
- // 创建加载提示标签
- Label label = new Label
- {
- Text = "正在交易,请稍候...",
- //ForeColor = Color.Red, // 设置提示文字为红色
- Font = new Font("Arial", 24),
- AutoSize = true
- };
- // 将标签添加到遮罩层
- loadingForm.Controls.Add(label);
- // 计算标签的居中位置
- Size labelSize = label.PreferredSize;
- label.Location = new Point((loadingForm.Width - labelSize.Width) / 2, (loadingForm.Height - labelSize.Height) / 2);
- */
- loadingForm.Show();
- }
- private void DisableControls(Control container)
- {
- foreach (Control control in container.Controls)
- {
- control.Enabled = false;
- if (control.HasChildren)
- {
- DisableControls(control);
- }
- }
- }
- private void StopLoading()
- {
- if (loadingForm != null)
- {
- loadingForm.Close();
- loadingForm.Dispose();
- loadingForm = null;
- }
- // 启用主窗体上的所有控件
- EnableControls(this);
- }
-
- private void EnableControls(Control container)
- {
- foreach (Control control in container.Controls)
- {
- control.Enabled = true;
- if (control.HasChildren)
- {
- EnableControls(control);
- }
- }
- }
- }
- }
|