using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using POSV.Component; using POSV.Utils; using POSV.ShoppingCart; using POSV.Entity; using POSV.HttpApi; namespace POSV.ComponentUI { [ToolboxItem(true)] public partial class LoginUIControl : BaseUserControl { public LoginUIControl() { InitializeComponent(); this.txtWorkerNo.GotFocus += OnInputGotFocus; } protected override void OnLoad(EventArgs e) { base.OnLoad(e); if (this.DesignMode) return; this.txtWorkerNo.Multiline = false; this.txtWorkerNo.Text = string.Empty; this.txtWorkerNo.IsEnterInputKey = true; this.txtPasswd.Multiline = false; this.txtPasswd.Text = string.Empty; this.txtPasswd.IsEnterInputKey = true; } public override bool Focused { get { if (this.ActiveControl == null) { this.ActiveControl = this.txtWorkerNo; } return this.ActiveControl.Focused; } } protected override void OnResize(EventArgs e) { this.Visible = false; base.OnResize(e); this.Visible = true; } /// /// 注册成功事件触发 /// public event UISucceedEventHandler Succeed; protected virtual void OnSucceed(UISucceedEventArgs e) { Succeed?.Invoke(this, e); } private void OnTouchBeforeClick(object sender, TouchEventArgs e) { } private void OnTouchClick(object sender, TouchEventArgs e) { switch (e.Value) { case "clear": //如果当前焦点控件是输入框 if (this.ActiveControl is NumericTextBox) { var activeControl = this.ActiveControl as NumericTextBox; activeControl.Text = string.Empty; } break; default: { InputSimulatorUtils.SendKey(KeyCodes.Map[e.Value]); } break; } } private bool ValidateInputValue() { if (string.IsNullOrEmpty(txtWorkerNo.Text.Trim())) { this.lblInfo.Text = "工号不能为空"; this.txtWorkerNo.Focus(); return false; } if (string.IsNullOrEmpty(txtPasswd.Text.Trim())) { this.lblInfo.Text = "密码不能为空"; this.txtPasswd.Focus(); return false; } if (!Global.Instance.AuthLogin) { DialogForm.ShowAlert(this, "特别提示", "您的POS使用权限已过期,请联系代理商续费!"); return false; } this.lblInfo.Text = "提交认证中"; return true; } /// /// 登录 /// /// /// private void OnTouchLoginClick(object sender, TouchEventArgs e) { var valid = ValidateInputValue(); if (valid) { Login(); } } private void Login() { try { var userName = this.txtWorkerNo.Text.Trim().PadLeft(5, '0'); var passwd = this.txtPasswd.Text.Trim(); Tuple response = null; //是否需要确认登录信息 bool isConfirm = false; if (Global.Instance.Online) { //调用业务系统开放平台 Tuple responsePosAuth = LoginApi.PosAuth(Global.Instance.Authc.StoreId, Global.Instance.Authc.PosNo); if (responsePosAuth.Item1) { PosAuth posAuth = responsePosAuth.Item3; if (posAuth.CostMode == 0) { try { lock (Global.Instance.SyncLock) { using (var db = Global.Instance.OpenDataBase) { using (var transaction = db.GetTransaction()) { string sql = "update pos_store_info set dueDate ='{0}'"; sql = string.Format(sql, posAuth.DueDate); db.Execute(sql, null); transaction.Complete(); } } } } catch (Exception ex) { LOGGER.Error(ex); } //租赁模式剩余时间小于0天,禁止登陆系统 if (posAuth.AuthDays < 0) { Global.Instance.AuthLogin = false; DialogForm.ShowAlert(this, "特别提示", "您的POS使用权限已过期,请联系代理商续费!"); return; } } } //调用业务系统开放平台 response = LoginApi.ValidateWorker(Global.Instance.Authc.StoreId, Global.Instance.Authc.PosNo, userName, passwd); isConfirm = false; } else { response = LoginApi.DatabaseLogin(userName, passwd); isConfirm = true; } //返回成功 if (response.Item1) { this.lblInfo.Text = response.Item2; Worker worker = response.Item3; if (isConfirm) { List dayList = OrderUtils.GetUnUpLoadDay(); StoreInfo storeInfo = null; using (var db = Global.Instance.OpenDataBase) { storeInfo = db.FirstOrDefault("where id = @0", Global.Instance.Authc.StoreId); } //如果门店设置最大登录天数大于0并且实际未上传数据的天数大于该天数,就不允许离线登录了 if (storeInfo != null && storeInfo.MaxOffLine > 0 && dayList.Count >= storeInfo.MaxOffLine) { this.lblInfo.Text = string.Format("门店最多允许离线登录{0}天,请联网登录!", storeInfo.MaxOffLine); return; } var confirm = new LoginConfirm(Global.Instance.Authc, worker); //确认登录 if (DialogResult.OK == confirm.ShowDialog()) { //登陆成功通知事件 this.OnSucceed(new UISucceedEventArgs(worker)); } else { this.txtWorkerNo.Text = string.Empty; this.txtPasswd.Text = string.Empty; this.lblInfo.Text = "工号不能为空"; this.txtWorkerNo.Focus(); } } else { //登陆成功通知事件 this.OnSucceed(new UISucceedEventArgs(worker)); } } else { this.lblInfo.Text = response.Item2; } } catch (Exception ex) { LOGGER.Error(ex, "收银员登录验证异常"); this.lblInfo.Text = "收银员登录异常"; } } /// /// 退出 /// /// /// private void OnTouchExitClick(object sender, TouchEventArgs e) { } private void OnInputEnterClick(object sender, EnterEventArg e) { var valid = ValidateInputValue(); if (valid) { Login(); } } /// /// 按钮改变 /// /// /// private void OnInputValueChange(object sender, EnterEventArg e) { } } public delegate void UISucceedEventHandler(object sender, UISucceedEventArgs e); public class UISucceedEventArgs : EventArgs { public readonly Worker Worker; public UISucceedEventArgs(Worker worker) { this.Worker = worker; } } }