using System;
using System.Configuration;
using System.Linq;
using System.Windows.Forms;
using AutoUpdaterDotNET;
using DevExpress.XtraEditors;
using Gs.DevApp.Entity;
using Gs.DevApp.Properties;
using Gs.DevApp.ToolBox;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace Gs.DevApp.DevFrm
{
public partial class FrmLogin : XtraForm
{
public FrmLogin()
{
InitializeComponent();
if (ConfigurationManager.AppSettings["IsAutoUpdater"] == "1")
_autoUpdate();
else
getTree();
Text = lbVersion.Text =
ConfigurationManager.AppSettings["ProductName"];
btnLogin.Click += BtnLogin_Click;
btnCancel.Click += BtnCancel_Click;
}
///
/// 读取组织
///
private void getTree()
{
var pgq = new PageQueryModel(1, 999999, "FID", "asc", "",
" and IS_STATUS=1");
var json = JsonConvert.SerializeObject(pgq);
try
{
var strReturn = UtilityHelper.HttpPost("",
"Organization/GetListPage", json);
var rtn = UtilityHelper.ReturnToTablePage(strReturn);
var dt = rtn.rtnData.list;
comOrg.DataSource = dt;
comOrg.DisplayMember = "name";
comOrg.ValueMember = "guid";
comOrg.SelectedIndex = 0;
_getRemember();
}
catch (Exception ex)
{
MsgHelper.Warning("提示:" + ex.Message);
Close();
Application.Exit();
}
}
private void BtnCancel_Click(object sender, EventArgs e)
{
if (MsgHelper.AskQuestion("确定要退出系统吗?")) Application.Exit();
}
private void BtnLogin_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtUser.Text.Trim()))
{
MsgHelper.Warning("登录账号不能为空!");
txtUser.Focus();
return;
}
if (string.IsNullOrEmpty(txtPwd.Text.Trim()))
{
MsgHelper.Warning("密码不能为空!");
txtUser.Focus();
return;
}
if (comOrg.SelectedValue == null ||
string.IsNullOrEmpty(comOrg.SelectedValue.ToString().Trim()))
{
MsgHelper.Warning("组织机构不能为空!");
txtUser.Focus();
return;
}
var _obj = new
{
accountPwd = txtPwd.Text.Trim(),
accountNo = txtUser.Text.Trim(),
orgGuid = comOrg.SelectedValue.ToString().Trim()
};
try
{
var strJson = UtilityHelper.HttpPost("", "User/UserLogin",
JsonConvert.SerializeObject(_obj));
var _rtn = UtilityHelper.ReturnToDynamic(strJson);
JObject _login = _rtn.rtnData;
if (_rtn.rtnCode > 0)
{
Settings.Default.userName = ckRemember.Checked
? txtUser.Text.Trim()
: "";
Settings.Default.userPwd =
ckRemember.Checked ? txtPwd.Text.Trim() : "";
Settings.Default.orgGuid = ckRemember.Checked
? comOrg.SelectedValue.ToString()
: "";
Settings.Default.remember = ckRemember.Checked;
Settings.Default.Save();
LoginInfoModel.CurrentUser.LoginUserGuid =
_login["loginGuid"].ToString();
LoginInfoModel.CurrentUser.LoginOrgGuid =
_login["loginOrgGuid"].ToString();
LoginInfoModel.CurrentUser.LoginTime =
DateTime.Now.ToString("yyyy-MM-dd hh:mm");
DialogResult = DialogResult.OK;
}
else
{
MsgHelper.Warning("提示:" + _rtn.rtnMsg);
DialogResult = DialogResult.None;
}
}
catch (Exception ex)
{
DialogResult = DialogResult.Cancel;
MsgHelper.Warning("提示:" + ex.Message);
}
}
///
/// 读取记住密码
///
private void _getRemember()
{
try
{
if (Settings.Default.remember)
{
txtUser.Text = Settings.Default.userName;
txtPwd.Text = Settings.Default.userPwd;
comOrg.SelectedValue = Settings.Default.orgGuid;
ckRemember.Checked = true;
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void _autoUpdate()
{
var _version = ConfigurationManager.AppSettings["Version"];
AutoUpdater.InstalledVersion = new Version(_version);
AutoUpdater.Start(
ConfigurationManager.AppSettings["AutoUpdaterXml"]);
AutoUpdater.CheckForUpdateEvent += AutoUpdater_CheckForUpdateEvent;
}
private void AutoUpdater_CheckForUpdateEvent(UpdateInfoEventArgs args)
{
if (args == null || string.IsNullOrEmpty(args.DownloadURL))
{
MsgHelper.ShowError("读取自动更新失败,无法登录,请联系管理员!");
Close();
Application.Exit();
return;
}
if (!args.IsUpdateAvailable)
{
getTree();
return;
}
var _strMsg =
string.Format(
$@"有新版本 {args.CurrentVersion} 可用,您使用的是 {args.InstalledVersion}版本,这是必需的更新,按“是(Y)”开始更新应用程序。");
if (!MsgHelper.AskQuestion(_strMsg))
{
Close();
Application.Exit();
return;
}
try
{
if (AutoUpdater.DownloadUpdate(args))
{
var config =
ConfigurationManager.OpenExeConfiguration(
ConfigurationUserLevel.None);
if (config != null)
{
var appSettings =
(AppSettingsSection)config.GetSection(
"appSettings");
if (appSettings.Settings.AllKeys.Contains("Version"))
appSettings.Settings["Version"].Value =
args.CurrentVersion;
else
appSettings.Settings.Add("Version",
args.CurrentVersion);
config.Save(ConfigurationSaveMode.Modified);
ConfigurationManager.RefreshSection("appSettings");
}
Application.Exit();
}
else
{
MsgHelper.ShowError("读取自动更新失败,无法登录,请联系管理员!");
Close();
Application.Exit();
}
}
catch (Exception exception)
{
MsgHelper.ShowError(exception.Message + ":" +
exception.GetType());
Close();
Application.Exit();
}
}
}
}