Files
20260324-42433647/login.mjs
2026-04-25 21:50:03 +08:00

76 lines
2.5 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 手动登录脚本
* 用法node login.mjs
*
* 打开浏览器 → 你输入验证码点登录 → 自动保存 Cookie
* Cookie 保存后export.mjs 会自动复用,不需要再登录
*/
import { chromium } from 'playwright';
import { writeFileSync, mkdirSync } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const COOKIE_FILE = path.join(__dirname, 'cookies.json');
console.log('========================================');
console.log(' 店小秘手动登录');
console.log(' 请在浏览器中输入验证码并登录');
console.log(' 登录成功后会自动保存 Cookie');
console.log('========================================\n');
const browser = await chromium.launch({
headless: false, // 有头模式,你能看到浏览器
channel: 'chrome', // 用系统 Chrome
});
const context = await browser.newContext({
viewport: { width: 1400, height: 900 },
locale: 'zh-CN',
});
const page = await context.newPage();
// 打开登录页
await page.goto('https://www.dianxiaomi.com/home.htm', { waitUntil: 'load', timeout: 30000 });
// 自动填入账号密码
try {
await page.waitForSelector('#exampleInputName', { timeout: 10000 });
await page.fill('#exampleInputName', 'MiLe-kf01');
await page.fill('#exampleInputPassword', 'Vxdas@302');
console.log('>> 已填入账号密码,请输入验证码后点击"登录"\n');
} catch {
console.log('>> 请手动填写账号密码和验证码\n');
}
// 轮询检测登录状态(最多等 5 分钟)
const startTime = Date.now();
const TIMEOUT = 5 * 60 * 1000;
while (Date.now() - startTime < TIMEOUT) {
await page.waitForTimeout(2000);
const isLogged = await page.evaluate(() => {
const text = document.body?.innerText || '';
return text.includes('MiLe-kf01') && (text.includes('待办事项') || text.includes('订单'));
}).catch(() => false);
if (isLogged) {
// 保存 Cookie
const cookies = await context.cookies();
writeFileSync(COOKIE_FILE, JSON.stringify(cookies, null, 2));
console.log(`\n>> ★ 登录成功Cookie 已保存(${cookies.length} 条)`);
console.log(`>> 文件: ${COOKIE_FILE}`);
console.log('>> 现在 export.mjs 会自动使用这个 Cookie 运行\n');
console.log('>> 3 秒后关闭浏览器...');
await page.waitForTimeout(3000);
await browser.close();
process.exit(0);
}
}
console.log('\n>> 等待超时5分钟未检测到登录成功');
await browser.close();
process.exit(1);