import { chmodSync, existsSync, mkdirSync, readFileSync, writeFileSync } from "fs"; import { join, dirname } from "path"; import { homedir } from "os"; import { z } from "zod"; export const ACCOUNT_CACHE_PATH = join( homedir(), ".config", "hose-use", "accounts.json" ); export function resolveAccountCachePath(): string { const envFile = process.env.HOSE_ACCOUNTS_FILE?.trim(); if (envFile) return envFile; const stateDir = process.env.HOSE_STATE_DIR?.trim(); if (stateDir) return join(stateDir, "accounts.json"); return ACCOUNT_CACHE_PATH; } export const AccountCorporationMetaSchema = z.object({ id: z.string(), name: z.string(), staffId: z.string(), cluster: z.string().optional().default("app"), baseUrl: z.string().optional().default("https://app.ekuaibao.com"), }); export const AccountEntrySchema = z.object({ accountId: z.string().describe("账号唯一标识(如手机号)"), userId: z.string().optional().describe("用户唯一 ID"), cloudAuthToken: z.string().optional().describe("业务中心会话凭证"), activeCorpId: z.string().optional().describe("当前选中的默认企业 ID"), createdAt: z.number().default(() => Date.now()), updatedAt: z.number().default(() => Date.now()), corporations: z.array(AccountCorporationMetaSchema).default([]), }); export const AccountStoreSchema = z.object({ activeAccountId: z.string().optional(), accounts: z.record(z.string(), AccountEntrySchema).default({}), }); export type AccountCorporationMeta = z.infer; export type AccountEntry = z.infer; export type AccountStore = z.infer; function ensureDir(path: string): void { const dir = dirname(path); if (!existsSync(dir)) { mkdirSync(dir, { recursive: true, mode: 0o700 }); } try { chmodSync(dir, 0o700); } catch {} } export function loadAccountStore(): AccountStore { const filePath = resolveAccountCachePath(); try { const content = readFileSync(filePath, "utf-8"); return AccountStoreSchema.parse(JSON.parse(content)); } catch { return { accounts: {} }; } } export function saveAccountStore(store: AccountStore): void { const filePath = resolveAccountCachePath(); ensureDir(filePath); writeFileSync(filePath, JSON.stringify(store, null, 2), { encoding: "utf-8", mode: 0o600, }); try { chmodSync(filePath, 0o600); } catch {} } export function saveAccount(entry: AccountEntry): void { const store = loadAccountStore(); store.accounts[entry.accountId] = { ...entry, updatedAt: Date.now(), }; if (!store.activeAccountId) { store.activeAccountId = entry.accountId; } saveAccountStore(store); } export function getAccount(accountId: string): AccountEntry | undefined { const store = loadAccountStore(); return store.accounts[accountId]; } export function listAccounts(): AccountEntry[] { const store = loadAccountStore(); return Object.values(store.accounts); } export function getActiveAccount(): AccountEntry | undefined { const store = loadAccountStore(); if (store.activeAccountId && store.accounts[store.activeAccountId]) { return store.accounts[store.activeAccountId]; } const all = Object.values(store.accounts); return all[0]; } export function setActiveAccount(accountId: string): boolean { const store = loadAccountStore(); if (!store.accounts[accountId]) return false; store.activeAccountId = accountId; saveAccountStore(store); return true; } export function deleteAccount(accountId: string): void { const store = loadAccountStore(); delete store.accounts[accountId]; if (store.activeAccountId === accountId) { const remaining = Object.keys(store.accounts); store.activeAccountId = remaining[0]; } saveAccountStore(store); }