using DevExpress.XtraEditors;
using HStation.RevitDev.RevitDataExport.Common;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Yw;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox;
namespace HStation.RevitDev.RevitDataExport
{
public partial class SystemLoginCtrl : DevExpress.XtraEditors.XtraUserControl, ILogin
{
public SystemLoginCtrl()
{
InitializeComponent();
// this.layoutControl1.SetupLayoutControl();
}
///
/// 登录开始事件
///
public event Action LoginStartEvent;
///
/// 登录取消事件
///
public event Action LoginCancelEvent;
///
/// 登录完成事件
///
public event Action LoginEndEvet;
private bool _isInitialized = false;//初始化
///
/// 初始化
///
public void Initial()
{
if (_isInitialized)
{
return;
}
_isInitialized = true;
}
//取消登录
private void btnCancel_Click(object sender, EventArgs e)
{
}
//登录
private void btnLogin_Click(object sender, EventArgs e)
{
Login();
}
//
private void txtLoginName_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
this.txtLoginPwd.Focus();
}
}
//
private void txtLoginPwd_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar == 13)
{
Login();
}
}
//验证
private bool Valid()
{
this.dxErrorProvider1.ClearErrors();
var loginName = this.txtLoginName.Text.Trim();
if (string.IsNullOrEmpty(loginName))
{
this.dxErrorProvider1.SetError(this.txtLoginName, "请输入用户名称");
return false;
}
var loginPwd = this.txtLoginPwd.Text.Trim();
if (string.IsNullOrEmpty(loginPwd))
{
this.dxErrorProvider1.SetError(this.txtLoginPwd, "请输入密码");
return false;
}
return true;
}
//登录
private void Login()
{
if (!Valid())
{
return;
}
// 用户登录
this.LoginStartEvent?.Invoke();
var loginModel = new
{
LoginName = this.txtLoginName.Text.Trim(),
LoginPwd = this.txtLoginPwd.Text.Trim(),
Software = GlobalResource.ConfigSettingModel.Software,
};
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/LoginSoftwareStandardBySystemAccount@V1.0", content)
.Result.Content.ReadAsStringAsync().Result;
var result = JsonHelper.ToObject(res);
this.LoginEndEvet?.Invoke(result, res);
}
}
}
}