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 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); } } } } }