1. 程式人生 > >C#機房收費系統刷卡上機功能簡述

C#機房收費系統刷卡上機功能簡述

前言

刷卡上機,一聽這個詞,我就覺得好有難度,可事實上卻不難實現。 我的小夥伴實現了這個功能,實現固然重要,但是複用對以後更有幫助。我便簡單的研究了下這個功能......

 

實現思路

首先,你當然需要一個刷卡機和幾張卡。我們的刷卡機是USB口的。其次,我們刷卡實現的是上機的功能,在我們的程式裡也就是登入,所以我們接下來要對登入窗體進行操作。

 

<1>引用ReaderBLL和ReaderDAL檔案。

 請在你的VS中新增引用,瀏覽找到這兩個檔案。在這裡筆者想說句抱歉的是,我也不知道這兩個dll檔案從哪裡出來的,此篇部落格只給實現刷卡提供一個思路

 

<2>新建刷卡功能類。 

在U層新建一個類庫,裡面準備編寫實現功能程式碼。這裡需要注意的是需要新增名稱空間using ReaderBLL;

   public  class MyTools
    {
        /// <summary>
        /// 從讀卡器讀取卡號的方法
        /// </summary>
        /// <returns></returns>
        public static string ReadCardNo()
        {
            string cardNo = "";
            string key = "";
            int sectors = 0;

            key = System.Configuration.ConfigurationManager.AppSettings["key"].ToString();
            sectors = Convert.ToInt16(System.Configuration.ConfigurationManager.AppSettings["sectors"].ToString());
            CardReader readCard = new CardReader(key, sectors);
            readCard.Connection();
            string strRead = readCard.Read();
            switch (strRead)
            {
                case "1":
                    try
                    {
                        readCard.Close();
                    }
                    catch
                    {
                        throw new Exception("尋卡失敗,請檢查前臺讀卡器是否完好");
    
                    }
                    break;
                case "2":


                    readCard.Close();
                    break;
                case "3":

                    readCard.Close();
                    break;
                default:
                    if (readCard.Read().Length == 64)
                    {
                        cardNo = readCard.Read().Substring(0, 32).ToString();

                        readCard.Close();
                    }
                    else
                    {
                        throw new Exception("卡號有問題,可能卡損壞了!");
                    }
                    break;
            }
            return cardNo;
        }
    }

功能好理解,但好像上面程式碼中有個類不知道在哪裡定義的,就是這個CardReader。它其實就是那個引用的dll檔案,我們來看看這個檔案裡邊寫的是啥。

 

<3>做一個刷卡窗體FrmCardLogin。控制元件方面需要加一個timer。另外再多說一句,skinLabel是我們的面板,其實就是label。

 

 

<4>FrmCardLogin的程式碼。

這是小僧窗體裡全部的程式碼。小僧也知道讀者看著亂。就簡單說下程式碼必要方法的邏輯。

①設定實體卡的編號和資料庫對應,這樣方便刷卡操作。

②設定Timer事件,用來讀取卡號。這樣,系統通過Timer事件感應到卡被貼上了,就可以繼續接來下的處理了。

③上機操作成功後還需要更新Online表,計算收費之類的後續操作,這些操作裡呼叫的各層方法都是以前用過的功能,這裡就不再贅述。

 

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.Runtime.InteropServices;

namespace UI.Common
{
    public partial class FrmCardLogin : Form
    {
        private Dictionary<string, string> dic = new Dictionary<string, string>();
        public FrmCardLogin()
        {
            InitializeComponent();

            timer1.Interval = 10;
            timer1.Enabled = true;
            //設定卡的編號對應資料庫中的卡號
            dic.Add("4AADF1485E0804008500AABBCCDDEEFF", "1");
            dic.Add("0598F148240804008500AABBCCDDEEFF", "2");
            dic.Add("ACE7BFD2260804006263646566676869", "3");
        }



        //應用單例模式
        private static FrmCardLogin fmpwd;    //frmStModifyPassword是窗體名字
        public static FrmCardLogin GetInstance()
        {
            if (fmpwd == null || fmpwd.IsDisposed)
            {
                fmpwd = new FrmCardLogin();
            }
            return fmpwd;
        }


        /// <summary>
        /// 切換為賬號密碼登入
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void skinLabel2_Click(object sender, EventArgs e)
        {
            FrmLogin frmlogin = new FrmLogin();
            frmlogin.Show();
            this.Close();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (txtuserid.Text.Trim() != "")
            {
                return;
            }

            //開始讀卡號
            string cardno = "";
            try
            {
                cardno = MyTools.ReadCardNo();
            }
            catch (Exception ex)
            {
                timer1.Enabled = false;
                DialogResult result = MessageBox.Show(ex.Message, "警告", MessageBoxButtons.OKCancel);
                if (result == DialogResult.OK)
                {
                    timer1.Enabled = true;
                }
            }
            txtuserid.Text = dic.ContainsKey(cardno) ? dic[cardno] : cardno;
        }

        private void skinTextBox1_Paint(object sender, PaintEventArgs e)
        {

        }

