121 lines
3.7 KiB
JavaScript
121 lines
3.7 KiB
JavaScript
/**
|
|
* 微博二维码登录 V3 - 直接访问登录地址
|
|
*/
|
|
|
|
const puppeteer = require('puppeteer-core');
|
|
const { getLaunchOptions } = require('../config/browser');
|
|
const fs = require('fs');
|
|
|
|
const SCREENSHOT_DIR = '/tmp/weibo_qr_login';
|
|
const delay = (ms) => new Promise(resolve => setTimeout(resolve, ms));
|
|
|
|
if (!fs.existsSync(SCREENSHOT_DIR)) {
|
|
fs.mkdirSync(SCREENSHOT_DIR, { recursive: true });
|
|
}
|
|
|
|
async function weiboQRLogin() {
|
|
console.log('🌐 启动浏览器...');
|
|
|
|
const browser = await puppeteer.launch({
|
|
...getLaunchOptions(),
|
|
headless: true,
|
|
});
|
|
|
|
const page = await browser.newPage();
|
|
await page.setViewport({ width: 1280, height: 900 });
|
|
|
|
try {
|
|
// 直接访问登录地址
|
|
const loginUrl = 'https://passport.weibo.com/sso/signin?entry=miniblog&source=miniblog&disp=popup&url=https%3A%2F%2Fweibo.com%2Fnewlogin%3Ftabtype%3Dweibo%26gid%3D102803%26openLoginLayer%3D0%26url%3Dhttps%3A%2F%2Fweibo.com%2F&from=weibopro';
|
|
|
|
console.log('🔄 访问登录页面...');
|
|
await page.goto(loginUrl, {
|
|
waitUntil: 'networkidle2',
|
|
timeout: 60000,
|
|
});
|
|
|
|
await delay(5000);
|
|
console.log('✅ 登录页面加载完成');
|
|
|
|
// 截图
|
|
await page.screenshot({ path: `${SCREENSHOT_DIR}/passport_login.png` });
|
|
console.log('📸 已截图登录页面');
|
|
|
|
// 检查页面内容
|
|
const title = await page.title();
|
|
console.log(`📄 页面标题: ${title}`);
|
|
console.log(`🔗 当前URL: ${page.url()}`);
|
|
|
|
// 查找二维码
|
|
const qrSelectors = [
|
|
'img[src*="qr"]',
|
|
'[class*="qr"]',
|
|
'[class*="qrcode"]',
|
|
'canvas',
|
|
'[class*="code"]',
|
|
];
|
|
|
|
let qrFound = false;
|
|
for (const sel of qrSelectors) {
|
|
const el = await page.$(sel);
|
|
if (el) {
|
|
console.log(`✅ 找到二维码元素: ${sel}`);
|
|
qrFound = true;
|
|
|
|
// 专门截图二维码元素
|
|
try {
|
|
await el.screenshot({ path: `${SCREENSHOT_DIR}/qr_only.png` });
|
|
console.log('📸 已单独截图二维码');
|
|
} catch(e) {}
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (!qrFound) {
|
|
console.log('⚠️ 未明显找到二维码元素');
|
|
}
|
|
|
|
// 复制到 web 目录
|
|
const fs = require('fs');
|
|
const files = ['passport_login.png', 'qr_only.png'].filter(f => fs.existsSync(`${SCREENSHOT_DIR}/${f}`));
|
|
|
|
for (const file of files) {
|
|
fs.copyFileSync(`${SCREENSHOT_DIR}/${file}`, `/home/ubuntu/nanocode/audiobook-player/static/${file}`);
|
|
console.log(`📤 已上传: http://129.211.173.87:8899/screenshots/${file}`);
|
|
}
|
|
|
|
console.log('\n' + '='.repeat(60));
|
|
console.log('📱 请使用微博 App 扫描二维码');
|
|
console.log('⏳ 等待 60 秒...');
|
|
console.log('='.repeat(60) + '\n');
|
|
|
|
// 等待扫码
|
|
await delay(60000);
|
|
|
|
// 检查状态
|
|
console.log('🔍 检查登录状态...');
|
|
await page.reload({ waitUntil: 'networkidle2' });
|
|
await delay(3000);
|
|
|
|
console.log(`🔗 当前URL: ${page.url()}`);
|
|
await page.screenshot({ path: `${SCREENSHOT_DIR}/after_scan.png` });
|
|
fs.copyFileSync(`${SCREENSHOT_DIR}/after_scan.png`, `/home/ubuntu/nanocode/audiobook-player/static/after_scan.png`);
|
|
|
|
// 判断是否跳转成功
|
|
if (page.url().includes('weibo.com') && !page.url().includes('passport')) {
|
|
console.log('✅ 看起来登录成功了!');
|
|
} else {
|
|
console.log('⏳ 扫码可能未完成');
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('❌ 错误:', error.message);
|
|
await page.screenshot({ path: `${SCREENSHOT_DIR}/error.png` });
|
|
fs.copyFileSync(`${SCREENSHOT_DIR}/error.png`, `/home/ubuntu/nanocode/audiobook-player/static/error.png`);
|
|
} finally {
|
|
await browser.close();
|
|
}
|
|
}
|
|
|
|
weiboQRLogin().catch(console.error);
|