dsh-sub2api 0.0.0-stage → 0.1.4
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/CHANGELOG.md +48 -0
- package/LICENSE +674 -0
- package/README.md +163 -2
- package/cordis.patch.yml +16 -0
- package/lib/adapter.js +205 -0
- package/lib/client.js +754 -0
- package/lib/config.js +86 -0
- package/lib/constants.js +64 -0
- package/lib/index.js +197 -0
- package/lib/pay.js +243 -0
- package/lib/routes.js +378 -0
- package/lib/schema.js +37 -0
- package/lib/sub2api/api.js +247 -0
- package/lib/sub2api/auth.js +106 -0
- package/lib/sub2api/catalog.js +330 -0
- package/lib/sub2api/provision.js +244 -0
- package/lib/sub2api/session.js +187 -0
- package/lib/tools.js +171 -0
- package/lib/util.js +225 -0
- package/package.json +76 -4
package/lib/client.js
ADDED
|
@@ -0,0 +1,754 @@
|
|
|
1
|
+
window.__ModuleLoader__.load({
|
|
2
|
+
id: "dsh-sub2api",
|
|
3
|
+
factory: (require) => {
|
|
4
|
+
const react = require("react");
|
|
5
|
+
var module = { exports: {} };
|
|
6
|
+
var exports = module.exports;
|
|
7
|
+
const h = react.createElement;
|
|
8
|
+
const { useCallback, useEffect, useRef, useState } = react;
|
|
9
|
+
|
|
10
|
+
// ---------------------------------------------------------------- api
|
|
11
|
+
|
|
12
|
+
const PREFIX = "/api/sub2api";
|
|
13
|
+
|
|
14
|
+
class ApiError extends Error {
|
|
15
|
+
constructor(message, code) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.code = code;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function call(path, options) {
|
|
22
|
+
const init = { method: options?.method ?? "GET", headers: {} };
|
|
23
|
+
if (options?.body !== undefined) {
|
|
24
|
+
init.headers["Content-Type"] = "application/json";
|
|
25
|
+
init.body = JSON.stringify(options.body);
|
|
26
|
+
}
|
|
27
|
+
let response;
|
|
28
|
+
try {
|
|
29
|
+
response = await fetch(PREFIX + path, init);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
throw new ApiError("网络请求失败:" + String(error && error.message ? error.message : error));
|
|
32
|
+
}
|
|
33
|
+
let payload;
|
|
34
|
+
try {
|
|
35
|
+
payload = await response.json();
|
|
36
|
+
} catch {
|
|
37
|
+
throw new ApiError("HTTP " + response.status + ":响应不是 JSON");
|
|
38
|
+
}
|
|
39
|
+
if (!payload || payload.ok !== true) {
|
|
40
|
+
throw new ApiError(
|
|
41
|
+
payload && payload.error ? payload.error : "HTTP " + response.status,
|
|
42
|
+
payload && payload.code,
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
return payload.data;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// -------------------------------------------------------------- theme
|
|
49
|
+
|
|
50
|
+
const C = {
|
|
51
|
+
text: "var(--dsw-alias-label-primary, #111827)",
|
|
52
|
+
muted: "var(--dsw-alias-label-tertiary, #8b929c)",
|
|
53
|
+
second: "var(--dsw-alias-label-secondary, #5b6472)",
|
|
54
|
+
line: "var(--dsw-alias-border-l3, rgba(0,0,0,0.10))",
|
|
55
|
+
surface: "var(--dsw-alias-bg-layer-1, #ffffff)",
|
|
56
|
+
sunken: "var(--dsw-alias-bg-layer-2, #f6f7f9)",
|
|
57
|
+
base: "var(--dsw-alias-bg-base, #ffffff)",
|
|
58
|
+
accent: "var(--dsw-alias-state-business-primary, #2563eb)",
|
|
59
|
+
ok: "var(--dsw-alias-state-success-primary, #15803d)",
|
|
60
|
+
warn: "var(--dsw-alias-state-warning-primary, #b45309)",
|
|
61
|
+
bad: "var(--dsw-alias-state-error-primary, #dc2626)",
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const T = {
|
|
65
|
+
card: {
|
|
66
|
+
border: "0.5px solid " + C.line,
|
|
67
|
+
borderRadius: 16,
|
|
68
|
+
background: C.surface,
|
|
69
|
+
marginBottom: 12,
|
|
70
|
+
overflow: "hidden",
|
|
71
|
+
},
|
|
72
|
+
cardHead: {
|
|
73
|
+
display: "flex",
|
|
74
|
+
alignItems: "center",
|
|
75
|
+
gap: 10,
|
|
76
|
+
padding: "14px 18px 12px",
|
|
77
|
+
borderBottom: "0.5px solid " + C.line,
|
|
78
|
+
},
|
|
79
|
+
cardTitle: { margin: 0, fontSize: 14, fontWeight: 600, lineHeight: "22px", color: C.text },
|
|
80
|
+
cardBody: { padding: "16px 18px" },
|
|
81
|
+
meta: { marginLeft: "auto", fontSize: 12, lineHeight: "18px", color: C.muted },
|
|
82
|
+
label: { fontSize: 13, fontWeight: 500, lineHeight: "20px", color: C.second },
|
|
83
|
+
hint: { fontSize: 12, lineHeight: "18px", color: C.muted },
|
|
84
|
+
small: { fontSize: 12, lineHeight: "18px", color: C.second },
|
|
85
|
+
row: { display: "flex", gap: 10, alignItems: "center", flexWrap: "wrap" },
|
|
86
|
+
mono: { fontFamily: "ui-monospace, SFMono-Regular, Menlo, monospace", fontSize: 12.5 },
|
|
87
|
+
};
|
|
88
|
+
|
|
89
|
+
function Button({ variant = "default", size = "md", style, children, ...rest }) {
|
|
90
|
+
const base = {
|
|
91
|
+
display: "inline-flex",
|
|
92
|
+
alignItems: "center",
|
|
93
|
+
justifyContent: "center",
|
|
94
|
+
gap: 6,
|
|
95
|
+
height: size === "sm" ? 30 : 36,
|
|
96
|
+
padding: size === "sm" ? "0 12px" : "0 16px",
|
|
97
|
+
borderRadius: 10,
|
|
98
|
+
fontSize: 13,
|
|
99
|
+
fontWeight: 500,
|
|
100
|
+
cursor: rest.disabled ? "not-allowed" : "pointer",
|
|
101
|
+
opacity: rest.disabled ? 0.5 : 1,
|
|
102
|
+
transition: "background 120ms ease, border-color 120ms ease",
|
|
103
|
+
whiteSpace: "nowrap",
|
|
104
|
+
border: "0.5px solid " + C.line,
|
|
105
|
+
background: C.sunken,
|
|
106
|
+
color: C.text,
|
|
107
|
+
};
|
|
108
|
+
const variants = {
|
|
109
|
+
primary: { border: "1px solid transparent", background: C.accent, color: "#fff" },
|
|
110
|
+
ghost: { border: "0.5px solid transparent", background: "transparent", color: C.second },
|
|
111
|
+
danger: { border: "0.5px solid " + C.line, background: "transparent", color: C.bad },
|
|
112
|
+
};
|
|
113
|
+
return h("button", { style: { ...base, ...(variants[variant] || {}), ...style }, ...rest }, children);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
function Card({ title, meta, children, bodyStyle, id }) {
|
|
117
|
+
return h("section", { id, style: T.card },
|
|
118
|
+
title
|
|
119
|
+
? h("div", { style: T.cardHead },
|
|
120
|
+
h("h3", { style: T.cardTitle }, title),
|
|
121
|
+
meta ? h("span", { style: T.meta }, meta) : null)
|
|
122
|
+
: null,
|
|
123
|
+
h("div", { style: { ...T.cardBody, ...bodyStyle } }, children),
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** A labelled input with an optional trailing control and a focus ring. */
|
|
128
|
+
function TextField({ label, hint, trailing, style, ...rest }) {
|
|
129
|
+
const [focused, setFocused] = useState(false);
|
|
130
|
+
return h("label", { style: { display: "flex", flexDirection: "column", gap: 6, minWidth: 0, ...style } },
|
|
131
|
+
label ? h("span", { style: T.label }, label) : null,
|
|
132
|
+
h("div", {
|
|
133
|
+
style: {
|
|
134
|
+
display: "flex",
|
|
135
|
+
alignItems: "center",
|
|
136
|
+
gap: 6,
|
|
137
|
+
height: 38,
|
|
138
|
+
padding: "0 4px 0 12px",
|
|
139
|
+
borderRadius: 10,
|
|
140
|
+
border: "0.5px solid " + (focused ? C.accent : C.line),
|
|
141
|
+
boxShadow: focused ? "0 0 0 3px " + "color-mix(in srgb, " + C.accent + " 14%, transparent)" : "none",
|
|
142
|
+
background: C.base,
|
|
143
|
+
transition: "border-color 120ms ease, box-shadow 120ms ease",
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
h("input", {
|
|
147
|
+
...rest,
|
|
148
|
+
onFocus: () => setFocused(true),
|
|
149
|
+
onBlur: () => setFocused(false),
|
|
150
|
+
style: {
|
|
151
|
+
flex: 1,
|
|
152
|
+
minWidth: 0,
|
|
153
|
+
height: "100%",
|
|
154
|
+
border: "none",
|
|
155
|
+
outline: "none",
|
|
156
|
+
background: "transparent",
|
|
157
|
+
color: C.text,
|
|
158
|
+
fontSize: 13.5,
|
|
159
|
+
},
|
|
160
|
+
}),
|
|
161
|
+
trailing ?? null,
|
|
162
|
+
),
|
|
163
|
+
hint ? h("span", { style: T.hint }, hint) : null,
|
|
164
|
+
);
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function Alert({ tone = "error", title, children, action }) {
|
|
168
|
+
const palette = {
|
|
169
|
+
error: { fg: C.bad, bg: "color-mix(in srgb, " + C.bad + " 8%, transparent)" },
|
|
170
|
+
warn: { fg: C.warn, bg: "color-mix(in srgb, " + C.warn + " 9%, transparent)" },
|
|
171
|
+
info: { fg: C.accent, bg: "color-mix(in srgb, " + C.accent + " 8%, transparent)" },
|
|
172
|
+
ok: { fg: C.ok, bg: "color-mix(in srgb, " + C.ok + " 8%, transparent)" },
|
|
173
|
+
}[tone];
|
|
174
|
+
return h("div", {
|
|
175
|
+
style: {
|
|
176
|
+
display: "flex",
|
|
177
|
+
gap: 10,
|
|
178
|
+
alignItems: "flex-start",
|
|
179
|
+
padding: "10px 12px",
|
|
180
|
+
borderRadius: 12,
|
|
181
|
+
background: palette.bg,
|
|
182
|
+
color: palette.fg,
|
|
183
|
+
fontSize: 12.5,
|
|
184
|
+
lineHeight: "19px",
|
|
185
|
+
},
|
|
186
|
+
},
|
|
187
|
+
h("div", { style: { flex: 1, minWidth: 0 } },
|
|
188
|
+
title ? h("div", { style: { fontWeight: 600, marginBottom: 2 } }, title) : null,
|
|
189
|
+
children),
|
|
190
|
+
action ?? null,
|
|
191
|
+
);
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
function money(value) {
|
|
195
|
+
const n = Number(value);
|
|
196
|
+
return Number.isFinite(n) ? "$" + n.toFixed(2) : "—";
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
function Divider() {
|
|
200
|
+
return h("div", { style: { height: 1, background: C.line, margin: "14px 0" } });
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// ---------------------------------------------------------- account
|
|
204
|
+
|
|
205
|
+
/** Never hand React an object: it would render "[object Object]". */
|
|
206
|
+
function asText(value) {
|
|
207
|
+
if (typeof value === "string") return value;
|
|
208
|
+
if (value === null || value === undefined) return "";
|
|
209
|
+
if (typeof value === "object") {
|
|
210
|
+
if (typeof value.message === "string") return value.message;
|
|
211
|
+
try {
|
|
212
|
+
return JSON.stringify(value);
|
|
213
|
+
} catch {
|
|
214
|
+
return "";
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return String(value);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
function ProvisionReport({ steps, ready }) {
|
|
221
|
+
if (!Array.isArray(steps)) return null;
|
|
222
|
+
const notable = steps.filter((s) => !s.ok && s.tone !== "warn");
|
|
223
|
+
if (notable.length === 0) return null;
|
|
224
|
+
return h("div", { style: { marginTop: 12, display: "flex", flexDirection: "column", gap: 6 } },
|
|
225
|
+
h("div", { style: { ...T.hint, fontWeight: 600, color: C.warn } },
|
|
226
|
+
ready === false ? "开通未完成" : "部分步骤未完成"),
|
|
227
|
+
notable.map((s) =>
|
|
228
|
+
h(Alert, { key: s.id, tone: "error", title: s.label }, asText(s.error) || "失败")),
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
function PasswordInput({ value, onChange, placeholder, autoComplete, onEnter }) {
|
|
233
|
+
const [visible, setVisible] = useState(false);
|
|
234
|
+
return h(TextField, {
|
|
235
|
+
label: "密码",
|
|
236
|
+
type: visible ? "text" : "password",
|
|
237
|
+
value,
|
|
238
|
+
placeholder,
|
|
239
|
+
autoComplete,
|
|
240
|
+
onChange: (e) => onChange(e.target.value),
|
|
241
|
+
onKeyDown: (e) => { if (e.key === "Enter" && onEnter) onEnter(); },
|
|
242
|
+
trailing: h(Button, {
|
|
243
|
+
variant: "ghost",
|
|
244
|
+
size: "sm",
|
|
245
|
+
type: "button",
|
|
246
|
+
onClick: () => setVisible((v) => !v),
|
|
247
|
+
style: { height: 30, padding: "0 10px", fontSize: 12, color: C.muted },
|
|
248
|
+
}, visible ? "隐藏" : "显示"),
|
|
249
|
+
});
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
function AccountCard({ state, busy, steps, ready, onAuth, onLogout }) {
|
|
253
|
+
const [email, setEmail] = useState("");
|
|
254
|
+
const [password, setPassword] = useState("");
|
|
255
|
+
const [promo, setPromo] = useState("");
|
|
256
|
+
const [showPromo, setShowPromo] = useState(false);
|
|
257
|
+
const [error, setError] = useState("");
|
|
258
|
+
|
|
259
|
+
const submit = useCallback(async () => {
|
|
260
|
+
setError("");
|
|
261
|
+
try {
|
|
262
|
+
await onAuth({ email: email.trim(), password, promoCode: promo.trim(), allowRegister: true });
|
|
263
|
+
setPassword("");
|
|
264
|
+
setPromo("");
|
|
265
|
+
} catch (e) {
|
|
266
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
267
|
+
}
|
|
268
|
+
}, [email, password, promo, onAuth]);
|
|
269
|
+
|
|
270
|
+
if (state.loggedIn) {
|
|
271
|
+
const user = state.user || {};
|
|
272
|
+
const balance = state.profile ? state.profile.balance : undefined;
|
|
273
|
+
return h(Card, {
|
|
274
|
+
title: "账户",
|
|
275
|
+
meta: state.apiBase,
|
|
276
|
+
},
|
|
277
|
+
h("div", { style: { display: "flex", alignItems: "center", gap: 12, flexWrap: "wrap" } },
|
|
278
|
+
h("div", { style: { display: "flex", flexDirection: "column", gap: 2, minWidth: 0 } },
|
|
279
|
+
h("span", { style: { fontSize: 14, fontWeight: 600, color: C.text } },
|
|
280
|
+
state.email || user.email || "已登录"),
|
|
281
|
+
h("span", { style: T.hint }, "余额 " + money(balance)),
|
|
282
|
+
),
|
|
283
|
+
h("div", { style: { marginLeft: "auto" } },
|
|
284
|
+
h(Button, { onClick: onLogout, disabled: busy }, "退出登录")),
|
|
285
|
+
),
|
|
286
|
+
Array.isArray(state.subscriptions?.subscriptions) && state.subscriptions.subscriptions.length > 0
|
|
287
|
+
? h("div", { style: { marginTop: 12 } },
|
|
288
|
+
h(Alert, { tone: "info" },
|
|
289
|
+
state.subscriptions.subscriptions
|
|
290
|
+
.map((s) => `${s.group_name || "订阅"} · 剩余 ${s.days_remaining ?? "?"} 天`)
|
|
291
|
+
.join(" ")))
|
|
292
|
+
: null,
|
|
293
|
+
h(ProvisionReport, { steps, ready }),
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
return h(Card, { title: "登录或注册", meta: state.apiBase },
|
|
298
|
+
h("div", { style: { display: "flex", flexDirection: "column", gap: 12 } },
|
|
299
|
+
h(TextField, {
|
|
300
|
+
label: "邮箱",
|
|
301
|
+
type: "email",
|
|
302
|
+
autoComplete: "username",
|
|
303
|
+
placeholder: "you@example.com",
|
|
304
|
+
value: email,
|
|
305
|
+
onChange: (e) => setEmail(e.target.value),
|
|
306
|
+
onKeyDown: (e) => { if (e.key === "Enter") submit(); },
|
|
307
|
+
}),
|
|
308
|
+
h(PasswordInput, {
|
|
309
|
+
value: password,
|
|
310
|
+
onChange: setPassword,
|
|
311
|
+
placeholder: "至少 6 位",
|
|
312
|
+
autoComplete: "current-password",
|
|
313
|
+
onEnter: submit,
|
|
314
|
+
}),
|
|
315
|
+
showPromo
|
|
316
|
+
? h(TextField, {
|
|
317
|
+
label: "优惠码(可选)",
|
|
318
|
+
placeholder: "注册时填写",
|
|
319
|
+
value: promo,
|
|
320
|
+
onChange: (e) => setPromo(e.target.value),
|
|
321
|
+
})
|
|
322
|
+
: null,
|
|
323
|
+
h(Button, {
|
|
324
|
+
variant: "primary",
|
|
325
|
+
onClick: submit,
|
|
326
|
+
disabled: busy || !email || password.length < 6,
|
|
327
|
+
style: { width: "100%", height: 38 },
|
|
328
|
+
}, busy ? "处理中…" : "继续"),
|
|
329
|
+
error ? h(Alert, { tone: "error" }, error) : null,
|
|
330
|
+
h(Divider),
|
|
331
|
+
h("div", { style: { display: "flex", alignItems: "center", gap: 8, flexWrap: "wrap" } },
|
|
332
|
+
h("span", { style: T.hint },
|
|
333
|
+
"没有账号会自动注册,注册后立即创建密钥 ", h("code", { style: T.mono }, state.keyName || "dsh"), " 并同步模型。"),
|
|
334
|
+
h(Button, {
|
|
335
|
+
variant: "ghost",
|
|
336
|
+
size: "sm",
|
|
337
|
+
onClick: () => setShowPromo((v) => !v),
|
|
338
|
+
style: { marginLeft: "auto" },
|
|
339
|
+
}, showPromo ? "取消优惠码" : "填写优惠码"),
|
|
340
|
+
),
|
|
341
|
+
),
|
|
342
|
+
);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
// -------------------------------------------------------------- key
|
|
346
|
+
|
|
347
|
+
function KeyCard({ state, busy, onCreate, onReveal }) {
|
|
348
|
+
const [revealed, setRevealed] = useState("");
|
|
349
|
+
const [copied, setCopied] = useState(false);
|
|
350
|
+
|
|
351
|
+
const copy = async () => {
|
|
352
|
+
try {
|
|
353
|
+
const text = revealed || (await onReveal()).key;
|
|
354
|
+
setRevealed(text);
|
|
355
|
+
if (text) await navigator.clipboard.writeText(text);
|
|
356
|
+
setCopied(true);
|
|
357
|
+
setTimeout(() => setCopied(false), 1500);
|
|
358
|
+
} catch {
|
|
359
|
+
// Clipboard may be blocked; the value is still rendered.
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
return h(Card, { title: "API 密钥", meta: state.hasKey ? "已保存" : "未创建" },
|
|
364
|
+
h("div", { style: T.row },
|
|
365
|
+
h("code", {
|
|
366
|
+
style: {
|
|
367
|
+
...T.mono,
|
|
368
|
+
flex: "1 1 220px",
|
|
369
|
+
minWidth: 0,
|
|
370
|
+
padding: "8px 10px",
|
|
371
|
+
borderRadius: 10,
|
|
372
|
+
background: C.sunken,
|
|
373
|
+
color: C.text,
|
|
374
|
+
overflow: "hidden",
|
|
375
|
+
textOverflow: "ellipsis",
|
|
376
|
+
whiteSpace: "nowrap",
|
|
377
|
+
},
|
|
378
|
+
}, revealed || state.keyMasked || "尚未创建"),
|
|
379
|
+
h(Button, { onClick: copy, disabled: !state.loggedIn || !state.hasKey },
|
|
380
|
+
copied ? "已复制" : "复制"),
|
|
381
|
+
h(Button, { onClick: onCreate, disabled: busy || !state.loggedIn },
|
|
382
|
+
state.hasKey ? "重建密钥" : "一键创建"),
|
|
383
|
+
),
|
|
384
|
+
h("div", { style: { ...T.hint, marginTop: 10 } },
|
|
385
|
+
"密钥固定命名为 ", h("code", { style: T.mono }, state.keyName || "dsh"),
|
|
386
|
+
",保存在本机凭据库;页面只显示掩码,「复制」时才临时取回明文。"),
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
// --------------------------------------------------------- recharge
|
|
391
|
+
|
|
392
|
+
const PRESETS = [10, 20, 50, 100];
|
|
393
|
+
|
|
394
|
+
function RechargeCard({ state, busy, onRecharge, onPoll, onRefresh }) {
|
|
395
|
+
const [amount, setAmount] = useState(20);
|
|
396
|
+
const [custom, setCustom] = useState("");
|
|
397
|
+
const [order, setOrder] = useState(null);
|
|
398
|
+
const [status, setStatus] = useState(null);
|
|
399
|
+
const [error, setError] = useState("");
|
|
400
|
+
const timer = useRef(null);
|
|
401
|
+
|
|
402
|
+
const value = custom !== "" ? Number(custom) : amount;
|
|
403
|
+
const credit = Number.isFinite(value) ? value * 2 : 0;
|
|
404
|
+
const balance = state.profile ? Number(state.profile.balance) : NaN;
|
|
405
|
+
|
|
406
|
+
useEffect(() => {
|
|
407
|
+
if (!order) return undefined;
|
|
408
|
+
timer.current = setInterval(async () => {
|
|
409
|
+
try {
|
|
410
|
+
const result = await onPoll(order.orderNo);
|
|
411
|
+
setStatus(result);
|
|
412
|
+
if (result.tone === "done" || result.tone === "error") {
|
|
413
|
+
clearInterval(timer.current);
|
|
414
|
+
timer.current = null;
|
|
415
|
+
if (result.tone === "done") await onRefresh();
|
|
416
|
+
}
|
|
417
|
+
} catch (e) {
|
|
418
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
419
|
+
clearInterval(timer.current);
|
|
420
|
+
timer.current = null;
|
|
421
|
+
}
|
|
422
|
+
}, 5000);
|
|
423
|
+
return () => { if (timer.current) clearInterval(timer.current); };
|
|
424
|
+
}, [order, onPoll, onRefresh]);
|
|
425
|
+
|
|
426
|
+
const start = async () => {
|
|
427
|
+
setError("");
|
|
428
|
+
const tab = window.open("", "_blank");
|
|
429
|
+
try {
|
|
430
|
+
const data = await onRecharge(value);
|
|
431
|
+
const target = data.checkoutUrl || data.url;
|
|
432
|
+
if (tab) tab.location.href = target;
|
|
433
|
+
else window.open(target, "_blank", "noopener");
|
|
434
|
+
if (data.orderNo) setOrder(data);
|
|
435
|
+
} catch (e) {
|
|
436
|
+
if (tab) tab.close();
|
|
437
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
438
|
+
}
|
|
439
|
+
};
|
|
440
|
+
|
|
441
|
+
return h(Card, {
|
|
442
|
+
id: "sub2api-recharge",
|
|
443
|
+
title: "余额与充值",
|
|
444
|
+
meta: Number.isFinite(balance) ? "当前 " + money(balance) : null,
|
|
445
|
+
},
|
|
446
|
+
h("div", { style: { ...T.row, marginBottom: 12 } },
|
|
447
|
+
h("span", { style: T.small }, "1 元 = 2.00 美元额度"),
|
|
448
|
+
h("span", { style: { ...T.hint, marginLeft: "auto" } },
|
|
449
|
+
Number.isFinite(balance) && balance <= 0 ? "余额为 0 时无法拉取模型" : ""),
|
|
450
|
+
),
|
|
451
|
+
h("div", { style: { ...T.row, marginBottom: 12 } },
|
|
452
|
+
PRESETS.map((preset) =>
|
|
453
|
+
h(Button, {
|
|
454
|
+
key: preset,
|
|
455
|
+
size: "sm",
|
|
456
|
+
variant: custom === "" && amount === preset ? "primary" : "default",
|
|
457
|
+
onClick: () => { setAmount(preset); setCustom(""); },
|
|
458
|
+
style: { minWidth: 58, borderRadius: 999 },
|
|
459
|
+
}, "¥" + preset),
|
|
460
|
+
),
|
|
461
|
+
h(TextField, {
|
|
462
|
+
placeholder: "自定义",
|
|
463
|
+
inputMode: "decimal",
|
|
464
|
+
value: custom,
|
|
465
|
+
onChange: (e) => setCustom(e.target.value.replace(/[^0-9.]/g, "")),
|
|
466
|
+
style: { flex: "0 1 120px", minWidth: 100 },
|
|
467
|
+
}),
|
|
468
|
+
),
|
|
469
|
+
h("div", { style: { ...T.row, marginBottom: 12 } },
|
|
470
|
+
h("span", { style: T.small }, "预计到账"),
|
|
471
|
+
h("span", { style: { fontSize: 15, fontWeight: 600, color: C.text } }, money(credit)),
|
|
472
|
+
h("div", { style: { marginLeft: "auto", display: "flex", gap: 8 } },
|
|
473
|
+
h(Button, { onClick: onRefresh, disabled: !state.loggedIn }, "刷新余额"),
|
|
474
|
+
h(Button, {
|
|
475
|
+
variant: "primary",
|
|
476
|
+
onClick: start,
|
|
477
|
+
disabled: busy || !state.loggedIn || !Number.isFinite(value) || value <= 0,
|
|
478
|
+
}, "去支付"),
|
|
479
|
+
),
|
|
480
|
+
),
|
|
481
|
+
h("div", { style: T.hint },
|
|
482
|
+
"「去支付」会在新标签页打开支付页,到账后余额自动更新;订单 30 分钟内有效。"),
|
|
483
|
+
order
|
|
484
|
+
? h("div", { style: { marginTop: 12 } },
|
|
485
|
+
h(Alert, {
|
|
486
|
+
tone: status ? (status.tone === "done" ? "ok" : status.tone === "error" ? "error" : "info") : "info",
|
|
487
|
+
title: "订单 " + order.orderNo,
|
|
488
|
+
action: status && status.tone === "done" ? null : h(Button, {
|
|
489
|
+
size: "sm",
|
|
490
|
+
variant: "ghost",
|
|
491
|
+
onClick: () => setOrder(null),
|
|
492
|
+
style: { color: "inherit" },
|
|
493
|
+
}, "关闭"),
|
|
494
|
+
}, status ? status.text : "已打开支付页,等待付款…"))
|
|
495
|
+
: null,
|
|
496
|
+
error ? h("div", { style: { marginTop: 10 } }, h(Alert, { tone: "error" }, error)) : null,
|
|
497
|
+
);
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
// ------------------------------------------------------------ models
|
|
501
|
+
|
|
502
|
+
function ModelsCard({ state, busy, onRefreshModels, onGoRecharge }) {
|
|
503
|
+
const models = state.models || { models: [], fetchedAt: 0, stale: false, error: null };
|
|
504
|
+
const list = models.models || [];
|
|
505
|
+
const [open, setOpen] = useState(false);
|
|
506
|
+
const [error, setError] = useState("");
|
|
507
|
+
|
|
508
|
+
const refresh = async () => {
|
|
509
|
+
setError("");
|
|
510
|
+
try {
|
|
511
|
+
const result = await onRefreshModels();
|
|
512
|
+
if (result && result.error) setError(result.error.message || String(result.error));
|
|
513
|
+
} catch (e) {
|
|
514
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
515
|
+
}
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
const reason = error || models.error?.message || "";
|
|
519
|
+
|
|
520
|
+
return h(Card, {
|
|
521
|
+
title: "模型",
|
|
522
|
+
meta: list.length > 0
|
|
523
|
+
? list.length + " 个 · " + (models.fetchedAt ? new Date(models.fetchedAt).toLocaleTimeString() : "—")
|
|
524
|
+
: "0 个 · 未同步",
|
|
525
|
+
},
|
|
526
|
+
models.needsRecharge
|
|
527
|
+
? h("div", { style: { marginBottom: 12 } },
|
|
528
|
+
h(Alert, {
|
|
529
|
+
tone: "warn",
|
|
530
|
+
title: "余额不足,暂时拿不到模型列表",
|
|
531
|
+
action: h(Button, {
|
|
532
|
+
size: "sm",
|
|
533
|
+
onClick: onGoRecharge,
|
|
534
|
+
style: { color: "inherit", borderColor: "currentColor", background: "transparent" },
|
|
535
|
+
}, "去充值"),
|
|
536
|
+
},
|
|
537
|
+
"Sub2API 在余额为 0 时会拒绝 /v1/models。充值到账后这里会自动同步,无需重新登录。"))
|
|
538
|
+
: null,
|
|
539
|
+
|
|
540
|
+
!models.needsRecharge && reason
|
|
541
|
+
? h("div", { style: { marginBottom: 12 } }, h(Alert, { tone: "error", title: "同步失败" }, reason))
|
|
542
|
+
: null,
|
|
543
|
+
|
|
544
|
+
h("div", { style: T.row },
|
|
545
|
+
h(Button, { onClick: refresh, disabled: busy || !state.loggedIn }, busy ? "同步中…" : "刷新模型"),
|
|
546
|
+
h(Button, { onClick: () => setOpen((v) => !v), disabled: list.length === 0 },
|
|
547
|
+
open ? "收起列表" : "查看列表"),
|
|
548
|
+
models.stale && list.length > 0 ? h("span", { style: T.hint }, "缓存已过期") : null,
|
|
549
|
+
),
|
|
550
|
+
|
|
551
|
+
open
|
|
552
|
+
? h("div", {
|
|
553
|
+
style: {
|
|
554
|
+
marginTop: 12,
|
|
555
|
+
maxHeight: 240,
|
|
556
|
+
overflow: "auto",
|
|
557
|
+
border: "0.5px solid " + C.line,
|
|
558
|
+
borderRadius: 12,
|
|
559
|
+
},
|
|
560
|
+
},
|
|
561
|
+
list.map((model) =>
|
|
562
|
+
h("div", {
|
|
563
|
+
key: model.id,
|
|
564
|
+
style: {
|
|
565
|
+
display: "flex",
|
|
566
|
+
gap: 10,
|
|
567
|
+
alignItems: "baseline",
|
|
568
|
+
padding: "7px 12px",
|
|
569
|
+
borderTop: "0.5px solid " + C.line,
|
|
570
|
+
},
|
|
571
|
+
},
|
|
572
|
+
h("code", { style: { ...T.mono, flex: "1 1 auto", minWidth: 0, overflow: "hidden", textOverflow: "ellipsis" } }, model.id),
|
|
573
|
+
model.contextWindow ? h("span", { style: T.hint }, Math.round(model.contextWindow / 1000) + "k") : null,
|
|
574
|
+
model.vision ? h("span", { style: T.hint }, "图片") : null,
|
|
575
|
+
),
|
|
576
|
+
))
|
|
577
|
+
: null,
|
|
578
|
+
|
|
579
|
+
h("div", { style: { ...T.hint, marginTop: 10 } },
|
|
580
|
+
"目录来自上游 /v1/models,登录后自动同步,10 分钟内不重复拉取。"),
|
|
581
|
+
);
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// ------------------------------------------------------------- root
|
|
585
|
+
|
|
586
|
+
function Section() {
|
|
587
|
+
const [state, setState] = useState(null);
|
|
588
|
+
const [busy, setBusy] = useState(false);
|
|
589
|
+
const [error, setError] = useState("");
|
|
590
|
+
const [steps, setSteps] = useState(null);
|
|
591
|
+
const [ready, setReady] = useState(undefined);
|
|
592
|
+
|
|
593
|
+
const refresh = useCallback(async () => {
|
|
594
|
+
try {
|
|
595
|
+
const data = await call("/status");
|
|
596
|
+
setState(data);
|
|
597
|
+
setError("");
|
|
598
|
+
return data;
|
|
599
|
+
} catch (e) {
|
|
600
|
+
setError(e instanceof Error ? e.message : String(e));
|
|
601
|
+
return null;
|
|
602
|
+
}
|
|
603
|
+
}, []);
|
|
604
|
+
|
|
605
|
+
useEffect(() => { refresh(); }, [refresh]);
|
|
606
|
+
|
|
607
|
+
const wrap = useCallback(async (fn) => {
|
|
608
|
+
setBusy(true);
|
|
609
|
+
setError("");
|
|
610
|
+
try {
|
|
611
|
+
return await fn();
|
|
612
|
+
} finally {
|
|
613
|
+
setBusy(false);
|
|
614
|
+
}
|
|
615
|
+
}, []);
|
|
616
|
+
|
|
617
|
+
const onAuth = useCallback((input) => wrap(async () => {
|
|
618
|
+
const data = await call("/auth", { method: "POST", body: input });
|
|
619
|
+
setState(data.state);
|
|
620
|
+
setSteps(data.steps ?? null);
|
|
621
|
+
setReady(data.ready);
|
|
622
|
+
return data;
|
|
623
|
+
}), [wrap]);
|
|
624
|
+
|
|
625
|
+
const onLogout = useCallback(() => wrap(async () => {
|
|
626
|
+
const data = await call("/logout", { method: "POST", body: {} });
|
|
627
|
+
setState(data.state);
|
|
628
|
+
setSteps(null);
|
|
629
|
+
setReady(undefined);
|
|
630
|
+
}), [wrap]);
|
|
631
|
+
|
|
632
|
+
const onCreateKey = useCallback(() => wrap(async () => {
|
|
633
|
+
const force = !!(state && state.hasKey);
|
|
634
|
+
const data = await call("/key", { method: "POST", body: force ? { force: true } : {} });
|
|
635
|
+
setState(data.state);
|
|
636
|
+
}), [wrap, state]);
|
|
637
|
+
|
|
638
|
+
const onReveal = useCallback(() => call("/key/reveal", { method: "POST", body: {} }), []);
|
|
639
|
+
|
|
640
|
+
const onRefreshModels = useCallback(() => wrap(async () => {
|
|
641
|
+
const result = await call("/models/refresh", { method: "POST", body: {} });
|
|
642
|
+
await refresh();
|
|
643
|
+
return result;
|
|
644
|
+
}), [wrap, refresh]);
|
|
645
|
+
|
|
646
|
+
const onRecharge = useCallback(async (amountYuan) => {
|
|
647
|
+
try {
|
|
648
|
+
return await call("/recharge", { method: "POST", body: { amountYuan } });
|
|
649
|
+
} catch {
|
|
650
|
+
const link = await call("/recharge/link", { method: "POST", body: {} });
|
|
651
|
+
return { url: link.url, orderNo: null };
|
|
652
|
+
}
|
|
653
|
+
}, []);
|
|
654
|
+
|
|
655
|
+
const onPoll = useCallback((orderNo) => call("/recharge/status", { method: "POST", body: { orderNo } }), []);
|
|
656
|
+
const onRefresh = useCallback(() => refresh(), [refresh]);
|
|
657
|
+
|
|
658
|
+
if (state === null) {
|
|
659
|
+
return h("div", { style: { padding: 18, color: C.muted, fontSize: 13 } },
|
|
660
|
+
error ? h(Alert, { tone: "error" }, error) : "加载中…");
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
return h("div", { style: { padding: "4px 0 18px" } },
|
|
664
|
+
h(AccountCard, { state, busy, steps, ready, onAuth, onLogout }),
|
|
665
|
+
h(KeyCard, { state, busy, onCreate: onCreateKey, onReveal }),
|
|
666
|
+
h(RechargeCard, { state, busy, onRecharge, onPoll, onRefresh }),
|
|
667
|
+
h(ModelsCard, {
|
|
668
|
+
state,
|
|
669
|
+
busy,
|
|
670
|
+
onRefreshModels,
|
|
671
|
+
onGoRecharge: () => {
|
|
672
|
+
const target = document.getElementById("sub2api-recharge");
|
|
673
|
+
if (target && typeof target.scrollIntoView === "function") {
|
|
674
|
+
target.scrollIntoView({ behavior: "smooth", block: "center" });
|
|
675
|
+
}
|
|
676
|
+
},
|
|
677
|
+
}),
|
|
678
|
+
error ? h(Alert, { tone: "error" }, error) : null,
|
|
679
|
+
);
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
// ---------------------------------------------------------- sidebar
|
|
683
|
+
|
|
684
|
+
function SidebarCard() {
|
|
685
|
+
const [state, setState] = useState(null);
|
|
686
|
+
|
|
687
|
+
useEffect(() => {
|
|
688
|
+
let cancelled = false;
|
|
689
|
+
const load = async () => {
|
|
690
|
+
if (document.visibilityState === "hidden") return;
|
|
691
|
+
try {
|
|
692
|
+
const data = await call("/status");
|
|
693
|
+
if (!cancelled) setState(data);
|
|
694
|
+
} catch {
|
|
695
|
+
// Keep the last figure on a transient failure.
|
|
696
|
+
}
|
|
697
|
+
};
|
|
698
|
+
load();
|
|
699
|
+
const timer = setInterval(load, 30000);
|
|
700
|
+
return () => { cancelled = true; clearInterval(timer); };
|
|
701
|
+
}, []);
|
|
702
|
+
|
|
703
|
+
if (!state || !state.loggedIn) return null;
|
|
704
|
+
const balance = state.profile ? Number(state.profile.balance) : NaN;
|
|
705
|
+
const low = Number.isFinite(balance) && balance <= 0;
|
|
706
|
+
|
|
707
|
+
return h("div", {
|
|
708
|
+
style: {
|
|
709
|
+
display: "flex",
|
|
710
|
+
alignItems: "center",
|
|
711
|
+
gap: 8,
|
|
712
|
+
padding: "8px 10px",
|
|
713
|
+
borderRadius: 12,
|
|
714
|
+
border: "0.5px solid " + C.line,
|
|
715
|
+
fontSize: 12,
|
|
716
|
+
lineHeight: "18px",
|
|
717
|
+
},
|
|
718
|
+
},
|
|
719
|
+
h("span", { style: { fontWeight: 600, color: C.text } }, "Sub2API"),
|
|
720
|
+
h("span", { style: { marginLeft: "auto", color: low ? C.bad : C.muted } },
|
|
721
|
+
Number.isFinite(balance) ? (low ? "余额不足" : money(balance)) : "已登录"),
|
|
722
|
+
);
|
|
723
|
+
}
|
|
724
|
+
|
|
725
|
+
// ----------------------------------------------------------- plugin
|
|
726
|
+
|
|
727
|
+
const inject = ["slots"];
|
|
728
|
+
|
|
729
|
+
function apply(ctx) {
|
|
730
|
+
try {
|
|
731
|
+
ctx.slots.inject("settings.section", () =>
|
|
732
|
+
ctx.slots.register(
|
|
733
|
+
{ name: "settings.section", id: "sub2api", order: 300, label: () => "Sub2API" },
|
|
734
|
+
Section,
|
|
735
|
+
),
|
|
736
|
+
);
|
|
737
|
+
} catch (error) {
|
|
738
|
+
console.warn("[dsh-sub2api] 设置页注册失败:", error);
|
|
739
|
+
}
|
|
740
|
+
try {
|
|
741
|
+
ctx.slots.inject("sidebar.footer.action", () =>
|
|
742
|
+
ctx.slots.register({ name: "sidebar.footer.action", id: "sub2api-quota" }, SidebarCard),
|
|
743
|
+
);
|
|
744
|
+
} catch {
|
|
745
|
+
// The sidebar slot is optional.
|
|
746
|
+
}
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
exports.apply = apply;
|
|
750
|
+
exports.inject = inject;
|
|
751
|
+
exports.Section = Section;
|
|
752
|
+
return module.exports;
|
|
753
|
+
},
|
|
754
|
+
});
|