@dplugin/core 2.0.11 → 2.1.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/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- const U = {
1
+ const I = {
2
2
  // 接口前缀。'' = 同源;也可以是 https://api.company.com
3
3
  apiBase: "",
4
4
  // 运行模式:'auto' 按同源自动判定;'cookie' 强制同域 Cookie;'sign' 强制跨域签名
@@ -20,141 +20,120 @@ const U = {
20
20
  // 登出或会话失效(可选),(reason) => void
21
21
  onLogout: null,
22
22
  // 原生能力体检:'warn' 只告警 / 'error' 直接抛错 / 'off' 不检查
23
- nativeGuard: "warn"
23
+ nativeGuard: "error"
24
24
  };
25
- function Nt(t) {
26
- return t && Object.keys(U).forEach((e) => {
27
- t[e] !== void 0 && (U[e] = t[e]);
28
- }), U;
25
+ function ie(e) {
26
+ return e && Object.keys(I).forEach((n) => {
27
+ e[n] !== void 0 && (I[n] = e[n]);
28
+ }), I;
29
29
  }
30
- function I() {
31
- return U;
30
+ function y() {
31
+ return I;
32
32
  }
33
- const E = At();
34
- function $(t, e) {
35
- if (typeof t != "function")
33
+ const a = se();
34
+ function B(e, n) {
35
+ if (typeof e != "function")
36
36
  return !1;
37
- let n;
37
+ let t;
38
38
  try {
39
- n = Function.prototype.toString.call(t);
40
- } catch (r) {
39
+ t = Function.prototype.toString.call(e);
40
+ } catch (i) {
41
41
  return !1;
42
42
  }
43
- const o = /^function\s+([\w$]+)\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.exec(n);
44
- return o ? o[1] === e : /\[native code\]/.test(n) && !/^function\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.test(n);
43
+ const r = /^function\s+([\w$]+)\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.exec(t);
44
+ return r ? r[1] === n : /\[native code\]/.test(t) && !/^function\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.test(t);
45
45
  }
46
- function Kt(t) {
46
+ function oe(e) {
47
47
  try {
48
- const e = t();
49
- return e && typeof e.then == "function" && Promise.resolve(e).catch(() => {
48
+ const n = e();
49
+ return n && typeof n.then == "function" && Promise.resolve(n).catch(() => {
50
50
  }), !1;
51
- } catch (e) {
52
- const n = String(e && e.message || "");
53
- return !!e && e.name === "TypeError" && /argument|required|参数/i.test(n);
51
+ } catch (n) {
52
+ const t = String(n && n.message || "");
53
+ return !!n && n.name === "TypeError" && /argument|required|参数/i.test(t);
54
54
  }
55
55
  }
56
- function At() {
57
- const t = globalThis, e = typeof t.window != "undefined" && t.window && typeof t.window.document != "undefined", n = {
56
+ function se() {
57
+ const e = globalThis, n = typeof e.window != "undefined" && e.window && typeof e.window.document != "undefined", t = {
58
58
  fetch: null,
59
59
  idbOpen: null,
60
60
  subtle: null,
61
61
  pristine: { fetch: !1, idbOpen: !1, subtle: !1 }
62
62
  };
63
- typeof t.fetch == "function" && (n.pristine.fetch = $(t.fetch, "fetch"), n.fetch = t.fetch.bind(t)), typeof t.indexedDB != "undefined" && t.indexedDB && typeof t.indexedDB.open == "function" && (n.pristine.idbOpen = $(t.indexedDB.open, "open") && (!e || Kt(() => t.indexedDB.open())), n.idbOpen = t.indexedDB.open.bind(t.indexedDB));
64
- const o = t.crypto && t.crypto.subtle ? t.crypto.subtle : null;
65
- if (o) {
66
- const r = ["generateKey", "exportKey", "importKey", "sign", "verify", "digest"], s = {};
67
- let a = !0;
68
- r.forEach((c) => {
69
- typeof o[c] == "function" && (s[c] = o[c].bind(o), a = a && $(o[c], c));
70
- }), n.subtle = s, n.pristine.subtle = a;
63
+ typeof e.fetch == "function" && (t.pristine.fetch = B(e.fetch, "fetch"), t.fetch = e.fetch.bind(e)), typeof e.indexedDB != "undefined" && e.indexedDB && typeof e.indexedDB.open == "function" && (t.pristine.idbOpen = B(e.indexedDB.open, "open") && (!n || oe(() => e.indexedDB.open())), t.idbOpen = e.indexedDB.open.bind(e.indexedDB));
64
+ const r = e.crypto && e.crypto.subtle ? e.crypto.subtle : null;
65
+ if (r) {
66
+ const i = ["generateKey", "exportKey", "importKey", "sign", "verify", "digest"], o = {};
67
+ let s = !0;
68
+ i.forEach((c) => {
69
+ typeof r[c] == "function" && (o[c] = r[c].bind(r), s = s && B(r[c], c));
70
+ }), t.subtle = o, t.pristine.subtle = s;
71
71
  }
72
- return n;
72
+ return t;
73
73
  }
74
- function xt() {
74
+ function ce() {
75
75
  if (typeof window == "undefined" || typeof window.document == "undefined")
76
76
  return [];
77
- const t = [];
78
- return E.fetch && !E.pristine.fetch && t.push("fetch"), E.idbOpen && !E.pristine.idbOpen && t.push("indexedDB.open"), E.subtle && !E.pristine.subtle && t.push("crypto.subtle"), t;
77
+ const e = [];
78
+ return a.fetch && !a.pristine.fetch && e.push("fetch"), a.idbOpen && !a.pristine.idbOpen && e.push("indexedDB.open"), a.subtle && !a.pristine.subtle && e.push("crypto.subtle"), e;
79
79
  }
80
- const u = {
80
+ const f = {
81
81
  OK: "200",
82
- PENDING: "202",
83
- AUTH_EXPIRED: "500900",
84
- FORBIDDEN: "401001",
85
- CORS_DENIED: "403",
86
- API_NOT_FOUND: "404",
82
+ LOGIN_PENDING: "202",
83
+ LOGIN_TIMEOUT: "g.login.timeout",
87
84
  NETWORK: "NETWORK",
88
85
  TIMEOUT: "TIMEOUT"
89
86
  };
90
- class m extends Error {
91
- constructor(e, n) {
92
- super(n || "请求失败"), this.name = "BizError", this.code = e;
87
+ class h extends Error {
88
+ constructor(n, t) {
89
+ super(t || "请求失败"), this.name = "BizError", this.code = n;
93
90
  }
94
91
  }
95
- class O extends m {
96
- constructor(e) {
97
- super(u.AUTH_EXPIRED, e || "登录已失效"), this.name = "AuthError";
92
+ class ue extends h {
93
+ constructor(n) {
94
+ super(f.LOGIN_TIMEOUT, n || "登录已失效"), this.name = "AuthError";
98
95
  }
99
96
  }
100
- function ge(t) {
101
- return t instanceof O || t && t.code === u.AUTH_EXPIRED;
102
- }
103
- const q = "dplugin.access", z = "dplugin.sid", lt = "dplugin.ds", J = "dplugin.exp", Q = "dplugin.cookie";
104
- function _t(t) {
97
+ const V = "dp.token", U = "dp.sid", $ = "dp.ds";
98
+ function ae(e) {
105
99
  try {
106
- return localStorage.getItem(t);
107
- } catch (e) {
100
+ return localStorage.getItem(e);
101
+ } catch (n) {
108
102
  return null;
109
103
  }
110
104
  }
111
- function ht(t, e) {
105
+ function q(e, n) {
112
106
  try {
113
- e == null || e === "" ? localStorage.removeItem(t) : localStorage.setItem(t, e);
114
- } catch (n) {
107
+ n == null || n === "" ? localStorage.removeItem(e) : localStorage.setItem(e, n);
108
+ } catch (t) {
115
109
  }
116
110
  }
117
- const G = {};
118
- function x(t) {
119
- const e = _t(t);
120
- return e === null ? G[t] === void 0 ? null : G[t] : e;
111
+ const D = {};
112
+ function F(e) {
113
+ const n = ae(e);
114
+ return n === null ? D[e] === void 0 ? null : D[e] : n;
121
115
  }
122
- function _(t, e) {
123
- G[t] = e, ht(t, e);
116
+ function H(e, n) {
117
+ D[e] = n, q(e, n);
124
118
  }
125
- const i = {
126
- get access() {
127
- return x(q);
119
+ const u = {
120
+ get token() {
121
+ return F(V);
128
122
  },
129
- set access(t) {
130
- _(q, t);
123
+ set token(e) {
124
+ H(V, e);
131
125
  },
132
126
  get sid() {
133
- return x(z);
127
+ return F(U);
134
128
  },
135
- set sid(t) {
136
- _(z, t);
129
+ set sid(e) {
130
+ H(U, e);
137
131
  },
138
132
  get ds() {
139
- return x(lt);
140
- },
141
- set ds(t) {
142
- _(lt, t);
143
- },
144
- /** access 的到期时间(毫秒时间戳)。仅用于界面判断,不要拿它做提前续期 */
145
- get exp() {
146
- const t = x(J);
147
- return t ? Number(t) : 0;
148
- },
149
- set exp(t) {
150
- _(J, t ? String(t) : null);
133
+ return F($);
151
134
  },
152
- /** cookie 模式:本地只记「可能已登录」,实际有效性由第一次请求的结果决定 */
153
- get cookie() {
154
- return x(Q) === "1";
155
- },
156
- set cookie(t) {
157
- _(Q, t ? "1" : null);
135
+ set ds(e) {
136
+ H($, e);
158
137
  },
159
138
  /**
160
139
  * 清登录态。
@@ -163,302 +142,297 @@ const i = {
163
142
  * 下次登录后端就会新建一条设备记录(common_login_device 无限膨胀)。
164
143
  */
165
144
  clear() {
166
- [q, z, J, Q].forEach((t) => {
167
- G[t] = null, ht(t, null);
145
+ [V, U].forEach((e) => {
146
+ D[e] = null, q(e, null);
168
147
  });
169
148
  }
170
- };
171
- function w(t, e) {
172
- const n = I().apiBase || "";
173
- let o = t;
174
- return /^https?:\/\//i.test(t) || (o = (n ? n.replace(/\/+$/, "") : "") + (t.startsWith("/") ? t : "/" + t)), e && (o += (o.includes("?") ? "&" : "?") + e), o;
175
- }
176
- function V(t) {
177
- const e = w(t);
178
- if (/^https?:\/\//i.test(e))
179
- try {
180
- return new URL(e).pathname;
181
- } catch (n) {
182
- return e.split("?")[0];
183
- }
184
- try {
185
- return new URL(e, "http://localhost").pathname;
186
- } catch (n) {
187
- return e.split("?")[0];
188
- }
189
- }
190
- let S = null;
191
- function Ct(t) {
192
- try {
193
- const e = typeof location != "undefined" ? location.href : void 0;
194
- return new URL(t || "", e).origin;
195
- } catch (e) {
196
- return null;
149
+ }, fe = "dplugin", le = 1, M = "keys", de = { name: "ECDSA", namedCurve: "P-256" }, ge = { name: "ECDSA", hash: "SHA-256" }, x = /* @__PURE__ */ new Map();
150
+ let K = null;
151
+ function v() {
152
+ if (!a.subtle) {
153
+ const e = typeof location != "undefined" && location.origin ? location.origin : "(未知 origin)";
154
+ throw new Error("[dplugin/core] crypto.subtle 不存在:浏览器只在**安全上下文**里提供它(https、http://localhost、http://127.0.0.1、http://[::1]、file://);当前 origin=" + e);
197
155
  }
156
+ return a.subtle;
198
157
  }
199
- function Ut() {
200
- S = null;
201
- }
202
- function pt() {
203
- if (S)
204
- return S;
205
- const t = I();
206
- if (t.mode === "sign" || t.mode === "cookie")
207
- return S = t.mode, S;
208
- const e = typeof location != "undefined" ? location.origin : null;
209
- if (!e)
210
- return S = "sign", S;
211
- const n = Ct(t.apiBase || e);
212
- return S = n && n === e ? "cookie" : "sign", S;
213
- }
214
- function b() {
215
- return pt() === "cookie";
216
- }
217
- async function h(t, e, n) {
218
- const o = I(), r = n || o.timeout, s = typeof AbortController != "undefined" ? new AbortController() : null, a = s ? setTimeout(() => s.abort(), r) : null, c = {
219
- method: "POST",
220
- // cookie 模式必须带上 Cookie(同源不涉及 CORS);sign 模式主动不带,减少暴露面
221
- credentials: b() ? "same-origin" : "omit",
222
- signal: s ? s.signal : void 0
223
- };
224
- if (typeof FormData != "undefined" && e instanceof FormData ? c.body = e : e != null && (c.headers = { "Content-Type": "text/plain" }, c.body = JSON.stringify(e)), !E.fetch)
225
- throw new m(u.NETWORK, "当前环境不支持 fetch");
226
- let f;
227
- try {
228
- f = await E.fetch(t, c);
229
- } catch (y) {
230
- throw a && clearTimeout(a), y && y.name === "AbortError" ? new m(u.TIMEOUT, "请求超时,请重试") : new m(u.NETWORK, "网络异常,请检查网络后重试");
231
- }
232
- a && clearTimeout(a);
233
- const d = await f.text();
234
- let l = null;
235
- if (d)
236
- try {
237
- l = JSON.parse(d);
238
- } catch (y) {
239
- l = null;
240
- }
241
- if (!f.ok)
242
- throw new m(String(f.status), l && l.message || d || "请求失败");
243
- if (!l || l.code === null || l.code === void 0)
244
- throw new m("500", "响应格式异常");
245
- return l.code = String(l.code), l;
246
- }
247
- const Mt = "dplugin-auth", kt = 1, Y = "keys", rt = "sso", it = "domain", vt = { name: "ECDSA", namedCurve: "P-256" }, Lt = { name: "ECDSA", hash: "SHA-256" }, F = /* @__PURE__ */ new Map();
248
- let v = null;
249
- function j() {
250
- if (!E.subtle) {
251
- const t = typeof location != "undefined" && location.origin ? location.origin : "(未知 origin)";
252
- throw new Error("[dplugin/core] crypto.subtle 不存在:浏览器只在**安全上下文**里提供它(https、http://localhost、http://127.0.0.1、http://[::1]、file://);当前 origin=" + t);
253
- }
254
- return E.subtle;
255
- }
256
- function Ft() {
257
- return v || (v = new Promise((t) => {
258
- if (!E.idbOpen) {
259
- t(null);
158
+ function he() {
159
+ return K || (K = new Promise((e) => {
160
+ if (!a.idbOpen) {
161
+ e(null);
260
162
  return;
261
163
  }
262
- let e;
164
+ let n;
263
165
  try {
264
- e = E.idbOpen(Mt, kt);
265
- } catch (n) {
266
- t(null);
166
+ n = a.idbOpen(fe, le);
167
+ } catch (t) {
168
+ e(null);
267
169
  return;
268
170
  }
269
- e.onupgradeneeded = () => {
270
- const n = e.result;
271
- n.objectStoreNames.contains(Y) || n.createObjectStore(Y);
272
- }, e.onsuccess = () => t(e.result), e.onerror = () => t(null), e.onblocked = () => t(null);
273
- }), v);
274
- }
275
- function Et(t, e) {
276
- return Ft().then((n) => n ? new Promise((o) => {
277
- let r;
171
+ n.onupgradeneeded = () => {
172
+ const t = n.result;
173
+ t.objectStoreNames.contains(M) || t.createObjectStore(M);
174
+ }, n.onsuccess = () => e(n.result), n.onerror = () => e(null), n.onblocked = () => e(null);
175
+ }), K);
176
+ }
177
+ function J(e, n) {
178
+ return he().then((t) => t ? new Promise((r) => {
179
+ let i;
278
180
  try {
279
- r = n.transaction(Y, t);
181
+ i = t.transaction(M, e);
280
182
  } catch (c) {
281
- o(null);
183
+ r(null);
282
184
  return;
283
185
  }
284
- const s = r.objectStore(Y);
285
- let a = null;
186
+ const o = i.objectStore(M);
187
+ let s = null;
286
188
  try {
287
- const c = e(s);
189
+ const c = n(o);
288
190
  c && (c.onsuccess = () => {
289
- a = c.result;
191
+ s = c.result;
290
192
  }, c.onerror = () => {
291
- a = null;
193
+ s = null;
292
194
  });
293
195
  } catch (c) {
294
- a = null;
196
+ s = null;
295
197
  }
296
- r.oncomplete = () => o(a), r.onerror = () => o(null), r.onabort = () => o(null);
198
+ i.oncomplete = () => r(s), i.onerror = () => r(null), i.onabort = () => r(null);
297
199
  }) : null);
298
200
  }
299
- function mt(t) {
300
- return F.has(t) ? Promise.resolve(F.get(t)) : Et("readonly", (e) => e.get(t)).then((e) => (e && F.set(t, e), e || null));
301
- }
302
- function Ot(t, e) {
303
- return F.set(t, e), Et("readwrite", (n) => n.put(e, t)).then(() => e);
304
- }
305
- async function st(t) {
306
- const n = await j().generateKey(vt, !1, ["sign", "verify"]);
307
- return Ot(t, n);
308
- }
309
- async function yt(t) {
310
- return mt(t);
311
- }
312
- async function wt(t, e) {
313
- return e ? Ot(t, e) : null;
314
- }
315
- async function Ht(t) {
316
- return await mt(t) || st(t);
317
- }
318
- function Gt() {
319
- return st(rt);
320
- }
321
- function Yt() {
322
- return st(it);
323
- }
324
- function ct() {
325
- return yt(rt);
201
+ async function N(e) {
202
+ return x.has(e) ? Promise.resolve(x.get(e)) : J("readonly", (n) => n.get(e)).then((n) => (n && x.set(e, n), n || null));
326
203
  }
327
- function bt() {
328
- return yt(it);
204
+ function Q(e) {
205
+ const n = new Uint8Array(e);
206
+ let t = "";
207
+ for (let r = 0; r < n.length; r++)
208
+ t += String.fromCharCode(n[r]);
209
+ return btoa(t).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
329
210
  }
330
- async function tt(t) {
331
- const n = await j().exportKey("spki", t.publicKey);
332
- let o = "";
333
- const r = new Uint8Array(n);
334
- for (let s = 0; s < r.length; s++)
335
- o += String.fromCharCode(r[s]);
336
- return btoa(o);
211
+ async function ee(e) {
212
+ let n = await N(e);
213
+ return n || (n = await v().generateKey(de, !1, ["sign", "verify"]), x.set(e, n), J("readwrite", (r) => r.put(n, e)).then(() => n));
337
214
  }
338
- function St(t) {
339
- const e = new Uint8Array(t);
340
- let n = "";
341
- for (let o = 0; o < e.length; o++)
342
- n += String.fromCharCode(e[o]);
343
- return btoa(n).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
215
+ function pe() {
216
+ return ee("sso");
344
217
  }
345
- async function Bt(t, e) {
346
- const o = await j().sign(Lt, t.privateKey, new TextEncoder().encode(e));
347
- return St(o);
218
+ function ze() {
219
+ return ee("domain");
348
220
  }
349
- async function It(t) {
350
- const e = j(), n = await e.exportKey("spki", t.publicKey), o = await e.digest("SHA-256", n);
351
- return St(o);
352
- }
353
- const et = {
354
- SSO: rt,
355
- DOMAIN: it
356
- }, nt = "-", Vt = "POST", N = {
221
+ async function we() {
222
+ return await N("sso");
223
+ }
224
+ async function me() {
225
+ return await N("domain");
226
+ }
227
+ async function Ze() {
228
+ let e = await N("domain");
229
+ const n = v(), t = await n.exportKey("spki", e.publicKey), r = await n.digest("SHA-256", t);
230
+ return Q(r);
231
+ }
232
+ async function ne(e) {
233
+ let n = await N(e);
234
+ const r = await v().exportKey("spki", n.publicKey);
235
+ let i = "";
236
+ const o = new Uint8Array(r);
237
+ for (let s = 0; s < o.length; s++)
238
+ i += String.fromCharCode(o[s]);
239
+ return btoa(i);
240
+ }
241
+ async function Oe(e, n) {
242
+ const r = await v().sign(ge, e.privateKey, new TextEncoder().encode(n));
243
+ return Q(r);
244
+ }
245
+ const ye = "-", _ = {
357
246
  BIZ: "BIZ",
358
- VERIFY: "VERIFY",
359
- VERIFY_SID: "VERIFY_SID",
360
- TOKEN: "TOKEN"
247
+ VTT: "VTT",
248
+ VSD: "VSD",
249
+ TOK: "TOK"
361
250
  };
362
- function p(t) {
363
- return t == null || t === "" ? nt : String(t);
251
+ function O(e) {
252
+ return e == null || e === "" ? ye : String(e);
364
253
  }
365
- function jt(t, e) {
366
- const n = p(e.origin);
254
+ function Ee(e, n) {
255
+ const t = O(n.origin);
367
256
  return [
368
- p(t),
369
- p(e.method),
370
- p(e.path),
371
- n === nt ? nt : n.toLowerCase(),
372
- p(e.ds),
373
- p(e.ts),
374
- p(e.subject),
375
- p(e.dna),
376
- p(e.redirect)
257
+ e,
258
+ n.method,
259
+ n.path,
260
+ t.toLowerCase(),
261
+ O(n.ds),
262
+ n.ts,
263
+ O(n.token),
264
+ O(n.dna),
265
+ O(n.redirect)
377
266
  ].join(`
378
267
  `);
379
268
  }
380
- function Xt(t, e, n) {
381
- return [p(t), p(e.subject), p(e.ds), p(e.ts), n].join(".");
269
+ function Se(e, n, t) {
270
+ return [e, O(n.token), O(n.ds), n.ts, t].join(".");
382
271
  }
383
- async function K(t, e, n) {
384
- const o = n === "sso" ? await ct() : await bt();
385
- if (!o)
386
- throw Tt(n === "sso" ? "缺少登录签名密钥" : "缺少业务签名密钥");
387
- const r = await Bt(o, jt(t, e));
388
- return Xt(t, e, r);
272
+ function be(e) {
273
+ const n = new Error(e);
274
+ return n.code = "KEY_MISSING", n;
389
275
  }
390
- async function at(t, e, n) {
391
- if (!e || !n)
392
- throw Tt("缺少 access 或 ds,无法签名");
393
- const o = await K(N.BIZ, {
394
- method: Vt,
395
- path: V(t),
396
- origin: A(),
397
- ds: n,
398
- ts: Date.now(),
399
- subject: e
400
- }, "domain");
401
- return "dsign=" + encodeURIComponent(o);
276
+ async function A(e, n, t) {
277
+ const r = t === "sso" ? await we() : await me();
278
+ if (!r)
279
+ throw be(t === "sso" ? "缺少登录签名密钥" : "缺少业务签名密钥");
280
+ let i = n.path.indexOf("?");
281
+ i > 0 && (n.path = n.path.substring(0, i));
282
+ const o = await Oe(r, Ee(e, n));
283
+ return Se(e, n, o);
402
284
  }
403
- function A() {
404
- if (typeof location == "undefined")
405
- return "";
285
+ let g = null;
286
+ function Te(e) {
287
+ try {
288
+ const n = typeof location != "undefined" ? location.href : void 0;
289
+ return new URL(e || "", n).origin;
290
+ } catch (n) {
291
+ return null;
292
+ }
293
+ }
294
+ function ke() {
295
+ g = null;
296
+ }
297
+ function Ie() {
298
+ if (g)
299
+ return g;
300
+ const e = y();
301
+ if (e.mode === "sign" || e.mode === "cookie")
302
+ return g = e.mode, g;
303
+ const n = typeof location != "undefined" ? location.origin : null;
304
+ if (!n)
305
+ return g = "sign", g;
306
+ const t = Te(e.apiBase || n);
307
+ return g = t && t === n ? "cookie" : "sign", g;
308
+ }
309
+ function E() {
310
+ return Ie() === "cookie";
311
+ }
312
+ function Ne(e, n) {
313
+ let t = y();
314
+ throw t.onError && n.showError && t.onError({ code: e.code, message: e.message }), new h(e.code, e.message);
315
+ }
316
+ let C, Y = null;
317
+ function Pe() {
318
+ return C || (C = Promise.resolve().then(() => Y()).finally(() => {
319
+ C = null;
320
+ })), C;
321
+ }
322
+ async function Re(e, n, t, r, i) {
323
+ const o = y(), s = i || o.timeout, c = typeof AbortController != "undefined" ? new AbortController() : null, P = c ? setTimeout(() => c.abort(), s) : null, m = {
324
+ method: n
325
+ };
326
+ if (e ? m.credentials = "same-origin" : m.credentials = "omit", m.signal = c ? c.signal : void 0, typeof FormData != "undefined" && r instanceof FormData ? m.body = r : r != null && (m.headers = { "Content-Type": "text/plain" }, m.body = JSON.stringify(r)), !a.fetch)
327
+ throw new h(f.NETWORK, "当前环境不支持 fetch");
328
+ let R;
329
+ try {
330
+ R = await a.fetch(t, m);
331
+ } catch (L) {
332
+ throw P && clearTimeout(P), L && L.name === "AbortError" ? new h(f.TIMEOUT, "请求超时,请重试") : new h(f.NETWORK, "网络异常,请检查网络后重试");
333
+ }
334
+ P && clearTimeout(P);
335
+ const G = await R.text();
336
+ let d = null;
337
+ if (G)
338
+ try {
339
+ d = JSON.parse(G);
340
+ } catch (L) {
341
+ d = null;
342
+ }
343
+ if (!R.ok)
344
+ throw new h(String(R.status), d && d.message || G || "请求失败");
345
+ if (!d || d.code === null || d.code === void 0)
346
+ throw new h("500", "响应格式异常");
347
+ return d.code = String(d.code), d;
348
+ }
349
+ async function l(e, n = null, t = null) {
350
+ const r = t || {};
351
+ r.method || (r.method = "POST"), r.showError = r.showError !== !1;
352
+ let i = !0;
353
+ /^https?:\/\/.+$/.test(e) ? e.indexOf(y().apiBase) === -1 && (i = !1) : e = y().apiBase + e, i && !E() && (i = !1);
354
+ let o = e;
355
+ if (!i && u.ds && u.token) {
356
+ let c = await A(_.BIZ, {
357
+ method: r.method,
358
+ path: new URL(e, location.href).pathname,
359
+ ds: u.ds,
360
+ ts: Date.now(),
361
+ token: u.token
362
+ }, "domain");
363
+ o.indexOf("?") > 0 ? o = o + "&dsign=" + c : o = o + "?dsign=" + c;
364
+ }
365
+ const s = await Re(i, r.method, o, n, r.timeout);
366
+ return s.code === f.LOGIN_TIMEOUT && !r.skipRefresh && Y && await Pe() ? l(e, n, Object.assign({}, r, { skipRefresh: !0 })) : (/^2.+/.test(s.code) || Ne(s, r), s);
367
+ }
368
+ async function Xe(e, n, t, r) {
369
+ const i = new FormData();
370
+ return Object.keys(t || {}).forEach((o) => {
371
+ const s = t[o];
372
+ s != null && i.append(o, s);
373
+ }), i.append("file", n), l(e, i, r);
374
+ }
375
+ function Ke(e) {
376
+ Y = e;
377
+ }
378
+ function Ce() {
379
+ const e = typeof navigator != "undefined" && navigator.userAgent || "", n = {
380
+ device_brand: xe(e),
381
+ device_model: De(e),
382
+ browser_name: Me(e),
383
+ browser_version: _e(e),
384
+ os_name: Ae(e),
385
+ os_version: ve(e)
386
+ }, t = u.ds;
387
+ return t && (n.device_id = t), n;
388
+ }
389
+ function w(e, n) {
390
+ const t = e.match(n);
391
+ return t ? t[1] : "";
392
+ }
393
+ function xe(e) {
394
+ return /iPhone|iPad|iPod/.test(e) ? "Apple" : /Huawei|HONOR/i.test(e) ? "Huawei" : /Xiaomi|Redmi|POCO/i.test(e) ? "Xiaomi" : /OPPO|realme/i.test(e) ? "OPPO" : /vivo/i.test(e) ? "vivo" : /Samsung|SM-|SAMSUNG/i.test(e) ? "Samsung" : /Macintosh|Mac OS X/.test(e) ? "Apple" : "";
395
+ }
396
+ function De(e) {
397
+ if (/iPhone/.test(e)) return "iPhone";
398
+ if (/iPad/.test(e)) return "iPad";
399
+ const n = e.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);
400
+ return n ? n[1].trim() : /Macintosh/.test(e) ? "Mac" : "";
401
+ }
402
+ function Me(e) {
403
+ return /MicroMessenger/i.test(e) ? "WeChat" : /Edg[A-Za-z]*\//.test(e) ? "Edge" : /OPR\/|Opera/.test(e) ? "Opera" : /Firefox\//.test(e) ? "Firefox" : /Chrome\/|CriOS\//.test(e) ? "Chrome" : /Safari\//.test(e) ? "Safari" : "";
404
+ }
405
+ function _e(e) {
406
+ return /MicroMessenger\/([\d.]+)/.test(e) ? w(e, /MicroMessenger\/([\d.]+)/) : /Edg[A-Za-z]*\/([\d.]+)/.test(e) ? w(e, /Edg[A-Za-z]*\/([\d.]+)/) : /OPR\/([\d.]+)/.test(e) ? w(e, /OPR\/([\d.]+)/) : /Firefox\/([\d.]+)/.test(e) ? w(e, /Firefox\/([\d.]+)/) : /CriOS\/([\d.]+)/.test(e) ? w(e, /CriOS\/([\d.]+)/) : /Chrome\/([\d.]+)/.test(e) ? w(e, /Chrome\/([\d.]+)/) : /Version\/([\d.]+).*Safari/.test(e) ? w(e, /Version\/([\d.]+)/) : "";
407
+ }
408
+ function Ae(e) {
409
+ return /Windows NT/.test(e) ? "Windows" : /iPhone|iPad|iPod/.test(e) ? "iOS" : /Android/.test(e) ? "Android" : /Mac OS X|Macintosh/.test(e) ? "macOS" : /HarmonyOS/.test(e) ? "HarmonyOS" : /Linux/.test(e) ? "Linux" : "";
410
+ }
411
+ function ve(e) {
412
+ if (/Windows NT 10\.0/.test(e)) return "10/11";
413
+ if (/Windows NT 6\.3/.test(e)) return "8.1";
414
+ if (/Windows NT 6\.1/.test(e)) return "7";
415
+ const n = e.match(/(?:iPhone )?OS (\d+[_.]\d+(?:[_.]\d+)?)/);
416
+ if (n) return n[1].replace(/_/g, ".");
417
+ if (/Android[ _]([\d.]+)/.test(e)) return w(e, /Android[ _]([\d.]+)/);
418
+ const t = e.match(/Mac OS X (\d+[_.]\d+(?:[_.]\d+)?)/);
419
+ return t ? t[1].replace(/_/g, ".") : "";
420
+ }
421
+ function te() {
406
422
  if (location.origin)
407
423
  return location.origin.toLowerCase();
408
424
  try {
409
425
  return new URL(location.href).origin.toLowerCase();
410
- } catch (t) {
426
+ } catch (e) {
411
427
  return "";
412
428
  }
413
429
  }
414
- function Tt(t) {
415
- const e = new Error(t);
416
- return e.code = "KEY_MISSING", e;
417
- }
418
- function Wt() {
419
- const t = typeof navigator != "undefined" && navigator.userAgent || "", e = {
420
- device_brand: Zt(t),
421
- device_model: $t(t),
422
- browser_name: qt(t),
423
- browser_version: zt(t),
424
- os_name: Jt(t),
425
- os_version: Qt(t)
426
- }, n = i.ds;
427
- return n && (e.device_id = n), e;
428
- }
429
- function T(t, e) {
430
- const n = t.match(e);
431
- return n ? n[1] : "";
432
- }
433
- function Zt(t) {
434
- return /iPhone|iPad|iPod/.test(t) ? "Apple" : /Huawei|HONOR/i.test(t) ? "Huawei" : /Xiaomi|Redmi|POCO/i.test(t) ? "Xiaomi" : /OPPO|realme/i.test(t) ? "OPPO" : /vivo/i.test(t) ? "vivo" : /Samsung|SM-|SAMSUNG/i.test(t) ? "Samsung" : /Macintosh|Mac OS X/.test(t) ? "Apple" : "";
435
- }
436
- function $t(t) {
437
- if (/iPhone/.test(t)) return "iPhone";
438
- if (/iPad/.test(t)) return "iPad";
439
- const e = t.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);
440
- return e ? e[1].trim() : /Macintosh/.test(t) ? "Mac" : "";
441
- }
442
- function qt(t) {
443
- return /MicroMessenger/i.test(t) ? "WeChat" : /Edg[A-Za-z]*\//.test(t) ? "Edge" : /OPR\/|Opera/.test(t) ? "Opera" : /Firefox\//.test(t) ? "Firefox" : /Chrome\/|CriOS\//.test(t) ? "Chrome" : /Safari\//.test(t) ? "Safari" : "";
444
- }
445
- function zt(t) {
446
- return /MicroMessenger\/([\d.]+)/.test(t) ? T(t, /MicroMessenger\/([\d.]+)/) : /Edg[A-Za-z]*\/([\d.]+)/.test(t) ? T(t, /Edg[A-Za-z]*\/([\d.]+)/) : /OPR\/([\d.]+)/.test(t) ? T(t, /OPR\/([\d.]+)/) : /Firefox\/([\d.]+)/.test(t) ? T(t, /Firefox\/([\d.]+)/) : /CriOS\/([\d.]+)/.test(t) ? T(t, /CriOS\/([\d.]+)/) : /Chrome\/([\d.]+)/.test(t) ? T(t, /Chrome\/([\d.]+)/) : /Version\/([\d.]+).*Safari/.test(t) ? T(t, /Version\/([\d.]+)/) : "";
447
- }
448
- function Jt(t) {
449
- return /Windows NT/.test(t) ? "Windows" : /iPhone|iPad|iPod/.test(t) ? "iOS" : /Android/.test(t) ? "Android" : /Mac OS X|Macintosh/.test(t) ? "macOS" : /HarmonyOS/.test(t) ? "HarmonyOS" : /Linux/.test(t) ? "Linux" : "";
450
- }
451
- function Qt(t) {
452
- if (/Windows NT 10\.0/.test(t)) return "10/11";
453
- if (/Windows NT 6\.3/.test(t)) return "8.1";
454
- if (/Windows NT 6\.1/.test(t)) return "7";
455
- const e = t.match(/(?:iPhone )?OS (\d+[_.]\d+(?:[_.]\d+)?)/);
456
- if (e) return e[1].replace(/_/g, ".");
457
- if (/Android[ _]([\d.]+)/.test(t)) return T(t, /Android[ _]([\d.]+)/);
458
- const n = t.match(/Mac OS X (\d+[_.]\d+(?:[_.]\d+)?)/);
459
- return n ? n[1].replace(/_/g, ".") : "";
460
- }
461
- const g = {
430
+ let j, z;
431
+ function W(e) {
432
+ const n = typeof location != "undefined" && location.search || "";
433
+ return (!j || z !== n) && (z = n, j = new URLSearchParams(n)), j.get(e);
434
+ }
435
+ const p = {
462
436
  SIGN: {
463
437
  VERIFY: "/api/unsafe/dplugin/login/s/verify",
464
438
  TOKEN: "/api/unsafe/dplugin/login/s/token",
@@ -470,388 +444,142 @@ const g = {
470
444
  REFRESH: "/api/unsafe/dplugin/login/c/refresh"
471
445
  },
472
446
  LOGOUT: "/api/safe/dplugin/login/logout"
473
- }, ut = 1800 * 1e3;
474
- let C = null, L = null, R = null, B = !1;
475
- function X() {
476
- return typeof location != "undefined" ? location.href : "";
477
- }
478
- function M(t) {
479
- return new Promise((e) => setTimeout(e, t));
480
- }
481
- function W() {
482
- const t = I();
483
- typeof t.onLogin == "function" && t.onLogin(Z());
484
- }
485
- function D(t, e) {
486
- return new m(t.code, t.message || e);
487
- }
488
- function Z() {
489
- return {
490
- mode: pt(),
491
- loggedIn: ft(),
492
- hasAccess: !!i.access,
493
- sid: i.sid,
494
- ds: i.ds
495
- };
496
- }
497
- function ft() {
498
- return b() ? !!i.cookie : !!(i.access || i.sid);
499
- }
500
- function Pt() {
501
- i.clear();
502
- }
503
- function P(t) {
504
- const e = ft();
505
- Pt();
506
- const n = I();
507
- if (e && typeof n.onLogout == "function" && n.onLogout(t || "expired"), B || !n.enableAuth || !n.loginUrl || typeof location == "undefined")
508
- return;
509
- B = !0;
510
- const o = n.loginUrl.includes("?") ? "&" : "?";
511
- location.href = n.loginUrl + o + "referer=" + encodeURIComponent(location.href);
512
- }
513
- function te(t, e) {
514
- return R || (R = ne(t, e).then((n) => (R = null, n), (n) => {
515
- throw R = null, ee(n), n;
516
- }), R);
517
- }
518
- function ee(t) {
519
- const e = I();
520
- typeof e.onError == "function" && e.onError({
521
- code: t && t.code,
522
- message: t && t.message,
523
- error: t
524
- });
525
- }
526
- async function ne(t, e) {
527
- const n = I(), o = Wt();
528
- if (b())
529
- return ie(t, e, o, n);
530
- const r = await ct(), s = await bt();
531
- try {
532
- return await oe(t, e, o, n);
533
- } catch (a) {
534
- throw await wt(et.SSO, r), await wt(et.DOMAIN, s), a;
535
- }
536
- }
537
- async function oe(t, e, n, o) {
538
- const r = await Gt(), s = await tt(r), a = await h(w(t), Object.assign({}, e, { device: n, pk: s }));
539
- if (a.code !== u.OK)
540
- throw D(a, "登录失败");
541
- const c = a.data && a.data.token;
542
- if (!c)
543
- throw D(a, "登录失败");
544
- const f = await Yt(), d = await It(f), l = await re(c, o, d), y = await h(w(g.SIGN.TOKEN), {
545
- dsign: await K(N.TOKEN, {
546
- origin: A(),
547
- ts: Date.now(),
548
- subject: l.code,
549
- dna: d
550
- }, "domain"),
551
- pk: await tt(f)
552
- });
553
- if (y.code !== u.OK || !y.data)
554
- throw D(y, "登录失败");
555
- return i.sid = l.sid, i.ds = y.data.ds || l.ds, i.access = y.data.token, i.exp = Date.now() + ut, B = !1, W(), Z();
556
- }
557
- async function re(t, e, n) {
558
- const o = X(), r = Math.max(1, Number(e.pollMax) || 5);
559
- for (let s = 1; s <= r; s++) {
560
- const a = i.ds || null, c = Date.now(), f = await K(N.VERIFY, {
561
- method: "POST",
562
- path: V(g.SIGN.VERIFY),
563
- origin: A(),
564
- ds: a,
565
- ts: c,
566
- subject: t,
567
- dna: n,
568
- redirect: o
569
- }, "sso");
570
- let d;
571
- try {
572
- d = await h(w(g.SIGN.VERIFY), { dsign: f, dna: n, redirect: o });
573
- } catch (l) {
574
- if ((l.code === u.NETWORK || l.code === u.TIMEOUT) && s < r) {
575
- await M(e.pollInterval);
576
- continue;
577
- }
578
- throw l;
579
- }
580
- if (d.code === u.OK && d.data)
581
- return d.data;
582
- if (d.code === u.PENDING) {
583
- if (s < r) {
584
- await M(e.pollInterval);
585
- continue;
586
- }
587
- break;
588
- }
589
- throw D(d, "登录失败");
590
- }
591
- throw new m(u.AUTH_EXPIRED, "登录超时,请重试");
592
- }
593
- async function ie(t, e, n, o) {
594
- const r = await h(w(t), Object.assign({}, e, { device: n }));
595
- if (r.code !== u.OK)
596
- throw D(r, "登录失败");
597
- return await se(o), i.cookie = !0, B = !1, W(), Z();
598
- }
599
- async function se(t) {
600
- const e = X(), n = Math.max(1, Number(t.pollMax) || 5);
601
- for (let o = 1; o <= n; o++) {
602
- let r;
603
- try {
604
- r = await h(w(g.COOKIE.VERIFY), { redirect: e });
605
- } catch (s) {
606
- if ((s.code === u.NETWORK || s.code === u.TIMEOUT) && o < n) {
607
- await M(t.pollInterval);
608
- continue;
609
- }
610
- throw s;
611
- }
612
- if (r.code === u.OK)
613
- return r.data;
614
- if (r.code === u.PENDING) {
615
- if (o < n) {
616
- await M(t.pollInterval);
617
- continue;
618
- }
619
- break;
620
- }
621
- throw D(r, "登录失败");
622
- }
623
- throw new m(u.AUTH_EXPIRED, "登录超时,请重试");
447
+ };
448
+ Ke(je);
449
+ function Ge(e) {
450
+ return new Promise((n) => setTimeout(n, e));
624
451
  }
625
- async function k(t) {
626
- const e = !!(t && t.force);
627
- return L || (L = (async () => {
628
- try {
629
- return b() ? await ce(e) : await ue(e);
630
- } catch (n) {
631
- if (n && (n.code === u.NETWORK || n.code === u.TIMEOUT))
632
- throw n;
633
- return !1;
634
- } finally {
635
- L = null;
452
+ let S = null;
453
+ async function qe(e, n) {
454
+ if (S)
455
+ return S;
456
+ const t = Ce();
457
+ return S = (E() ? Le(e, n, t) : Be(e, n, t)).finally(() => {
458
+ S = null;
459
+ }), S;
460
+ }
461
+ async function Le(e, n, t) {
462
+ let r = await l(e, Object.assign({}, n, { device: t }));
463
+ return await re(r.data, !0, !0);
464
+ }
465
+ async function Be(e, n, t) {
466
+ await pe();
467
+ const r = await ne("sso"), i = await l(e, Object.assign({}, n, { device: t, pk: r }));
468
+ return await re(i.data, !0, !0);
469
+ }
470
+ let b;
471
+ async function re(e, n = !1, t = !1) {
472
+ return b || (b = Ve(e, n, t).finally(() => {
473
+ b = null;
474
+ }), b);
475
+ }
476
+ async function Ve(e, n, t) {
477
+ if (e || (e = {}), t) {
478
+ let r = y(), i;
479
+ for (let o = 0; o < r.pollMax; o++) {
480
+ if (i = E() ? await Z(e, n) : await X(e, n), i.code !== f.LOGIN_PENDING)
481
+ return i;
482
+ await Ge(r.pollInterval);
636
483
  }
637
- })()), L;
638
- }
639
- async function ce(t) {
640
- return !t && i.cookie ? !0 : (await h(w(g.COOKIE.VERIFY), { redirect: X() })).code !== u.OK ? (i.cookie = !1, !1) : (i.cookie = !0, W(), !0);
484
+ return i;
485
+ } else
486
+ return E() ? await Z(e, n) : await X(e, n);
641
487
  }
642
- async function ae() {
643
- if (!i.sid || !i.ds || !await ct())
644
- return !1;
645
- const e = i.ds, n = await Ht(et.DOMAIN), o = await It(n), r = X(), s = await K(N.VERIFY_SID, {
646
- method: "POST",
647
- path: V(g.SIGN.VERIFY),
648
- origin: A(),
649
- ds: e,
650
- ts: Date.now(),
651
- subject: i.sid,
652
- dna: o,
653
- redirect: r
654
- }, "sso"), a = await h(w(g.SIGN.VERIFY), { dsign: s, dna: o, redirect: r });
655
- if (a.code !== u.OK || !a.data)
656
- return !1;
657
- const c = await h(w(g.SIGN.TOKEN), {
658
- dsign: await K(N.TOKEN, {
659
- origin: A(),
660
- ts: Date.now(),
661
- subject: a.data.code,
662
- dna: o
663
- }, "domain"),
664
- pk: await tt(n)
665
- });
666
- return c.code !== u.OK || !c.data ? !1 : (i.access = c.data.token, c.data.ds && (i.ds = c.data.ds), i.exp = Date.now() + ut, W(), !0);
667
- }
668
- async function ue(t) {
669
- return !t && i.access ? !0 : ae();
670
- }
671
- function dt() {
672
- return C || (C = (b() ? fe() : de()).then((t) => (C = null, t), (t) => {
673
- throw C = null, t;
674
- })), C;
488
+ async function Z(e, n) {
489
+ return await l(p.COOKIE.VERIFY, { redirect: W("referer") }, { skipRefresh: !0, showError: n });
675
490
  }
676
- async function fe() {
677
- const t = await h(w(g.COOKIE.TOKEN));
678
- if (t.code === u.OK)
679
- return !0;
680
- const e = await h(w(g.COOKIE.REFRESH));
681
- if (e.code === u.OK)
682
- return !0;
683
- if (e.code === u.AUTH_EXPIRED || t.code === u.AUTH_EXPIRED)
684
- throw new O(e.message || t.message);
685
- if (e.code === "500" && /重复/.test(e.message || ""))
686
- return await M(800), !0;
687
- throw new m(e.code, e.message);
688
- }
689
- async function de() {
690
- const t = i.access, e = i.ds;
691
- if (!t || !e)
692
- throw new O();
693
- const n = await K(N.BIZ, {
491
+ async function X(e, n) {
492
+ let t = {
694
493
  method: "POST",
695
- path: V(g.SIGN.REFRESH),
696
- origin: A(),
697
- ds: e,
494
+ path: p.SIGN.VERIFY,
495
+ origin: te(),
698
496
  ts: Date.now(),
699
- subject: t
700
- }, "domain"), o = await h(w(g.SIGN.REFRESH, "dsign=" + encodeURIComponent(n)));
701
- if (o.code === u.OK && o.data)
702
- return i.access = o.data.token, o.data.ds && (i.ds = o.data.ds), i.exp = Date.now() + ut, i.access;
703
- const r = await le(t, 1200);
704
- if (r)
705
- return r;
706
- throw o.code === u.AUTH_EXPIRED ? new O(o.message) : new m(o.code, o.message);
707
- }
708
- function le(t, e) {
709
- const n = Date.now() + e;
710
- return new Promise((o) => {
711
- const r = () => {
712
- const s = i.access;
713
- if (s && s !== t) {
714
- o(s);
715
- return;
716
- }
717
- if (Date.now() >= n) {
718
- o(null);
719
- return;
720
- }
721
- setTimeout(r, 150);
722
- };
723
- r();
724
- });
725
- }
726
- async function we() {
727
- try {
728
- let e = await gt();
729
- e.code === u.AUTH_EXPIRED && (await dt(), e = await gt());
730
- } catch (e) {
731
- }
732
- Pt();
733
- const t = I();
734
- typeof t.onLogout == "function" && t.onLogout("logout");
735
- }
736
- async function gt() {
737
- if (b())
738
- return h(w(g.LOGOUT));
739
- const t = await at(g.LOGOUT, i.access, i.ds);
740
- return h(w(g.LOGOUT, t));
741
- }
742
- async function he(t, e, n) {
743
- const o = n || {};
744
- return o.auth !== !1 && await Rt(), H(t, e || {}, o, !1);
745
- }
746
- async function Rt() {
747
- if (b()) {
748
- if (i.cookie || await k())
749
- return;
750
- throw P("expired"), new O();
751
- }
752
- if (!i.access && !await k())
753
- throw P("expired"), new O();
754
- }
755
- async function H(t, e, n, o) {
756
- const r = n.auth !== !1, s = r && !b();
757
- let a = "", c = null;
758
- if (s) {
759
- c = i.access;
760
- try {
761
- a = await at(t, c, i.ds);
762
- } catch (d) {
763
- if (!o && await k({ force: !0 }))
764
- return H(t, e, n, !0);
765
- throw P("expired"), new O();
766
- }
767
- }
768
- const f = await h(w(t, a), e, n.timeout);
769
- if (f.code === u.OK)
770
- return f.data;
771
- if (r && f.code === u.AUTH_EXPIRED && !o) {
772
- if (s && c && i.access !== c)
773
- return H(t, e, n, !0);
774
- try {
775
- await dt();
776
- } catch (d) {
777
- throw d instanceof O && P("expired"), d;
778
- }
779
- return H(t, e, n, !0);
497
+ ds: u.ds,
498
+ dna: W("dna"),
499
+ redirect: W("referer")
500
+ }, r;
501
+ if (e && e.token)
502
+ t.token = e.token, r = await A(_.VTT, t, "sso");
503
+ else {
504
+ if (!u.sid || !u.ds)
505
+ throw new h(f.LOGIN_TIMEOUT, "未登录");
506
+ t.token = u.sid, r = await A(_.VSD, t, "sso");
780
507
  }
781
- throw r && f.code === u.AUTH_EXPIRED ? (P("expired"), new O(f.message)) : Dt(f, n);
508
+ const i = await l(p.SIGN.VERIFY, { dsign: r, dna: t.dna, redirect: t.redirect }, { skipRefresh: !0, showError: n });
509
+ return i.code === f.OK && (u.token = i.data.token, i.data.ds && (u.ds = i.data.ds)), i;
782
510
  }
783
- function Dt(t, e) {
784
- const n = new m(t.code, t.message), o = I();
785
- return o.onError && !(e && e.silent) && o.onError({ code: t.code, message: t.message, error: n }), n;
511
+ let T;
512
+ async function Je(e) {
513
+ return T || (T = Ue(e).finally(() => {
514
+ T = null;
515
+ }), T);
786
516
  }
787
- async function pe(t, e, n, o) {
788
- return (b() ? !i.cookie : !i.access) && await Rt(), ot(t, e, n, o || {}, !1);
517
+ async function Ue(e) {
518
+ return E() ? await Fe() : await He(e);
789
519
  }
790
- async function ot(t, e, n, o, r) {
791
- let s = "";
792
- if (!b())
793
- try {
794
- s = await at(t, i.access, i.ds);
795
- } catch (f) {
796
- if (!r && await k({ force: !0 }))
797
- return ot(t, e, n, o, !0);
798
- throw P("expired"), new O();
799
- }
800
- const a = new FormData();
801
- Object.keys(n || {}).forEach((f) => {
802
- const d = n[f];
803
- d != null && a.append(f, d);
804
- }), a.append("file", e);
805
- const c = await h(w(t, s), a, o.timeout);
806
- if (c.code === u.OK)
807
- return c.data;
808
- if (c.code === u.AUTH_EXPIRED && !r) {
809
- try {
810
- await dt();
811
- } catch (f) {
812
- throw f instanceof O && P("expired"), f;
813
- }
814
- return ot(t, e, n, o, !0);
815
- }
816
- throw Dt(c, o);
817
- }
818
- function Ee(t) {
819
- const e = Nt(t);
820
- if (Ut(), e.nativeGuard !== "off") {
821
- const n = xt();
822
- if (n.length) {
823
- const o = "[dplugin/core] 这些原生能力在模块加载前已被替换:" + n.join(", ") + "(签名/请求可能被中间人截获,检查是否有脚本抢跑)";
824
- if (e.nativeGuard === "error")
825
- throw new Error(o);
826
- typeof console != "undefined" && console.warn(o);
520
+ async function Fe(e) {
521
+ return await l(p.COOKIE.TOKEN, null, { skipRefresh: !0 });
522
+ }
523
+ async function He(e) {
524
+ let n = {
525
+ method: "POST",
526
+ path: p.SIGN.TOKEN,
527
+ origin: te(),
528
+ ts: Date.now(),
529
+ ds: u.ds
530
+ }, t;
531
+ if (e && e.code)
532
+ n.token = e.code, t = await A(_.TOK, n, "domain");
533
+ else
534
+ throw new h(f.LOGIN_TIMEOUT, "未登录");
535
+ let r = await ne("domain");
536
+ const i = await l(p.SIGN.TOKEN, { dsign: t, pk: r }, { skipRefresh: !0 });
537
+ return i.code === f.OK && (u.token = i.data.token, i.data.ds && (u.ds = i.data.ds)), i;
538
+ }
539
+ let k;
540
+ async function je() {
541
+ return k || (k = We().finally(() => {
542
+ k = null;
543
+ }), k);
544
+ }
545
+ async function We() {
546
+ return E() ? await Ye() : await $e();
547
+ }
548
+ async function Ye() {
549
+ return await l(p.COOKIE.REFRESH, null, { skipRefresh: !0, showError: !1 });
550
+ }
551
+ async function $e() {
552
+ const e = u.token, n = u.ds;
553
+ if (!e || !n)
554
+ throw new ue();
555
+ const t = await l(p.SIGN.REFRESH, null, { skipRefresh: !0, showError: !1 });
556
+ return t.code === f.OK && (t.data.ds && (u.ds = t.data.ds), u.token = t.data.token), t;
557
+ }
558
+ async function Qe() {
559
+ (await l(p.LOGOUT)).code === f.OK && u.clear();
560
+ }
561
+ function en(e) {
562
+ const n = ie(e);
563
+ if (ke(), n.nativeGuard !== "off") {
564
+ const t = ce();
565
+ if (t.length) {
566
+ const r = "[dplugin/core] 这些原生能力在模块加载前已被替换:" + t.join(", ") + "(签名/请求可能被中间人截获,检查是否有脚本抢跑)";
567
+ if (n.nativeGuard === "error")
568
+ throw new Error(r);
569
+ typeof console != "undefined" && console.warn(r);
827
570
  }
828
571
  }
829
572
  }
830
- const me = {
831
- login: te,
832
- logout: we,
833
- isLogin: ft,
834
- getState: Z,
835
- /** 页面加载时恢复登录态;返回是否已登录 */
836
- restore: k
837
- };
838
573
  export {
839
- O as AuthError,
840
- m as BizError,
841
- u as CODE,
842
- me as auth,
843
- Pt as clear,
844
- P as expire,
845
- pt as getMode,
846
- Z as getState,
847
- ge as isAuthError,
848
- ft as isLogin,
849
- te as login,
850
- we as logout,
851
- he as request,
852
- Ee as setup,
853
- i as store,
854
- pe as upload,
855
- k as verifyLogin
574
+ ze as createDomainPair,
575
+ Ze as getDna,
576
+ ne as getPublicKey,
577
+ qe as login,
578
+ Qe as logout,
579
+ l as request,
580
+ en as setup,
581
+ Je as token,
582
+ Xe as upload,
583
+ re as verify
856
584
  };
857
585
  //# sourceMappingURL=index.mjs.map