You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

472 lines
17 KiB
C#

9 months ago
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.Entity;
using POSV.HttpApi;
using POSV.ShoppingCart;
using POSV.Utils;
using POSV.Common.Util;
using POS.Language.Language;
namespace POSV.Component
{
[ToolboxItem(true)]
public partial class LoginControlExt : BaseUserControl
{
public LoginControlExt()
{
InitializeComponent();
labelX2.Text = LangProxy.ToLang(labelX2.Text);
lblStoreNo.Text = LangProxy.ToLang(lblStoreNo.Text);
lblStoreName.Text = LangProxy.ToLang(lblStoreName.Text);
labelX4.Text = LangProxy.ToLang(labelX4.Text);
labelX5.Text = LangProxy.ToLang(labelX5.Text);
labelX6.Text = LangProxy.ToLang(labelX6.Text);
txtWorkerNo.WatermarkText = LangProxy.ToLang(txtWorkerNo.WatermarkText);
txtPasswd.WatermarkText = LangProxy.ToLang(txtPasswd.WatermarkText);
touchButtonX2.Text = LangProxy.ToLang(touchButtonX2.Text);
touchButtonX1.Text = LangProxy.ToLang(touchButtonX1.Text);
touchButtonX5.Text = LangProxy.ToLang(touchButtonX5.Text);
this.txtWorkerNo.GotFocus += OnInputGotFocus;
foreach (var item in this.tableLayoutPanel1.Controls)
{
var _btn = item as TouchButtonX;
if (_btn != null)
{
//zhangy 2020-03-20 Edit 注释以下代码原因是Win10触摸屏不兼容
//_btn.Click += _btn_Click;
//_btn.DoubleClick += _btn_Click;
//zhangy 2020-03-20 Add 支持Win10触摸屏不兼容
_btn.TouchClick += OnTouchClick;
}
}
}
private void OnTouchClick(object sender, Component.TouchEventArgs e)
{
var _btn = sender as TouchButtonX;
if (_btn == null)
{
return;
}
switch (_btn.KeyCode)
{
case "清除":
{
var _txt = this.ActiveControl as NumericTextBox;
if (_txt == null)
{
return;
}
InputSimulatorUtils.SendClear(_txt.Text.Length);
}
break;
case "{BACKSPACE}":
{
InputSimulatorUtils.SendKey(KeyCodes.Map["back"]);
}
break;
default:
{
if (KeyCodes.Map.ContainsKey(_btn.Text))
{
InputSimulatorUtils.SendKey(KeyCodes.Map[_btn.Text]);
}
}
break;
}
}
//private void _btn_Click(object sender, EventArgs e)
//{
// var _btn = sender as TouchButtonX;
// if (_btn == null)
// {
// return;
// }
// switch (_btn.KeyCode)
// {
// case "清除":
// {
// var _txt = this.ActiveControl as NumericTextBox;
// if (_txt == null)
// {
// return;
// }
// InputSimulatorUtils.SendClear(_txt.Text.Length);
// }
// break;
// case "{BACKSPACE}":
// {
// InputSimulatorUtils.SendKey(KeyCodes.Map["back"]);
// }
// break;
// default:
// {
// if (KeyCodes.Map.ContainsKey(_btn.Text))
// {
// InputSimulatorUtils.SendKey(KeyCodes.Map[_btn.Text]);
// }
// }
// break;
// }
//}
private Timer _timer = new Timer();
private List<SlotCardAuthorizationData> _slotCardAuthorizations = new List<SlotCardAuthorizationData>();
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.DesignMode) return;
this.txtWorkerNo.Height = 35;
this.txtWorkerNo.Multiline = false;
this.txtWorkerNo.Text = string.Empty;
this.txtWorkerNo.IsEnterInputKey = true;
this.txtPasswd.Height = 35;
this.txtPasswd.Multiline = false;
this.txtPasswd.Text = string.Empty;
this.txtPasswd.IsEnterInputKey = true;
this.lblStoreNo.Text = Global.Instance.Authc.StoreNo;
this.lblStoreName.Text = Global.Instance.Authc.StoreName;
CreditCardAuthorization();
}
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;
}
private bool ValidateInputValue()
{
this.lblErrorMessage.Visible = false;
if (string.IsNullOrEmpty(txtWorkerNo.Text.Trim()))
{
this.lblErrorMessage.Text = LangProxy.ToLang("工号不能为空");
this.lblErrorMessage.Visible = true;
this.txtWorkerNo.Focus();
return false;
}
if (string.IsNullOrEmpty(txtPasswd.Text.Trim()))
{
this.lblErrorMessage.Text = LangProxy.ToLang("密码不能为空");
this.lblErrorMessage.Visible = true;
this.txtPasswd.Focus();
return false;
}
if (!Global.Instance.AuthLogin)
{
DialogForm.ShowAlert(this, LangProxy.ToLang("特别提示"), LangProxy.ToLang("您的POS使用权限已过期,请联系代理商续费!"));
return false;
}
return true;
}
private void OnLoginTouchClick(object sender, TouchEventArgs e)
{
var valid = ValidateInputValue();
if (valid)
{
Login();
}
}
private void OnCloseTouchClick(object sender, TouchEventArgs e)
{
this.OnClosed(new SucceedEventArgs(null, true));
}
/// <summary>
/// 关闭事件触发
/// </summary>
public event SucceedEventHandler Closed;
protected virtual void OnClosed(SucceedEventArgs e)
{
Closed?.Invoke(this, e);
}
/// <summary>
/// 注册成功事件触发
/// </summary>
public event SucceedEventHandler Succeed;
protected virtual void OnSucceed(SucceedEventArgs e)
{
_timer.Stop();
Succeed?.Invoke(this, e);
}
private void Login()
{
try
{
this._timer.Interval = 5000;
var userName = this.txtWorkerNo.Text.Trim().PadLeft(5, '0');
var passwd = this.txtPasswd.Text.Trim();
Tuple<bool, string, Worker> response = null;
//是否需要确认登录信息
bool isConfirm = false;
if (Global.Instance.Online)
{
//调用业务系统开放平台
Tuple<bool, string, PosAuth> 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, LangProxy.ToLang("特别提示"), LangProxy.ToLang("您的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.ShowMessage(this.lblInfo, response.Item2, false);
this.lblErrorMessage.Text = response.Item2;
this.lblErrorMessage.Visible = true;
this.creditCardAuthorization1.lblInfo.Text = response.Item2;
this.creditCardAuthorization1.lblInfo.Visible = true;
Worker worker = response.Item3;
if (isConfirm)
{
List<string> dayList = OrderUtils.GetUnUpLoadDay();
StoreInfo storeInfo = null;
using (var db = Global.Instance.OpenDataBase)
{
storeInfo = db.FirstOrDefault<StoreInfo>("where id = @0", Global.Instance.Authc.StoreId);
}
//如果门店设置最大登录天数大于0并且实际未上传数据的天数大于该天数,就不允许离线登录了
if (storeInfo != null && storeInfo.MaxOffLine > 0 && dayList.Count >= storeInfo.MaxOffLine)
{
this.lblErrorMessage.Text = LangProxy.ToLang("请联网登录");
this.lblErrorMessage.Visible = true;
this.creditCardAuthorization1.lblInfo.Text = LangProxy.ToLang("请联网登录");
this.creditCardAuthorization1.lblInfo.Visible = true;
//this.ShowMessage(this.lblInfo, string.Format("门店最多允许离线登录{0}天,请联网登录!", storeInfo.MaxOffLine), false);
return;
}
var confirm = new LoginConfirm(Global.Instance.Authc, worker);
//确认登录
if (DialogResult.OK == confirm.ShowDialog())
{
//登陆成功通知事件
this.OnSucceed(new SucceedEventArgs(worker));
}
else
{
this.txtWorkerNo.Text = string.Empty;
this.txtPasswd.Text = string.Empty;
this.lblErrorMessage.Text = LangProxy.ToLang("工号不能为空");
this.lblErrorMessage.Visible = true;
this.creditCardAuthorization1.lblInfo.Text = LangProxy.ToLang("工号不能为空");
this.creditCardAuthorization1.lblInfo.Visible = true;
this.txtWorkerNo.Focus();
}
}
else
{
//登陆成功通知事件
this.OnSucceed(new SucceedEventArgs(worker));
}
}
else
{
this.lblErrorMessage.Text = response.Item2;
this.lblErrorMessage.Visible = true;
this.creditCardAuthorization1.lblInfo.Text = response.Item2; ;
this.creditCardAuthorization1.lblInfo.Visible = true;
}
}
catch (Exception ex)
{
LOGGER.Error(ex, "收银员登录验证异常");
this.lblErrorMessage.Text = LangProxy.ToLang("登录验证异常");
this.lblErrorMessage.Visible = true;
this.creditCardAuthorization1.lblInfo.Text = LangProxy.ToLang("登录验证异常!");
this.creditCardAuthorization1.lblInfo.Visible = true;
}
}
private void OnInputEnterClick(object sender, EnterEventArg e)
{
var valid = ValidateInputValue();
if (valid)
{
Login();
}
}
/// <summary>
/// 刷卡授权
/// </summary>
private void CreditCardAuthorization()
{
var _enabled = Global.Instance.GlobalConfigStringValue(ConfigConstant.DEVICE_SWIPING_CARD_ENABLED, "false");
if (!_enabled.ToLower().Equals("true"))
{
return;
}
creditCardAuthorization1.Dock = DockStyle.Fill;
creditCardAuthorization1.BringToFront();
creditCardAuthorization1.Visible = true;
creditCardAuthorization1.visibleEventHandler += VisibleEventHandler;
_timer.Interval = 1000;
_timer.Tick += _timer_Tick;
_timer.Start();
_slotCardAuthorizations = JSON.Deserialize<List<SlotCardAuthorizationData>>(Global.Instance.GlobalConfigStringValue(ConfigConstant.DEVICE_SWIPING_CARD_DATA, "[]"));
}
private void VisibleEventHandler(object sender)
{
this._timer.Stop();
this._timer.Enabled = false;
this.txtPasswd.Text = string.Empty;
this.txtWorkerNo.Text = string.Empty;
}
private void _timer_Tick(object sender, EventArgs e)
{
var result = Utils.CardOperateUtils.Instance.ReadCardNo();
if (result.Item1)
{
var _card = result.Item2;
var _data = this._slotCardAuthorizations.FirstOrDefault(f => f.CardNo == _card);
if (_data == null)
{
creditCardAuthorization1.lblInfo.Text = LangProxy.ToLang("此卡无授权!");
creditCardAuthorization1.lblInfo.Visible = true;
this._timer.Interval = 5000;
return;
}
this.txtWorkerNo.Text = _data.Account;
this.txtPasswd.Text = _data.Password;
Login();
}
else
{
if (result.Item2.Contains("请将会员卡放置在读卡区"))
{
return;
}
result = Utils.CardOperateUtilsOther.Instance.ReadCardNo();
creditCardAuthorization1.lblInfo.Text = result.Item2;
creditCardAuthorization1.lblInfo.Visible = true;
this._timer.Interval = 5000;
}
}
private void LoginControlExt_VisibleChanged(object sender, EventArgs e)
{
this.lblStoreNo.Text = Global.Instance.Authc.StoreNo;
this.lblStoreName.Text = Global.Instance.Authc.StoreName;
}
}
}