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.Utils; using POSV.Entity; using NPoco; using POSV.HttpApi; using POS.Language.Language; namespace POSV.Component { [ToolboxItem(true)] public partial class RegisterControl : BaseUserControl { private PollCode _pollCode = null; public RegisterControl() { InitializeComponent(); //注册键盘事件 this.keyboardX1.TouchAfter += OnKeyboartTouchAfter; this.lblStoreNo.Text = string.Empty; this.lblStoreName.Text = string.Empty; } protected override void OnLoad(EventArgs e) { SetStyle(ControlStyles.Selectable , true); base.OnLoad(e); if (this.DesignMode) return; this.txtTenantNo.Multiline = false; this.txtAuthCode.Multiline = false; this.lblStoreNo.Text = string.Empty; this.lblStoreName.Text = string.Empty; } public override bool Focused { get { if (this.ActiveControl == null) { this.ActiveControl = this.txtTenantNo; } return this.ActiveControl.Focused; } } //protected override void OnPaint(PaintEventArgs e) //{ // base.OnPaint(e); // //if (Focused) // //{ // // Rectangle rect = ClientRectangle; // // rect.Inflate(-1 , -1); // // ControlPaint.DrawBorder( // // e.Graphics , ClientRectangle , // // Color.White , // // ButtonBorderStyle.Dashed); // //} //} protected override void OnResize(EventArgs e) { this.Visible = false; base.OnResize(e); this.Visible = true; } private void OnKeyboartTouchAfter(object sender , KeyboardEventArgs e) { switch (e.KeyCode) { case "clear": //如果当前焦点控件是输入框 if (this.ActiveControl is NumericTextBox) { var activeControl = this.ActiveControl as NumericTextBox; activeControl.Text = string.Empty; } break; case "accept": //验证通过 { ValidatePollCode(); } break; default: { InputSimulatorUtils.SendKey(KeyCodes.Map[e.KeyCode]); } break; } } /// /// 调用开放平台获取门店注册信息 /// /// private void ValidatePollCode() { bool isSuccess = false; try { if (string.IsNullOrEmpty(txtTenantNo.Text.Trim())) { this.ShowMessage(this.lblInfo , LangProxy.ToLang("请输入企业编码") , false); this.txtTenantNo.Focus(); return; } if (string.IsNullOrEmpty(txtAuthCode.Text.Trim())) { this.ShowMessage(this.lblInfo , LangProxy.ToLang("请输入提取密码") , false); this.txtAuthCode.Focus(); return; } //调用业务系统开放平台 var response = PosApi.ValidatePollCode(txtTenantNo.Text.Trim() , txtAuthCode.Text.Trim()); //返回成功 if (response.Item1) { this._pollCode = response.Item3; this.lblStoreNo.Text = this._pollCode.Response.StoreNo; this.lblStoreName.Text = this._pollCode.Response.StoreName; this.storePanel.Visible = true; this.ShowMessage(this.lblInfo , LangProxy.ToLang("门店编码和名称审核后,请点击立即注册") , false); this.ButtonOK.Visible = true; this.ButtonOK.Focus(); isSuccess = true; } else { this.storePanel.Visible = false; this.ButtonOK.Visible = false; this.ShowMessage(this.lblInfo , response.Item2 , true); isSuccess = false; } } catch (Exception ex) { isSuccess = false; this._pollCode = null; LOGGER.Error(ex , "POS注册码验证异常"); this.ShowMessage(this.lblInfo , LangProxy.ToLang("POS注册码验证异常") , true); } finally { //门店POS注册码验证通过 if (isSuccess) { } } } /// /// 注册成功事件触发 /// public event RegistedEventHandler Registed; protected virtual void OnRegisted(RegistedEventArgs e) { Registed?.Invoke(this , e); } private void OnButtonOkClick(object sender , EventArgs e) { bool isSuccess = false; Authc newAuthc = null; try { if(this._pollCode != null) { var tenantNo = this.txtTenantNo.Text.Trim(); var authCode = this.txtAuthCode.Text.Trim(); if (!tenantNo.Equals(this._pollCode.TenantId) || !authCode.Equals(this._pollCode.AuthCode)) { this.ShowMessage(this.lblInfo , LangProxy.ToLang("变更输入信息,请点击确认获取门店信息") , true); return; } this.ShowMessage(this.lblInfo , LangProxy.ToLang("提交认证请求......") , false); newAuthc = new Authc(); newAuthc.Id = IdWorkerUtils.Instance.NextId(); //硬盘序列号 newAuthc.DiskSerialNumber = DeviceUtils.Instance.DiskID; newAuthc.CpuSerialNumber = DeviceUtils.Instance.CpuID; newAuthc.CompterName = DeviceUtils.Instance.ComputerName; newAuthc.MacAddress = DeviceUtils.Instance.MacAddress; var response = PosApi.PosRegister(newAuthc , this._pollCode); //POS注册成功 if (response.Item1) { //租户是否变更 var isTenantChanged = false; //门店是否变更 var isStoreChanged = false; lock (Global.Instance.SyncLock) { using (var db = Global.Instance.OpenDataBase) { var _authc = db.Query().FirstOrDefault(); if (_authc != null) { var oldAuthc = _authc; //如果存在数据 newAuthc.Id = oldAuthc.Id; //租户是否变更 isTenantChanged = !(newAuthc.TenantId.Equals(oldAuthc.TenantId)); //门店是否变更 isStoreChanged = !(newAuthc.StoreId.Equals(oldAuthc.StoreId)); } //获取门店AppKey和AppSecret var business = db.Query("where apiType = @0" , ApiType.Business.ToString()).FirstOrDefault(); //获取会员AppKey和AppSecret var cards = db.Query("where apiType = @0" , ApiType.Card.ToString()).FirstOrDefault(); //获取外卖平台的AppKey和AppSecret var waimais = db.Query("where apiType = @0", ApiType.WaiMai.ToString()).FirstOrDefault(); using (var transaction = db.GetTransaction()) { db.Save(newAuthc); if (business != null) { business.TenantId = newAuthc.TenantId; business.ApiType = ApiType.Business.ToString(); business.AppKey = this._pollCode.Response.AppKey; business.AppSecret = this._pollCode.Response.AppSecret; db.Update(business); } if(cards != null && this._pollCode.CardAuth != null) { cards.TenantId = newAuthc.TenantId; cards.ApiType = ApiType.Card.ToString(); cards.AppKey = this._pollCode.CardAuth.AppKey; cards.AppSecret = this._pollCode.CardAuth.AppSecret; db.Update(cards); } if (waimais!=null) { waimais.TenantId = newAuthc.TenantId; waimais.ApiType = ApiType.WaiMai.ToString(); waimais.AppKey = this._pollCode.Response.AppKey; waimais.AppSecret = this._pollCode.Response.AppSecret; db.Update(waimais); } transaction.Complete(); } } } this.ShowMessage(this.lblInfo , response.Item2 , false); //清理数据,初始化系统 InitUtils.Instance.Init(newAuthc.TenantId , newAuthc.StoreId , isTenantChanged , isStoreChanged); //刷新Http请求的AppKey,避免调用错误 OpenApiUtils.Instance.Refresh(); //清理输入的内容 this.txtTenantNo.Text = string.Empty; this.txtAuthCode.Text = string.Empty; //出发注册成功通知事件 this.OnRegisted(new RegistedEventArgs(newAuthc)); isSuccess = true; } else { this.ShowMessage(this.lblInfo , response.Item2 , true); isSuccess = false; } } else { isSuccess = false; } } catch (Exception ex) { isSuccess = false; LOGGER.Error(ex , "保存POS注册信息异常"); } finally { //POS注册成功,通知会员系统 if (isSuccess && newAuthc != null) { var mr = PosApi.MemberRegister(this._pollCode , newAuthc); LOGGER.Info("通知会员系统POS注册结果<{0},{1}>" , mr.Item1 , mr.Item2); } } } private void OnInputEnterClick(object sender , EnterEventArg e) { ValidatePollCode(); } } public delegate void RegistedEventHandler(object sender , RegistedEventArgs e); public class RegistedEventArgs : EventArgs { public readonly Authc Authc; public RegistedEventArgs(Authc authc) { this.Authc = authc; } } }