refactor: ♻️ 重构文件结构
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
import dayjs from "dayjs";
|
||||
|
||||
/** 将 Date 对象格式化为 'YYYY-MM-DD HH:mm:ss' 字符串 */
|
||||
export function formatTime(date: Date): string {
|
||||
return dayjs(date).format("YYYY-MM-DD HH:mm:ss");
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
import { load, type Store } from "@tauri-apps/plugin-store";
|
||||
|
||||
let storeInstance: Store | null = null;
|
||||
|
||||
async function getStore(): Promise<Store> {
|
||||
if (!storeInstance) {
|
||||
storeInstance = await load("config.json", { defaults: {}, autoSave: true });
|
||||
}
|
||||
return storeInstance;
|
||||
}
|
||||
|
||||
export async function getItem<T = unknown>(key: string): Promise<T | null> {
|
||||
const store = await getStore();
|
||||
const value = await store.get<T>(key);
|
||||
return value ?? null;
|
||||
}
|
||||
|
||||
export async function setItem<T = unknown>(
|
||||
key: string,
|
||||
value: T,
|
||||
): Promise<void> {
|
||||
const store = await getStore();
|
||||
await store.set(key, value);
|
||||
}
|
||||
|
||||
export async function removeItem(key: string): Promise<void> {
|
||||
const store = await getStore();
|
||||
await store.delete(key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 localStorage 迁移数据到文件存储(仅首次升级时执行)
|
||||
*/
|
||||
export async function migrateFromLocalStorage(): Promise<boolean> {
|
||||
const store = await getStore();
|
||||
const migrated = await store.get<boolean>("_migrated");
|
||||
if (migrated) return false;
|
||||
|
||||
let hasMigrated = false;
|
||||
for (const key of ["crm_credentials", "crm_config"]) {
|
||||
const raw = localStorage.getItem(key);
|
||||
if (raw) {
|
||||
try {
|
||||
await store.set(key, JSON.parse(raw));
|
||||
localStorage.removeItem(key);
|
||||
hasMigrated = true;
|
||||
} catch {
|
||||
/* ignore invalid JSON */
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
await store.set("_migrated", true);
|
||||
return hasMigrated;
|
||||
}
|
||||
Reference in New Issue
Block a user