using DevExpress.XtraEditors;
using HStation.RevitDev.RevitDataExport.Common;
using System;
using System.Net.Http;
using System.Text;
using Timer = System.Windows.Forms.Timer;
namespace HStation.RevitDev.RevitDataExport
{
public partial class SmsLoginCtrl : DevExpress.XtraEditors.XtraUserControl, ILogin
{
public SmsLoginCtrl()
{
InitializeComponent();
// this.layoutControl1.SetupLayoutControl();
}
///
/// 登录开始事件
///
public event Action LoginStartEvent;
///
/// 登录取消事件
///
public event Action LoginCancelEvent;
///
/// 登录完成事件
///
public event Action LoginEndEvet;
private bool _isInitialized = false;//初始化
private const string _template = "hzkw_sms_template";//sms登录模板
///
/// 初始化
///
public void Initial()
{
if (_isInitialized)
{
return;
}
_isInitialized = true;
}
//手机号验证
private bool IsValidMobileNumber(string mobile)
{
var regex = new System.Text.RegularExpressions.Regex(@"^1\d{10}$");
return regex.IsMatch(mobile);
}
//发送验证码
private void btnSendCode_Click(object sender, EventArgs e)
{
var Software = GlobalResource.ConfigSettingModel.Software;
this.dxErrorProvider1.ClearErrors();
var mobile = this.txtMobileNumber.Text.Trim();
if (!IsValidMobileNumber(mobile))
{
XtraMessageBox.Show("手机号有误!");
return;
}
using (var client = new HttpClient())
{
var res = client.GetAsync(GlobalResource.ConfigSettingModel.ApiUrl + $"/Auth/Tool/SMS/SendCode@V1.0?TemplateCode={_template}&MobileNumber={mobile}&Software={Software}")
.Result.Content.ReadAsStringAsync().Result;
var result = JsonHelper.ToObject(res);
if (result.Code == 0)
{
this.labSendCode.Enabled = false;
StartCountdown();
}
else
{
XtraMessageBox.Show(result.Message);
this.labSendCode.Enabled = true;
}
}
}
private void StartCountdown()
{
int countdown = 60;
Timer timer = new Timer();
timer.Interval = 1000; // 1秒
timer.Tick += (s, e) =>
{
countdown--;
if (countdown > 0)
{
this.labSendCode.Text = $"{countdown}秒后重新发送";
}
else
{
timer.Stop();
this.labSendCode.Text = "发送验证码";
this.labSendCode.Enabled = true;
}
};
timer.Start();
}
//取消登录
private void btnCancel_Click(object sender, EventArgs e)
{
this.LoginCancelEvent?.Invoke();
}
//登录
private void btnLogin_Click(object sender, EventArgs e)
{
this.dxErrorProvider1.ClearErrors();
if (string.IsNullOrEmpty(this.txtCode.Text.Trim()))
{
this.dxErrorProvider1.SetError(this.txtCode, "请输入验证码");
return;
}
this.LoginStartEvent?.Invoke();
var loginModel = new
{
TemplateCode = _template,
MobileNumber = this.txtMobileNumber.Text.Trim(),
Software = GlobalResource.ConfigSettingModel.Software,
ValidCode = this.txtCode.Text.Trim(),
};
using (var client = new HttpClient())
{
var content = new StringContent(JsonHelper.ToJson(loginModel), Encoding.UTF8, "application/json");
var res = client.PostAsync(GlobalResource.ConfigSettingModel.ApiUrl + "/Auth/User/Login/LoginSoftwareStandardBySmsAccount@V1.0", content)
.Result.Content.ReadAsStringAsync().Result;
var result = JsonHelper.ToObject(res);
this.LoginEndEvet?.Invoke(result, res);
}
}
}
}