|
| 1 | +/** |
| 2 | + * Shared helpers for Danjuan (蛋卷基金) adapters. |
| 3 | + * |
| 4 | + * Core design: a single page.evaluate call fetches the gain overview AND |
| 5 | + * all per-account holdings in parallel (Promise.all), minimising Node↔Browser |
| 6 | + * round-trips to exactly one. |
| 7 | + */ |
| 8 | + |
| 9 | +import type { IPage } from '../../types.js'; |
| 10 | + |
| 11 | +export const DANJUAN_DOMAIN = 'danjuanfunds.com'; |
| 12 | +export const DANJUAN_ASSET_PAGE = `https://${DANJUAN_DOMAIN}/my-money`; |
| 13 | + |
| 14 | +const GAIN_URL = `https://${DANJUAN_DOMAIN}/djapi/fundx/profit/assets/gain?gains=%5B%22private%22%5D`; |
| 15 | +const SUMMARY_URL = `https://${DANJUAN_DOMAIN}/djapi/fundx/profit/assets/summary?invest_account_id=`; |
| 16 | + |
| 17 | +// --------------------------------------------------------------------------- |
| 18 | +// Types — keep everything explicit so TS consumers get autocomplete. |
| 19 | +// --------------------------------------------------------------------------- |
| 20 | + |
| 21 | +export interface DanjuanAccount { |
| 22 | + accountId: string; |
| 23 | + accountName: string; |
| 24 | + accountType: string; |
| 25 | + accountCode: string; |
| 26 | + marketValue: number | null; |
| 27 | + dailyGain: number | null; |
| 28 | + mainFlag: boolean; |
| 29 | +} |
| 30 | + |
| 31 | +export interface DanjuanHolding { |
| 32 | + accountId: string; |
| 33 | + accountName: string; |
| 34 | + accountType: string; |
| 35 | + fdCode: string; |
| 36 | + fdName: string; |
| 37 | + category: string; |
| 38 | + marketValue: number | null; |
| 39 | + volume: number | null; |
| 40 | + usableRemainShare: number | null; |
| 41 | + dailyGain: number | null; |
| 42 | + holdGain: number | null; |
| 43 | + holdGainRate: number | null; |
| 44 | + totalGain: number | null; |
| 45 | + nav: number | null; |
| 46 | + marketPercent: number | null; |
| 47 | +} |
| 48 | + |
| 49 | +export interface DanjuanSnapshot { |
| 50 | + asOf: string | null; |
| 51 | + totalAssetAmount: number | null; |
| 52 | + totalAssetDailyGain: number | null; |
| 53 | + totalAssetHoldGain: number | null; |
| 54 | + totalAssetTotalGain: number | null; |
| 55 | + totalFundMarketValue: number | null; |
| 56 | + accounts: DanjuanAccount[]; |
| 57 | + holdings: DanjuanHolding[]; |
| 58 | +} |
| 59 | + |
| 60 | +// --------------------------------------------------------------------------- |
| 61 | +// Single-evaluate fetcher |
| 62 | +// --------------------------------------------------------------------------- |
| 63 | + |
| 64 | +/** |
| 65 | + * Fetch the complete Danjuan fund picture in ONE browser round-trip. |
| 66 | + * |
| 67 | + * Inside the browser context we: |
| 68 | + * 1. Fetch the gain/assets overview (contains account list) |
| 69 | + * 2. Promise.all → fetch every account's holdings in parallel |
| 70 | + * 3. Return the combined result to Node |
| 71 | + */ |
| 72 | +export async function fetchDanjuanAll(page: IPage): Promise<DanjuanSnapshot> { |
| 73 | + const raw: any = await page.evaluate(` |
| 74 | + (async () => { |
| 75 | + const f = async (u) => { |
| 76 | + const r = await fetch(u, { credentials: 'include' }); |
| 77 | + if (!r.ok) return { _err: r.status }; |
| 78 | + try { return await r.json(); } catch { return { _err: 'parse' }; } |
| 79 | + }; |
| 80 | + const n = (v) => { const x = Number(v); return Number.isFinite(x) ? x : null; }; |
| 81 | +
|
| 82 | + const gain = await f(${JSON.stringify(GAIN_URL)}); |
| 83 | + if (gain._err) return { _httpError: gain._err }; |
| 84 | +
|
| 85 | + const root = gain.data || {}; |
| 86 | + const fundSec = (root.items || []).find(i => i && i.summary_type === 'FUND'); |
| 87 | + const rawAccs = fundSec && Array.isArray(fundSec.invest_account_list) |
| 88 | + ? fundSec.invest_account_list : []; |
| 89 | +
|
| 90 | + const accounts = rawAccs.map(a => ({ |
| 91 | + accountId: String(a.invest_account_id || ''), |
| 92 | + accountName: a.invest_account_name || '', |
| 93 | + accountType: a.invest_account_type || '', |
| 94 | + accountCode: a.invest_account_code || '', |
| 95 | + marketValue: n(a.market_value), |
| 96 | + dailyGain: n(a.daily_gain), |
| 97 | + mainFlag: !!a.main_flag, |
| 98 | + })); |
| 99 | +
|
| 100 | + if (!accounts.length) { |
| 101 | + return { _emptyAccounts: true }; |
| 102 | + } |
| 103 | +
|
| 104 | + const details = await Promise.all( |
| 105 | + accounts.map(a => f(${JSON.stringify(SUMMARY_URL)} + encodeURIComponent(a.accountId))) |
| 106 | + ); |
| 107 | +
|
| 108 | + const holdings = []; |
| 109 | + const detailErrors = []; |
| 110 | + for (let i = 0; i < accounts.length; i++) { |
| 111 | + const d = details[i]; |
| 112 | + if (d._err) { |
| 113 | + detailErrors.push({ |
| 114 | + accountId: accounts[i].accountId, |
| 115 | + accountName: accounts[i].accountName, |
| 116 | + error: d._err, |
| 117 | + }); |
| 118 | + continue; |
| 119 | + } |
| 120 | + const data = d.data || {}; |
| 121 | + const funds = Array.isArray(data.items) ? data.items : []; |
| 122 | + const acc = accounts[i]; |
| 123 | + for (const fd of funds) { |
| 124 | + holdings.push({ |
| 125 | + accountId: acc.accountId, |
| 126 | + accountName: data.invest_account_name || acc.accountName, |
| 127 | + accountType: data.invest_account_type || acc.accountType, |
| 128 | + fdCode: fd.fd_code || '', |
| 129 | + fdName: fd.fd_name || '', |
| 130 | + category: fd.category_text || fd.category || '', |
| 131 | + marketValue: n(fd.market_value), |
| 132 | + volume: n(fd.volume), |
| 133 | + usableRemainShare:n(fd.usable_remain_share), |
| 134 | + dailyGain: n(fd.daily_gain), |
| 135 | + holdGain: n(fd.hold_gain), |
| 136 | + holdGainRate: n(fd.hold_gain_rate), |
| 137 | + totalGain: n(fd.total_gain), |
| 138 | + nav: n(fd.nav), |
| 139 | + marketPercent: n(fd.market_percent), |
| 140 | + }); |
| 141 | + } |
| 142 | + } |
| 143 | +
|
| 144 | + return { |
| 145 | + asOf: root.daily_gain_date || null, |
| 146 | + totalAssetAmount: n(root.amount), |
| 147 | + totalAssetDailyGain: n(root.daily_gain), |
| 148 | + totalAssetHoldGain: n(root.hold_gain), |
| 149 | + totalAssetTotalGain: n(root.total_gain), |
| 150 | + totalFundMarketValue:n(fundSec && fundSec.amount), |
| 151 | + accounts, |
| 152 | + holdings, |
| 153 | + detailErrors, |
| 154 | + }; |
| 155 | + })() |
| 156 | + `); |
| 157 | + |
| 158 | + if (raw?._httpError) { |
| 159 | + throw new Error(`HTTP ${raw._httpError} — Hint: not logged in to ${DANJUAN_DOMAIN}?`); |
| 160 | + } |
| 161 | + if (raw?._emptyAccounts) { |
| 162 | + throw new Error(`No fund accounts found — Hint: not logged in to ${DANJUAN_DOMAIN}?`); |
| 163 | + } |
| 164 | + if (Array.isArray(raw?.detailErrors) && raw.detailErrors.length > 0) { |
| 165 | + const failedAccounts = raw.detailErrors |
| 166 | + .map((item: { accountName?: string; accountId?: string; error?: string | number }) => { |
| 167 | + const label = item.accountName && item.accountId |
| 168 | + ? `${item.accountName} (${item.accountId})` |
| 169 | + : item.accountName || item.accountId || 'unknown account'; |
| 170 | + return `${label}: ${item.error}`; |
| 171 | + }) |
| 172 | + .join(', '); |
| 173 | + throw new Error(`Failed to fetch Danjuan account details: ${failedAccounts}`); |
| 174 | + } |
| 175 | + return raw as DanjuanSnapshot; |
| 176 | +} |
0 commit comments