        //定義全域性變數userID,password,level
        public static string level;
        public static int userID;
        public static string name;
        public static string password;


        private void txtuserid_TextChanged(object sender, EventArgs e)
        {
            FrmLogin.userID =Convert.ToInt32(txtuserid.Text);
            
            //判斷是否輸入為空
            if (txtuserid.Text.Trim() == "")
            {
                MessageBox.Show("請刷卡!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                  return;
            }

            //獲取文字框ID,密碼
            userID = Convert.ToInt32(txtuserid.Text.Trim());

            //判斷賬戶是否存在
            Entity.UserEntity user = new Entity.UserEntity();
            user.UserID = Convert.ToInt32(txtuserid.Text);
            Facade.LoginFacade loginfacade = new Facade.LoginFacade();
            List<Entity.UserEntity> list1 = new List<Entity.UserEntity>();
            list1 = loginfacade.SelectCard(user);


            //如果不存在
            if (list1.Count <= 0 || list1[0].UserID != userID)
            {
                MessageBox.Show("刷卡上機失敗!");
                txtuserid.Text = "";
                return;
            }

            //如果存在
            else if (list1[0].UserID == userID)
            {
                level = list1[0].Level; //獲取使用者級別
                name = list1[0].UserName;
                FrmLogin.level = level;
                //如果賬戶級別為一般使用者
                if (list1[0].Level == "一般使用者")
                {
                    //判斷賬戶是否已經登入
                    Entity.OnlineEntity onlineuser = new Entity.OnlineEntity();
                    onlineuser.CardNo = Convert.ToInt32(txtuserid.Text);
                    Facade.LoginFacade onlinefacade = new Facade.LoginFacade();
                    bool isOnline = new bool();
                    isOnline = onlinefacade.OnlineUserID(onlineuser);

                    //如果已登入
                    if (isOnline == false)
                    {
                        MessageBox.Show("該使用者已登入!");
                        return;
                    }
                    else
                    {
                        //例項化出學生表,準備比較餘額
                        Entity.StudentEntity student = new Entity.StudentEntity();
                        student.CardNo = Convert.ToInt32(txtuserid.Text.Trim());
                        Facade.LoginFacade selectcash = new Facade.LoginFacade();
                        List<Entity.StudentEntity> list2 = new List<Entity.StudentEntity>();
                        list2 = selectcash.QueryStuCash(student);
                        //如果當前餘額小於最低上機金額4元,無法上機
                        if (list2[0].Cash < 4)
                        {
                            MessageBox.Show("抱歉,您的餘額不足,請先充值再登入!");
                            return;
                        }
                        else
                        {
                            //更新Online表資訊
                            Facade.LoginFacade addonline = new Facade.LoginFacade();
                            Entity.OnlineEntity online = new Entity.OnlineEntity();
                            online.CardNo = Convert.ToInt32(txtuserid.Text);
                            online.StudentNo = Convert.ToInt32(txtuserid.Text);
                            online.StudentName = name;


                            online.OnDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")).ToString();
                            online.OnTime = DateTime.Now.ToLongTimeString().ToString();

                            online.OnDate = Convert.ToString(DateTime.Now.ToString("yyyy-MM-dd"));
                            online.OnTime = Convert.ToString(DateTime.Now.ToLongTimeString());

                            addonline.InsertOnline(online);
                        }
                    }
                }

                //如果賬戶級別為操作員或管理員
                if (list1[0].Level != "一般使用者")
                {
                    //判斷賬戶是否已經登入
                    Entity.OnWorkEntity onworkuser = new Entity.OnWorkEntity();
                    onworkuser.UserID = Convert.ToInt32(txtuserid.Text);
                    Facade.LoginFacade onworkfacade = new Facade.LoginFacade();
                    bool isOnwork = new bool();
                    isOnwork = onworkfacade.OnworkUserID(onworkuser);

                    //如果已登入
                    if (isOnwork == false)
                    {
                        MessageBox.Show("該使用者已登入");
                        return;
                    }
                    else
                    {
                        //更新Onwork表資訊
                        Facade.LoginFacade addonwork = new Facade.LoginFacade();
                        Entity.OnWorkEntity onwork = new Entity.OnWorkEntity();
                        onwork.UserID = Convert.ToInt32(txtuserid.Text);
                        onwork.UserName = name;
                        onwork.Level = level;

                        onwork.OnDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")).ToString();
                        onwork.OnTime = DateTime.Now.ToLongTimeString().ToString();

                        onwork.OnDate = Convert.ToString(DateTime.Now.ToString("yyyy-MM-dd"));
                        onwork.OnTime = Convert.ToString(DateTime.Now.ToLongTimeString().ToString());

                        onwork.Computer = System.Environment.MachineName;
                        addonwork.InsertOnwork(onwork);
                    }
                }

                //跳轉到主窗體
                this.Hide();
                // this.DialogResult = System.Windows.Forms.DialogResult.OK;
                FrmMain frm = new FrmMain();
                frm.Show();
            }
        }

 

看看效果,沒插卡系統自動檢測尋卡失敗~