diff --git a/src/checkin.js b/src/checkin.js index daf75fa..8a2ce18 100644 --- a/src/checkin.js +++ b/src/checkin.js @@ -295,7 +295,12 @@ async function runTickInner(opts) { if (outcome.status === 'checked-in') { memo[account.id] = todayFor; account.lastCheckin = new Date().toISOString(); - logger.info('CHECKIN', '{label} 领取成功 +{amount} {unit}', { label, amount: outcome.claimedAmount, unit: CHECKIN_UNIT[account.provider] || 'Credits' }); + const amount = Number.isFinite(outcome.claimedAmount) ? outcome.claimedAmount : null; + if (amount === null) { + logger.info('CHECKIN', '{label} 领取成功(接口未返回积分数量)', { label }); + } else { + logger.info('CHECKIN', '{label} 领取成功 +{amount} {unit}', { label, amount, unit: CHECKIN_UNIT[account.provider] || 'Credits' }); + } } else if (outcome.status === 'already') { memo[account.id] = todayFor; account.lastCheckin = account.lastCheckin || new Date().toISOString(); diff --git a/src/dumateClient.js b/src/dumateClient.js index 41d7ebe..a7dc046 100644 --- a/src/dumateClient.js +++ b/src/dumateClient.js @@ -112,6 +112,56 @@ export function extractDumateUserId(cookies) { return token; } +/** + * 补齐 bce-user-info:扫码登录、本机导入拿到的会话常常只有 BDUSS / bce-sessionid, + * 而领取接口需要 csrftoken,其值就是 bce-user-info 的原文(等于 bce 账户 ID)。 + * 控制台首页不一定下发该 cookie,这里调 /api/dumate/app/token/get, + * 从 Set-Cookie 的 bce-login-accountid 取账户 ID 补上。 + * @returns {Promise} 补好后的 cookie 数组;无需补或失败返回 null + */ +export async function healDumateCookies(cookies, fetchImpl) { + const list = Array.isArray(cookies) ? cookies : parseDumateCookies(cookies); + if (list.some((c) => c.name === 'bce-user-info' && c.value)) return null; + const doIt = fetchImpl || ((u, init) => fetchJsonRace(u, { ...init, timeoutMs: FETCH_TIMEOUT_MS })); + try { + const res = await doIt(`${DUMATE_CONSOLE_ORIGIN}/api/dumate/app/token/get`, { + headers: { + Cookie: buildDumateCookieHeader(list), + Accept: 'application/json', + Origin: DUMATE_CONSOLE_ORIGIN, + Referer: `${DUMATE_CONSOLE_ORIGIN}/dumate/channel`, + 'User-Agent': DUMATE_USER_AGENT, + }, + redirect: 'manual', + }); + if (res.status < 200 || res.status >= 300) return null; + const raw = typeof res.headers?.getSetCookie === 'function' ? res.headers.getSetCookie() : []; + const extra = []; + let accountId = ''; + for (const item of raw) { + const [pair] = String(item).split(';'); + const i = pair.indexOf('='); + if (i <= 0) continue; + const name = pair.slice(0, i).trim(); + const value = pair.slice(i + 1).trim(); + if (!name) continue; + if (name === 'bce-login-accountid') { + try { + accountId = decodeURIComponent(value); + } catch { + accountId = value; + } + } + extra.push({ name, value, domain: DUMATE_CONSOLE_HOST }); + } + if (!accountId) return null; + const kept = list.filter((c) => !extra.some((e) => e.name === c.name)); + return [...kept, ...extra, { name: 'bce-user-info', value: accountId, domain: DUMATE_CONSOLE_HOST }]; + } catch { + return null; + } +} + /** * 账号脱敏掩码显示 */ @@ -196,8 +246,17 @@ export async function verifyDumateSession(credentialInput, { fetchImpl } = {}) { */ export async function checkinDumate(account, { fetchImpl } = {}) { const cookie = account?.meta?.cookieSiteSession?.cookie || account?.token || ''; - const cookies = parseDumateCookies(cookie); - const csrftoken = extractDumateCsrfToken(cookies); + let cookies = parseDumateCookies(cookie); + let csrftoken = extractDumateCsrfToken(cookies); + + // 会话里缺 bce-user-info 时先补齐(扫码登录、本机导入常见) + if (!csrftoken) { + const healed = await healDumateCookies(cookies, fetchImpl).catch(() => null); + if (healed) { + cookies = healed; + csrftoken = extractDumateCsrfToken(cookies); + } + } if (!csrftoken) { return { status: 'failed', error: t('Cookie 缺少 bce-user-info(无法生成 csrftoken)') }; diff --git a/src/dumateQr.js b/src/dumateQr.js index ef5154e..1de2d39 100644 --- a/src/dumateQr.js +++ b/src/dumateQr.js @@ -31,6 +31,7 @@ import { randomBytes } from 'node:crypto'; import { FETCH_TIMEOUT_MS } from './constants.js'; +import { healDumateCookies } from './dumateClient.js'; export const DUMATE_QR_CONSOLE_URL = 'https://console.bce.baidu.com/dumate/channel'; export const DUMATE_QR_UA = @@ -161,7 +162,15 @@ export async function qrQuickLogin(pp) { if (!loc) { const onConsole = /^https:\/\/console\.bce\.baidu\.com\//.test(url); const hasSess = jar.has('bce-sessionid'); - if (onConsole && r.status === 200 && hasSess) return { cookies: jar.all(), account: pf.username }; + if (onConsole && r.status === 200 && hasSess) { + // 控制台首页不一定下发 bce-user-info,缺了就用 dumate 应用 token 接口补齐账户 ID + let cookies = jar.all(); + if (!jar.has('bce-user-info')) { + const healed = await healDumateCookies(cookies).catch(() => null); + if (healed) cookies = healed; + } + return { cookies, account: pf.username }; + } break; } url = new URL(loc, url).href;