using JwKdsV.Core; using JwKdsV.Core.HttpApi; using JwKdsV.Core.Utils; using JwKdsV.Entity; using JwKdsV.Entity.Common; using JwKdsV.Entity.Tenant; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace JwKdsV { public partial class TenantInitForm : BaseForm { private Apis _apis = null; public enum TenantInitStep { 获取验证, 立即注册 } public TenantInitForm() { InitializeComponent(); this.Text = string.Format("巨为厨显认证({0})", Application.ProductVersion); KeyboardUtils.Instance.RegisterHook(this.AppHookKeyDown); } protected override void OnLoad(EventArgs e) { base.OnLoad(e); this.touchButtonX1.Text1 = TenantInitStep.获取验证.ToString(); this.panelStoreInfo.Visible = false; this.txtTenantId.Focus(); this.txtTenantId.Select(); } 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; } } protected override void OnFormClosing(FormClosingEventArgs e) { base.OnFormClosing(e); KeyboardUtils.Instance.UnRegisterHook(this.AppHookKeyDown); } private bool AcceptValid() { TenantInitStep step = (TenantInitStep)Enum.Parse(typeof(TenantInitStep), this.touchButtonX1.Text1); switch (step) { case TenantInitStep.获取验证: if (string.IsNullOrEmpty(this.txtTenantId.Text)) { this.txtTenantId.SelectAll(); ShowMessage(this.infoLabel, "请录入企业编码", true); return false; } else { this.txtPwd.Focus(); } if (string.IsNullOrEmpty(this.txtPwd.Text)) { this.txtPwd.Focus(); return false; } return true; case TenantInitStep.立即注册: if (string.IsNullOrEmpty(this.txtPwd.Text)) { ShowMessage(this.infoLabel, "请录入认证密码", true); return false; } if (_apis == null) { ShowMessage(this.infoLabel, "请先获取验证", true); return false; } return true; } return false; } protected override void OnEnterClick(object sender, EventArgs e) { base.OnEnterClick(sender, e); OnAcceptClick(sender, e); } private void OnAcceptClick(object sender, EventArgs e) { if (!AcceptValid()) return; TenantInitStep step = (TenantInitStep)Enum.Parse(typeof(TenantInitStep), this.touchButtonX1.Text1); switch (step) { case TenantInitStep.获取验证: var response = TenantApi.QueryPosValid(this.txtTenantId.Text, this.txtPwd.Text); if (response.Status == 1) { //获取成功 _apis = new Apis(); _apis.TenantId = this.txtTenantId.Text; _apis.AppKey = response.Data.PosValid.AppKey; _apis.AppSecret = response.Data.PosValid.AppSecret; //展示信息 this.labelStoreNo.Text = response.Data.PosValid.StoreNo; this.labelStoreName.Text = response.Data.PosValid.StoreName; this.panelStoreInfo.Visible = true; ShowMessage(this.infoLabel, "验证成功,请注册", false); this.touchButtonX1.Text1 = TenantInitStep.立即注册.ToString(); } else { //失败 _apis = null; ShowMessage(this.infoLabel, response.Message, true); } break; case TenantInitStep.立即注册: var device = Global.Instance.DeviceInfo; var response2 = TenantApi.PosRegister(this.txtPwd.Text, device.ComputerName, device.Mac, device.Disk, device.Cpu, _apis); if (response2.Status == 1) { //注册成功 Authc authc = new Authc(); authc.Id = IdWorkerUtils.Instance.NextId(); authc.TenantId = _apis.TenantId; authc.CompterName = device.ComputerName; authc.MacAddress = device.Mac; authc.DiskSerialNumber = device.Disk; authc.CpuSerialNumber = device.Cpu; authc.StoreId = response2.Data.StoreId; authc.StoreNo = response2.Data.StoreNo; authc.StoreName = response2.Data.StoreName; authc.PosNo = response2.Data.PosNo; lock (Global.Instance.SyncLock) { using (var db = Global.Instance.OpenDataBase) { var apis = db.Query("where apiType = @0", "Business").FirstOrDefault(); using (var transaction = db.GetTransaction()) { db.Delete("where 1=1"); db.Save(authc); if(apis != null) { apis.TenantId = _apis.TenantId; apis.AppKey = _apis.AppKey; apis.AppSecret = _apis.AppSecret; apis.ModifyDate = DateTime.Now; db.Update(apis); } transaction.Complete(); } } } //刷新当前的appKey OpenApiUtils.Instance.Refresh(); ShowMessage(this.infoLabel, "注册成功!", false); this.DialogResult = DialogResult.OK; this.Close(); } else { //失败 ShowMessage(this.infoLabel, response2.Message, true); } break; } } private void OnKeyboardButtonClick(object sender, Component.LoginKeyboardEventArgs e) { var keyCode = e.Key; switch (keyCode) { case "clear": this.ActiveControl.Text = ""; break; default: InputSimulatorUtils.SendKey(KeyCodes.Map[keyCode]); break; } } } }