creditdaddy 1.7.2 → 1.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +445 -440
- package/build.json +3 -3
- package/package.json +34 -34
- package/src/accounts.js +15 -0
- package/src/assets/donate-alipay.svg +1 -1
- package/src/assets/donate-ifdian.svg +1 -1
- package/src/assets/donate-wechat.svg +1 -1
- package/src/assets/providers/catpaw.png +0 -0
- package/src/assets/providers/codebuddy-ide.png +0 -0
- package/src/assets/providers/dumate.png +0 -0
- package/src/assets/providers/loomy.png +0 -0
- package/src/assets/providers/meoo.png +0 -0
- package/src/assets/providers/mirasim.png +0 -0
- package/src/bgdaemon.js +341 -341
- package/src/buildInfo.js +65 -65
- package/src/catpawClient.js +103 -103
- package/src/catpawLocal.js +236 -236
- package/src/checkin.js +7 -2
- package/src/codebuddyIdeLocal.js +381 -381
- package/src/constants.js +121 -117
- package/src/cookieSites.js +340 -301
- package/src/daemon.js +2682 -2443
- package/src/doubaoClient.js +167 -167
- package/src/dumateClient.js +517 -0
- package/src/dumateLocal.js +298 -0
- package/src/dumateQr.js +337 -0
- package/src/externalQuota.js +577 -574
- package/src/formatDuration.js +17 -17
- package/src/i18n/en.json +161 -7
- package/src/i18n/ja.json +161 -7
- package/src/i18n/ko.json +161 -7
- package/src/i18n/zh-TW.json +161 -7
- package/src/i18n.js +47 -47
- package/src/lingxiClient.js +373 -373
- package/src/localDetect.js +147 -147
- package/src/longcatClient.js +260 -260
- package/src/loomyAuth.js +230 -230
- package/src/loomyClient.js +366 -366
- package/src/loomyLocal.js +206 -206
- package/src/meooAuth.js +246 -0
- package/src/meooClient.js +250 -0
- package/src/minimaxGateway.js +520 -520
- package/src/minimaxLocal.js +344 -344
- package/src/mirasimClient.js +211 -211
- package/src/mirasimLocal.js +392 -392
- package/src/modelscopeClient.js +275 -275
- package/src/panel.html +374 -48
- package/src/prodIcons.json +7 -5
- package/src/providers.js +34 -0
- package/src/qoderApp.js +578 -578
- package/src/qoderClient.js +266 -266
- package/src/qoderUmid.js +199 -199
- package/src/quotaRefresh.js +119 -119
- package/src/refreshGuard.js +75 -75
- package/src/riverClient.js +372 -372
- package/src/store.js +2 -1
- package/src/tenrouterAccounts.js +237 -232
- package/src/traeAuth.js +601 -599
- package/src/traeClient.js +356 -360
- package/src/traeGateway.js +875 -875
- package/src/traeLocal.js +463 -371
- package/src/transfer.js +4 -0
- package/src/usageSync.js +535 -523
- package/src/winProc.js +134 -134
- package/src/xiaomiMimoClient.js +206 -206
- package/src/zcodeAuth.js +131 -131
- package/src/zcodeGateway.js +850 -850
- package/src/zcodeLocal.js +340 -340
- package/src/zcrypto.js +160 -160
package/src/longcatClient.js
CHANGED
|
@@ -1,260 +1,260 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* LongCat(longcat.chat,美团)Token 资源包余额抓取(issue #29)。
|
|
3
|
-
*
|
|
4
|
-
* 纯只读接入——资源包为购买/赠送发放,无签到动作端点。
|
|
5
|
-
* 凭据 = longcat.chat 会话 cookie(美团账号体系 SSO),打平台 API 走 cookie 直连
|
|
6
|
-
* (fetch wrapper 只加 Content-Type + x-requested-with,无 Bearer/CSRF——2026-10-15
|
|
7
|
-
* chunk 考古 + 登录态实测;未登录 401 `{"code":401,"msg":"用户未登录,请先登录"}`)。
|
|
8
|
-
*
|
|
9
|
-
* 数据端点(事实源 = 登录态响应,2026-10-15):
|
|
10
|
-
* POST /api/pay/quota/metering/token-packs/summary body {}
|
|
11
|
-
* → data.currentLot 当前消耗中的包(含 remainingToken/consumedToken/totalToken/
|
|
12
|
-
* expireTime/consumeOrder/grantCategory/source,可能为 null——全部包耗尽时)
|
|
13
|
-
* → data.otherLots[] 其余可用包(同形状;expireTime "YYYY-MM-DD HH:mm:ss")
|
|
14
|
-
* (/commercial/entitlements/token-packs/list 实测返回空分页壳,summary 已含全部字段,不采用)
|
|
15
|
-
*
|
|
16
|
-
* 卡面口径(issue #37 定版「字段即真相」):Balance 汇总行 = 全部 ACTIVE 包
|
|
17
|
-
* remaining 之和(isCreditBalance + summarizesDetail,used/total 推 Σ消耗/Σ总量,
|
|
18
|
-
* 10R 的池条按真实消耗画空段);逐包明细腿 = currentLot + otherLots 各一行
|
|
19
|
-
* (detailOnly,used=consumed、total=包总量、remainingPercentage=剩余%、
|
|
20
|
-
* name 纯文案「获赠资源包/资源包」,到期日走 resetAt),排序按 consumeOrder
|
|
21
|
-
* ——官方「赠送先扣、近到期先扣」的消耗顺序。
|
|
22
|
-
*
|
|
23
|
-
* 安全纪律:会话 cookie 只存本机账号库(0600),推 10Router 只带 token 数字标量。
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
import { fetchJsonRace } from './zcodeClient.js';
|
|
27
|
-
import { FETCH_TIMEOUT_MS } from './constants.js';
|
|
28
|
-
import { t } from './i18n.js';
|
|
29
|
-
|
|
30
|
-
export const LC_BASE = 'https://longcat.chat';
|
|
31
|
-
export const LC_PACKS_SUMMARY_URL = `${LC_BASE}/api/pay/quota/metering/token-packs/summary`;
|
|
32
|
-
|
|
33
|
-
const LC_UA =
|
|
34
|
-
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36';
|
|
35
|
-
|
|
36
|
-
function lcHeaders(cookie) {
|
|
37
|
-
return {
|
|
38
|
-
Accept: 'application/json, text/plain, */*',
|
|
39
|
-
'Content-Type': 'application/json',
|
|
40
|
-
Cookie: String(cookie || '').trim(),
|
|
41
|
-
Referer: `${LC_BASE}/platform/usage`,
|
|
42
|
-
Origin: LC_BASE,
|
|
43
|
-
// 站点 fetch wrapper 的事实头(考古 index-6651bc05 chunk);m-appkey 实测非必需,带上同形
|
|
44
|
-
'x-requested-with': 'XMLHttpRequest',
|
|
45
|
-
'm-appkey': 'fe_com.sankuai.friday.longcat.platform',
|
|
46
|
-
'X-Client-Language': 'zh',
|
|
47
|
-
'User-Agent': LC_UA,
|
|
48
|
-
};
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
/** "YYYY-MM-DD HH:mm:ss"(UTC+8)→ ISO;解析不出返 null */
|
|
52
|
-
export function lcTimeToIso(s) {
|
|
53
|
-
const m = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2}):(\d{2})/.exec(String(s || ''));
|
|
54
|
-
if (!m) return null;
|
|
55
|
-
// 站点时间按美团口径为 UTC+8(remainSeconds 与 expireTime 实测自洽)
|
|
56
|
-
const ts = Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4] - 8, +m[5], +m[6]);
|
|
57
|
-
return Number.isFinite(ts) ? new Date(ts).toISOString() : null;
|
|
58
|
-
}
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* summary 响应 → 全部 ACTIVE 包列表。宽容解析:currentLot/otherLots 缺失按空处理。
|
|
62
|
-
* @returns {{ ok: true, packs: Array<{total, remaining, consumed, expireAt, grantCategory, source, consumeOrder}>, raw?: object }
|
|
63
|
-
* | { ok: false, error: string, status?: number, authExpired?: boolean, raw?: object }}
|
|
64
|
-
*/
|
|
65
|
-
export function parsePacksSummary(body) {
|
|
66
|
-
const d = body?.data && typeof body.data === 'object' ? body.data : null;
|
|
67
|
-
if (!d) return { ok: false, error: 'summary 响应无 data', raw: body };
|
|
68
|
-
const lots = [];
|
|
69
|
-
if (d.currentLot && typeof d.currentLot === 'object') lots.push(d.currentLot);
|
|
70
|
-
if (Array.isArray(d.otherLots)) lots.push(...d.otherLots.filter((x) => x && typeof x === 'object'));
|
|
71
|
-
const packs = [];
|
|
72
|
-
for (const it of lots) {
|
|
73
|
-
if (String(it.status || 'ACTIVE') !== 'ACTIVE') continue;
|
|
74
|
-
const remaining = Number(it.remainingToken);
|
|
75
|
-
const total = Number(it.totalToken);
|
|
76
|
-
if (!Number.isFinite(remaining) || !Number.isFinite(total)) continue;
|
|
77
|
-
packs.push({
|
|
78
|
-
total,
|
|
79
|
-
remaining: Math.max(0, remaining),
|
|
80
|
-
consumed: Number(it.consumedToken) || 0,
|
|
81
|
-
expireAt: lcTimeToIso(it.expireTime),
|
|
82
|
-
grantCategory: String(it.grantCategory || ''),
|
|
83
|
-
source: String(it.source || ''),
|
|
84
|
-
consumeOrder: Number(it.consumeOrder) || 99,
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
packs.sort((a, b) => a.consumeOrder - b.consumeOrder || String(a.expireAt).localeCompare(String(b.expireAt)));
|
|
88
|
-
return { ok: true, packs };
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* 用会话 cookie 探测资源包余额(捕获验证 + 推卡 live 读共用)。
|
|
93
|
-
* 401({"code":401,"msg":"用户未登录…"})/403 → 会话失效(authExpired)。
|
|
94
|
-
* code!==0 的其他业务错归结构/状态异常(分开归类,对齐既有探测口径)。
|
|
95
|
-
* @returns {Promise<{ok: boolean, packs?: Array, raw?: object, error?: string, status?: number, authExpired?: boolean}>}
|
|
96
|
-
*/
|
|
97
|
-
export async function fetchLongcatPacks(cookie, { fetchImpl } = {}) {
|
|
98
|
-
const doFetch = fetchImpl || ((u, init) => fetchJsonRace(u, { headers: init.headers, timeoutMs: FETCH_TIMEOUT_MS, method: init.method, body: init.body }));
|
|
99
|
-
try {
|
|
100
|
-
const res = await doFetch(LC_PACKS_SUMMARY_URL, { method: 'POST', headers: lcHeaders(cookie), body: '{}' });
|
|
101
|
-
if (res.status === 401 || res.status === 403) {
|
|
102
|
-
return { ok: false, status: res.status, authExpired: true, error: `会话失效(HTTP ${res.status})` };
|
|
103
|
-
}
|
|
104
|
-
if (!res.ok) return { ok: false, status: res.status, error: `HTTP ${res.status}` };
|
|
105
|
-
const body = await res.json().catch(() => null);
|
|
106
|
-
if (!body || typeof body !== 'object') return { ok: false, status: res.status, error: '非 JSON 响应' };
|
|
107
|
-
if (Number(body.code) === 401) {
|
|
108
|
-
return { ok: false, status: 401, authExpired: true, error: String(body.msg || '会话失效') };
|
|
109
|
-
}
|
|
110
|
-
if (Number(body.code) !== 0) {
|
|
111
|
-
return { ok: false, status: res.status, error: `业务错误 code ${body.code}:${body.msg || ''}`.trim(), raw: body };
|
|
112
|
-
}
|
|
113
|
-
const parsed = parsePacksSummary(body);
|
|
114
|
-
if (!parsed.ok) return { ok: false, status: res.status, error: parsed.error, raw: body };
|
|
115
|
-
return { ok: true, packs: parsed.packs, raw: body };
|
|
116
|
-
} catch (e) {
|
|
117
|
-
return { ok: false, error: e?.message || String(e) };
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
/**
|
|
122
|
-
* 身份解析(2026-10-15 实测):GET /api/v1/user-current(cookie 直连,GET 无 body)。
|
|
123
|
-
* 信封形状宽容解析:{code:0,data:{…}}(对齐 packs API);401/403 归会话失效(fail-soft,
|
|
124
|
-
* 调用方把身份缺失当无身份单例处理,不阻断捕获)。
|
|
125
|
-
* @returns {Promise<{ ok: boolean, session?: {userId, email, name}, raw?: object, error?: string, status?: number }>}
|
|
126
|
-
*/
|
|
127
|
-
export async function fetchLongcatIdentity(cookie, { fetchImpl } = {}) {
|
|
128
|
-
const doFetch = fetchImpl || ((u, init) => fetchJsonRace(u, { headers: init.headers, timeoutMs: FETCH_TIMEOUT_MS, method: init.method }));
|
|
129
|
-
try {
|
|
130
|
-
const res = await doFetch(`${LC_BASE}/api/v1/user-current`, { method: 'GET', headers: lcHeaders(cookie) });
|
|
131
|
-
if (res.status === 401 || res.status === 403) return { ok: false, status: res.status, error: `会话失效(HTTP ${res.status})` };
|
|
132
|
-
if (!res.ok) return { ok: false, status: res.status, error: `HTTP ${res.status}` };
|
|
133
|
-
const body = await res.json().catch(() => null);
|
|
134
|
-
if (!body || typeof body !== 'object') return { ok: false, status: res.status, error: '非 JSON 响应' };
|
|
135
|
-
const d = body?.data && typeof body.data === 'object' ? body.data : body?.user && typeof body.user === 'object' ? body.user : null;
|
|
136
|
-
if (!d) return { ok: false, status: res.status, error: 'user-current 响应无 data', raw: body };
|
|
137
|
-
const userId = d.userId ?? d.id ?? d.uid ?? null;
|
|
138
|
-
const email = d.email ?? d.mail ?? null;
|
|
139
|
-
const name = d.name ?? d.nickname ?? d.username ?? null;
|
|
140
|
-
if (userId == null && !email && !name) return { ok: false, status: res.status, error: 'user-current 无身份字段', raw: body };
|
|
141
|
-
const uid = d.uid ?? null;
|
|
142
|
-
return {
|
|
143
|
-
ok: true,
|
|
144
|
-
session: {
|
|
145
|
-
...(userId != null ? { userId: String(userId) } : {}),
|
|
146
|
-
...(uid != null && String(uid) !== String(userId ?? '') ? { uid: String(uid) } : {}),
|
|
147
|
-
...(email ? { email: String(email) } : {}),
|
|
148
|
-
...(name ? { name: String(name) } : {}),
|
|
149
|
-
},
|
|
150
|
-
};
|
|
151
|
-
} catch (e) {
|
|
152
|
-
return { ok: false, error: e?.message || String(e) };
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
|
|
156
|
-
/** 快照入库形状(capture/reprobe 共用):全部 ACTIVE 包 + 时间戳 */
|
|
157
|
-
export function longcatSnapshot(packs) {
|
|
158
|
-
return {
|
|
159
|
-
packs,
|
|
160
|
-
at: new Date().toISOString(),
|
|
161
|
-
};
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* 组装 LongCat 资源包的 10Router external-quota 载荷(储值卡 + 逐包明细)。
|
|
166
|
-
* - Balance 汇总行 = 全部 ACTIVE 包 remaining 之和(isCreditBalance + summarizesDetail,
|
|
167
|
-
* used/total 推 Σ消耗/Σ总量——池条据此画消耗空段);
|
|
168
|
-
* - 逐包明细腿 = 每包一行(detailOnly,used=consumed、total=包总量、name 纯文案),
|
|
169
|
-
* 0 剩余包也出行;
|
|
170
|
-
* - 无可用包(全部耗尽/过期)→ message 状态卡。
|
|
171
|
-
* @param {object} account meta.longcatPacks(快照)
|
|
172
|
-
* @param {{ packs?: Array, error?: string, authInvalid?: boolean }} [liveData]
|
|
173
|
-
* authInvalid = live 拉取明确 401/403——不回退旧快照,降级 message 状态卡
|
|
174
|
-
* @returns {{ source: string, payload: object }}
|
|
175
|
-
*/
|
|
176
|
-
export function buildLongcatQuotaPayload(account, liveData = null) {
|
|
177
|
-
const sess = account?.meta?.longcatSession || {};
|
|
178
|
-
const userId = sess.userId || account?.uid || '';
|
|
179
|
-
const short = String(userId).toLowerCase().replace(/[^a-z0-9-]/g, '').slice(0, 24);
|
|
180
|
-
const source = short ? `longcat-${short}`.slice(0, 32) : `longcat-${(account?.id || 'default').slice(0, 24)}`;
|
|
181
|
-
const nameStr = String(account?.name || '').trim();
|
|
182
|
-
const email = sess.email || /[\w.+-]+@[\w-]+\.[\w.]+/.exec(nameStr)?.[0] || '';
|
|
183
|
-
const label = (nameStr || (email ? `LongCat (${email})` : 'LongCat')).slice(0, 100);
|
|
184
|
-
|
|
185
|
-
if (liveData?.authInvalid) {
|
|
186
|
-
return {
|
|
187
|
-
source,
|
|
188
|
-
payload: {
|
|
189
|
-
provider: 'longcat',
|
|
190
|
-
label,
|
|
191
|
-
message: liveData.error || t('LongCat 会话已失效,请在 CreditDaddy「网页会话」页重新登录捕获'),
|
|
192
|
-
},
|
|
193
|
-
};
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
// live 优先,快照兜底(非 live 且 >24h 由 snapshotAgeTag 标注陈粮)
|
|
197
|
-
let packs = liveData?.packs;
|
|
198
|
-
let ageTag = '';
|
|
199
|
-
if (!Array.isArray(packs)) {
|
|
200
|
-
const snap = account?.meta?.quotaSnapshot;
|
|
201
|
-
packs = account?.meta?.longcatPacks?.packs;
|
|
202
|
-
ageTag = snapshotAge(snap?.at ?? account?.meta?.longcatPacks?.at);
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
const active = (packs || []).filter((p) => p && Number.isFinite(Number(p.remaining)));
|
|
206
|
-
const balance = active.reduce((s, p) => s + Math.max(0, Number(p.remaining)), 0);
|
|
207
|
-
const payload = { provider: 'longcat', label, plan: t('Token 资源包') + ageTag };
|
|
208
|
-
|
|
209
|
-
if (active.length && balance > 0) {
|
|
210
|
-
const f = (n) => {
|
|
211
|
-
const v = Number(n) || 0;
|
|
212
|
-
return v >= 1e4 ? `${Math.round(v / 1e4 * 100) / 100} 万` : String(v);
|
|
213
|
-
};
|
|
214
|
-
// 字段即真相(issue #37):used/total/remainingPercentage 推真实数字,
|
|
215
|
-
// name 只做纯文案——10R 渲染链自会出倒计时、剩余/总量与消耗百分比,
|
|
216
|
-
// 数字拼进 name 只会让卡面同一数值印两遍、进度条与文字互相矛盾。
|
|
217
|
-
const pct1 = (rem, tot) => (tot > 0 ? Math.max(0, Math.min(100, Math.round((rem / tot) * 1000) / 10)) : 0);
|
|
218
|
-
const totalAll = active.reduce((s, p) => s + Math.max(0, Number(p.total) || 0), 0);
|
|
219
|
-
const usedAll = active.reduce((s, p) => s + Math.max(0, Number(p.consumed) || 0), 0);
|
|
220
|
-
payload.quotas = {
|
|
221
|
-
Balance: {
|
|
222
|
-
name: `${t('剩余')} ${f(balance)} (${t('Token')})`,
|
|
223
|
-
used: usedAll,
|
|
224
|
-
total: totalAll,
|
|
225
|
-
remainingPercentage: pct1(balance, totalAll),
|
|
226
|
-
displayRemaining: true,
|
|
227
|
-
isCreditBalance: true,
|
|
228
|
-
summarizesDetail: true,
|
|
229
|
-
},
|
|
230
|
-
};
|
|
231
|
-
for (const [i, p] of active.entries()) {
|
|
232
|
-
const kind = p.grantCategory === 'GIFT' || p.source === 'FREE_PACK';
|
|
233
|
-
payload.quotas[`Pack${i + 1}`] = {
|
|
234
|
-
name: kind ? t('获赠资源包') : t('资源包'),
|
|
235
|
-
used: Math.max(0, Number(p.consumed) || 0),
|
|
236
|
-
total: Number(p.total),
|
|
237
|
-
remainingPercentage: pct1(Math.max(0, Number(p.remaining)), Number(p.total)),
|
|
238
|
-
displayRemaining: true,
|
|
239
|
-
isCreditBalance: true,
|
|
240
|
-
...(kind ? { giftPack: true } : {}),
|
|
241
|
-
detailOnly: true,
|
|
242
|
-
resetAt: p.expireAt || undefined,
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
} else {
|
|
246
|
-
payload.message = liveData?.error || t('LongCat 资源包已耗尽或会话未捕获,请在 CreditDaddy「网页会话」页重新登录捕获');
|
|
247
|
-
}
|
|
248
|
-
|
|
249
|
-
return { source, payload };
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
/** 快照陈粮标注(与 externalQuota.snapshotAgeTag 同口径;此处避免循环依赖本地实现) */
|
|
253
|
-
function snapshotAge(at) {
|
|
254
|
-
const ts = Date.parse(at || '');
|
|
255
|
-
if (!Number.isFinite(ts)) return '';
|
|
256
|
-
if (Date.now() - ts < 24 * 3600_000) return '';
|
|
257
|
-
const d = new Date(ts);
|
|
258
|
-
const p2 = (n) => String(n).padStart(2, '0');
|
|
259
|
-
return t('(数据 {m}-{d} {hh}:{mm})', { m: d.getMonth() + 1, d: d.getDate(), hh: p2(d.getHours()), mm: p2(d.getMinutes()) });
|
|
260
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* LongCat(longcat.chat,美团)Token 资源包余额抓取(issue #29)。
|
|
3
|
+
*
|
|
4
|
+
* 纯只读接入——资源包为购买/赠送发放,无签到动作端点。
|
|
5
|
+
* 凭据 = longcat.chat 会话 cookie(美团账号体系 SSO),打平台 API 走 cookie 直连
|
|
6
|
+
* (fetch wrapper 只加 Content-Type + x-requested-with,无 Bearer/CSRF——2026-10-15
|
|
7
|
+
* chunk 考古 + 登录态实测;未登录 401 `{"code":401,"msg":"用户未登录,请先登录"}`)。
|
|
8
|
+
*
|
|
9
|
+
* 数据端点(事实源 = 登录态响应,2026-10-15):
|
|
10
|
+
* POST /api/pay/quota/metering/token-packs/summary body {}
|
|
11
|
+
* → data.currentLot 当前消耗中的包(含 remainingToken/consumedToken/totalToken/
|
|
12
|
+
* expireTime/consumeOrder/grantCategory/source,可能为 null——全部包耗尽时)
|
|
13
|
+
* → data.otherLots[] 其余可用包(同形状;expireTime "YYYY-MM-DD HH:mm:ss")
|
|
14
|
+
* (/commercial/entitlements/token-packs/list 实测返回空分页壳,summary 已含全部字段,不采用)
|
|
15
|
+
*
|
|
16
|
+
* 卡面口径(issue #37 定版「字段即真相」):Balance 汇总行 = 全部 ACTIVE 包
|
|
17
|
+
* remaining 之和(isCreditBalance + summarizesDetail,used/total 推 Σ消耗/Σ总量,
|
|
18
|
+
* 10R 的池条按真实消耗画空段);逐包明细腿 = currentLot + otherLots 各一行
|
|
19
|
+
* (detailOnly,used=consumed、total=包总量、remainingPercentage=剩余%、
|
|
20
|
+
* name 纯文案「获赠资源包/资源包」,到期日走 resetAt),排序按 consumeOrder
|
|
21
|
+
* ——官方「赠送先扣、近到期先扣」的消耗顺序。
|
|
22
|
+
*
|
|
23
|
+
* 安全纪律:会话 cookie 只存本机账号库(0600),推 10Router 只带 token 数字标量。
|
|
24
|
+
*/
|
|
25
|
+
|
|
26
|
+
import { fetchJsonRace } from './zcodeClient.js';
|
|
27
|
+
import { FETCH_TIMEOUT_MS } from './constants.js';
|
|
28
|
+
import { t } from './i18n.js';
|
|
29
|
+
|
|
30
|
+
export const LC_BASE = 'https://longcat.chat';
|
|
31
|
+
export const LC_PACKS_SUMMARY_URL = `${LC_BASE}/api/pay/quota/metering/token-packs/summary`;
|
|
32
|
+
|
|
33
|
+
const LC_UA =
|
|
34
|
+
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36';
|
|
35
|
+
|
|
36
|
+
function lcHeaders(cookie) {
|
|
37
|
+
return {
|
|
38
|
+
Accept: 'application/json, text/plain, */*',
|
|
39
|
+
'Content-Type': 'application/json',
|
|
40
|
+
Cookie: String(cookie || '').trim(),
|
|
41
|
+
Referer: `${LC_BASE}/platform/usage`,
|
|
42
|
+
Origin: LC_BASE,
|
|
43
|
+
// 站点 fetch wrapper 的事实头(考古 index-6651bc05 chunk);m-appkey 实测非必需,带上同形
|
|
44
|
+
'x-requested-with': 'XMLHttpRequest',
|
|
45
|
+
'm-appkey': 'fe_com.sankuai.friday.longcat.platform',
|
|
46
|
+
'X-Client-Language': 'zh',
|
|
47
|
+
'User-Agent': LC_UA,
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** "YYYY-MM-DD HH:mm:ss"(UTC+8)→ ISO;解析不出返 null */
|
|
52
|
+
export function lcTimeToIso(s) {
|
|
53
|
+
const m = /^(\d{4})-(\d{2})-(\d{2})[ T](\d{2}):(\d{2}):(\d{2})/.exec(String(s || ''));
|
|
54
|
+
if (!m) return null;
|
|
55
|
+
// 站点时间按美团口径为 UTC+8(remainSeconds 与 expireTime 实测自洽)
|
|
56
|
+
const ts = Date.UTC(+m[1], +m[2] - 1, +m[3], +m[4] - 8, +m[5], +m[6]);
|
|
57
|
+
return Number.isFinite(ts) ? new Date(ts).toISOString() : null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* summary 响应 → 全部 ACTIVE 包列表。宽容解析:currentLot/otherLots 缺失按空处理。
|
|
62
|
+
* @returns {{ ok: true, packs: Array<{total, remaining, consumed, expireAt, grantCategory, source, consumeOrder}>, raw?: object }
|
|
63
|
+
* | { ok: false, error: string, status?: number, authExpired?: boolean, raw?: object }}
|
|
64
|
+
*/
|
|
65
|
+
export function parsePacksSummary(body) {
|
|
66
|
+
const d = body?.data && typeof body.data === 'object' ? body.data : null;
|
|
67
|
+
if (!d) return { ok: false, error: 'summary 响应无 data', raw: body };
|
|
68
|
+
const lots = [];
|
|
69
|
+
if (d.currentLot && typeof d.currentLot === 'object') lots.push(d.currentLot);
|
|
70
|
+
if (Array.isArray(d.otherLots)) lots.push(...d.otherLots.filter((x) => x && typeof x === 'object'));
|
|
71
|
+
const packs = [];
|
|
72
|
+
for (const it of lots) {
|
|
73
|
+
if (String(it.status || 'ACTIVE') !== 'ACTIVE') continue;
|
|
74
|
+
const remaining = Number(it.remainingToken);
|
|
75
|
+
const total = Number(it.totalToken);
|
|
76
|
+
if (!Number.isFinite(remaining) || !Number.isFinite(total)) continue;
|
|
77
|
+
packs.push({
|
|
78
|
+
total,
|
|
79
|
+
remaining: Math.max(0, remaining),
|
|
80
|
+
consumed: Number(it.consumedToken) || 0,
|
|
81
|
+
expireAt: lcTimeToIso(it.expireTime),
|
|
82
|
+
grantCategory: String(it.grantCategory || ''),
|
|
83
|
+
source: String(it.source || ''),
|
|
84
|
+
consumeOrder: Number(it.consumeOrder) || 99,
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
packs.sort((a, b) => a.consumeOrder - b.consumeOrder || String(a.expireAt).localeCompare(String(b.expireAt)));
|
|
88
|
+
return { ok: true, packs };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* 用会话 cookie 探测资源包余额(捕获验证 + 推卡 live 读共用)。
|
|
93
|
+
* 401({"code":401,"msg":"用户未登录…"})/403 → 会话失效(authExpired)。
|
|
94
|
+
* code!==0 的其他业务错归结构/状态异常(分开归类,对齐既有探测口径)。
|
|
95
|
+
* @returns {Promise<{ok: boolean, packs?: Array, raw?: object, error?: string, status?: number, authExpired?: boolean}>}
|
|
96
|
+
*/
|
|
97
|
+
export async function fetchLongcatPacks(cookie, { fetchImpl } = {}) {
|
|
98
|
+
const doFetch = fetchImpl || ((u, init) => fetchJsonRace(u, { headers: init.headers, timeoutMs: FETCH_TIMEOUT_MS, method: init.method, body: init.body }));
|
|
99
|
+
try {
|
|
100
|
+
const res = await doFetch(LC_PACKS_SUMMARY_URL, { method: 'POST', headers: lcHeaders(cookie), body: '{}' });
|
|
101
|
+
if (res.status === 401 || res.status === 403) {
|
|
102
|
+
return { ok: false, status: res.status, authExpired: true, error: `会话失效(HTTP ${res.status})` };
|
|
103
|
+
}
|
|
104
|
+
if (!res.ok) return { ok: false, status: res.status, error: `HTTP ${res.status}` };
|
|
105
|
+
const body = await res.json().catch(() => null);
|
|
106
|
+
if (!body || typeof body !== 'object') return { ok: false, status: res.status, error: '非 JSON 响应' };
|
|
107
|
+
if (Number(body.code) === 401) {
|
|
108
|
+
return { ok: false, status: 401, authExpired: true, error: String(body.msg || '会话失效') };
|
|
109
|
+
}
|
|
110
|
+
if (Number(body.code) !== 0) {
|
|
111
|
+
return { ok: false, status: res.status, error: `业务错误 code ${body.code}:${body.msg || ''}`.trim(), raw: body };
|
|
112
|
+
}
|
|
113
|
+
const parsed = parsePacksSummary(body);
|
|
114
|
+
if (!parsed.ok) return { ok: false, status: res.status, error: parsed.error, raw: body };
|
|
115
|
+
return { ok: true, packs: parsed.packs, raw: body };
|
|
116
|
+
} catch (e) {
|
|
117
|
+
return { ok: false, error: e?.message || String(e) };
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* 身份解析(2026-10-15 实测):GET /api/v1/user-current(cookie 直连,GET 无 body)。
|
|
123
|
+
* 信封形状宽容解析:{code:0,data:{…}}(对齐 packs API);401/403 归会话失效(fail-soft,
|
|
124
|
+
* 调用方把身份缺失当无身份单例处理,不阻断捕获)。
|
|
125
|
+
* @returns {Promise<{ ok: boolean, session?: {userId, email, name}, raw?: object, error?: string, status?: number }>}
|
|
126
|
+
*/
|
|
127
|
+
export async function fetchLongcatIdentity(cookie, { fetchImpl } = {}) {
|
|
128
|
+
const doFetch = fetchImpl || ((u, init) => fetchJsonRace(u, { headers: init.headers, timeoutMs: FETCH_TIMEOUT_MS, method: init.method }));
|
|
129
|
+
try {
|
|
130
|
+
const res = await doFetch(`${LC_BASE}/api/v1/user-current`, { method: 'GET', headers: lcHeaders(cookie) });
|
|
131
|
+
if (res.status === 401 || res.status === 403) return { ok: false, status: res.status, error: `会话失效(HTTP ${res.status})` };
|
|
132
|
+
if (!res.ok) return { ok: false, status: res.status, error: `HTTP ${res.status}` };
|
|
133
|
+
const body = await res.json().catch(() => null);
|
|
134
|
+
if (!body || typeof body !== 'object') return { ok: false, status: res.status, error: '非 JSON 响应' };
|
|
135
|
+
const d = body?.data && typeof body.data === 'object' ? body.data : body?.user && typeof body.user === 'object' ? body.user : null;
|
|
136
|
+
if (!d) return { ok: false, status: res.status, error: 'user-current 响应无 data', raw: body };
|
|
137
|
+
const userId = d.userId ?? d.id ?? d.uid ?? null;
|
|
138
|
+
const email = d.email ?? d.mail ?? null;
|
|
139
|
+
const name = d.name ?? d.nickname ?? d.username ?? null;
|
|
140
|
+
if (userId == null && !email && !name) return { ok: false, status: res.status, error: 'user-current 无身份字段', raw: body };
|
|
141
|
+
const uid = d.uid ?? null;
|
|
142
|
+
return {
|
|
143
|
+
ok: true,
|
|
144
|
+
session: {
|
|
145
|
+
...(userId != null ? { userId: String(userId) } : {}),
|
|
146
|
+
...(uid != null && String(uid) !== String(userId ?? '') ? { uid: String(uid) } : {}),
|
|
147
|
+
...(email ? { email: String(email) } : {}),
|
|
148
|
+
...(name ? { name: String(name) } : {}),
|
|
149
|
+
},
|
|
150
|
+
};
|
|
151
|
+
} catch (e) {
|
|
152
|
+
return { ok: false, error: e?.message || String(e) };
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/** 快照入库形状(capture/reprobe 共用):全部 ACTIVE 包 + 时间戳 */
|
|
157
|
+
export function longcatSnapshot(packs) {
|
|
158
|
+
return {
|
|
159
|
+
packs,
|
|
160
|
+
at: new Date().toISOString(),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* 组装 LongCat 资源包的 10Router external-quota 载荷(储值卡 + 逐包明细)。
|
|
166
|
+
* - Balance 汇总行 = 全部 ACTIVE 包 remaining 之和(isCreditBalance + summarizesDetail,
|
|
167
|
+
* used/total 推 Σ消耗/Σ总量——池条据此画消耗空段);
|
|
168
|
+
* - 逐包明细腿 = 每包一行(detailOnly,used=consumed、total=包总量、name 纯文案),
|
|
169
|
+
* 0 剩余包也出行;
|
|
170
|
+
* - 无可用包(全部耗尽/过期)→ message 状态卡。
|
|
171
|
+
* @param {object} account meta.longcatPacks(快照)
|
|
172
|
+
* @param {{ packs?: Array, error?: string, authInvalid?: boolean }} [liveData]
|
|
173
|
+
* authInvalid = live 拉取明确 401/403——不回退旧快照,降级 message 状态卡
|
|
174
|
+
* @returns {{ source: string, payload: object }}
|
|
175
|
+
*/
|
|
176
|
+
export function buildLongcatQuotaPayload(account, liveData = null) {
|
|
177
|
+
const sess = account?.meta?.longcatSession || {};
|
|
178
|
+
const userId = sess.userId || account?.uid || '';
|
|
179
|
+
const short = String(userId).toLowerCase().replace(/[^a-z0-9-]/g, '').slice(0, 24);
|
|
180
|
+
const source = short ? `longcat-${short}`.slice(0, 32) : `longcat-${(account?.id || 'default').slice(0, 24)}`;
|
|
181
|
+
const nameStr = String(account?.name || '').trim();
|
|
182
|
+
const email = sess.email || /[\w.+-]+@[\w-]+\.[\w.]+/.exec(nameStr)?.[0] || '';
|
|
183
|
+
const label = (nameStr || (email ? `LongCat (${email})` : 'LongCat')).slice(0, 100);
|
|
184
|
+
|
|
185
|
+
if (liveData?.authInvalid) {
|
|
186
|
+
return {
|
|
187
|
+
source,
|
|
188
|
+
payload: {
|
|
189
|
+
provider: 'longcat',
|
|
190
|
+
label,
|
|
191
|
+
message: liveData.error || t('LongCat 会话已失效,请在 CreditDaddy「网页会话」页重新登录捕获'),
|
|
192
|
+
},
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
// live 优先,快照兜底(非 live 且 >24h 由 snapshotAgeTag 标注陈粮)
|
|
197
|
+
let packs = liveData?.packs;
|
|
198
|
+
let ageTag = '';
|
|
199
|
+
if (!Array.isArray(packs)) {
|
|
200
|
+
const snap = account?.meta?.quotaSnapshot;
|
|
201
|
+
packs = account?.meta?.longcatPacks?.packs;
|
|
202
|
+
ageTag = snapshotAge(snap?.at ?? account?.meta?.longcatPacks?.at);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const active = (packs || []).filter((p) => p && Number.isFinite(Number(p.remaining)));
|
|
206
|
+
const balance = active.reduce((s, p) => s + Math.max(0, Number(p.remaining)), 0);
|
|
207
|
+
const payload = { provider: 'longcat', label, plan: t('Token 资源包') + ageTag };
|
|
208
|
+
|
|
209
|
+
if (active.length && balance > 0) {
|
|
210
|
+
const f = (n) => {
|
|
211
|
+
const v = Number(n) || 0;
|
|
212
|
+
return v >= 1e4 ? `${Math.round(v / 1e4 * 100) / 100} 万` : String(v);
|
|
213
|
+
};
|
|
214
|
+
// 字段即真相(issue #37):used/total/remainingPercentage 推真实数字,
|
|
215
|
+
// name 只做纯文案——10R 渲染链自会出倒计时、剩余/总量与消耗百分比,
|
|
216
|
+
// 数字拼进 name 只会让卡面同一数值印两遍、进度条与文字互相矛盾。
|
|
217
|
+
const pct1 = (rem, tot) => (tot > 0 ? Math.max(0, Math.min(100, Math.round((rem / tot) * 1000) / 10)) : 0);
|
|
218
|
+
const totalAll = active.reduce((s, p) => s + Math.max(0, Number(p.total) || 0), 0);
|
|
219
|
+
const usedAll = active.reduce((s, p) => s + Math.max(0, Number(p.consumed) || 0), 0);
|
|
220
|
+
payload.quotas = {
|
|
221
|
+
Balance: {
|
|
222
|
+
name: `${t('剩余')} ${f(balance)} (${t('Token')})`,
|
|
223
|
+
used: usedAll,
|
|
224
|
+
total: totalAll,
|
|
225
|
+
remainingPercentage: pct1(balance, totalAll),
|
|
226
|
+
displayRemaining: true,
|
|
227
|
+
isCreditBalance: true,
|
|
228
|
+
summarizesDetail: true,
|
|
229
|
+
},
|
|
230
|
+
};
|
|
231
|
+
for (const [i, p] of active.entries()) {
|
|
232
|
+
const kind = p.grantCategory === 'GIFT' || p.source === 'FREE_PACK';
|
|
233
|
+
payload.quotas[`Pack${i + 1}`] = {
|
|
234
|
+
name: kind ? t('获赠资源包') : t('资源包'),
|
|
235
|
+
used: Math.max(0, Number(p.consumed) || 0),
|
|
236
|
+
total: Number(p.total),
|
|
237
|
+
remainingPercentage: pct1(Math.max(0, Number(p.remaining)), Number(p.total)),
|
|
238
|
+
displayRemaining: true,
|
|
239
|
+
isCreditBalance: true,
|
|
240
|
+
...(kind ? { giftPack: true } : {}),
|
|
241
|
+
detailOnly: true,
|
|
242
|
+
resetAt: p.expireAt || undefined,
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
} else {
|
|
246
|
+
payload.message = liveData?.error || t('LongCat 资源包已耗尽或会话未捕获,请在 CreditDaddy「网页会话」页重新登录捕获');
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
return { source, payload };
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** 快照陈粮标注(与 externalQuota.snapshotAgeTag 同口径;此处避免循环依赖本地实现) */
|
|
253
|
+
function snapshotAge(at) {
|
|
254
|
+
const ts = Date.parse(at || '');
|
|
255
|
+
if (!Number.isFinite(ts)) return '';
|
|
256
|
+
if (Date.now() - ts < 24 * 3600_000) return '';
|
|
257
|
+
const d = new Date(ts);
|
|
258
|
+
const p2 = (n) => String(n).padStart(2, '0');
|
|
259
|
+
return t('(数据 {m}-{d} {hh}:{mm})', { m: d.getMonth() + 1, d: d.getDate(), hh: p2(d.getHours()), mm: p2(d.getMinutes()) });
|
|
260
|
+
}
|