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.

358 lines
12 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.HttpApi;
using POSV.Component;
using POSV.Utils;
using POSV.Entity;
namespace POSV.ComponentUI
{
[ToolboxItem(true)]
public partial class RegisterUIControl : BaseUserControl
{
private PollCode _pollCode = null;
public RegisterUIControl()
{
InitializeComponent();
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 OnResize(EventArgs e)
{
this.Visible = false;
base.OnResize(e);
this.Visible = true;
}
/// <summary>
/// 按钮操作
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnTouchClick(object sender, Component.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;
}
}
/// <summary>
/// 立即注册
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnTouchRegisterClick(object sender, Component.TouchEventArgs 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.lblInfo.Text = "变更输入信息,请点击确认获取门店信息";
return;
}
this.lblInfo.Text = "提交认证请求......";
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<Authc>().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<Apis>("where apiType = @0", ApiType.Business.ToString()).FirstOrDefault();
//获取会员AppKey和AppSecret
var cards = db.Query<Apis>("where apiType = @0", ApiType.Card.ToString()).FirstOrDefault();
//获取外卖平台的AppKey和AppSecret
var waimais = db.Query<Apis>("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.lblInfo.Text = response.Item2;
//清理数据,初始化系统
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 UIRegistedEventArgs(newAuthc));
isSuccess = true;
}
else
{
this.lblInfo.Text = response.Item2;
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);
}
}
}
/// <summary>
/// 注册校验
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void OnTouchRegisterCheckClick(object sender, Component.TouchEventArgs e)
{
ValidatePollCode();
}
/// <summary>
/// 调用开放平台获取门店注册信息
/// </summary>
/// <returns></returns>
private void ValidatePollCode()
{
bool isSuccess = false;
try
{
if (string.IsNullOrEmpty(txtTenantNo.Text.Trim()))
{
this.lblInfo.Text = "请输入企业编码";
this.txtTenantNo.Focus();
return;
}
if (string.IsNullOrEmpty(txtAuthCode.Text.Trim()))
{
this.lblInfo.Text = "请输入提取密码";
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.lblInfo.Text = "门店编码和名称审核后,请点击立即注册";
//this.ButtonOK.Visible = true;
//this.ButtonOK.Focus();
isSuccess = true;
}
else
{
//this.storePanel.Visible = false;
//this.ButtonOK.Visible = false;
this.lblInfo.Text = response.Item2;
isSuccess = false;
}
}
catch (Exception ex)
{
isSuccess = false;
this._pollCode = null;
LOGGER.Error(ex, "POS注册码验证异常");
this.lblInfo.Text = "POS注册码验证异常";
}
finally
{
//门店POS注册码验证通过
if (isSuccess)
{
}
}
}
/// <summary>
/// 注册成功事件触发
/// </summary>
public event UIRegistedEventHandler Registed;
protected virtual void OnRegisted(UIRegistedEventArgs e)
{
Registed?.Invoke(this, e);
}
private void OnInputEnterClick(object sender, EnterEventArg e)
{
ValidatePollCode();
}
}
public delegate void UIRegistedEventHandler(object sender, UIRegistedEventArgs e);
public class UIRegistedEventArgs : EventArgs
{
public readonly Authc Authc;
public UIRegistedEventArgs(Authc authc)
{
this.Authc = authc;
}
}
}