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.

448 lines
15 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.Entity;
using POSV.Utils;
using POS.Language.Language;
namespace POSV.Component
{
[ToolboxItem(true)]
public partial class RegisterControlExt : BaseUserControl
{
private PollCode _pollCode = null;
public RegisterControlExt()
{
InitializeComponent();
labelX5.Text = LangProxy.ToLang(labelX5.Text);
labelX6.Text = LangProxy.ToLang(labelX6.Text);
labelX2.Text = LangProxy.ToLang(labelX2.Text);
lblStoreNo.Text = LangProxy.ToLang(lblStoreNo.Text);
labelX4.Text = LangProxy.ToLang(labelX4.Text);
lblStoreName.Text = LangProxy.ToLang(lblStoreName.Text);
touchButtonX5.Text = LangProxy.ToLang(touchButtonX5.Text);
touchButtonX2.Text = LangProxy.ToLang(touchButtonX2.Text);
touchButtonX1.Text = LangProxy.ToLang(touchButtonX1.Text);
txtTenantNo.WatermarkText = LangProxy.ToLang(txtTenantNo.WatermarkText);
txtAuthCode.WatermarkText = LangProxy.ToLang(txtTenantNo.WatermarkText);
this.lblStoreNo.Text = string.Empty;
this.lblStoreName.Text = string.Empty;
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;
// }
//}
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>
/// <returns></returns>
private void ValidatePollCode()
{
bool isSuccess = false;
try
{
if (string.IsNullOrEmpty(txtTenantNo.Text.Trim()))
{
this.lblErrorMessage.Text = LangProxy.ToLang("请输入企业编码");
this.lblErrorMessage.Visible = true;
//this.ShowMessage(this.lblInfo, "请输入企业编码", false);
this.txtTenantNo.Focus();
return;
}
if (string.IsNullOrEmpty(txtAuthCode.Text.Trim()))
{
this.lblErrorMessage.Text = LangProxy.ToLang("请输入提取密码");
this.lblErrorMessage.Visible = true;
//this.ShowMessage(this.lblInfo, "请输入提取密码", 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.lblErrorMessage.Text = LangProxy.ToLang("请点击立即注册");
this.lblErrorMessage.Visible = true;
//this.ShowMessage(this.lblInfo, "门店编码和名称审核后,请点击立即注册", false);
//this.ButtonOK.Visible = true;
//this.ButtonOK.Focus();
isSuccess = true;
}
else
{
this.lblStoreNo.Text = string.Empty;
this.lblStoreName.Text = string.Empty;
//this.ButtonOK.Visible = false;
this.lblErrorMessage.Text = response.Item2;
this.lblErrorMessage.Visible = true;
isSuccess = false;
}
}
catch (Exception ex)
{
isSuccess = false;
this._pollCode = null;
LOGGER.Error(ex, "POS注册码验证异常");
//this.ShowMessage(this.lblInfo, "POS注册码验证异常", true);
}
finally
{
//门店POS注册码验证通过
if (isSuccess)
{
}
}
}
/// <summary>
/// 注册成功事件触发
/// </summary>
public event RegistedEventHandler Registed;
protected virtual void OnRegisted(RegistedEventArgs e)
{
Registed?.Invoke(this, e);
}
private void OnButtonOkClick(object sender, EventArgs e)
{
}
private void OnInputEnterClick(object sender, EnterEventArg e)
{
ValidatePollCode();
}
private void BtnValidatePollCode(object sender, EventArgs e)
{
}
private void OnButtonOkTouchClick(object sender, 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.ShowMessage(this.lblInfo, "变更输入信息,请点击确认获取门店信息", true);
return;
}
//this.ShowMessage(this.lblInfo, "提交认证请求......", 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<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.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 touchButtonX1_TouchClick(object sender, TouchEventArgs e)
{
ValidatePollCode();
}
}
}