101 lines
2.6 KiB
C#
101 lines
2.6 KiB
C#
using System.Text.Json;
|
|
|
|
namespace WorkCameraExport.Models
|
|
{
|
|
/// <summary>
|
|
/// 应用程序设置
|
|
/// </summary>
|
|
public class AppSettings
|
|
{
|
|
private static readonly string SettingsPath = Path.Combine(
|
|
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),
|
|
"WorkCameraExport",
|
|
"settings.json");
|
|
|
|
/// <summary>
|
|
/// 服务器地址
|
|
/// </summary>
|
|
public string ServerUrl { get; set; } = "";
|
|
|
|
/// <summary>
|
|
/// 用户名
|
|
/// </summary>
|
|
public string Username { get; set; } = "";
|
|
|
|
/// <summary>
|
|
/// 记住登录状态
|
|
/// </summary>
|
|
public bool RememberMe { get; set; }
|
|
|
|
/// <summary>
|
|
/// 保存的Token
|
|
/// </summary>
|
|
public string SavedToken { get; set; } = "";
|
|
|
|
/// <summary>
|
|
/// Token过期时间
|
|
/// </summary>
|
|
public DateTime? TokenExpireTime { get; set; }
|
|
|
|
/// <summary>
|
|
/// 默认导出路径
|
|
/// </summary>
|
|
public string DefaultExportPath { get; set; } = "";
|
|
|
|
/// <summary>
|
|
/// 加载设置
|
|
/// </summary>
|
|
public static AppSettings Load()
|
|
{
|
|
try
|
|
{
|
|
if (File.Exists(SettingsPath))
|
|
{
|
|
var json = File.ReadAllText(SettingsPath);
|
|
return JsonSerializer.Deserialize<AppSettings>(json) ?? new AppSettings();
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
// 忽略加载错误,返回默认设置
|
|
}
|
|
return new AppSettings();
|
|
}
|
|
|
|
/// <summary>
|
|
/// 保存设置
|
|
/// </summary>
|
|
public void Save()
|
|
{
|
|
try
|
|
{
|
|
var directory = Path.GetDirectoryName(SettingsPath);
|
|
if (!string.IsNullOrEmpty(directory) && !Directory.Exists(directory))
|
|
{
|
|
Directory.CreateDirectory(directory);
|
|
}
|
|
|
|
var json = JsonSerializer.Serialize(this, new JsonSerializerOptions
|
|
{
|
|
WriteIndented = true
|
|
});
|
|
File.WriteAllText(SettingsPath, json);
|
|
}
|
|
catch
|
|
{
|
|
// 忽略保存错误
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 清除登录信息
|
|
/// </summary>
|
|
public void ClearLoginInfo()
|
|
{
|
|
SavedToken = "";
|
|
TokenExpireTime = null;
|
|
Save();
|
|
}
|
|
}
|
|
}
|