package user;
|
|
import java.io.FileInputStream;
|
import java.io.FileOutputStream;
|
import java.io.IOException;
|
import java.util.Properties;
|
|
/**
|
* 用户属性管理类
|
* 负责初始化加载user.properties中的属性值,并提供获取和修改功能
|
*/
|
public class Usrdell {
|
private static final String PROPERTIES_FILE = "user.properties";
|
private static Properties userProperties;
|
|
// 属性键名常量
|
private static final String KEY_EMAIL = "email";
|
private static final String KEY_LANGUAGE = "language";
|
private static final String KEY_LAST_LOGIN_TIME = "lastLoginTime";
|
private static final String KEY_PASSWORD = "password";
|
private static final String KEY_REGISTRATION_TIME = "registrationTime";
|
private static final String KEY_STATUS = "status";
|
private static final String KEY_USER_ID = "userId";
|
private static final String KEY_USER_NAME = "userName";
|
private static final String KEY_REMEMBER_PASSWORD = "rememberPassword";
|
|
/**
|
* 初始化方法:从user.properties文件加载所有属性值到内存
|
*/
|
public static void initialize() {
|
synchronized (Usrdell.class) {
|
userProperties = new Properties();
|
try (FileInputStream input = new FileInputStream(PROPERTIES_FILE)) {
|
userProperties.load(input);
|
System.out.println("用户属性初始化成功!");
|
} catch (IOException e) {
|
System.err.println("初始化失败,文件未找到或读取错误: " + e.getMessage());
|
// 如果文件不存在,创建默认属性
|
userProperties = new Properties();
|
}
|
|
// 确保rememberPassword属性存在,如果不存在则设置为默认值"0"
|
if (!userProperties.containsKey(KEY_REMEMBER_PASSWORD)) {
|
userProperties.setProperty(KEY_REMEMBER_PASSWORD, "0");
|
// 保存到文件
|
try (FileOutputStream output = new FileOutputStream(PROPERTIES_FILE)) {
|
userProperties.store(output, "Updated User Properties");
|
} catch (IOException e) {
|
System.err.println("保存默认rememberPassword属性失败: " + e.getMessage());
|
}
|
}
|
}
|
}
|
|
/**
|
* 初始化并获取所有属性值,返回Usr对象
|
* @return 包含所有属性值的Usr对象
|
*/
|
public static Usr initializeAndGetAll() {
|
if (userProperties == null) {
|
initialize();
|
}
|
|
Usr usr = new Usr();
|
usr.setEmail(getProperty(KEY_EMAIL));
|
usr.setLanguage(getProperty(KEY_LANGUAGE));
|
usr.setLastLoginTime(getProperty(KEY_LAST_LOGIN_TIME));
|
usr.setPassword(getProperty(KEY_PASSWORD));
|
usr.setRegistrationTime(getProperty(KEY_REGISTRATION_TIME));
|
usr.setStatus(getProperty(KEY_STATUS));
|
usr.setUserId(getProperty(KEY_USER_ID));
|
usr.setUserName(getProperty(KEY_USER_NAME));
|
usr.setRememberPassword(getProperty(KEY_REMEMBER_PASSWORD));
|
|
return usr;
|
}
|
|
/**
|
* 获取指定属性的值
|
* @param key 属性键名
|
* @return 属性值,如果不存在返回null
|
*/
|
public static String getProperty(String key) {
|
if (userProperties == null) {
|
initialize();
|
}
|
String value = userProperties.getProperty(key);
|
if (value == null || value.trim().isEmpty() || "-1".equals(value.trim())) {
|
return null;
|
}
|
return value.trim();
|
}
|
|
/**
|
* 更新属性值并保存到文件
|
* @param key 属性键名
|
* @param value 属性值
|
*/
|
public static void updateProperty(String key, String value) {
|
synchronized (Usrdell.class) {
|
if (userProperties == null) {
|
initialize();
|
}
|
|
if (value == null || value.trim().isEmpty()) {
|
userProperties.setProperty(key, "-1");
|
} else {
|
userProperties.setProperty(key, value.trim());
|
}
|
|
try (FileOutputStream output = new FileOutputStream(PROPERTIES_FILE)) {
|
userProperties.store(output, "Updated User Properties");
|
} catch (IOException e) {
|
System.err.println("更新失败,文件写入错误: " + e.getMessage());
|
}
|
}
|
}
|
|
/**
|
* 获取用户邮箱
|
* @return 用户邮箱
|
*/
|
public static String getEmail() {
|
return getProperty(KEY_EMAIL);
|
}
|
|
/**
|
* 设置用户邮箱
|
* @param email 用户邮箱
|
*/
|
public static void setEmail(String email) {
|
updateProperty(KEY_EMAIL, email);
|
}
|
|
/**
|
* 获取用户邮箱
|
* @return 用户邮箱
|
*/
|
public static String getUserEmail() {
|
return getEmail();
|
}
|
|
/**
|
* 获取语言设置
|
* @return 语言设置
|
*/
|
public static String getLanguage() {
|
return getProperty(KEY_LANGUAGE);
|
}
|
|
/**
|
* 设置语言设置
|
* @param language 语言设置
|
*/
|
public static void setLanguage(String language) {
|
updateProperty(KEY_LANGUAGE, language);
|
}
|
|
/**
|
* 获取最后登录时间
|
* @return 最后登录时间
|
*/
|
public static String getLastLoginTime() {
|
return getProperty(KEY_LAST_LOGIN_TIME);
|
}
|
|
/**
|
* 设置最后登录时间
|
* @param lastLoginTime 最后登录时间
|
*/
|
public static void setLastLoginTime(String lastLoginTime) {
|
updateProperty(KEY_LAST_LOGIN_TIME, lastLoginTime);
|
}
|
|
/**
|
* 获取密码
|
* @return 密码
|
*/
|
public static String getPassword() {
|
return getProperty(KEY_PASSWORD);
|
}
|
|
/**
|
* 设置密码
|
* @param password 密码
|
*/
|
public static void setPassword(String password) {
|
updateProperty(KEY_PASSWORD, password);
|
}
|
|
/**
|
* 获取注册时间
|
* @return 注册时间
|
*/
|
public static String getRegistrationTime() {
|
return getProperty(KEY_REGISTRATION_TIME);
|
}
|
|
/**
|
* 设置注册时间
|
* @param registrationTime 注册时间
|
*/
|
public static void setRegistrationTime(String registrationTime) {
|
updateProperty(KEY_REGISTRATION_TIME, registrationTime);
|
}
|
|
/**
|
* 获取用户状态
|
* @return 用户状态
|
*/
|
public static String getStatus() {
|
return getProperty(KEY_STATUS);
|
}
|
|
/**
|
* 设置用户状态
|
* @param status 用户状态
|
*/
|
public static void setStatus(String status) {
|
updateProperty(KEY_STATUS, status);
|
}
|
|
/**
|
* 获取用户ID
|
* @return 用户ID
|
*/
|
public static String getUserId() {
|
return getProperty(KEY_USER_ID);
|
}
|
|
/**
|
* 设置用户ID
|
* @param userId 用户ID
|
*/
|
public static void setUserId(String userId) {
|
updateProperty(KEY_USER_ID, userId);
|
}
|
|
/**
|
* 获取用户名
|
* @return 用户名
|
*/
|
public static String getUserName() {
|
return getProperty(KEY_USER_NAME);
|
}
|
|
/**
|
* 设置用户名
|
* @param userName 用户名
|
*/
|
public static void setUserName(String userName) {
|
updateProperty(KEY_USER_NAME, userName);
|
}
|
|
/**
|
* 获取是否记住登录密码
|
* @return 是否记住登录密码(1表示记住,0表示不记住),如果未设置则返回"0"
|
*/
|
public static String getRememberPassword() {
|
String value = getProperty(KEY_REMEMBER_PASSWORD);
|
return value != null ? value : "0"; // 默认返回"0"表示不记住
|
}
|
|
/**
|
* 设置是否记住登录密码
|
* @param rememberPassword 是否记住登录密码(1表示记住,0表示不记住)
|
*/
|
public static void setRememberPassword(String rememberPassword) {
|
updateProperty(KEY_REMEMBER_PASSWORD, rememberPassword);
|
}
|
|
/**
|
* 批量更新用户属性
|
* @param usr 包含要更新属性值的Usr对象
|
*/
|
public static void updateAll(Usr usr) {
|
if (usr == null) {
|
return;
|
}
|
|
synchronized (Usrdell.class) {
|
if (userProperties == null) {
|
initialize();
|
}
|
|
if (usr.getEmail() != null) {
|
userProperties.setProperty(KEY_EMAIL, usr.getEmail());
|
}
|
if (usr.getLanguage() != null) {
|
userProperties.setProperty(KEY_LANGUAGE, usr.getLanguage());
|
}
|
if (usr.getLastLoginTime() != null) {
|
userProperties.setProperty(KEY_LAST_LOGIN_TIME, usr.getLastLoginTime());
|
}
|
if (usr.getPassword() != null) {
|
userProperties.setProperty(KEY_PASSWORD, usr.getPassword());
|
}
|
if (usr.getRegistrationTime() != null) {
|
userProperties.setProperty(KEY_REGISTRATION_TIME, usr.getRegistrationTime());
|
}
|
if (usr.getStatus() != null) {
|
userProperties.setProperty(KEY_STATUS, usr.getStatus());
|
}
|
if (usr.getUserId() != null) {
|
userProperties.setProperty(KEY_USER_ID, usr.getUserId());
|
}
|
if (usr.getUserName() != null) {
|
userProperties.setProperty(KEY_USER_NAME, usr.getUserName());
|
}
|
if (usr.getRememberPassword() != null) {
|
userProperties.setProperty(KEY_REMEMBER_PASSWORD, usr.getRememberPassword());
|
}
|
|
try (FileOutputStream output = new FileOutputStream(PROPERTIES_FILE)) {
|
userProperties.store(output, "Updated User Properties");
|
} catch (IOException e) {
|
System.err.println("批量更新失败,文件写入错误: " + e.getMessage());
|
}
|
}
|
}
|
}
|