public LoginForm() { InitializeComponent(); //密码格式为● passwordBox.PasswordChar = '●'; //加载用户数据 userData = DataUitls.loadData(); //写入输入框 if (userData.isSaveUser) { rememberUser.Checked = userData.isSaveUser; usernameBox.Text = userData.user.username; passwordBox.Text = userData.user.password; } }
/// <summary> /// 登录按钮事件 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void loginButton_Click(object sender, EventArgs e) { lockButton(); bool isSave = rememberUser.Checked; string username = usernameBox.Text.Trim(); string password = passwordBox.Text.Trim(); //输入框不能为空 if (username.Equals("") || password.Equals("")) { addAlter(EnumExtend.GetDisplayText(LoginEnum.NULLINPUT), CxFlatAlertBox.AlertType.Error); unlockButton(); return; } //用户名格式错误 if (!UserUitls.IsOkUsername(username)) { addAlter(EnumExtend.GetDisplayText(LoginEnum.ERRORURN), CxFlatAlertBox.AlertType.Error); unlockButton(); return; } //密码格式错误 if (!UserUitls.IsOkPassword(password)) { addAlter(EnumExtend.GetDisplayText(LoginEnum.ERRORPWD), CxFlatAlertBox.AlertType.Error); unlockButton(); return; } User user = new User(); user.username = username; user.password = password; //记住密码则写入数据 if (isSave) { userData.isSaveUser = isSave; userData.user = user; DataUitls.saveData(userData); } else { userData.isSaveUser = false; userData.user = new User(); DataUitls.saveData(userData); } //发送HTTP请求访问服务器 try { string url = Resources.Server + Resources.LoginUrl; string data = JsonConvert.SerializeObject(user); string response = HttpUitls.POST(url, data); OkeResult <LoginInfo> okeResult = JsonConvert.DeserializeObject <OkeResult <LoginInfo> >(response); if (okeResult.success) { addAlter(EnumExtend.GetDisplayText(LoginEnum.SUCC_LOGIN), CxFlatAlertBox.AlertType.Success); LoginInfo.CurrentUser.sessionId = okeResult.data.sessionId; LoginInfo.CurrentUser.data = okeResult.data.data; timer.Stop(); timer.Tick += formClose_Tick; timer.Start(); } else { addAlter(EnumExtend.GetDisplayText(LoginEnum.FAIL_LOGIN), CxFlatAlertBox.AlertType.Error); unlockButton(); } } catch (Exception) { addAlter(Resources.ExceptionTip, CxFlatAlertBox.AlertType.Error); unlockButton(); } }