236 lines
7.8 KiB
TypeScript
236 lines
7.8 KiB
TypeScript
import {
|
|
fullPasswordLogin,
|
|
fullSmsLogin,
|
|
fullBrowserSyncLogin,
|
|
sendSmsVerificationCode,
|
|
importF12Credentials,
|
|
parseHeadersOrCurl,
|
|
refreshAccountCorporations,
|
|
} from "../src/core/auth/login.js";
|
|
import { listAccounts, getActiveAccount } from "../src/core/auth/account-store.js";
|
|
import { loadCorpConfigs } from "../src/core/config/loader.js";
|
|
import readline from "readline";
|
|
|
|
function prompt(question: string): Promise<string> {
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout,
|
|
});
|
|
return new Promise((resolve) => {
|
|
rl.question(question, (answer) => {
|
|
rl.close();
|
|
resolve(answer.trim());
|
|
});
|
|
});
|
|
}
|
|
|
|
async function handleMenu() {
|
|
console.log("请选择登录或凭证导入方式:");
|
|
console.log(" [1] 账号密码登录 (RSA 自动加密,自动拉取名下全部企业)");
|
|
console.log(" [2] 手机短信验证码登录");
|
|
console.log(" [3] F12 抓包导入 (粘贴 accessToken/corpId/staffId 或 cURL / Headers)");
|
|
console.log(" [4] 浏览器会话一键同步 (WebBridge)");
|
|
console.log(" [5] 刷新账号名下的所有企业 Token (批量保活)");
|
|
console.log(" [6] 查看当前已配置的账号与企业列表\n");
|
|
|
|
const choice = await prompt("请输入选项序号 [1-6] (默认 1): ") || "1";
|
|
|
|
switch (choice) {
|
|
case "1":
|
|
return doPasswordLogin();
|
|
case "2":
|
|
return doSmsLogin();
|
|
case "3":
|
|
return doF12Import();
|
|
case "4":
|
|
return doBrowserSync();
|
|
case "5":
|
|
return doRefreshAccounts();
|
|
case "6":
|
|
return doShowStatus();
|
|
default:
|
|
console.error("无效选项");
|
|
}
|
|
}
|
|
|
|
async function doPasswordLogin(phoneInput?: string, pwdInput?: string) {
|
|
const phone = phoneInput || (await prompt("请输入手机号: "));
|
|
const password = pwdInput || (await prompt("请输入密码: "));
|
|
|
|
if (!phone || !password) {
|
|
console.error("手机号和密码不能为空");
|
|
return;
|
|
}
|
|
|
|
console.log("\n正在进行全自动 RSA 账密登录...");
|
|
const result = await fullPasswordLogin({
|
|
phone,
|
|
password,
|
|
saveAll: true,
|
|
});
|
|
|
|
console.log("\n[SUCCESS] 账密登录成功!");
|
|
console.log(`账号标识: ${result.accountId}`);
|
|
console.log(`当前激活企业: ${result.selectedCorp.name} (${result.selectedCorp.id})`);
|
|
console.log(`共保存企业配置: ${result.allCorps.length} 家`);
|
|
result.allCorps.forEach((c, idx) => {
|
|
console.log(` ${idx + 1}. [${c.id}] ${c.name} (${c.credentials.staffId})`);
|
|
});
|
|
}
|
|
|
|
async function doSmsLogin() {
|
|
const phone = await prompt("请输入手机号: ");
|
|
if (!phone) {
|
|
console.error("手机号不能为空");
|
|
return;
|
|
}
|
|
|
|
console.log(`\n正在向 ${phone} 请求发送短信验证码...`);
|
|
let codeId: string | undefined;
|
|
try {
|
|
const sendRes = await sendSmsVerificationCode({ phone });
|
|
codeId = sendRes.codeId;
|
|
console.log("短信验证码已成功发送!");
|
|
} catch (e) {
|
|
console.warn("直接发送提示:", e instanceof Error ? e.message : String(e));
|
|
console.log("如果网页端触发了人机验证,请在浏览器中点击获取验证码后在下方输入。");
|
|
}
|
|
|
|
const code = await prompt("请输入收到的 6 位短信验证码: ");
|
|
if (!code) {
|
|
console.error("验证码不能为空");
|
|
return;
|
|
}
|
|
|
|
console.log("\n正在验证短信验证码并拉取企业...");
|
|
const result = await fullSmsLogin({
|
|
phone,
|
|
code,
|
|
codeId,
|
|
saveAll: true,
|
|
});
|
|
|
|
console.log("\n[SUCCESS] 短信验证码登录成功!");
|
|
console.log(`账号标识: ${result.accountId}`);
|
|
console.log(`当前激活企业: ${result.selectedCorp.name} (${result.selectedCorp.id})`);
|
|
console.log(`共保存企业配置: ${result.allCorps.length} 家`);
|
|
}
|
|
|
|
async function doF12Import() {
|
|
console.log("\n提示:您可以直接粘贴从 F12 Network 复制的 Request Headers、cURL 文本,或手动输入三要素。");
|
|
const raw = await prompt("请粘贴 F12 抓包文本 (直接回车则手动逐项输入): ");
|
|
|
|
let accessToken = "";
|
|
let corpId = "";
|
|
let staffId = "";
|
|
let name = "";
|
|
|
|
if (raw) {
|
|
const parsed = parseHeadersOrCurl(raw);
|
|
accessToken = parsed.accessToken || "";
|
|
corpId = parsed.corpId || "";
|
|
staffId = parsed.staffId || "";
|
|
}
|
|
|
|
if (!accessToken) accessToken = await prompt("请输入 accessToken: ");
|
|
if (!corpId) corpId = await prompt("请输入 corpId: ");
|
|
if (!staffId) staffId = await prompt("请输入 staffId: ");
|
|
name = await prompt("请输入企业名称(选填): ");
|
|
|
|
if (!accessToken || !corpId || !staffId) {
|
|
console.error("accessToken, corpId, staffId 均为必填!");
|
|
return;
|
|
}
|
|
|
|
console.log("\n正在校验凭证并保存企业配置...");
|
|
const imported = await importF12Credentials({
|
|
accessToken,
|
|
corpId,
|
|
staffId,
|
|
name: name || undefined,
|
|
});
|
|
|
|
console.log("\n[SUCCESS] 成功导入并保存企业凭证!");
|
|
console.log(`企业名称: ${imported.name}`);
|
|
console.log(`企业 ID: ${imported.id}`);
|
|
console.log(`员工工号: ${imported.credentials.staffId}`);
|
|
console.log(`保存路径: enterprises/${imported.id}.json`);
|
|
}
|
|
|
|
async function doBrowserSync() {
|
|
console.log("\n正在从本地运行的浏览器会话中同步登录态...");
|
|
const result = await fullBrowserSyncLogin({ saveAll: true });
|
|
console.log("\n[SUCCESS] 成功从浏览器同步企业凭证!");
|
|
console.log(`当前激活企业: ${result.selectedCorp.name} (${result.selectedCorp.id})`);
|
|
console.log(`共保存企业配置: ${result.allCorps.length} 家`);
|
|
}
|
|
|
|
async function doRefreshAccounts() {
|
|
console.log("\n正在使用主账号登录态刷新所有关联企业的 AccessToken...");
|
|
const result = await refreshAccountCorporations(undefined, { saveAll: true });
|
|
console.log("\n[SUCCESS] 成功批量刷新并更新企业配置!");
|
|
console.log(`账号标识: ${result.accountId}`);
|
|
console.log(`共刷新企业: ${result.allCorps.length} 家`);
|
|
result.allCorps.forEach((c, idx) => {
|
|
console.log(` ${idx + 1}. [${c.id}] ${c.name}`);
|
|
});
|
|
}
|
|
|
|
async function doShowStatus() {
|
|
const accounts = listAccounts();
|
|
const activeAccount = getActiveAccount();
|
|
const corps = loadCorpConfigs();
|
|
|
|
console.log("\n====== 账号与企业状态概览 ======");
|
|
console.log(`总账号数: ${accounts.length},当前激活账号: ${activeAccount?.accountId || "无"}`);
|
|
accounts.forEach((acc, i) => {
|
|
console.log(`\n账号 [${i + 1}]: ${acc.accountId} (用户 ID: ${acc.userId || "未知"})`);
|
|
console.log(`关联企业数: ${acc.corporations.length}`);
|
|
});
|
|
|
|
console.log(`\n总加载本地企业配置: ${corps.length} 个`);
|
|
corps.forEach((c, i) => {
|
|
console.log(` ${i + 1}. [${c.id}] ${c.name} (来源: ${c.source || "manual"}, 账号: ${c.accountId || "无"})`);
|
|
});
|
|
console.log("================================");
|
|
}
|
|
|
|
async function main() {
|
|
const args = process.argv.slice(2);
|
|
const isSync = args.includes("--sync");
|
|
const isSms = args.includes("--sms");
|
|
const isRefresh = args.includes("--refresh");
|
|
const isStatus = args.includes("--status");
|
|
const phoneArg = args.find((a) => a.startsWith("--phone="))?.split("=")[1];
|
|
const pwdArg = args.find((a) => a.startsWith("--password="))?.split("=")[1];
|
|
|
|
console.log("==========================================");
|
|
console.log(" 合思 (ekb-mcp) 认证与凭证管理 ");
|
|
console.log("==========================================\n");
|
|
|
|
try {
|
|
if (phoneArg && pwdArg) {
|
|
return await doPasswordLogin(phoneArg, pwdArg);
|
|
}
|
|
if (isSync) {
|
|
return await doBrowserSync();
|
|
}
|
|
if (isSms) {
|
|
return await doSmsLogin();
|
|
}
|
|
if (isRefresh) {
|
|
return await doRefreshAccounts();
|
|
}
|
|
if (isStatus) {
|
|
return await doShowStatus();
|
|
}
|
|
|
|
await handleMenu();
|
|
} catch (error) {
|
|
console.error("\n[ERROR] 操作失败:", error instanceof Error ? error.message : String(error));
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
main();
|