dsh-sub2api 0.4.1 → 0.4.2

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.
Files changed (3) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/lib/client.js +141 -13
  3. package/package.json +1 -1
package/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.2
4
+
5
+ Recharge and key handling, which are the two places where a mistake costs
6
+ something real.
7
+
8
+ - **Order countdown and recovery.** The pay centre expires an unpaid order after
9
+ 30 minutes; the panel now counts it down, says so in plain words when it runs
10
+ out, and offers "重新打开支付页" so a closed tab is not a lost order.
11
+ - **"去支付" cannot be double-clicked** into two tabs, and a failed status poll
12
+ now says so instead of quietly stopping.
13
+ - **Rebuilding the key takes two clicks.** It revokes the key DSH is using right
14
+ now, so it asks once — with the button turning red — and offers 取消.
15
+ - **A revealed key puts itself back behind the mask** after eight seconds; the
16
+ clipboard already has it.
17
+ - Model refresh failures surface as a toast instead of an unhandled rejection.
18
+ - Which sections you left open is remembered per browser, and toasts announce
19
+ themselves to screen readers.
20
+
3
21
  ## 0.4.1
4
22
 
5
23
  - The panel shows the site as **Psapi** instead of Sub2API, matching the
package/lib/client.js CHANGED
@@ -212,6 +212,8 @@ window.__ModuleLoader__.load({
212
212
  const variants = {
213
213
  primary: { border: "1px solid transparent", background: C.accent, color: "#fff" },
214
214
  ghost: { border: "0.5px solid transparent", background: "transparent", color: C.second },
215
+ // Destructive: rebuilding the key revokes the one in use.
216
+ danger: { border: "1px solid " + C.bad, background: "transparent", color: C.bad },
215
217
  };
216
218
  return h("button", { style: { ...base, ...(variants[variant] || {}), ...style }, ...rest }, children);
217
219
  }
@@ -440,7 +442,11 @@ window.__ModuleLoader__.load({
440
442
 
441
443
  function Toasts({ items }) {
442
444
  if (items.length === 0) return null;
445
+ const worst = items.some((item) => item.tone === "error") ? "assertive" : "polite";
443
446
  return h("div", {
447
+ role: "status",
448
+ "aria-live": worst,
449
+ "aria-atomic": "false",
444
450
  style: {
445
451
  position: "sticky",
446
452
  top: 0,
@@ -472,6 +478,25 @@ window.__ModuleLoader__.load({
472
478
 
473
479
  // ----------------------------------------------------------- section
474
480
 
481
+ /**
482
+ * Which blocks the user left open, remembered per browser.
483
+ *
484
+ * The panel is reopened often and every block resets to the same default
485
+ * otherwise, which means scrolling past the ones you never look at.
486
+ */
487
+ const OPEN_KEY = "dsh-sub2api:sections";
488
+ const DEFAULT_OPEN = { models: true, recharge: false, account: false, key: false, diagnose: false };
489
+
490
+ function readOpenSections() {
491
+ try {
492
+ const raw = window.localStorage.getItem(OPEN_KEY);
493
+ const parsed = raw ? JSON.parse(raw) : null;
494
+ return parsed && typeof parsed === "object" ? { ...DEFAULT_OPEN, ...parsed } : DEFAULT_OPEN;
495
+ } catch {
496
+ return DEFAULT_OPEN;
497
+ }
498
+ }
499
+
475
500
  /** A titled block that can be folded away, so the page stays scannable. */
476
501
  function Collapsible({ title, meta, open, onToggle, children, id }) {
477
502
  return h("section", { id, style: T.card },
@@ -689,6 +714,10 @@ window.__ModuleLoader__.load({
689
714
 
690
715
  function KeyDetail({ state, pending, onCreate, onReveal }) {
691
716
  const [revealed, setRevealed] = useState("");
717
+ const [confirming, setConfirming] = useState(false);
718
+ const hideTimer = useRef(null);
719
+
720
+ useEffect(() => () => { if (hideTimer.current) clearTimeout(hideTimer.current); }, []);
692
721
 
693
722
  const copy = async () => {
694
723
  try {
@@ -696,11 +725,25 @@ window.__ModuleLoader__.load({
696
725
  setRevealed(text);
697
726
  if (text) await navigator.clipboard.writeText(text);
698
727
  toastStore.push("密钥已复制");
728
+ // Put the mask back: a plaintext key left on screen is a screenshot away
729
+ // from leaking, and the clipboard already has it.
730
+ if (hideTimer.current) clearTimeout(hideTimer.current);
731
+ hideTimer.current = setTimeout(() => setRevealed(""), 8000);
699
732
  } catch {
700
733
  toastStore.push("复制失败,请手动选择", "error");
701
734
  }
702
735
  };
703
736
 
737
+ // Rebuilding revokes the key DSH is using right now, so it takes two clicks.
738
+ const rebuild = async () => {
739
+ if (!confirming) {
740
+ setConfirming(true);
741
+ return;
742
+ }
743
+ setConfirming(false);
744
+ await onCreate();
745
+ };
746
+
704
747
  return h("div", null,
705
748
  h("div", { style: T.row },
706
749
  h("code", {
@@ -718,8 +761,22 @@ window.__ModuleLoader__.load({
718
761
  },
719
762
  }, revealed || state.keyMasked || "尚未创建"),
720
763
  h(Button, { onClick: copy, disabled: !state.hasKey }, "复制"),
721
- h(Button, { onClick: onCreate, disabled: pending.key },
722
- pending.key ? "处理中…" : state.hasKey ? "重建" : "创建"),
764
+ h(Button, {
765
+ onClick: rebuild,
766
+ disabled: pending.key,
767
+ variant: confirming && state.hasKey ? "danger" : "default",
768
+ title: confirming ? "再点一次将作废旧密钥" : undefined,
769
+ },
770
+ pending.key
771
+ ? "处理中…"
772
+ : !state.hasKey
773
+ ? "创建"
774
+ : confirming
775
+ ? "确认重建"
776
+ : "重建"),
777
+ confirming
778
+ ? h(Button, { variant: "ghost", onClick: () => setConfirming(false) }, "取消")
779
+ : null,
723
780
  ),
724
781
  h("div", { style: { ...T.hint, marginTop: 10 } },
725
782
  "密钥固定命名为 ", h("code", { style: T.mono }, state.keyName || "dsh"),
@@ -731,6 +788,16 @@ window.__ModuleLoader__.load({
731
788
 
732
789
  const PRESETS = [10, 20, 50, 100];
733
790
 
791
+ /** How long the pay centre keeps an unpaid order open. */
792
+ const ORDER_TTL_MS = 30 * 60 * 1000;
793
+
794
+ function mmss(ms) {
795
+ const total = Math.max(0, Math.floor(ms / 1000));
796
+ const m = Math.floor(total / 60);
797
+ const sec = total % 60;
798
+ return `${m}:${String(sec).padStart(2, "0")}`;
799
+ }
800
+
734
801
  function RechargeDetail({ state, pending, onRecharge, onPoll, onRefresh, onRedeem }) {
735
802
  const [amount, setAmount] = useState(20);
736
803
  const [custom, setCustom] = useState("");
@@ -738,14 +805,28 @@ window.__ModuleLoader__.load({
738
805
  const [status, setStatus] = useState(null);
739
806
  const [code, setCode] = useState("");
740
807
  const [showRedeem, setShowRedeem] = useState(false);
808
+ const [opening, setOpening] = useState(false);
809
+ const [now, setNow] = useState(() => Date.now());
741
810
  const timer = useRef(null);
742
811
 
743
812
  const multiplier = Number(state.rechargeMultiplier) > 0 ? Number(state.rechargeMultiplier) : 2;
744
813
  const value = custom !== "" ? Number(custom) : amount;
745
814
  const credit = Number.isFinite(value) ? value * multiplier : 0;
746
815
 
816
+ // An order expires in 30 minutes; showing the clock beats leaving the user
817
+ // staring at "等待付款…" that will never resolve.
747
818
  useEffect(() => {
748
819
  if (!order) return undefined;
820
+ const tick = setInterval(() => setNow(Date.now()), 1000);
821
+ return () => clearInterval(tick);
822
+ }, [order]);
823
+
824
+ const expiresAt = order && order.createdAt ? order.createdAt + ORDER_TTL_MS : null;
825
+ const leftMs = expiresAt === null ? null : expiresAt - now;
826
+ const expired = leftMs !== null && leftMs <= 0;
827
+
828
+ useEffect(() => {
829
+ if (!order || expired) return undefined;
749
830
  timer.current = setInterval(async () => {
750
831
  try {
751
832
  const result = await onPoll(order.orderNo);
@@ -758,28 +839,42 @@ window.__ModuleLoader__.load({
758
839
  await onRefresh();
759
840
  }
760
841
  }
761
- } catch {
842
+ } catch (e) {
762
843
  clearInterval(timer.current);
763
844
  timer.current = null;
845
+ toastStore.push(
846
+ "查询订单失败:" + (e instanceof Error ? e.message : String(e)) + ";可点「刷新」看余额",
847
+ "error",
848
+ );
764
849
  }
765
850
  }, 5000);
766
851
  return () => { if (timer.current) clearInterval(timer.current); };
767
- }, [order, onPoll, onRefresh]);
852
+ }, [order, onPoll, onRefresh, expired]);
768
853
 
769
854
  const start = async () => {
855
+ if (opening) return;
770
856
  const tab = window.open("", "_blank");
857
+ setOpening(true);
771
858
  try {
772
859
  const data = await onRecharge(value);
773
860
  const target = data.checkoutUrl || data.url;
774
861
  if (tab) tab.location.href = target;
775
862
  else window.open(target, "_blank", "noopener");
776
- if (data.orderNo) setOrder(data);
863
+ if (data.orderNo) setOrder({ ...data, createdAt: Date.now() });
777
864
  } catch (e) {
778
865
  if (tab) tab.close();
779
866
  toastStore.push(e instanceof Error ? e.message : String(e), "error");
867
+ } finally {
868
+ setOpening(false);
780
869
  }
781
870
  };
782
871
 
872
+ const reopen = () => {
873
+ const target = order && (order.checkoutUrl || order.url);
874
+ if (target) window.open(target, "_blank", "noopener");
875
+ else toastStore.push("没有可用的支付链接,请重新下单", "error");
876
+ };
877
+
783
878
  const redeem = async () => {
784
879
  try {
785
880
  const result = await onRedeem(code.trim());
@@ -822,16 +917,30 @@ window.__ModuleLoader__.load({
822
917
  h(Button, {
823
918
  variant: "primary",
824
919
  onClick: start,
825
- disabled: !state.loggedIn || !Number.isFinite(value) || value <= 0,
826
- }, "去支付"),
920
+ disabled: !state.loggedIn || !Number.isFinite(value) || value <= 0 || opening,
921
+ }, opening ? "正在下单…" : "去支付"),
827
922
  ),
828
923
  ),
829
924
  order
830
925
  ? h("div", { style: { marginTop: 12 } },
831
926
  h(Alert, {
832
- tone: status ? (status.tone === "done" ? "ok" : status.tone === "error" ? "error" : "info") : "info",
833
- title: "订单 " + order.orderNo,
834
- }, status ? status.text : "已打开支付页,等待付款…"))
927
+ tone: expired
928
+ ? "warn"
929
+ : status
930
+ ? status.tone === "done" ? "ok" : status.tone === "error" ? "error" : "info"
931
+ : "info",
932
+ title: "订单 " + order.orderNo + (leftMs !== null && !expired ? ` · 剩余 ${mmss(leftMs)}` : ""),
933
+ action: h(Button, {
934
+ size: "sm",
935
+ onClick: reopen,
936
+ style: { color: "inherit", borderColor: "currentColor", background: "transparent" },
937
+ }, expired ? "重新下单" : "重新打开支付页"),
938
+ },
939
+ expired
940
+ ? "订单已过 30 分钟有效期,没有支付的话已经作废,重新下单即可。"
941
+ : status
942
+ ? status.text
943
+ : "已打开支付页,等待付款…到账后余额会自动更新,这页不用守着。"))
835
944
  : null,
836
945
  h("div", { style: { marginTop: 12, display: "flex", gap: 8, alignItems: "center" } },
837
946
  h(Button, {
@@ -1013,8 +1122,17 @@ window.__ModuleLoader__.load({
1013
1122
  h("span", { style: { ...T.small, fontWeight: 500 } },
1014
1123
  showingAll && !dirty ? `全部 ${everything.length} 个` : `已选 ${checked.length} / ${everything.length}`),
1015
1124
  h("div", { style: { marginLeft: "auto", display: "flex", gap: 8 } },
1016
- h(Button, { size: "sm", onClick: onRefreshModels, disabled: pending.models },
1017
- pending.models ? "同步中…" : "刷新"),
1125
+ h(Button, {
1126
+ size: "sm",
1127
+ onClick: async () => {
1128
+ try {
1129
+ await onRefreshModels();
1130
+ } catch (e) {
1131
+ toastStore.push(e instanceof Error ? e.message : String(e), "error");
1132
+ }
1133
+ },
1134
+ disabled: pending.models,
1135
+ }, pending.models ? "同步中…" : "刷新"),
1018
1136
  h(Button, { size: "sm", onClick: () => setExpanded((v) => !v) },
1019
1137
  expanded ? "收起" : "管理"),
1020
1138
  ),
@@ -1115,7 +1233,15 @@ window.__ModuleLoader__.load({
1115
1233
  const [steps, setSteps] = useState(null);
1116
1234
  const [ready, setReady] = useState(undefined);
1117
1235
  const [announcements, setAnnouncements] = useState([]);
1118
- const [open, setOpen] = useState({ models: true, recharge: false, account: false, key: false, diagnose: false });
1236
+ const [open, setOpen] = useState(readOpenSections);
1237
+
1238
+ useEffect(() => {
1239
+ try {
1240
+ window.localStorage.setItem(OPEN_KEY, JSON.stringify(open));
1241
+ } catch {
1242
+ // Private mode or a full quota: folding still works, it just forgets.
1243
+ }
1244
+ }, [open]);
1119
1245
 
1120
1246
  const setBusy = (key, value) => setPending((prev) => ({ ...prev, [key]: value }));
1121
1247
  const run = useCallback(async (key, fn) => {
@@ -1467,6 +1593,8 @@ window.__ModuleLoader__.load({
1467
1593
  exports.inject = inject;
1468
1594
  exports.Section = Section;
1469
1595
  exports.BalanceBadge = BalanceBadge;
1596
+ // Exposed for the load test: pure read/write of the remembered layout.
1597
+ exports.__sections = { readOpenSections, DEFAULT_OPEN, OPEN_KEY };
1470
1598
  return module.exports;
1471
1599
  },
1472
1600
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-sub2api",
3
- "version": "0.4.1",
3
+ "version": "0.4.2",
4
4
  "description": "Psapi (sub.pscly.cc) provider for DeepSeek Harness: zero-friction signup, auto-provisioned API key, upstream model sync, balance and recharge",
5
5
  "type": "module",
6
6
  "license": "GPL-3.0-only",