| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- using System.IO.Ports;
- using System.Threading;
- using Newtonsoft.Json.Linq;
- using Ini;
- using FileInfoClass;
- namespace DGPosPay.Window
- {
- public partial class WaitWindow : Form
- {
- FileClass fileclass = new FileClass();
- Thread th;
- IniFile ini = new IniFile();
- public string label3data = "";
- public int countDownTime = 60;
- public string posData = "";
- public string outstring = "";
- private SerialPort serialPort = new SerialPort();
- private System.Timers.Timer closeTimer;
- private bool isCountingDown = false;
- int closeDownTime = 60;
- public WaitWindow(SerialPort SerialPort)
- {
- InitializeComponent();
- countDownTime = int.Parse(ini.IniReadValue("Config", "waitTime"));
- string closeDownTimeinfo = ini.IniReadValue("Config", "closeDownTime");
- if (closeDownTimeinfo != "")
- {
- closeDownTime= int.Parse(closeDownTimeinfo);
- }
- this.label1.Text = "请操作POS,正在等待POS 返回.....";
- // 获取当前屏幕的工作区域
- Rectangle screenRectangle = Screen.GetWorkingArea(this);
- // 计算窗体居中的位置
- int left = screenRectangle.Left + (screenRectangle.Width - this.Width) / 2;
- int top = screenRectangle.Top + (screenRectangle.Height - this.Height) / 2;
- // 设置窗体的位置
- this.Location = new Point(left, top);
- serialPort = SerialPort;
- if (th == null || !th.IsAlive)
- {
- th = new Thread(Run);
- th.IsBackground = true;
- th.Start();
- }
- // 注册事件处理程序
- this.FormClosing += MainForm_FormClosing;
- }
- // FormClosing 事件处理 - 允许取消关闭
- private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
- {
- // e.CloseReason 包含关闭原因(用户点击按钮、程序调用等)
- if (e.CloseReason == CloseReason.UserClosing)
- {
- // 如果正在倒计时,忽略本次关闭请求
- if (isCountingDown)
- {
- e.Cancel = true;
- return;
- }
- int warningTime = int.Parse(ini.IniReadValue("Config", "warningTime"));
- try
- {
-
- // 显示初始倒计时
- UpdateCountdownLabel(closeDownTime);
- // 标记为正在倒计时
- isCountingDown = true;
- // 防止多次启动计时器
- if (closeTimer != null && closeTimer.Enabled)
- return;
- // 创建计时器
- closeTimer = new System.Timers.Timer(1000);
- closeTimer.Elapsed += (s, args) =>
- {
- if (closeDownTime > 0)
- {
- closeDownTime--;
- // 更新UI
- this.Invoke((MethodInvoker)delegate
- {
- UpdateCountdownLabel(closeDownTime);
- // 时间不足时改变颜色
- if (closeDownTime <= warningTime)
- {
- label2.ForeColor = Color.Red;
- }
- });
- }
- else
- {
- closeTimer.Stop();
- // 移除事件处理,防止递归
- this.FormClosing -= MainForm_FormClosing;
- // 关闭窗体
- this.Invoke((MethodInvoker)delegate
- {
- this.Close();
- });
- }
- };
- // 阻止默认关闭行为
- e.Cancel = true;
- closeTimer.Start();
- }
- catch (Exception ex)
- {
- MessageBox.Show($"关闭时出错: {ex.Message}", "错误",
- MessageBoxButtons.OK, MessageBoxIcon.Error);
- // 发生错误时允许正常关闭
- e.Cancel = false;
- }
- }
- }
- private void UpdateCountdownLabel(int closeDownTime)
- {
- if ((closeDownTime!=0)&& (closeDownTime != 1))
- {
- label2.Text = $"将在 {closeDownTime} 秒后关闭,请操作POS取消";
- }
-
- }
- private void Run()
- {
- string errorCode = "-1";
- string errorMessage = "";
- JObject jObject = new JObject();
- int warningTime = int.Parse(ini.IniReadValue("Config", "warningTime"));
- try
- {
- Thread.Sleep(1000);
- this.Invoke((MethodInvoker)delegate
- {
- this.TopMost = false;
- this.BringToFront();
- this.TopMost = true;
- });
- System.Timers.Timer timer = new System.Timers.Timer(1000);
- timer.Elapsed += (sender, e) =>
- {
- if (countDownTime > 0)
- {
- countDownTime--;
- if (countDownTime < warningTime)
- {
- this.Invoke((MethodInvoker)delegate
- {
- this.label2.ForeColor = Color.Red;
- });
- }
- if (posData == "")
- {
- if (serialPort.BytesToRead > 0)
- {
- StringBuilder txt = new StringBuilder();
- while (serialPort.BytesToRead > 0)
- {
- string nextData = serialPort.ReadExisting();
- txt.Append(nextData);
- Thread.Sleep(100);
- }
- string receivedData = txt.ToString();
- string logPath = ini.IniReadValue("LOG", "path");
- string DateTimeinfo = DateTime.Now.TimeOfDay.ToString();
- fileclass.WriteLogFile(logPath, " POS返参: " + receivedData);
- posData = receivedData;
- countDownTime = warningTime;
- JObject posDataobj = JObject.Parse(posData);
- JObject jObject2 = new JObject();
- string status = posDataobj["status"].ToString();
- errorMessage = posDataobj["message"].ToString();
-
- this.Invoke((MethodInvoker)delegate
- {
- this.label3.Text = errorMessage;
- });
- if (status == "100")
- {
- errorCode = "0";
- countDownTime = 0;
- closeDownTime = 0;
- }
- else
- {
- errorCode = status;
- }
- jObject2.Add("errorCode", errorCode);
- jObject2.Add("errorMessage", errorMessage);
- jObject2.Add("posData", receivedData);
- outstring = jObject2.ToString();
- }
- }
- else
- {
- if (errorMessage != "")
- {
- this.Invoke((MethodInvoker)delegate
- {
- this.label3.Text = errorMessage;
- });
- }
- this.Invoke((MethodInvoker)delegate
- {
- this.label2.Text = countDownTime + "";
- });
- closeDownTime = 0;
- }
- }
- else
- {
- timer.Stop();
- this.Invoke((MethodInvoker)delegate
- {
- this.Close();
- });
- }
- };
- timer.Start();
- }
- catch (Exception ex2)
- {
- errorCode = "-1";
- errorMessage = ex2.Message;
- JObject jObject2 = new JObject();
- jObject2.Add("errorCode", errorCode);
- jObject2.Add("errorMessage", errorMessage);
- outstring = jObject2.ToString();
- }
- }
- }
- }
|