feat(auth): 实现三种登录覆盖矩阵与企业配置自动生成 (账密/短信/浏览器同步)
This commit is contained in:
1 parent
2085a97e34
commit
255c7aee8d
7 files changed
+643
-6
No files matched your search
@@ -1,13 +1,18 @@
|
||||
import { readFileSync, readdirSync } from "fs";
|
||||
import { readFileSync, readdirSync, writeFileSync, mkdirSync } from "fs";
|
||||
import { join } from "path";
|
||||
import { CorpConfig, CorpConfigSchema } from "./types";
|
||||
import { CorpConfig, CorpConfigSchema } from "./types.js";
|
||||
|
||||
const ENTERPRISES_DIR = "./enterprises";
|
||||
|
||||
export function loadCorpConfigs(): CorpConfig[] {
|
||||
function ensureDir(dir: string): void {
|
||||
mkdirSync(dir, { recursive: true });
|
||||
}
|
||||
|
||||
export function loadCorpConfigs(dir: string = ENTERPRISES_DIR): CorpConfig[] {
|
||||
let files: string[];
|
||||
try {
|
||||
files = readdirSync(ENTERPRISES_DIR).filter((f) => f.endsWith(".json"));
|
||||
ensureDir(dir);
|
||||
files = readdirSync(dir).filter((f) => f.endsWith(".json"));
|
||||
} catch (error) {
|
||||
console.error(
|
||||
`[config] failed to read enterprises directory: ${error instanceof Error ? error.message : String(error)}`
|
||||
@@ -18,7 +23,7 @@ export function loadCorpConfigs(): CorpConfig[] {
|
||||
const configs: CorpConfig[] = [];
|
||||
for (const file of files) {
|
||||
try {
|
||||
const content = readFileSync(join(ENTERPRISES_DIR, file), "utf-8");
|
||||
const content = readFileSync(join(dir, file), "utf-8");
|
||||
const parsed = JSON.parse(content);
|
||||
configs.push(CorpConfigSchema.parse(parsed));
|
||||
} catch (error) {
|
||||
@@ -30,3 +35,18 @@ export function loadCorpConfigs(): CorpConfig[] {
|
||||
|
||||
return configs;
|
||||
}
|
||||
|
||||
export function saveCorpConfig(config: CorpConfig, dir: string = ENTERPRISES_DIR): string {
|
||||
ensureDir(dir);
|
||||
const validated = CorpConfigSchema.parse(config);
|
||||
const filePath = join(dir, `${validated.id}.json`);
|
||||
writeFileSync(filePath, JSON.stringify(validated, null, 2), "utf-8");
|
||||
return filePath;
|
||||
}
|
||||
|
||||
export function saveMultipleCorpConfigs(
|
||||
configs: CorpConfig[],
|
||||
dir: string = ENTERPRISES_DIR
|
||||
): string[] {
|
||||
return configs.map((config) => saveCorpConfig(config, dir));
|
||||
}
|
||||
Reference in new issue
Block a user