/// <summary> /// 设置用户信息 /// </summary> /// <param name="userInfo"></param> public void setUserInfo(UserInfo userInfo, UserLocalInfo userLocalInfo) { this.userInfo = userInfo; this.userLocalInfo = userLocalInfo; initializeConfig(); initializeWidget(); }
/// <summary> /// 初始化配置 /// </summary> void initializeConfig() { if (checkFirstUse()) { MyConfig.ConfigFile configFile = MyConfig.readConfig(MyConfig.CONFIG_FILE_PATH); serverURI = (string)configFile.TableSync[MyConfig.ConfigFile.Sync.KEY_SERVER_URI]; serverPort = int.Parse(configFile.TableSync[MyConfig.ConfigFile.Sync.KEY_SERVER_PORT].ToString()); saveLoginConfig(); } else { MyConfig.ConfigFile configFile = MyConfig.readConfig(MyConfig.CONFIG_FILE_PATH); isRememberPassword = (bool)configFile.TableLogin[MyConfig.ConfigFile.Login.KEY_REMEMBER_PASSWORD]; isAutoLogin = (bool)configFile.TableLogin[MyConfig.ConfigFile.Login.KEY_AUTO_LOGIN]; serverURI = configFile.TableSync[MyConfig.ConfigFile.Sync.KEY_SERVER_URI].ToString(); serverPort = int.Parse(configFile.TableSync[MyConfig.ConfigFile.Sync.KEY_SERVER_PORT].ToString()); //textBox_password.Text = configFile.TableLogin[MyConfig.ConfigFile.Login.KEY_LAST_LOGIN_PASSWORD].ToString(); comboBox_user.Text = configFile.TableLogin[MyConfig.ConfigFile.Login.KEY_LAST_LOGIN_ACCOUNT].ToString(); UserLocalInfo userLocalInfo = MyConfig.getUserLocalInfo(configFile.TableLogin[MyConfig.ConfigFile.Login.KEY_LAST_LOGIN_ACCOUNT].ToString()); if (userLocalInfo != null) { if (userLocalInfo.IsRemeberPassword) { textBox_password.Text = userLocalInfo.Password; } } } }
/// <summary> /// 创建或修改用户本地信息 /// </summary> /// <param name="path"></param> /// <param name="userLocalInfo"></param> public static void createOrModifyUserLocalInfo(UserLocalInfo userLocalInfo) { if (!Directory.Exists(PATH_USER + "/" + userLocalInfo.UserId)) { Directory.CreateDirectory(PATH_USER + "/" + userLocalInfo.UserId); } if (!Directory.Exists(PATH_USER + "/" + userLocalInfo.UserId + "/" + NAME_FOLDER_SYNC)) { Directory.CreateDirectory(PATH_USER + "/" + userLocalInfo.UserId + "/" + NAME_FOLDER_SYNC); } UserLocalInfo user_localInfo = userLocalInfo; /* 设置起始默认同步目录 */ UserLocalInfo temp = getUserLocalInfo(userLocalInfo.UserId); if (temp == null) { user_localInfo.SyncPath = PATH_USER + "/" + user_localInfo.UserId + "/" + NAME_FOLDER_SYNC; } else if (temp.SyncPath == null || (!Directory.Exists(temp.SyncPath))) { user_localInfo.SyncPath = Path.GetFullPath(PATH_USER + "/" + user_localInfo.UserId + "/" + NAME_FOLDER_SYNC); } StreamWriter infoWriter = new StreamWriter(PATH_USER + "/" + userLocalInfo.UserId + "/" + NAME_USER_INFO, false, Encoding.Default); infoWriter.Write(Int32Dec64Convert.encryptSerialToBase64Code(JsonHelper.getSerializeString(user_localInfo), PASSWORD_USER_FILE_ENCRYPTION, 0x400)); infoWriter.Close(); }
/// <summary> /// 保存用户本地信息 /// </summary> void saveUserLocalInfo() { /* 用户本地信息 */ UserLocalInfo userLocalInfo = MyConfig.getUserLocalInfo(comboBox_user.Text); if (userLocalInfo == null) { userLocalInfo = new UserLocalInfo(); userLocalInfo.UserId = User_Info.UserID; userLocalInfo.SyncPath = Path.GetFullPath(MyConfig.PATH_USER + "/" + userLocalInfo.UserId + "/sync/"); //MyConfig.createOrModifyUserDirectory(userLocalInfo.UserId, User_Info); //MyConfig.createOrModifyUserLocalInfo(userLocalInfo); } userLocalInfo.IsRemeberPassword = checkBox_rememberPW.Checked; if (checkBox_rememberPW.Checked) { userLocalInfo.Password = textBox_password.Text; } else { userLocalInfo.Password = ""; } MyConfig.createOrModifyUserLocalInfo(userLocalInfo); }
/// <summary> /// 导航去主窗体 /// </summary> void navigateToMainWindow(UserInfo user_info) { //UserInfo userManiWindow = new UserInfo(); saveLoginConfig(); saveUserLocalInfo(); mw = new MainWindow(); UserLocalInfo userLocalInfo = MyConfig.getUserLocalInfo(user_info.UserID); mw.setUserInfo(user_info, userLocalInfo); mw.form_ParentLogin = this; this.Hide(); mw.Show(); //this.Show(); }
/// <summary> /// 改变账户时密码框清空 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void comboBox_user_Click(object sender, EventArgs e) { UserLocalInfo userLocalInfo = MyConfig.getUserLocalInfo(comboBox_user.Text); if (userLocalInfo != null) { if (userLocalInfo.IsRemeberPassword) { textBox_password.Text = userLocalInfo.Password; } else { textBox_password.Text = ""; } } else { textBox_password.Text = ""; } }
/// <summary> /// 登录按钮点击事件 /// </summary> /// <param name="obj"></param> /// <param name="ea"></param> void btn_login_Click(object obj, EventArgs ea) { string user = comboBox_user.Text; // matching password string password_input = textBox_password.Text; Order order = new Order(); UserInfo userInfo = new UserInfo(); userInfo.UserID = user; userID = user; userInfo.Password = password_input; Password = password_input; userInfo.Order = order.OrderCodes["login"]; button_login.Enabled = false; LoadingForm loadingForm = new LoadingForm(); loadingForm.setInfo(userID, Password, serverURI, serverPort); DialogResult dialogResult = loadingForm.ShowDialog(); if (dialogResult.Equals(DialogResult.OK)) { label_errorInfo.Visible = false; //loadingForm.Visible = false; MyConfig.writeUserTrack(comboBox_user.Text); User_Info = loadingForm.User_Info; UserLocalInfo User_LocalInfo = MyConfig.getUserLocalInfo(User_Info.UserID); if (!Directory.Exists(User_LocalInfo.SyncPath)) { User_LocalInfo.SyncPath = MyConfig.PATH_USER + "/" + User_Info.UserID + "/" + MyConfig.NAME_FOLDER_SYNC; } navigateToMainWindow(User_Info); this.Hide(); loadingForm.Dispose(); } else if (dialogResult.Equals(DialogResult.No)) { User_Info = loadingForm.User_Info; ErrorCode errorCode = new ErrorCode(); label_errorInfo.Text = "登录失败,原因: " + errorCode.TableErrorCode[User_Info.error_code]; //label_errorInfo.Text = "登录失败,错误码:" + User_Info.error_code.ToString(); label_errorInfo.Visible = true; button_login.Enabled = true; } else { button_login.Enabled = true; } if (loadingForm != null) { loadingForm.Close(); } /* 测试主窗体 * * User_Info.UserID = comboBox_user.Text; * User_Info.UserName = "******"; * User_Info.Password = textBox_password.Text; * User_Info.SyncServerAddress = "http://192.168.222.128/helo"; * User_Info.FileKey = MyConfig.PASSWORD_FILE_ENCRYPTION; * UserLocalInfo User_LocalInfo = MyConfig.getUserLocalInfo(User_Info.UserID); * if (User_LocalInfo == null) * { * User_LocalInfo = new UserLocalInfo(); * User_LocalInfo.SyncPath = @"./sync/"; * } * User_LocalInfo.UserId = User_Info.UserID; */ MyConfig.writeUserTrack(comboBox_user.Text); navigateToMainWindow(User_Info); }
/// <summary> /// 设置用户信息 /// </summary> /// <param name="userLocalInfo"></param> public void setInfo(UserLocalInfo userLocalInfo) { User_LocalInfo = userLocalInfo; initializeConfig(); initializeWidget(); }