| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068 |
- using Newtonsoft.Json.Linq;
- using NPOI.SS.UserModel;
- using PTMedicalInsurance.CardReaders;
- using PTMedicalInsurance.Common;
- using PTMedicalInsurance.Forms;
- using PTMedicalInsurance.Helper;
- using PTMedicalInsurance.Variables;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace PTMedicalInsurance.Forms
- {
- public partial class ChooseCardNew : Form
- {
- #region Windows API
- [DllImport("user32.dll")]
- private static extern bool SetForegroundWindow(IntPtr hWnd);
- [DllImport("user32.dll")]
- private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
- [DllImport("user32.dll")]
- private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
- [DllImport("user32.dll")]
- private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam);
- [DllImport("user32.dll")]
- private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
- private const int SW_MINIMIZE = 6;
- private const int SW_RESTORE = 9;
- private const int SW_SHOW = 5;
- private const int WM_SYSCOMMAND = 0x0112;
- private const int SC_MINIMIZE = 0xF020;
- private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
- private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
- private const uint SWP_NOMOVE = 0x0002;
- private const uint SWP_NOSIZE = 0x0001;
- private const uint SWP_SHOWWINDOW = 0x0040;
- #endregion
- #region 属性定义
- /// <summary>
- /// 证件类型:01 医保电子凭证 02 居民身份证 03 社会保障卡 05 终端扫脸
- /// </summary>
- public string CertType { get; set; }
-
- /// <summary>
- /// 证件号码
- /// </summary>
- public string CertNo { get; set; }
-
- /// <summary>
- /// 业务类型编码
- /// </summary>
- public string BusinessType { get; set; }
-
- /// <summary>
- /// 是否工伤
- /// </summary>
- public bool IsWorkInjury { get; set; }
- /// <summary>
- /// 读卡Invoker
- /// </summary>
- private readonly InvokeHelper _invoker = new InvokeHelper();
- #endregion
- public ChooseCardNew()
- {
- InitializeComponent();
- }
- private void ChooseCardNew_Load(object sender, EventArgs e)
- {
- // 初始化默认选项
- rbSocialCard.Checked = true;
- rbMedicalInsurance.Checked = true;
- // 初始化患者信息表格
- InitPatientInfoGrid();
- InitBusinessType();
- InitCertType();
- // 初始化无卡结算控件状态
- cbxNoCardTag_CheckedChanged(null, null);
- }
- private void InitBusinessType()
- {
- // 使用字典初始化业务类型
- DictionaryHelper dicHelper = new DictionaryHelper();
- dicHelper.SetComboxDatasource(cbBusinessType, "business_type");
- }
- private void InitCertType()
- {
- // 使用字典初始化证件类型
- DictionaryHelper dicHelper = new DictionaryHelper();
- dicHelper.SetComboxDatasource(cbCertType, "psn_cert_type");
- }
- /// <summary>
- /// 初始化患者信息表格(多行选择模式)
- /// </summary>
- private void InitPatientInfoGrid()
- {
- dgvPatientInfo.Columns.Clear();
- dgvPatientInfo.Columns.Add("psnName", "姓名");
- dgvPatientInfo.Columns.Add("gend", "性别");
- dgvPatientInfo.Columns.Add("certno", "身份证号");
- dgvPatientInfo.Columns.Add("psnNo", "人员编号");
- dgvPatientInfo.Columns.Add("insuplcAdmdvs", "参保地");
- dgvPatientInfo.Columns.Add("psnType", "人员类别");
-
- // 设置列宽
- dgvPatientInfo.Columns["psnName"].Width = 100;
- dgvPatientInfo.Columns["gend"].Width = 60;
- dgvPatientInfo.Columns["certno"].Width = 180;
- dgvPatientInfo.Columns["psnNo"].Width = 150;
- dgvPatientInfo.Columns["insuplcAdmdvs"].Width = 150;
- dgvPatientInfo.Columns["psnType"].Width = 100;
-
- // 设置表格属性
- dgvPatientInfo.AllowUserToAddRows = false;
- dgvPatientInfo.AllowUserToDeleteRows = false;
- dgvPatientInfo.RowHeadersVisible = false;
- dgvPatientInfo.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
- dgvPatientInfo.MultiSelect = false;
- dgvPatientInfo.ReadOnly = true;
- dgvPatientInfo.BackgroundColor = System.Drawing.Color.White;
- dgvPatientInfo.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
- dgvPatientInfo.RowTemplate.Height = 35;
- }
- private void dgvPatientInfo_ColumnAdded(object sender, DataGridViewColumnEventArgs e)
- {
- e.Column.SortMode = DataGridViewColumnSortMode.NotSortable;
- }
- #region 读卡按钮事件
- /// <summary>
- /// 读卡按钮 - 根据选择的证件类型读取
- /// </summary>
- private void btnReadCard_Click(object sender, EventArgs e)
- {
- // 根据选择的证件类型确定证件类型码
- string readType;
- if (rbSocialCard.Checked)
- {
- // 社会保障卡
- Global.pat.mdtrtcertType = "03";
- readType = "社会保障卡";
- }
- else if (rbIDCard.Checked)
- {
- // 居民身份证
- Global.pat.mdtrtcertType = "02";
- readType = "居民身份证";
- }
- else if (rbECToken.Checked)
- {
- // 医保电子凭证
- Global.pat.mdtrtcertType = "01";
- readType = "医保电子凭证";
- }
- else if (rbFace.Checked)
- {
- // 终端扫脸
- Global.pat.mdtrtcertType = "05";
- readType = "终端扫脸";
- }
- else
- {
- MessageBox.Show("请先选择证件类型!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- // 根据证件类型调用读卡
- ReadCard(Global.pat.mdtrtcertType, "1001", readType);
- }
- /// <summary>
- /// 修改密码
- /// </summary>
- private void btnPasswordModify_Click(object sender, EventArgs e)
- {
- try
- {
- string errorMsg = "";
- string sInput = JsonHelper.setCenterInpar("1193", "");
- JObject joRtn = _invoker.invokeCenterService("1193", sInput);
- if (JsonHelper.parseCenterRtnValue(joRtn, out errorMsg) != 0)
- {
- MessageBox.Show("修改卡密码失败:" + errorMsg, "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- }
- else
- {
- MessageBox.Show("修改卡密码成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("修改密码失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- #endregion
- #region 读卡核心方法
- /// <summary>
- /// 读卡并获取信息
- /// </summary>
- /// <param name="certType">证件类型</param>
- /// <param name="tradeCode">交易码</param>
- /// <param name="readType">读卡类型描述</param>
- private void ReadCard(string certType, string tradeCode, string readType)
- {
- // 先禁用读卡按钮,防止重复点击
- btnReadCard.Enabled = false;
- btnReadCard.Text = "读卡中...";
- CertType = certType;
- // 根据证件类型选择不同的读卡方式
- switch (certType)
- {
- case "01":
- case "02":
- case "03":
- // 电子凭证、身份证、社保卡:都走COM组件调用方式
- ReadSocialCardNewTwo(certType);
- break;
- case "05":
- // 扫脸:使用 CardReader.FaceRecognition
- ReadFaceCard();
- break;
- default:
- MessageBox.Show("不支持的证件类型!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- btnReadCard.Enabled = true;
- btnReadCard.Text = "读 卡";
- break;
- }
- }
- /// <summary>
- /// 读取社保卡(使用COM控件)
- /// </summary>
- /// <param name="certType">证件类型:01电子凭证 02身份证 03社保卡</param>
- private void ReadSocialCard(string certType)
- {
- // 在后台线程执行COM控件调用,避免阻塞UI
- Task.Run(() =>
- {
- try
- {
- string errorMsg = "";
- // 从UI控件获取参数值(需要Invoke确保线程安全)
- string certNo = "";
- string certTypeCode = "";
- string betaFlag = "";
- string noCardTag = "";
- this.Invoke(new Action(() =>
- {
- certNo = txtCertNo.Text.Trim();
- certTypeCode = cbCertType.SelectedValue?.ToString() ?? "";
- // 如果用户勾选了无卡结算,使用用户选择;否则根据证件类型自动判断
- if (cbxNoCardTag.Checked)
- {
- noCardTag = "1";
- }
- else
- {
- // 根据证件类型设置 no_card_tag
- // 01-电子凭证 02-身份证 03-社保卡
- switch (certType)
- {
- case "01":
- noCardTag = "1";
- break;
- case "02":
- noCardTag = "2";
- break;
- default:
- noCardTag = "";
- break;
- }
- }
- }));
- // COM控件调用需要在STA线程
- string input = "{" +
- "\"begntime\": \"\"," +
- "\"no_card_tag\": \"" + noCardTag + "\"," +
- "\"certno\": \"" + certNo + "\"," +
- "\"psn_cert_typ\": \"" + certTypeCode + "\"," +
- "\"beta_flag\": \"" + betaFlag + "\"," +
- "\"cro_prov_flag\": \"\"" +
- "}";
- int result = -1;
- InvokeComCenter2026 carder = new InvokeComCenter2026();
- // 获取当前窗口句柄
- IntPtr thisHandle = this.Handle;
- // 使用 STA 线程调用 COM 控件
- Thread staThread = new Thread(() =>
- {
- // 先将当前窗口最小化(使用 SendMessage 避免焦点问题)
- SendMessage(thisHandle, WM_SYSCOMMAND, (IntPtr)SC_MINIMIZE, IntPtr.Zero);
- // 等待最小化完成
- Thread.Sleep(200);
- // 调用读卡
- result = carder.Business("1001", ref input, ref errorMsg);
- // 读卡完成后恢复窗口
- ShowWindow(thisHandle, SW_RESTORE);
- SetWindowPos(thisHandle, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
- });
- staThread.SetApartmentState(ApartmentState.STA);
- staThread.Start();
- staThread.Join();
- // 回到UI线程更新界面
- this.Invoke(new Action(() =>
- {
- // 取消置顶
- SetWindowPos(thisHandle, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
- this.BringToFront();
- this.Activate();
- btnReadCard.Enabled = true;
- btnReadCard.Text = "读 卡";
- if (result != 0)
- {
- MessageBox.Show("读社保卡失败:" + errorMsg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- // 更新界面上的单选按钮
- UpdateRadioButton(CertType);
- // 解析1001返回的多行数据
- JObject joRtn = JObject.Parse(errorMsg);
- ParseAndShowPatientList(joRtn);
- }));
- }
- catch (Exception ex)
- {
- this.Invoke(new Action(() =>
- {
- // 恢复窗口
- ShowWindow(this.Handle, SW_RESTORE);
- this.BringToFront();
- btnReadCard.Enabled = true;
- btnReadCard.Text = "读 卡";
- MessageBox.Show("读社保卡失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }));
- }
- });
- }
- /// <summary>
- /// 读取社保卡(使用COM控件,支持CEF环境模态效果)
- /// 签名与ReadSocialCard相同
- /// </summary>
- /// <param name="certType">证件类型:01电子凭证 02身份证 03社保卡</param>
- private void ReadSocialCardNew(string certType)
- {
- // 获取CEF Browser窗口句柄并禁用
- IntPtr cefHwnd = IntPtr.Zero;
- try
- {
- cefHwnd = WinApi.GetWindowsHandle("prBrowser");
- Global.writeLog("ReadSocialCardNew", "1-获取CEF窗口", $"hwnd={cefHwnd}");
- }
- catch (Exception ex)
- {
- Global.writeLog("ReadSocialCardNew", "1-获取CEF窗口失败", ex.Message);
- }
- // 禁用CEF Browser窗口
- if (cefHwnd != IntPtr.Zero)
- {
- WinApi.EnableWindow(cefHwnd, false);
- Global.writeLog("ReadSocialCardNew", "2-禁用CEF窗口", $"Enabled={WinApi.IsWindowEnabled(cefHwnd)}");
- }
-
- // 禁用当前窗口
- this.Enabled = false;
- Global.writeLog("ReadSocialCardNew", "3-禁用当前窗口", $"this.Enabled={this.Enabled}");
-
- // 将当前窗口置顶并激活(确保COM窗口在它前面弹出)
- this.TopMost = true;
- this.BringToFront();
- this.Activate();
- Application.DoEvents();
- Thread.Sleep(100);
- Global.writeLog("ReadSocialCardNew", "4-置顶当前窗口", $"hwnd={this.Handle}");
- this.TopMost = false; // 临时置顶后立即取消,让COM窗口有机会置顶
- // 在后台线程执行COM控件调用
- Task.Run(() =>
- {
- try
- {
- string errorMsg = "";
- Global.writeLog("ReadSocialCardNew", "5-后台线程开始", "");
- // 从UI控件获取参数值(需要Invoke确保线程安全)
- string certNo = "";
- string certTypeCode = "";
- string betaFlag = "";
- string noCardTag = "";
- this.Invoke(new Action(() =>
- {
- certNo = txtCertNo.Text.Trim();
- certTypeCode = cbCertType.SelectedValue?.ToString() ?? "";
- if (cbxNoCardTag.Checked)
- {
- noCardTag = "1";
- }
- else
- {
- switch (certType)
- {
- case "01":
- noCardTag = "1";
- break;
- case "02":
- noCardTag = "2";
- break;
- default:
- noCardTag = "";
- break;
- }
- }
- }));
- // COM控件调用需要在STA线程
- string input = "{" +
- "\"begntime\": \"\"," +
- "\"no_card_tag\": \"" + noCardTag + "\"," +
- "\"certno\": \"" + certNo + "\"," +
- "\"psn_cert_typ\": \"" + certTypeCode + "\"," +
- "\"beta_flag\": \"" + betaFlag + "\"," +
- "\"cro_prov_flag\": \"\"" +
- "}";
- int result = -1;
- InvokeComCenter2026 carder = new InvokeComCenter2026();
- // 使用 STA 线程调用 COM 控件
- Global.writeLog("ReadSocialCardNew", "6-准备启动STA线程", input);
- Thread staThread = new Thread(() =>
- {
- Global.writeLog("ReadSocialCardNew", "7-STA线程开始", "");
- // 调用读卡
- result = carder.Business("1001", ref input, ref errorMsg);
- Global.writeLog("ReadSocialCardNew", "8-读卡完成", $"result={result}");
- });
- staThread.SetApartmentState(ApartmentState.STA);
- staThread.Start();
- staThread.Join();
- Global.writeLog("ReadSocialCardNew", "9-后台线程结束", "");
- // 回到UI线程更新界面
- this.Invoke(new Action(() =>
- {
- Global.writeLog("ReadSocialCardNew", "10-恢复窗口", "");
- // 恢复CEF Browser窗口
- if (cefHwnd != IntPtr.Zero)
- {
- WinApi.EnableWindow(cefHwnd, true);
- Global.writeLog("ReadSocialCardNew", "11-CEF窗口已恢复", $"Enabled={WinApi.IsWindowEnabled(cefHwnd)}");
- }
- // 恢复当前窗口
- this.Enabled = true;
- this.BringToFront();
- this.Activate();
- btnReadCard.Enabled = true;
- btnReadCard.Text = "读 卡";
- if (result != 0)
- {
- MessageBox.Show("读社保卡失败:" + errorMsg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- // 更新界面上的单选按钮
- UpdateRadioButton(CertType);
- // 解析1001返回的多行数据
- JObject joRtn = JObject.Parse(errorMsg);
- ParseAndShowPatientList(joRtn);
- }));
- }
- catch (Exception ex)
- {
- this.Invoke(new Action(() =>
- {
- // 恢复CEF Browser窗口
- if (cefHwnd != IntPtr.Zero)
- {
- WinApi.EnableWindow(cefHwnd, true);
- }
- // 恢复当前窗口
- this.Enabled = true;
- this.BringToFront();
- btnReadCard.Enabled = true;
- btnReadCard.Text = "读 卡";
- MessageBox.Show("读社保卡失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }));
- }
- });
- }
- /// <summary>
- /// 读取社保卡(方法二:找到COM窗口并置顶)
- /// </summary>
- /// <param name="certType">证件类型</param>
- private void ReadSocialCardNewTwo(string certType)
- {
- // 获取CEF Browser窗口句柄并禁用
- IntPtr cefHwnd = IntPtr.Zero;
- try
- {
- cefHwnd = WinApi.GetWindowsHandle("prBrowser");
- Global.writeLog("ReadSocialCardNewTwo", "1-获取CEF窗口", $"hwnd={cefHwnd}");
- }
- catch (Exception ex)
- {
- Global.writeLog("ReadSocialCardNewTwo", "1-获取CEF窗口失败", ex.Message);
- }
- // 禁用CEF Browser窗口
- if (cefHwnd != IntPtr.Zero)
- {
- WinApi.EnableWindow(cefHwnd, false);
- Global.writeLog("ReadSocialCardNewTwo", "2-禁用CEF窗口", "");
- }
-
- // 禁用当前窗口
- this.Enabled = false;
- Global.writeLog("ReadSocialCardNewTwo", "3-禁用当前窗口", "");
-
- // 给系统时间完成禁用操作
- Application.DoEvents();
- Thread.Sleep(100);
- Global.writeLog("ReadSocialCardNewTwo", "4-准备调用COM", "");
- // 在后台线程执行COM控件调用
- Task.Run(() =>
- {
- try
- {
- string errorMsg = "";
- Global.writeLog("ReadSocialCardNewTwo", "5-后台线程开始", "");
- // 从UI控件获取参数值
- string certNo = "";
- string certTypeCode = "";
- string betaFlag = "";
- string noCardTag = "";
- this.Invoke(new Action(() =>
- {
- certNo = txtCertNo.Text.Trim();
- certTypeCode = cbCertType.SelectedValue?.ToString() ?? "";
- if (cbxNoCardTag.Checked)
- {
- noCardTag = "1";
- }
- else
- {
- switch (certType)
- {
- case "01":
- noCardTag = "1";
- break;
- case "02":
- noCardTag = "2";
- break;
- default:
- noCardTag = "";
- break;
- }
- }
- }));
- string input = "{" +
- "\"begntime\": \"\"," +
- "\"no_card_tag\": \"" + noCardTag + "\"," +
- "\"certno\": \"" + certNo + "\"," +
- "\"psn_cert_typ\": \"" + certTypeCode + "\"," +
- "\"beta_flag\": \"" + betaFlag + "\"," +
- "\"cro_prov_flag\": \"\"" +
- "}";
- int result = -1;
- InvokeComCenter2026 carder = new InvokeComCenter2026();
- // 使用 STA 线程调用 COM 控件
- Thread staThread = new Thread(() =>
- {
- Global.writeLog("ReadSocialCardNewTwo", "6-STA线程开始", "开始调用COM");
-
- // 在单独的线程中循环查找并置顶COM窗口(枚举所有窗口)
- Thread searchThread = new Thread(() =>
- {
- int count = 0;
- while (count < 50) // 最多等待5秒
- {
- IntPtr comHwnd = WinApi.FindAndSetTopComWindowAll(this.Handle);
- if (comHwnd != IntPtr.Zero)
- {
- string comTitle = WinApi.GetComWindowTitle();
- Global.writeLog("ReadSocialCardNewTwo", "6.5-找到COM窗口", $"hwnd={comHwnd}, 窗口标题={comTitle}");
- break;
- }
- Thread.Sleep(100);
- count++;
- }
- if (count >= 50)
- {
- Global.writeLog("ReadSocialCardNewTwo", "6.5-未找到COM窗口", "超时未找到");
- }
- });
- searchThread.IsBackground = true;
- searchThread.Start();
- // 调用读卡
- result = carder.Business("1001", ref input, ref errorMsg);
- Global.writeLog("ReadSocialCardNewTwo", "7-读卡完成", $"result={result}");
- });
- staThread.SetApartmentState(ApartmentState.STA);
- staThread.Start();
- staThread.Join();
- Global.writeLog("ReadSocialCardNewTwo", "8-后台线程结束", "");
- // 回到UI线程更新界面
- this.Invoke(new Action(() =>
- {
- Global.writeLog("ReadSocialCardNewTwo", "9-恢复窗口", "");
- // 恢复CEF Browser窗口
- if (cefHwnd != IntPtr.Zero)
- {
- WinApi.EnableWindow(cefHwnd, true);
- Global.writeLog("ReadSocialCardNewTwo", "10-CEF窗口已恢复", "");
- }
- // 恢复当前窗口
- this.Enabled = true;
- this.BringToFront();
- this.Activate();
- btnReadCard.Enabled = true;
- btnReadCard.Text = "读 卡";
- if (result != 0)
- {
- MessageBox.Show("读社保卡失败:" + errorMsg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- UpdateRadioButton(CertType);
- JObject joRtn = JObject.Parse(errorMsg);
- ParseAndShowPatientList(joRtn);
- }));
- }
- catch (Exception ex)
- {
- this.Invoke(new Action(() =>
- {
- if (cefHwnd != IntPtr.Zero)
- {
- WinApi.EnableWindow(cefHwnd, true);
- }
- this.Enabled = true;
- this.BringToFront();
- btnReadCard.Enabled = true;
- btnReadCard.Text = "读 卡";
- MessageBox.Show("读社保卡失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }));
- }
- });
- }
- /// <summary>
- /// 读取扫脸
- /// </summary>
- private void ReadFaceCard()
- {
- try
- {
- CardReader reader = new CardReader();
- string msg;
- int result = reader.FaceRecognition(out msg);
- btnReadCard.Enabled = true;
- btnReadCard.Text = "读 卡";
- if (result != 0)
- {
- MessageBox.Show("人脸识别失败:" + msg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- // 解析人脸识别返回的JSON数据
- JObject joRtn = JObject.Parse(msg);
- string code = JsonHelper.getDestValue(joRtn, "code");
- if (code != "0")
- {
- string errorMsg = JsonHelper.getDestValue(joRtn, "message");
- MessageBox.Show("人脸识别失败:" + errorMsg, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- // 获取 data 对象
- JObject dataObj = joRtn.SelectToken("data") as JObject;
- if (dataObj == null)
- {
- MessageBox.Show("未获取到人脸识别数据!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- // 提取关键信息
- string userName = JsonHelper.getDestValue(dataObj, "userName");
- string idNo = JsonHelper.getDestValue(dataObj, "idNo");
- string idType = JsonHelper.getDestValue(dataObj, "idType");
- string insuOrg = JsonHelper.getDestValue(dataObj, "insuOrg");
- string ecToken = JsonHelper.getDestValue(dataObj, "ecToken");
- string birthday = JsonHelper.getDestValue(dataObj, "birthday");
- string gender = JsonHelper.getDestValue(dataObj, "gender");
- // 清空现有数据
- dgvPatientInfo.DataSource = null;
- dgvPatientInfo.Rows.Clear();
- dgvPatientInfo.Columns.Clear();
- InitPatientInfoGrid();
- // 添加人脸识别数据到表格(只显示单条)
- int rowIndex = dgvPatientInfo.Rows.Add(userName, gender, idNo, "", insuOrg, "");
- // 保存原始数据到行标签
- JObject rowData = new JObject();
- rowData.Add("userName", userName);
- rowData.Add("idNo", idNo);
- rowData.Add("idType", idType);
- rowData.Add("insuOrg", insuOrg);
- rowData.Add("ecToken", ecToken);
- rowData.Add("birthday", birthday);
- rowData.Add("gender", gender);
- rowData.Add("certType", "05"); // 扫脸类型
- dgvPatientInfo.Rows[rowIndex].Tag = rowData;
- // 设置证件类型
- CertType = "05";
- // 更新单选按钮
- rbFace.Checked = true;
- MessageBox.Show("人脸识别成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- catch (Exception ex)
- {
- btnReadCard.Enabled = true;
- btnReadCard.Text = "读 卡";
- MessageBox.Show("人脸识别失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- /// <summary>
- /// 解析并显示患者列表(多行)
- /// </summary>
- private void ParseAndShowPatientList(JObject joRtn)
- {
- try
- {
- // 清空现有数据
- dgvPatientInfo.DataSource = null;
- dgvPatientInfo.Rows.Clear();
- // 获取返回代码
- string rtnCode = JsonHelper.getDestValue(joRtn, "code");
- string message = JsonHelper.getDestValue(joRtn, "message");
- // 检查返回状态
- if (rtnCode != "1" && rtnCode != "0")
- {
- MessageBox.Show("读卡失败:" + message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- // 获取 data 对象
- JObject dataObj = joRtn.SelectToken("data") as JObject;
- if (dataObj == null)
- {
- MessageBox.Show("未获取到数据!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- return;
- }
- // 获取基本信息
- string certno = JsonHelper.getDestValue(dataObj, "certno");
- string psnName = JsonHelper.getDestValue(dataObj, "psn_name");
- string insuinfoCnt = JsonHelper.getDestValue(dataObj, "insuinfo_cnt");
- string cardSn = JsonHelper.getDestValue(dataObj, "card_sn");
- // 获取参保信息列表(insuinfo)
- JArray insuinfoList = dataObj.SelectToken("insuinfo") as JArray;
- if (insuinfoList == null || insuinfoList.Count == 0)
- {
- MessageBox.Show("未获取到参保信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- // 清除现有列并重新设置
- dgvPatientInfo.Columns.Clear();
- InitPatientInfoGrid();
- // 填充数据 - 根据实际的JSON字段名
- foreach (JObject insuinfo in insuinfoList)
- {
- string insuplcAdmdvs = JsonHelper.getDestValue(insuinfo, "insuplc_admdvs");
- string insuplcAdmdvsName = JsonHelper.getDestValue(insuinfo, "insuplc_admdvs_name");
- string psnType = JsonHelper.getDestValue(insuinfo, "psn_type");
- string psnNo = JsonHelper.getDestValue(insuinfo, "psn_insu_rlts_id");
- string empName = JsonHelper.getDestValue(insuinfo, "emp_name");
- // 合并参保地和名称
- string insuplcFull = string.IsNullOrEmpty(insuplcAdmdvsName)
- ? insuplcAdmdvs
- : insuplcAdmdvs + " " + insuplcAdmdvsName;
- int rowIndex = dgvPatientInfo.Rows.Add(psnName, "", certno, psnNo, insuplcFull, psnType);
- // 保存原始数据到行标签
- JObject rowData = new JObject();
- rowData.Add("psn_name", psnName);
- rowData.Add("certno", certno);
- rowData.Add("card_sn", cardSn);
- rowData.Add("insuplc_admdvs", insuplcAdmdvs);
- rowData.Add("insuplc_admdvs_name", insuplcAdmdvsName);
- rowData.Add("psn_type", psnType);
- rowData.Add("psn_no", psnNo);
- rowData.Add("emp_name", empName);
- rowData.Add("insuinfo", insuinfo);
- rowData.Add("data", dataObj);
- dgvPatientInfo.Rows[rowIndex].Tag = rowData;
- }
- // 如果只有一条记录,自动选中
- if (dgvPatientInfo.Rows.Count == 1)
- {
- dgvPatientInfo.Rows[0].Selected = true;
- SelectPatientAndFillInfo(0);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show("解析患者列表失败:" + ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- }
- /// <summary>
- /// 选中患者并填充信息到全局变量
- /// </summary>
- private void SelectPatientAndFillInfo(int rowIndex)
- {
- if (rowIndex < 0 || rowIndex >= dgvPatientInfo.Rows.Count)
- return;
- JObject item = dgvPatientInfo.Rows[rowIndex].Tag as JObject;
- if (item == null)
- return;
- // 根据证件类型选择不同的读卡方式
- switch (CertType)
- {
- case "01":
- case "02":
- case "03":
- // 电子凭证、身份证、社保卡:都走COM组件调用方式
- // 获取data对象(包含基础信息)
- JObject dataObj = item.SelectToken("data") as JObject;
- // 获取insuinfo对象(包含参保信息)
- JObject insuinfo = item.SelectToken("insuinfo") as JObject;
- // 填充基础信息
- Global.pat.name = JsonHelper.getDestValue(item, "psn_name");
- Global.pat.certNO = JsonHelper.getDestValue(item, "certno");
- Global.pat.card.SN = JsonHelper.getDestValue(item, "card_sn");
- Global.pat.card.NO = JsonHelper.getDestValue(item, "card_sn");
- // 填充参保信息
- Global.pat.insuplc_admdvs = JsonHelper.getDestValue(insuinfo, "insuplc_admdvs");
- //Global.pat.psn_no = JsonHelper.getDestValue(insuinfo, "psn_insu_rlts_id");
- // 填充证件类型(从data中获取)
- Global.pat.mdtrtcertType = JsonHelper.getDestValue(dataObj, "mdtrt_cert_type");
- Global.pat.mdtrtcertNO = JsonHelper.getDestValue(dataObj, "mdtrt_cert_no");
- Global.pat.certType = JsonHelper.getDestValue(dataObj, "psn_cert_type");
- break;
- case "05":
- // 填充基础信息
- Global.pat.name = JsonHelper.getDestValue(item, "userName");
- Global.pat.certNO = JsonHelper.getDestValue(item, "idNo");
- Global.pat.IDType = JsonHelper.getDestValue(item, "idType");
- Global.pat.card.SN = "";
- Global.pat.card.NO = "";
- // 填充参保信息
- Global.pat.insuplc_admdvs = JsonHelper.getDestValue(item, "insuOrg");
- Global.pat.psn_no = "";
- // 填充证件类型(从data中获取)
- Global.pat.mdtrtcertType = "05";
- Global.pat.mdtrtcertNO = JsonHelper.getDestValue(item, "ecToken");
- Global.pat.certType = JsonHelper.getDestValue(item, "idType");
- break;
- default:
- MessageBox.Show("不支持的证件类型!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
- break;
- }
-
- }
- /// <summary>
- /// 更新证件类型单选按钮
- /// </summary>
- private void UpdateRadioButton(string certType)
- {
- switch (certType)
- {
- case "01":
- rbECToken.Checked = true;
- break;
- case "02":
- rbIDCard.Checked = true;
- break;
- case "03":
- rbSocialCard.Checked = true;
- break;
- case "05":
- rbFace.Checked = true;
- break;
- }
- }
- /// <summary>
- /// DataGridView行选择变化事件
- /// </summary>
- private void dgvPatientInfo_SelectionChanged(object sender, EventArgs e)
- {
- if (dgvPatientInfo.SelectedRows.Count > 0)
- {
- int selectedIndex = dgvPatientInfo.SelectedRows[0].Index;
- SelectPatientAndFillInfo(selectedIndex);
- }
- }
- #endregion
- #region 确定取消按钮事件
- /// <summary>
- /// 确定按钮
- /// </summary>
- private void btnConfirm_Click(object sender, EventArgs e)
- {
- // 检查是否已选择患者
- if (dgvPatientInfo.SelectedRows.Count == 0)
- {
- MessageBox.Show("请先读卡并选择患者信息!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
- return;
- }
- // 获取选中的患者信息
- int selectedIndex = dgvPatientInfo.SelectedRows[0].Index;
- SelectPatientAndFillInfo(selectedIndex);
- // 获取业务类型编码(从ComboBox获取代码)
- if (cbBusinessType.SelectedItem != null)
- {
- DataRowView drv = cbBusinessType.SelectedItem as DataRowView;
- if (drv != null)
- {
- BusinessType = drv["code"].ToString();
- }
- else
- {
-
- }
- }
- // 设置工伤标志
- IsWorkInjury = rbWorkInjury.Checked;
- // 保存到全局变量
- Global.pat.card.ecBizType = BusinessType;
- Global.pat.isWorkInjury = IsWorkInjury;
- this.DialogResult = DialogResult.OK;
- this.Close();
- }
- /// <summary>
- /// 取消按钮
- /// </summary>
- private void btnCancel_Click(object sender, EventArgs e)
- {
- this.DialogResult = DialogResult.Cancel;
- this.Close();
- }
- #endregion
- private void uiPanel5_Click(object sender, EventArgs e)
- {
- }
- private void rbFace_CheckedChanged(object sender, EventArgs e)
- {
- }
- private void cbxNoCardTag_CheckedChanged(object sender, EventArgs e)
- {
- // 根据无卡结算CheckBox状态控制证件号和证件类型控件的可用性
- bool isEnabled = cbxNoCardTag.Checked;
- txtCertNo.Enabled = isEnabled;
- cbCertType.Enabled = isEnabled;
- Global.pat.isNoCardSettle = cbxNoCardTag.Checked;
- }
- private void dgvPatientInfo_CellContentClick(object sender, DataGridViewCellEventArgs e)
- {
- }
- }
- }
|