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.

282 lines
9.5 KiB
C#

9 months ago
using JwKdsV.Core;
using JwKdsV.Core.HttpApi;
using JwKdsV.Core.Utils;
using JwKdsV.Entity.Common;
using JwKdsV.Entity.Tenant;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace JwKdsV
{
public partial class LoginForm : BaseForm
{
public LoginForm()
{
InitializeComponent();
this.Text = string.Format("巨为厨显({0})", Application.ProductVersion);
KeyboardUtils.Instance.RegisterHook(this.AppHookKeyDown);
}
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
if (this.DesignMode) return;
logger.Info("启动KDS登录窗口");
logger.Info("当前KDS版本{0}", Application.ProductVersion);
//登录信息panel放在屏幕中间
this.loginPanel.Location = new Point((this.Width - this.loginPanel.Width) / 2, (this.Height - this.loginPanel.Height) / 2);
//注册虚拟键盘事件
this.loginKeyboardX1.LoginKeyboardButtonClick += LoginKeyboardX1_LoginKeyboardButtonClick; ;
this.txtUserName.Focus();
//清空提示信息
infoLabel.Text = string.Empty;
//初始化门店信息
InitGlobalStoreInfo();
//显示门店信息
if (Global.Instance.StoreInfo != null)
{
this.storeNoLabel.Text = Global.Instance.StoreInfo.StoreNo;
this.storeNameLabel.Text = Global.Instance.StoreInfo.StoreName;
this.kdsNoLabel.Text = Global.Instance.StoreInfo.PosNo;
}
Task.Factory.StartNew(() =>
{
CheckNewVersion();
//var client = MqttClientUtils.Instance;
});
}
private void CheckNewVersion()
{
try
{
//联机模式下,下载数据
if (Global.Instance.Online)
{
string autoUpdateFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"AutoUpdater.exe");
bool isNewAutoUpdate = false;
//当前内嵌自动更新的版本
string newAutoUpdateVersion = Properties.Resources.AutoUpdateVersion;
if (!File.Exists(autoUpdateFilePath))
{
isNewAutoUpdate = true;
}
else
{
logger.Info("AutoUpdate Version:{0}", newAutoUpdateVersion);
FileVersionInfo info = FileVersionInfo.GetVersionInfo(autoUpdateFilePath);
//检测版本
Version oldVersion = new Version(info.ProductVersion);
Version newVersion = new Version(newAutoUpdateVersion);
if (newVersion > oldVersion)
{
File.Delete(autoUpdateFilePath);
isNewAutoUpdate = true;
}
}
if (isNewAutoUpdate)
{
string installerPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AutoUpdater.exe");
File.WriteAllBytes(installerPath, Properties.Resources.AutoUpdater);
}
//有自动更新程序
if (File.Exists(autoUpdateFilePath) && Global.Instance.StoreInfo != null)
{
//args = new string[] { "373001" , "00008" , "cy2" , "1", "x86" , "1.0.0" , "POSV.exe" };
//判断是否有新版本发布
string tenantId = Global.Instance.StoreInfo.TenantId;
string posNo = Global.Instance.StoreInfo.PosNo;
string appSign = Constant.APP_SIGN;
string versionType = "1";
string terminalType = Constant.TERMINAL_TYPE;
string versionNum = Application.ProductVersion;
string starter = Application.ProductName + ".exe";
string arguments = (tenantId + " " + posNo + " " + appSign + " " + versionType + " " + terminalType + " " + versionNum + " " + starter);
System.Diagnostics.Process.Start("AutoUpdater.exe", arguments);
}
}
}
catch (Exception ex)
{
logger.Error(ex, "检测新版本出现异常.....");
}
}
protected override void AppHookKeyDown(object sender, KeyEventArgs e)
{
switch (e.KeyCode)
{
case Keys.Enter:
this.OnEnterClick(sender, e);
break;
case Keys.Escape:
//this.OnCloseClick(sender, e);
break;
}
}
/// <summary>
/// 初始化storeInfo
/// </summary>
private void InitGlobalStoreInfo()
{
Authc authc;
using (var db = Global.Instance.OpenDataBase)
{
authc = db.FirstOrDefault<Authc>("");
}
if (authc != null)
{
StoreInfo store = new StoreInfo();
store.PosNo = authc.PosNo;
store.StoreId = authc.StoreId;
store.StoreNo = authc.StoreNo;
store.StoreName = authc.StoreName;
store.TenantId = authc.TenantId;
Global.Instance.StoreInfo = store;
}
else
{
logger.Error("未获取Authc组装Global.StoreInfo失败");
}
}
private void LoginKeyboardX1_LoginKeyboardButtonClick(object sender, Component.LoginKeyboardEventArgs e)
{
var keyCode = e.Key;
switch (keyCode)
{
case "clear":
this.ActiveControl.Text = "";
break;
default:
InputSimulatorUtils.SendKey(KeyCodes.Map[keyCode]);
break;
}
}
/// <summary>
/// 登录前校验
/// </summary>
/// <returns></returns>
private bool ValidBeforeLogin()
{
if (string.IsNullOrEmpty(this.txtUserName.Text))
{
this.txtUserName.Focus();
return false;
}
else
{
this.txtUserPwd.Focus();
}
if (string.IsNullOrEmpty(this.txtUserPwd.Text))
{
this.txtUserPwd.Focus();
return false;
}
return true;
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
base.OnFormClosing(e);
KeyboardUtils.Instance.UnRegisterHook(this.AppHookKeyDown);
}
/// <summary>
/// 登录
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected override void OnEnterClick(object sender, EventArgs e)
{
if (!ValidBeforeLogin()) return;
//用户名长度5位默认前补零
var userName = this.txtUserName.Text.PadLeft(5, '0');
if (HttpClientUtils.IsAvailable(ApiType.Business))
{
//在线登录
var loginRes = LoginApi.OperatorLogin(userName, this.txtUserPwd.Text);
if (loginRes.Status == 1)
{
loginRes.Data.WorkerOnlineLogin = true;
Global.Instance.Worker = loginRes.Data;
Global.Instance.Worker.TenantId = Global.Instance.StoreInfo.TenantId;
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
ShowMessage(this.infoLabel, loginRes.Message, true);
}
}
else
{
//离线登录、数据库登录
var loginRes = LoginApi.OperatorLoginByDb(userName, this.txtUserPwd.Text);
if (loginRes.Item1)
{
//离线登录确认
var confirm = new LoginOffLineConfirmForm(loginRes.Item3);
KeyboardUtils.Instance.UnRegisterHook(this.AppHookKeyDown);
if (confirm.ShowDialog() == DialogResult.OK)
{
Global.Instance.Worker = loginRes.Item3;
Global.Instance.Worker.WorkerOnlineLogin = false;
this.DialogResult = DialogResult.OK;
this.Close();
}
else
{
KeyboardUtils.Instance.RegisterHook(this.AppHookKeyDown);
}
}
else
{
ShowMessage(this.infoLabel, loginRes.Item2, true);
}
}
}
private void SystemInitClick(object sender, EventArgs e)
{
TenantResetForm resetForm = new TenantResetForm();
resetForm.ShowDialog();
}
}
}