magpie-plugin-loomy-auth 0.1.18 → 0.1.21
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/AGENTS.md +4 -3
- package/CHANGELOG.md +14 -0
- package/CHECKLIST.md +6 -6
- package/NOTICE.md +35 -35
- package/README.md +126 -126
- package/docs/internals.md +350 -350
- package/index.mjs +611 -611
- package/package.json +5 -4
- package/publish-npm.mjs +15 -22
- package/src/api.mjs +436 -436
- package/src/chat.mjs +63 -2
- package/src/constants.mjs +72 -72
- package/src/desktop.mjs +173 -173
- package/src/icon.mjs +12 -12
- package/src/models.mjs +125 -125
- package/src/pure.mjs +818 -818
package/src/api.mjs
CHANGED
|
@@ -1,436 +1,436 @@
|
|
|
1
|
-
// Loomy · 网络层
|
|
2
|
-
//
|
|
3
|
-
// 两条通道(实测 2026-10-26):
|
|
4
|
-
// 业务/积分/目录 INTEGRATION_BASE 鉴权 = token 双头(session 即 token)
|
|
5
|
-
// 账号/短信 XFYUN_ACCOUNT_BASE 鉴权 = HMAC-SHA1 签名(客户端公开常量)
|
|
6
|
-
//
|
|
7
|
-
// 延迟实测(同账号各 3 次):/points/balance 61~233ms,/points/records 50~78ms。
|
|
8
|
-
// ⇒ 热路径只用一次请求;quota 从凭据里取,不为它多发一次。
|
|
9
|
-
|
|
10
|
-
import { _pure } from "./pure.mjs"
|
|
11
|
-
import { secrets, traceId } from "./crypto.mjs"
|
|
12
|
-
import {
|
|
13
|
-
APP_ID,
|
|
14
|
-
AUTH_ERROR_CODES,
|
|
15
|
-
CLIENT_DEVICE_ID,
|
|
16
|
-
CLIENT_MODEL_ID,
|
|
17
|
-
CLIENT_UA,
|
|
18
|
-
CLIENT_VERSION,
|
|
19
|
-
INTEGRATION_BASE,
|
|
20
|
-
LEDGER_MAX_PAGES,
|
|
21
|
-
LEDGER_PAGE_SIZE,
|
|
22
|
-
REQUEST_TIMEOUT_MS,
|
|
23
|
-
XFYUN_ACCOUNT_BASE,
|
|
24
|
-
} from "./constants.mjs"
|
|
25
|
-
|
|
26
|
-
const {
|
|
27
|
-
businessCode,
|
|
28
|
-
dailyConsumedByCycle,
|
|
29
|
-
ensureSuccess,
|
|
30
|
-
isAuthErrorCode,
|
|
31
|
-
looksLikeInsufficientPoints,
|
|
32
|
-
normalizeCode,
|
|
33
|
-
normalizePhone,
|
|
34
|
-
parseModels,
|
|
35
|
-
signedHeaders,
|
|
36
|
-
} = _pure
|
|
37
|
-
|
|
38
|
-
// ════════════════════════════════════════════════════════════════════
|
|
39
|
-
// 集成网关(token 双头)
|
|
40
|
-
// ════════════════════════════════════════════════════════════════════
|
|
41
|
-
|
|
42
|
-
function integrationHeaders(session) {
|
|
43
|
-
// 实测:两个头都要,只给 Authorization 上游回鉴权失败
|
|
44
|
-
return { token: session, Authorization: `Bearer ${session}` }
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
48
|
-
* 一次业务请求。
|
|
49
|
-
*
|
|
50
|
-
* ⚠ 上游有部分错误是 **HTTP 200 + 信封 `{code:"100002"}`**,
|
|
51
|
-
* 所以「状态码 ok」不等于「成功」—— 一律交给 ensureSuccess 判业务码。
|
|
52
|
-
*/
|
|
53
|
-
export async function tokenRequest(method, pathAndQuery, session, body, what, fetcher = fetch) {
|
|
54
|
-
const headers = { ...integrationHeaders(assertSession(session)), Accept: "application/json" }
|
|
55
|
-
let payload
|
|
56
|
-
if (body !== undefined) headers["Content-Type"] = "application/json"
|
|
57
|
-
if (body !== undefined) payload = JSON.stringify(body)
|
|
58
|
-
|
|
59
|
-
let res
|
|
60
|
-
try {
|
|
61
|
-
res = await fetcher(`${INTEGRATION_BASE}${pathAndQuery}`, {
|
|
62
|
-
method,
|
|
63
|
-
headers,
|
|
64
|
-
...(payload === undefined ? {} : { body: payload }),
|
|
65
|
-
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
66
|
-
})
|
|
67
|
-
} catch (error) {
|
|
68
|
-
throw httpError(502, `${what}失败:${error?.message ?? error}`)
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
const json = await readJson(res)
|
|
72
|
-
if (!res.ok) {
|
|
73
|
-
const message = json === null ? "" : businessMessage(json)
|
|
74
|
-
if (json !== null && isAuthErrorCode(businessCode(json))) throw signInExpired(message || `${what}:登录已失效`)
|
|
75
|
-
throw httpError(res.status, `${what}返回 HTTP ${res.status}${message ? `:${message}` : ""}`)
|
|
76
|
-
}
|
|
77
|
-
if (json === null) throw httpError(502, `${what}失败:响应不是 JSON`)
|
|
78
|
-
ensureSuccess(json, what)
|
|
79
|
-
return unwrap(json)
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
/** 信封形状在两版接口里出现过两种:`data` 与 `data.data`。都认。 */
|
|
83
|
-
function unwrap(json) {
|
|
84
|
-
return json?.data?.data ?? json?.data ?? {}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
function businessMessage(json) {
|
|
88
|
-
const t = json?.desc ?? json?.message ?? json?.msg
|
|
89
|
-
return typeof t === "string" ? t.trim() : ""
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
function assertSession(session) {
|
|
93
|
-
const s = typeof session === "string" ? session.trim() : ""
|
|
94
|
-
if (!s) throw signInExpired("Loomy 账号缺少 session,请重新登录")
|
|
95
|
-
return s
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function signInExpired(message) {
|
|
99
|
-
return Object.assign(new Error(message), { signIn: "expired", status: 401 })
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
function httpError(status, message) {
|
|
103
|
-
return Object.assign(new Error(message), { status })
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
async function readJson(res) {
|
|
107
|
-
try {
|
|
108
|
-
return await res.json()
|
|
109
|
-
} catch {
|
|
110
|
-
return null
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
// ════════════════════════════════════════════════════════════════════
|
|
115
|
-
// 余额与额度
|
|
116
|
-
// ════════════════════════════════════════════════════════════════════
|
|
117
|
-
|
|
118
|
-
/**
|
|
119
|
-
* 只读余额(**首选端点**)。
|
|
120
|
-
*
|
|
121
|
-
* `/api/v1/points/balance` 实测只回三个数字,载荷最小 —— 市场现有三家都在用
|
|
122
|
-
* 带 `list` 的 `/points/records`,白拉一页流水。
|
|
123
|
-
*
|
|
124
|
-
* 注意它**不回 dailyQuota**(实测),所以占比还得靠凭据里的 quota 或流水推导。
|
|
125
|
-
*/
|
|
126
|
-
export async function fetchBalances(session, fetcher = fetch) {
|
|
127
|
-
const data = await tokenRequest("GET", "/api/v1/points/balance", session, undefined, "积分查询", fetcher)
|
|
128
|
-
const permanent = readNum(data.balance)
|
|
129
|
-
// balance 是核心字段:读不到说明形状变了,**不编造成 0**
|
|
130
|
-
if (permanent === undefined) return null
|
|
131
|
-
const daily = readNum(data.dailyBalance) ?? 0
|
|
132
|
-
const total = readNum(data.availableBalance) ?? permanent + daily
|
|
133
|
-
return { permanent, daily, total }
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
/**
|
|
137
|
-
* 从流水推今天的 daily 消耗(降级路径,只在凭据里没有当天 quota 时用)。
|
|
138
|
-
*
|
|
139
|
-
* 翻页而非取一页:耗尽的号可能一天就烧掉几十上百条,只看第一页会把
|
|
140
|
-
* 消耗**算少**,于是把已空的号误报成「还剩很多」—— 那是往反方向错。
|
|
141
|
-
* 上限 LEDGER_MAX_PAGES 页;翻不满就如实标 partial 并放弃推导(宁缺毋谎)。
|
|
142
|
-
*/
|
|
143
|
-
export async function fetchDailyLedger(session, cycleDate, fetcher = fetch) {
|
|
144
|
-
let rows = []
|
|
145
|
-
let total = 0
|
|
146
|
-
let complete = false
|
|
147
|
-
for (let page = 1; page <= LEDGER_MAX_PAGES; page++) {
|
|
148
|
-
const data = await tokenRequest(
|
|
149
|
-
"GET",
|
|
150
|
-
`/api/v1/points/records?pageNo=${page}&pageSize=${LEDGER_PAGE_SIZE}&recordType=all`,
|
|
151
|
-
session,
|
|
152
|
-
undefined,
|
|
153
|
-
"积分流水查询",
|
|
154
|
-
fetcher,
|
|
155
|
-
)
|
|
156
|
-
const list = Array.isArray(data?.list) ? data.list : []
|
|
157
|
-
rows = rows.concat(list)
|
|
158
|
-
total = readNum(data?.total) ?? rows.length
|
|
159
|
-
if (rows.length >= total || list.length === 0) {
|
|
160
|
-
complete = true
|
|
161
|
-
break
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
if (!complete) return { partial: true, consumed: 0, latestCycle: undefined }
|
|
165
|
-
|
|
166
|
-
const consumed = dailyConsumedByCycle(rows, cycleDate)
|
|
167
|
-
// 本业务日一条 daily 都没有:可能今天还没用过(消耗 0),也可能厂商改了字段。
|
|
168
|
-
// 用「流水里出现过的最新业务日」区分这两件事。
|
|
169
|
-
const cycles = new Set()
|
|
170
|
-
for (const r of rows) {
|
|
171
|
-
if (r?.consumeSource === "daily" && typeof r.dailyCycleDate === "string" && r.dailyCycleDate.trim()) {
|
|
172
|
-
cycles.add(r.dailyCycleDate.trim())
|
|
173
|
-
}
|
|
174
|
-
}
|
|
175
|
-
const latestCycle = cycles.size ? [...cycles].sort().at(-1) : undefined
|
|
176
|
-
return { partial: false, consumed, latestCycle, today: latestCycle === cycleDate }
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
// ════════════════════════════════════════════════════════════════════
|
|
180
|
-
// 签到(写端点,幂等)
|
|
181
|
-
// ════════════════════════════════════════════════════════════════════
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* 触发每日额度刷新。
|
|
185
|
-
*
|
|
186
|
-
* ⚠ `dailyQuota` / `dailyConsumed` / `dailyCycleDate` **只有这个响应里有**,
|
|
187
|
-
* 而它是写端点 —— 所以「打开面板」绝不能调它(会悄悄签到),
|
|
188
|
-
* 只有 magpie 的 checkin 钩子与登录成功后的那一次可以。
|
|
189
|
-
* ⚠ 幂等判据是 `alreadyProcessed`,重复调用同样回 HTTP 200。
|
|
190
|
-
*/
|
|
191
|
-
export async function claimDailyQuota(session, options = {}) {
|
|
192
|
-
const { fetcher = fetch, inviteCode, deviceId } = options
|
|
193
|
-
// ⚠ 首次登录初始化接受 `{inviteCode, deviceId}`:新账号在这一刻就能把邀请码
|
|
194
|
-
// 带上去(客户端 completeFirstLogin 同款)。老账号传了也无害 —— 服务端
|
|
195
|
-
// 只在首次初始化记录,重复调用不覆盖。
|
|
196
|
-
const body = {}
|
|
197
|
-
if (inviteCode) body.inviteCode = String(inviteCode).trim()
|
|
198
|
-
if (body.inviteCode && deviceId) body.deviceId = String(deviceId)
|
|
199
|
-
try {
|
|
200
|
-
const data = await tokenRequest("POST", "/api/v1/points/first-login", session, body, "每日签到", fetcher)
|
|
201
|
-
return {
|
|
202
|
-
ok: true,
|
|
203
|
-
alreadyProcessed: data?.alreadyProcessed === true,
|
|
204
|
-
dailyQuota: readNum(data?.dailyQuota),
|
|
205
|
-
dailyBalance: readNum(data?.dailyBalance),
|
|
206
|
-
dailyConsumed: readNum(data?.dailyConsumed),
|
|
207
|
-
dailyCycleDate: typeof data?.dailyCycleDate === "string" ? data.dailyCycleDate.trim() : undefined,
|
|
208
|
-
}
|
|
209
|
-
} catch (error) {
|
|
210
|
-
return { ok: false, message: String(error?.message ?? error).slice(0, 300) }
|
|
211
|
-
}
|
|
212
|
-
}
|
|
213
|
-
|
|
214
|
-
// ════════════════════════════════════════════════════════════════════
|
|
215
|
-
// 模型目录
|
|
216
|
-
// ════════════════════════════════════════════════════════════════════
|
|
217
|
-
|
|
218
|
-
export async function fetchModels(session, fetcher = fetch) {
|
|
219
|
-
const json = await rawGet(`${INTEGRATION_BASE}/api/v1/models`, integrationHeaders(assertSession(session)), fetcher)
|
|
220
|
-
if (json === null || !json.ok) throw httpError(502, `模型目录查询失败${json?.message ? `:${json.message}` : ""}`)
|
|
221
|
-
const models = parseModels(json.body)
|
|
222
|
-
if (!models.length) throw httpError(502, "Loomy 模型目录为空或格式不认识")
|
|
223
|
-
return models
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
function readNum(value) {
|
|
227
|
-
const n = typeof value === "string" ? Number(value.trim()) : Number(value)
|
|
228
|
-
return Number.isFinite(n) ? n : undefined
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
function readNum2(value, fallback) {
|
|
232
|
-
const n = readNum(value)
|
|
233
|
-
return n === undefined ? fallback : n
|
|
234
|
-
}
|
|
235
|
-
|
|
236
|
-
// ════════════════════════════════════════════════════════════════════
|
|
237
|
-
// 账号接口(签名 POST)
|
|
238
|
-
// ════════════════════════════════════════════════════════════════════
|
|
239
|
-
|
|
240
|
-
function clientBase() {
|
|
241
|
-
return {
|
|
242
|
-
appid: APP_ID,
|
|
243
|
-
modelid: CLIENT_MODEL_ID,
|
|
244
|
-
version: CLIENT_VERSION,
|
|
245
|
-
devid: CLIENT_DEVICE_ID,
|
|
246
|
-
ua: CLIENT_UA,
|
|
247
|
-
traceid: traceId(),
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
export async function signedPost(path, param, what, fetcher = fetch) {
|
|
252
|
-
const body = JSON.stringify({ base: clientBase(), param })
|
|
253
|
-
const headers = {
|
|
254
|
-
...signedHeaders("POST", path, body, secrets),
|
|
255
|
-
"Content-Type": "application/json",
|
|
256
|
-
}
|
|
257
|
-
let res
|
|
258
|
-
try {
|
|
259
|
-
res = await fetcher(`${XFYUN_ACCOUNT_BASE}${path}`, { method: "POST", headers, body, signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) })
|
|
260
|
-
} catch (error) {
|
|
261
|
-
throw httpError(502, `${what}失败:${error?.message ?? error}`)
|
|
262
|
-
}
|
|
263
|
-
const json = await readJson(res)
|
|
264
|
-
if (!res.ok) throw httpError(res.status, `${what}返回 HTTP ${res.status}`)
|
|
265
|
-
ensureSuccess(json ?? {}, what)
|
|
266
|
-
return json ?? {}
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
/** 发送短信验证码。回 msgid 供 checkCode 用。 */
|
|
270
|
-
export async function sendSmsCode(phone, fetcher = fetch) {
|
|
271
|
-
const payload = await signedPost("/login/phone/sendMsgCode", { ccode: "86", phone: normalizePhone(phone), expire: 300 }, "发送验证码", fetcher)
|
|
272
|
-
const msgid = pickMsgid(payload)
|
|
273
|
-
if (!msgid) throw httpError(502, "验证码已发出但上游没有返回 msgid")
|
|
274
|
-
return msgid
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
/** 验证码换 session。Loomy **没有续期接口**,这个 session 就是 14 天的全部。 */
|
|
278
|
-
export async function loginBySmsCode(phone, code, msgid, fetcher = fetch) {
|
|
279
|
-
const payload = await signedPost(
|
|
280
|
-
"/login/phone/checkCode",
|
|
281
|
-
{ ccode: "86", phone: normalizePhone(phone), mcode: normalizeCode(code), msgid, expire: 14 * 24 * 3600 },
|
|
282
|
-
"登录",
|
|
283
|
-
fetcher,
|
|
284
|
-
)
|
|
285
|
-
const data = payload?.data ?? {}
|
|
286
|
-
const session = typeof data.session === "string" ? data.session.trim() : ""
|
|
287
|
-
const userid = typeof data.userid === "string" ? data.userid.trim() : ""
|
|
288
|
-
if (!session) throw httpError(502, "登录成功但上游没有返回 session")
|
|
289
|
-
return { session, userid }
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
function pickMsgid(payload) {
|
|
293
|
-
for (const v of [payload?.data?.msgid, payload?.data?.msgId, payload?.data?.data?.msgid, payload?.data?.data?.msgId]) {
|
|
294
|
-
if (typeof v === "string" && v.trim()) return v.trim()
|
|
295
|
-
}
|
|
296
|
-
return ""
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
async function rawGet(url, headers, fetcher) {
|
|
300
|
-
try {
|
|
301
|
-
const res = await fetcher(url, { method: "GET", headers, signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) })
|
|
302
|
-
const body = await res.json().catch(() => null)
|
|
303
|
-
return { ok: res.ok, status: res.status, body }
|
|
304
|
-
} catch {
|
|
305
|
-
return null
|
|
306
|
-
}
|
|
307
|
-
}
|
|
308
|
-
|
|
309
|
-
export { looksLikeInsufficientPoints, AUTH_ERROR_CODES, readNum2 as readNumOr }
|
|
310
|
-
|
|
311
|
-
// ════════════════════════════════════════════════════════════════════
|
|
312
|
-
// 新手任务(onboarding)
|
|
313
|
-
// ════════════════════════════════════════════════════════════════════
|
|
314
|
-
|
|
315
|
-
/**
|
|
316
|
-
* 读新手任务状态(**只读**)。
|
|
317
|
-
*
|
|
318
|
-
* `GET /api/v1/onboarding/tasks` 实测回 `{tasks:{key:bool}, earned, total}`。
|
|
319
|
-
* ⚠ 不要在 usage 的热路径上调它(另一个端点 + 会频繁刷新)。
|
|
320
|
-
*/
|
|
321
|
-
export async function fetchOnboarding(session, fetcher = fetch) {
|
|
322
|
-
const data = await tokenRequest("GET", "/api/v1/onboarding/tasks", session, undefined, "新手任务查询", fetcher)
|
|
323
|
-
return {
|
|
324
|
-
tasks: _pure.normalizeTasks(data?.tasks),
|
|
325
|
-
earned: readNum(data?.earned) ?? _pure.earnedPoints(_pure.normalizeTasks(data?.tasks)),
|
|
326
|
-
total: readNum(data?.total),
|
|
327
|
-
}
|
|
328
|
-
}
|
|
329
|
-
|
|
330
|
-
/**
|
|
331
|
-
* 领取一个任务(**幂等写**)。
|
|
332
|
-
*
|
|
333
|
-
* 判据是响应体的 `alreadyCompleted`(重复领取同样回 200)。
|
|
334
|
-
* 返回 `{ ok, alreadyCompleted, balance }`,不抛错 —— 批量领取不该因一项失败中断。
|
|
335
|
-
*/
|
|
336
|
-
export async function completeTask(session, key, fetcher = fetch) {
|
|
337
|
-
try {
|
|
338
|
-
const data = await tokenRequest(
|
|
339
|
-
"POST",
|
|
340
|
-
"/api/v1/onboarding/tasks/complete",
|
|
341
|
-
session,
|
|
342
|
-
{ key },
|
|
343
|
-
"新手任务领取",
|
|
344
|
-
fetcher,
|
|
345
|
-
)
|
|
346
|
-
return {
|
|
347
|
-
ok: true,
|
|
348
|
-
alreadyCompleted: data?.alreadyCompleted === true,
|
|
349
|
-
balance: readNum(data?.balance),
|
|
350
|
-
}
|
|
351
|
-
} catch (error) {
|
|
352
|
-
return { ok: false, message: String(error?.message ?? error).slice(0, 200) }
|
|
353
|
-
}
|
|
354
|
-
}
|
|
355
|
-
|
|
356
|
-
/**
|
|
357
|
-
* 一次性领完所有未完成的任务。
|
|
358
|
-
*
|
|
359
|
-
* ⚠ 按**分值从高到低**领:万一中途失败,领到的是大的那几项。
|
|
360
|
-
* ⚠ 每项之间不并发:上游对同一账号的并发不友好(对话通道实测会 403),
|
|
361
|
-
* 串行虽然慢一点,但不会把号打成风控。
|
|
362
|
-
*/
|
|
363
|
-
export async function claimAllTasks(session, tasks, fetcher = fetch) {
|
|
364
|
-
const pending = _pure.pendingTasks(tasks ?? {})
|
|
365
|
-
const claimed = []
|
|
366
|
-
const failed = []
|
|
367
|
-
for (const item of pending) {
|
|
368
|
-
const r = await completeTask(session, item.key, fetcher)
|
|
369
|
-
if (r.ok) claimed.push({ ...item, alreadyCompleted: r.alreadyCompleted })
|
|
370
|
-
else failed.push({ ...item, message: r.message })
|
|
371
|
-
}
|
|
372
|
-
return { claimed, failed }
|
|
373
|
-
}
|
|
374
|
-
|
|
375
|
-
// ════════════════════════════════════════════════════════════════════
|
|
376
|
-
// 邀请码(referral)
|
|
377
|
-
// ════════════════════════════════════════════════════════════════════
|
|
378
|
-
//
|
|
379
|
-
// 实测(2026-10-26,两个真实账号):
|
|
380
|
-
// GET /api/v1/invitation-codes → {list:[{inviteCode,usedCount,maxUses,status}]}
|
|
381
|
-
// 189****8051 → E4DPT6(上限 1000,已用 0)
|
|
382
|
-
// 181****0632 → FDW3CU(上限 1000,已用 0)
|
|
383
|
-
// GET /api/v1/points/activation → {activated:false}
|
|
384
|
-
// POST /api/v1/points/activation → 绑定别人的邀请码(**每个账号只能一次**)
|
|
385
|
-
//
|
|
386
|
-
// 客户端自己的文案(asar 里 `gde()` 的常量 `nb = 5e3`):
|
|
387
|
-
// 「分享有礼 — 邀请好友双方各获取 5000 积分」
|
|
388
|
-
// ⚠ 是 **5000**,别和「新手任务里 share_soul 的 2000」搞混 —— 两个不同的奖励。
|
|
389
|
-
//
|
|
390
|
-
// 还有一个更省事的路子:`POST /api/v1/points/first-login` **本身接受
|
|
391
|
-
// `{inviteCode, deviceId}`**(见客户端 `completeFirstLogin`)。也就是说新账号
|
|
392
|
-
// 在**首次登录初始化**那一刻就能把邀请码带上去,不用事后再绑定一次。
|
|
393
|
-
// 我们的 `claimDailyQuota` 调的正是这个端点,所以给它加一个可选参数即可。
|
|
394
|
-
|
|
395
|
-
/** 查自己的邀请码(只读)。多个码时优先 `status === "active"`。 */
|
|
396
|
-
export async function fetchInviteCodes(session, fetcher = fetch) {
|
|
397
|
-
const data = await tokenRequest("GET", "/api/v1/invitation-codes", session, undefined, "邀请码查询", fetcher)
|
|
398
|
-
const list = Array.isArray(data?.list) ? data.list : []
|
|
399
|
-
return list
|
|
400
|
-
.map((x) => ({
|
|
401
|
-
code: typeof x?.inviteCode === "string" ? x.inviteCode.trim() : "",
|
|
402
|
-
usedCount: readNum(x?.usedCount) ?? 0,
|
|
403
|
-
maxUses: readNum(x?.maxUses) ?? 0,
|
|
404
|
-
status: typeof x?.status === "string" ? x.status.trim() : "",
|
|
405
|
-
}))
|
|
406
|
-
.filter((x) => x.code)
|
|
407
|
-
}
|
|
408
|
-
|
|
409
|
-
/** 本账号是否已经用过邀请码(只读)。**用过就不能再用**(客户端文案:兑换一次)。 */
|
|
410
|
-
export async function fetchActivation(session, fetcher = fetch) {
|
|
411
|
-
const data = await tokenRequest("GET", "/api/v1/points/activation", session, undefined, "邀请状态查询", fetcher)
|
|
412
|
-
return { activated: data?.activated === true }
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
/**
|
|
416
|
-
* 绑定别人的邀请码(**一次性写**)。
|
|
417
|
-
*
|
|
418
|
-
* 返回 `{ ok, activated, alreadyActivated, message }`,不抛错 ——
|
|
419
|
-
* 失败不该让调用方(登录流程)整个挂掉。
|
|
420
|
-
*/
|
|
421
|
-
export async function bindInviteCode(session, inviteCode, deviceId, fetcher = fetch) {
|
|
422
|
-
const code = String(inviteCode ?? "").trim()
|
|
423
|
-
if (!code) return { ok: false, message: "缺少邀请码" }
|
|
424
|
-
const body = { inviteCode: code }
|
|
425
|
-
if (deviceId) body.deviceId = String(deviceId)
|
|
426
|
-
try {
|
|
427
|
-
const data = await tokenRequest("POST", "/api/v1/points/activation", session, body, "邀请码绑定", fetcher)
|
|
428
|
-
return {
|
|
429
|
-
ok: true,
|
|
430
|
-
activated: data?.activated === true,
|
|
431
|
-
alreadyActivated: data?.alreadyActivated === true,
|
|
432
|
-
}
|
|
433
|
-
} catch (error) {
|
|
434
|
-
return { ok: false, message: String(error?.message ?? error).slice(0, 200) }
|
|
435
|
-
}
|
|
436
|
-
}
|
|
1
|
+
// Loomy · 网络层
|
|
2
|
+
//
|
|
3
|
+
// 两条通道(实测 2026-10-26):
|
|
4
|
+
// 业务/积分/目录 INTEGRATION_BASE 鉴权 = token 双头(session 即 token)
|
|
5
|
+
// 账号/短信 XFYUN_ACCOUNT_BASE 鉴权 = HMAC-SHA1 签名(客户端公开常量)
|
|
6
|
+
//
|
|
7
|
+
// 延迟实测(同账号各 3 次):/points/balance 61~233ms,/points/records 50~78ms。
|
|
8
|
+
// ⇒ 热路径只用一次请求;quota 从凭据里取,不为它多发一次。
|
|
9
|
+
|
|
10
|
+
import { _pure } from "./pure.mjs"
|
|
11
|
+
import { secrets, traceId } from "./crypto.mjs"
|
|
12
|
+
import {
|
|
13
|
+
APP_ID,
|
|
14
|
+
AUTH_ERROR_CODES,
|
|
15
|
+
CLIENT_DEVICE_ID,
|
|
16
|
+
CLIENT_MODEL_ID,
|
|
17
|
+
CLIENT_UA,
|
|
18
|
+
CLIENT_VERSION,
|
|
19
|
+
INTEGRATION_BASE,
|
|
20
|
+
LEDGER_MAX_PAGES,
|
|
21
|
+
LEDGER_PAGE_SIZE,
|
|
22
|
+
REQUEST_TIMEOUT_MS,
|
|
23
|
+
XFYUN_ACCOUNT_BASE,
|
|
24
|
+
} from "./constants.mjs"
|
|
25
|
+
|
|
26
|
+
const {
|
|
27
|
+
businessCode,
|
|
28
|
+
dailyConsumedByCycle,
|
|
29
|
+
ensureSuccess,
|
|
30
|
+
isAuthErrorCode,
|
|
31
|
+
looksLikeInsufficientPoints,
|
|
32
|
+
normalizeCode,
|
|
33
|
+
normalizePhone,
|
|
34
|
+
parseModels,
|
|
35
|
+
signedHeaders,
|
|
36
|
+
} = _pure
|
|
37
|
+
|
|
38
|
+
// ════════════════════════════════════════════════════════════════════
|
|
39
|
+
// 集成网关(token 双头)
|
|
40
|
+
// ════════════════════════════════════════════════════════════════════
|
|
41
|
+
|
|
42
|
+
function integrationHeaders(session) {
|
|
43
|
+
// 实测:两个头都要,只给 Authorization 上游回鉴权失败
|
|
44
|
+
return { token: session, Authorization: `Bearer ${session}` }
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* 一次业务请求。
|
|
49
|
+
*
|
|
50
|
+
* ⚠ 上游有部分错误是 **HTTP 200 + 信封 `{code:"100002"}`**,
|
|
51
|
+
* 所以「状态码 ok」不等于「成功」—— 一律交给 ensureSuccess 判业务码。
|
|
52
|
+
*/
|
|
53
|
+
export async function tokenRequest(method, pathAndQuery, session, body, what, fetcher = fetch) {
|
|
54
|
+
const headers = { ...integrationHeaders(assertSession(session)), Accept: "application/json" }
|
|
55
|
+
let payload
|
|
56
|
+
if (body !== undefined) headers["Content-Type"] = "application/json"
|
|
57
|
+
if (body !== undefined) payload = JSON.stringify(body)
|
|
58
|
+
|
|
59
|
+
let res
|
|
60
|
+
try {
|
|
61
|
+
res = await fetcher(`${INTEGRATION_BASE}${pathAndQuery}`, {
|
|
62
|
+
method,
|
|
63
|
+
headers,
|
|
64
|
+
...(payload === undefined ? {} : { body: payload }),
|
|
65
|
+
signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS),
|
|
66
|
+
})
|
|
67
|
+
} catch (error) {
|
|
68
|
+
throw httpError(502, `${what}失败:${error?.message ?? error}`)
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const json = await readJson(res)
|
|
72
|
+
if (!res.ok) {
|
|
73
|
+
const message = json === null ? "" : businessMessage(json)
|
|
74
|
+
if (json !== null && isAuthErrorCode(businessCode(json))) throw signInExpired(message || `${what}:登录已失效`)
|
|
75
|
+
throw httpError(res.status, `${what}返回 HTTP ${res.status}${message ? `:${message}` : ""}`)
|
|
76
|
+
}
|
|
77
|
+
if (json === null) throw httpError(502, `${what}失败:响应不是 JSON`)
|
|
78
|
+
ensureSuccess(json, what)
|
|
79
|
+
return unwrap(json)
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
/** 信封形状在两版接口里出现过两种:`data` 与 `data.data`。都认。 */
|
|
83
|
+
function unwrap(json) {
|
|
84
|
+
return json?.data?.data ?? json?.data ?? {}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function businessMessage(json) {
|
|
88
|
+
const t = json?.desc ?? json?.message ?? json?.msg
|
|
89
|
+
return typeof t === "string" ? t.trim() : ""
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function assertSession(session) {
|
|
93
|
+
const s = typeof session === "string" ? session.trim() : ""
|
|
94
|
+
if (!s) throw signInExpired("Loomy 账号缺少 session,请重新登录")
|
|
95
|
+
return s
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function signInExpired(message) {
|
|
99
|
+
return Object.assign(new Error(message), { signIn: "expired", status: 401 })
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
function httpError(status, message) {
|
|
103
|
+
return Object.assign(new Error(message), { status })
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function readJson(res) {
|
|
107
|
+
try {
|
|
108
|
+
return await res.json()
|
|
109
|
+
} catch {
|
|
110
|
+
return null
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// ════════════════════════════════════════════════════════════════════
|
|
115
|
+
// 余额与额度
|
|
116
|
+
// ════════════════════════════════════════════════════════════════════
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* 只读余额(**首选端点**)。
|
|
120
|
+
*
|
|
121
|
+
* `/api/v1/points/balance` 实测只回三个数字,载荷最小 —— 市场现有三家都在用
|
|
122
|
+
* 带 `list` 的 `/points/records`,白拉一页流水。
|
|
123
|
+
*
|
|
124
|
+
* 注意它**不回 dailyQuota**(实测),所以占比还得靠凭据里的 quota 或流水推导。
|
|
125
|
+
*/
|
|
126
|
+
export async function fetchBalances(session, fetcher = fetch) {
|
|
127
|
+
const data = await tokenRequest("GET", "/api/v1/points/balance", session, undefined, "积分查询", fetcher)
|
|
128
|
+
const permanent = readNum(data.balance)
|
|
129
|
+
// balance 是核心字段:读不到说明形状变了,**不编造成 0**
|
|
130
|
+
if (permanent === undefined) return null
|
|
131
|
+
const daily = readNum(data.dailyBalance) ?? 0
|
|
132
|
+
const total = readNum(data.availableBalance) ?? permanent + daily
|
|
133
|
+
return { permanent, daily, total }
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* 从流水推今天的 daily 消耗(降级路径,只在凭据里没有当天 quota 时用)。
|
|
138
|
+
*
|
|
139
|
+
* 翻页而非取一页:耗尽的号可能一天就烧掉几十上百条,只看第一页会把
|
|
140
|
+
* 消耗**算少**,于是把已空的号误报成「还剩很多」—— 那是往反方向错。
|
|
141
|
+
* 上限 LEDGER_MAX_PAGES 页;翻不满就如实标 partial 并放弃推导(宁缺毋谎)。
|
|
142
|
+
*/
|
|
143
|
+
export async function fetchDailyLedger(session, cycleDate, fetcher = fetch) {
|
|
144
|
+
let rows = []
|
|
145
|
+
let total = 0
|
|
146
|
+
let complete = false
|
|
147
|
+
for (let page = 1; page <= LEDGER_MAX_PAGES; page++) {
|
|
148
|
+
const data = await tokenRequest(
|
|
149
|
+
"GET",
|
|
150
|
+
`/api/v1/points/records?pageNo=${page}&pageSize=${LEDGER_PAGE_SIZE}&recordType=all`,
|
|
151
|
+
session,
|
|
152
|
+
undefined,
|
|
153
|
+
"积分流水查询",
|
|
154
|
+
fetcher,
|
|
155
|
+
)
|
|
156
|
+
const list = Array.isArray(data?.list) ? data.list : []
|
|
157
|
+
rows = rows.concat(list)
|
|
158
|
+
total = readNum(data?.total) ?? rows.length
|
|
159
|
+
if (rows.length >= total || list.length === 0) {
|
|
160
|
+
complete = true
|
|
161
|
+
break
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
if (!complete) return { partial: true, consumed: 0, latestCycle: undefined }
|
|
165
|
+
|
|
166
|
+
const consumed = dailyConsumedByCycle(rows, cycleDate)
|
|
167
|
+
// 本业务日一条 daily 都没有:可能今天还没用过(消耗 0),也可能厂商改了字段。
|
|
168
|
+
// 用「流水里出现过的最新业务日」区分这两件事。
|
|
169
|
+
const cycles = new Set()
|
|
170
|
+
for (const r of rows) {
|
|
171
|
+
if (r?.consumeSource === "daily" && typeof r.dailyCycleDate === "string" && r.dailyCycleDate.trim()) {
|
|
172
|
+
cycles.add(r.dailyCycleDate.trim())
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
const latestCycle = cycles.size ? [...cycles].sort().at(-1) : undefined
|
|
176
|
+
return { partial: false, consumed, latestCycle, today: latestCycle === cycleDate }
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// ════════════════════════════════════════════════════════════════════
|
|
180
|
+
// 签到(写端点,幂等)
|
|
181
|
+
// ════════════════════════════════════════════════════════════════════
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* 触发每日额度刷新。
|
|
185
|
+
*
|
|
186
|
+
* ⚠ `dailyQuota` / `dailyConsumed` / `dailyCycleDate` **只有这个响应里有**,
|
|
187
|
+
* 而它是写端点 —— 所以「打开面板」绝不能调它(会悄悄签到),
|
|
188
|
+
* 只有 magpie 的 checkin 钩子与登录成功后的那一次可以。
|
|
189
|
+
* ⚠ 幂等判据是 `alreadyProcessed`,重复调用同样回 HTTP 200。
|
|
190
|
+
*/
|
|
191
|
+
export async function claimDailyQuota(session, options = {}) {
|
|
192
|
+
const { fetcher = fetch, inviteCode, deviceId } = options
|
|
193
|
+
// ⚠ 首次登录初始化接受 `{inviteCode, deviceId}`:新账号在这一刻就能把邀请码
|
|
194
|
+
// 带上去(客户端 completeFirstLogin 同款)。老账号传了也无害 —— 服务端
|
|
195
|
+
// 只在首次初始化记录,重复调用不覆盖。
|
|
196
|
+
const body = {}
|
|
197
|
+
if (inviteCode) body.inviteCode = String(inviteCode).trim()
|
|
198
|
+
if (body.inviteCode && deviceId) body.deviceId = String(deviceId)
|
|
199
|
+
try {
|
|
200
|
+
const data = await tokenRequest("POST", "/api/v1/points/first-login", session, body, "每日签到", fetcher)
|
|
201
|
+
return {
|
|
202
|
+
ok: true,
|
|
203
|
+
alreadyProcessed: data?.alreadyProcessed === true,
|
|
204
|
+
dailyQuota: readNum(data?.dailyQuota),
|
|
205
|
+
dailyBalance: readNum(data?.dailyBalance),
|
|
206
|
+
dailyConsumed: readNum(data?.dailyConsumed),
|
|
207
|
+
dailyCycleDate: typeof data?.dailyCycleDate === "string" ? data.dailyCycleDate.trim() : undefined,
|
|
208
|
+
}
|
|
209
|
+
} catch (error) {
|
|
210
|
+
return { ok: false, message: String(error?.message ?? error).slice(0, 300) }
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ════════════════════════════════════════════════════════════════════
|
|
215
|
+
// 模型目录
|
|
216
|
+
// ════════════════════════════════════════════════════════════════════
|
|
217
|
+
|
|
218
|
+
export async function fetchModels(session, fetcher = fetch) {
|
|
219
|
+
const json = await rawGet(`${INTEGRATION_BASE}/api/v1/models`, integrationHeaders(assertSession(session)), fetcher)
|
|
220
|
+
if (json === null || !json.ok) throw httpError(502, `模型目录查询失败${json?.message ? `:${json.message}` : ""}`)
|
|
221
|
+
const models = parseModels(json.body)
|
|
222
|
+
if (!models.length) throw httpError(502, "Loomy 模型目录为空或格式不认识")
|
|
223
|
+
return models
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
function readNum(value) {
|
|
227
|
+
const n = typeof value === "string" ? Number(value.trim()) : Number(value)
|
|
228
|
+
return Number.isFinite(n) ? n : undefined
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
function readNum2(value, fallback) {
|
|
232
|
+
const n = readNum(value)
|
|
233
|
+
return n === undefined ? fallback : n
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// ════════════════════════════════════════════════════════════════════
|
|
237
|
+
// 账号接口(签名 POST)
|
|
238
|
+
// ════════════════════════════════════════════════════════════════════
|
|
239
|
+
|
|
240
|
+
function clientBase() {
|
|
241
|
+
return {
|
|
242
|
+
appid: APP_ID,
|
|
243
|
+
modelid: CLIENT_MODEL_ID,
|
|
244
|
+
version: CLIENT_VERSION,
|
|
245
|
+
devid: CLIENT_DEVICE_ID,
|
|
246
|
+
ua: CLIENT_UA,
|
|
247
|
+
traceid: traceId(),
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
export async function signedPost(path, param, what, fetcher = fetch) {
|
|
252
|
+
const body = JSON.stringify({ base: clientBase(), param })
|
|
253
|
+
const headers = {
|
|
254
|
+
...signedHeaders("POST", path, body, secrets),
|
|
255
|
+
"Content-Type": "application/json",
|
|
256
|
+
}
|
|
257
|
+
let res
|
|
258
|
+
try {
|
|
259
|
+
res = await fetcher(`${XFYUN_ACCOUNT_BASE}${path}`, { method: "POST", headers, body, signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) })
|
|
260
|
+
} catch (error) {
|
|
261
|
+
throw httpError(502, `${what}失败:${error?.message ?? error}`)
|
|
262
|
+
}
|
|
263
|
+
const json = await readJson(res)
|
|
264
|
+
if (!res.ok) throw httpError(res.status, `${what}返回 HTTP ${res.status}`)
|
|
265
|
+
ensureSuccess(json ?? {}, what)
|
|
266
|
+
return json ?? {}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
/** 发送短信验证码。回 msgid 供 checkCode 用。 */
|
|
270
|
+
export async function sendSmsCode(phone, fetcher = fetch) {
|
|
271
|
+
const payload = await signedPost("/login/phone/sendMsgCode", { ccode: "86", phone: normalizePhone(phone), expire: 300 }, "发送验证码", fetcher)
|
|
272
|
+
const msgid = pickMsgid(payload)
|
|
273
|
+
if (!msgid) throw httpError(502, "验证码已发出但上游没有返回 msgid")
|
|
274
|
+
return msgid
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/** 验证码换 session。Loomy **没有续期接口**,这个 session 就是 14 天的全部。 */
|
|
278
|
+
export async function loginBySmsCode(phone, code, msgid, fetcher = fetch) {
|
|
279
|
+
const payload = await signedPost(
|
|
280
|
+
"/login/phone/checkCode",
|
|
281
|
+
{ ccode: "86", phone: normalizePhone(phone), mcode: normalizeCode(code), msgid, expire: 14 * 24 * 3600 },
|
|
282
|
+
"登录",
|
|
283
|
+
fetcher,
|
|
284
|
+
)
|
|
285
|
+
const data = payload?.data ?? {}
|
|
286
|
+
const session = typeof data.session === "string" ? data.session.trim() : ""
|
|
287
|
+
const userid = typeof data.userid === "string" ? data.userid.trim() : ""
|
|
288
|
+
if (!session) throw httpError(502, "登录成功但上游没有返回 session")
|
|
289
|
+
return { session, userid }
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
function pickMsgid(payload) {
|
|
293
|
+
for (const v of [payload?.data?.msgid, payload?.data?.msgId, payload?.data?.data?.msgid, payload?.data?.data?.msgId]) {
|
|
294
|
+
if (typeof v === "string" && v.trim()) return v.trim()
|
|
295
|
+
}
|
|
296
|
+
return ""
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
async function rawGet(url, headers, fetcher) {
|
|
300
|
+
try {
|
|
301
|
+
const res = await fetcher(url, { method: "GET", headers, signal: AbortSignal.timeout(REQUEST_TIMEOUT_MS) })
|
|
302
|
+
const body = await res.json().catch(() => null)
|
|
303
|
+
return { ok: res.ok, status: res.status, body }
|
|
304
|
+
} catch {
|
|
305
|
+
return null
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
export { looksLikeInsufficientPoints, AUTH_ERROR_CODES, readNum2 as readNumOr }
|
|
310
|
+
|
|
311
|
+
// ════════════════════════════════════════════════════════════════════
|
|
312
|
+
// 新手任务(onboarding)
|
|
313
|
+
// ════════════════════════════════════════════════════════════════════
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* 读新手任务状态(**只读**)。
|
|
317
|
+
*
|
|
318
|
+
* `GET /api/v1/onboarding/tasks` 实测回 `{tasks:{key:bool}, earned, total}`。
|
|
319
|
+
* ⚠ 不要在 usage 的热路径上调它(另一个端点 + 会频繁刷新)。
|
|
320
|
+
*/
|
|
321
|
+
export async function fetchOnboarding(session, fetcher = fetch) {
|
|
322
|
+
const data = await tokenRequest("GET", "/api/v1/onboarding/tasks", session, undefined, "新手任务查询", fetcher)
|
|
323
|
+
return {
|
|
324
|
+
tasks: _pure.normalizeTasks(data?.tasks),
|
|
325
|
+
earned: readNum(data?.earned) ?? _pure.earnedPoints(_pure.normalizeTasks(data?.tasks)),
|
|
326
|
+
total: readNum(data?.total),
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* 领取一个任务(**幂等写**)。
|
|
332
|
+
*
|
|
333
|
+
* 判据是响应体的 `alreadyCompleted`(重复领取同样回 200)。
|
|
334
|
+
* 返回 `{ ok, alreadyCompleted, balance }`,不抛错 —— 批量领取不该因一项失败中断。
|
|
335
|
+
*/
|
|
336
|
+
export async function completeTask(session, key, fetcher = fetch) {
|
|
337
|
+
try {
|
|
338
|
+
const data = await tokenRequest(
|
|
339
|
+
"POST",
|
|
340
|
+
"/api/v1/onboarding/tasks/complete",
|
|
341
|
+
session,
|
|
342
|
+
{ key },
|
|
343
|
+
"新手任务领取",
|
|
344
|
+
fetcher,
|
|
345
|
+
)
|
|
346
|
+
return {
|
|
347
|
+
ok: true,
|
|
348
|
+
alreadyCompleted: data?.alreadyCompleted === true,
|
|
349
|
+
balance: readNum(data?.balance),
|
|
350
|
+
}
|
|
351
|
+
} catch (error) {
|
|
352
|
+
return { ok: false, message: String(error?.message ?? error).slice(0, 200) }
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* 一次性领完所有未完成的任务。
|
|
358
|
+
*
|
|
359
|
+
* ⚠ 按**分值从高到低**领:万一中途失败,领到的是大的那几项。
|
|
360
|
+
* ⚠ 每项之间不并发:上游对同一账号的并发不友好(对话通道实测会 403),
|
|
361
|
+
* 串行虽然慢一点,但不会把号打成风控。
|
|
362
|
+
*/
|
|
363
|
+
export async function claimAllTasks(session, tasks, fetcher = fetch) {
|
|
364
|
+
const pending = _pure.pendingTasks(tasks ?? {})
|
|
365
|
+
const claimed = []
|
|
366
|
+
const failed = []
|
|
367
|
+
for (const item of pending) {
|
|
368
|
+
const r = await completeTask(session, item.key, fetcher)
|
|
369
|
+
if (r.ok) claimed.push({ ...item, alreadyCompleted: r.alreadyCompleted })
|
|
370
|
+
else failed.push({ ...item, message: r.message })
|
|
371
|
+
}
|
|
372
|
+
return { claimed, failed }
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// ════════════════════════════════════════════════════════════════════
|
|
376
|
+
// 邀请码(referral)
|
|
377
|
+
// ════════════════════════════════════════════════════════════════════
|
|
378
|
+
//
|
|
379
|
+
// 实测(2026-10-26,两个真实账号):
|
|
380
|
+
// GET /api/v1/invitation-codes → {list:[{inviteCode,usedCount,maxUses,status}]}
|
|
381
|
+
// 189****8051 → E4DPT6(上限 1000,已用 0)
|
|
382
|
+
// 181****0632 → FDW3CU(上限 1000,已用 0)
|
|
383
|
+
// GET /api/v1/points/activation → {activated:false}
|
|
384
|
+
// POST /api/v1/points/activation → 绑定别人的邀请码(**每个账号只能一次**)
|
|
385
|
+
//
|
|
386
|
+
// 客户端自己的文案(asar 里 `gde()` 的常量 `nb = 5e3`):
|
|
387
|
+
// 「分享有礼 — 邀请好友双方各获取 5000 积分」
|
|
388
|
+
// ⚠ 是 **5000**,别和「新手任务里 share_soul 的 2000」搞混 —— 两个不同的奖励。
|
|
389
|
+
//
|
|
390
|
+
// 还有一个更省事的路子:`POST /api/v1/points/first-login` **本身接受
|
|
391
|
+
// `{inviteCode, deviceId}`**(见客户端 `completeFirstLogin`)。也就是说新账号
|
|
392
|
+
// 在**首次登录初始化**那一刻就能把邀请码带上去,不用事后再绑定一次。
|
|
393
|
+
// 我们的 `claimDailyQuota` 调的正是这个端点,所以给它加一个可选参数即可。
|
|
394
|
+
|
|
395
|
+
/** 查自己的邀请码(只读)。多个码时优先 `status === "active"`。 */
|
|
396
|
+
export async function fetchInviteCodes(session, fetcher = fetch) {
|
|
397
|
+
const data = await tokenRequest("GET", "/api/v1/invitation-codes", session, undefined, "邀请码查询", fetcher)
|
|
398
|
+
const list = Array.isArray(data?.list) ? data.list : []
|
|
399
|
+
return list
|
|
400
|
+
.map((x) => ({
|
|
401
|
+
code: typeof x?.inviteCode === "string" ? x.inviteCode.trim() : "",
|
|
402
|
+
usedCount: readNum(x?.usedCount) ?? 0,
|
|
403
|
+
maxUses: readNum(x?.maxUses) ?? 0,
|
|
404
|
+
status: typeof x?.status === "string" ? x.status.trim() : "",
|
|
405
|
+
}))
|
|
406
|
+
.filter((x) => x.code)
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/** 本账号是否已经用过邀请码(只读)。**用过就不能再用**(客户端文案:兑换一次)。 */
|
|
410
|
+
export async function fetchActivation(session, fetcher = fetch) {
|
|
411
|
+
const data = await tokenRequest("GET", "/api/v1/points/activation", session, undefined, "邀请状态查询", fetcher)
|
|
412
|
+
return { activated: data?.activated === true }
|
|
413
|
+
}
|
|
414
|
+
|
|
415
|
+
/**
|
|
416
|
+
* 绑定别人的邀请码(**一次性写**)。
|
|
417
|
+
*
|
|
418
|
+
* 返回 `{ ok, activated, alreadyActivated, message }`,不抛错 ——
|
|
419
|
+
* 失败不该让调用方(登录流程)整个挂掉。
|
|
420
|
+
*/
|
|
421
|
+
export async function bindInviteCode(session, inviteCode, deviceId, fetcher = fetch) {
|
|
422
|
+
const code = String(inviteCode ?? "").trim()
|
|
423
|
+
if (!code) return { ok: false, message: "缺少邀请码" }
|
|
424
|
+
const body = { inviteCode: code }
|
|
425
|
+
if (deviceId) body.deviceId = String(deviceId)
|
|
426
|
+
try {
|
|
427
|
+
const data = await tokenRequest("POST", "/api/v1/points/activation", session, body, "邀请码绑定", fetcher)
|
|
428
|
+
return {
|
|
429
|
+
ok: true,
|
|
430
|
+
activated: data?.activated === true,
|
|
431
|
+
alreadyActivated: data?.alreadyActivated === true,
|
|
432
|
+
}
|
|
433
|
+
} catch (error) {
|
|
434
|
+
return { ok: false, message: String(error?.message ?? error).slice(0, 200) }
|
|
435
|
+
}
|
|
436
|
+
}
|