@dplugin/core 2.0.10 → 2.0.11

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.cjs CHANGED
@@ -1,3 +1,3 @@
1
- "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const U={apiBase:"",mode:"auto",loginUrl:"/login/",timeout:15e3,pollInterval:2e3,pollMax:5,enableAuth:!1,onError:null,onLogin:null,onLogout:null,nativeGuard:"warn"};function Kt(t){return t&&Object.keys(U).forEach(e=>{t[e]!==void 0&&(U[e]=t[e])}),U}function I(){return U}const m=Ct();function Z(t,e){if(typeof t!="function")return!1;let n;try{n=Function.prototype.toString.call(t)}catch(r){return!1}const o=/^function\s+([\w$]+)\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.exec(n);return o?o[1]===e:/\[native code\]/.test(n)&&!/^function\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.test(n)}function xt(t){try{const e=t();return e&&typeof e.then=="function"&&Promise.resolve(e).catch(()=>{}),!1}catch(e){const n=String(e&&e.message||"");return!!e&&e.name==="TypeError"&&/argument|required|参数/i.test(n)}}function Ct(){const t=globalThis,e=typeof t.window!="undefined"&&t.window&&typeof t.window.document!="undefined",n={fetch:null,idbOpen:null,subtle:null,pristine:{fetch:!1,idbOpen:!1,subtle:!1}};typeof t.fetch=="function"&&(n.pristine.fetch=Z(t.fetch,"fetch"),n.fetch=t.fetch.bind(t)),typeof t.indexedDB!="undefined"&&t.indexedDB&&typeof t.indexedDB.open=="function"&&(n.pristine.idbOpen=Z(t.indexedDB.open,"open")&&(!e||xt(()=>t.indexedDB.open())),n.idbOpen=t.indexedDB.open.bind(t.indexedDB));const o=t.crypto&&t.crypto.subtle?t.crypto.subtle:null;if(o){const r=["generateKey","exportKey","importKey","sign","verify","digest"],s={};let u=!0;r.forEach(c=>{typeof o[c]=="function"&&(s[c]=o[c].bind(o),u=u&&Z(o[c],c))}),n.subtle=s,n.pristine.subtle=u}return n}function _t(){if(typeof window=="undefined"||typeof window.document=="undefined")return[];const t=[];return m.fetch&&!m.pristine.fetch&&t.push("fetch"),m.idbOpen&&!m.pristine.idbOpen&&t.push("indexedDB.open"),m.subtle&&!m.pristine.subtle&&t.push("crypto.subtle"),t}const a={OK:"200",PENDING:"202",AUTH_EXPIRED:"500900",FORBIDDEN:"401001",CORS_DENIED:"403",API_NOT_FOUND:"404",NETWORK:"NETWORK",TIMEOUT:"TIMEOUT"};class E extends Error{constructor(e,n){super(n||"请求失败"),this.name="BizError",this.code=e}}class O extends E{constructor(e){super(a.AUTH_EXPIRED,e||"登录已失效"),this.name="AuthError"}}function Mt(t){return t instanceof O||t&&t.code===a.AUTH_EXPIRED}const $="dplugin.access",J="dplugin.sid",wt="dplugin.ds",Q="dplugin.exp",tt="dplugin.cookie";function Ut(t){try{return localStorage.getItem(t)}catch(e){return null}}function Et(t,e){try{e==null||e===""?localStorage.removeItem(t):localStorage.setItem(t,e)}catch(n){}}const G={};function C(t){const e=Ut(t);return e===null?G[t]===void 0?null:G[t]:e}function _(t,e){G[t]=e,Et(t,e)}const i={get access(){return C($)},set access(t){_($,t)},get sid(){return C(J)},set sid(t){_(J,t)},get ds(){return C(wt)},set ds(t){_(wt,t)},get exp(){const t=C(Q);return t?Number(t):0},set exp(t){_(Q,t?String(t):null)},get cookie(){return C(tt)==="1"},set cookie(t){_(tt,t?"1":null)},clear(){[$,J,Q,tt].forEach(t=>{G[t]=null,Et(t,null)})}};function g(t,e){const n=I().apiBase||"";let o=t;return/^https?:\/\//i.test(t)||(o=(n?n.replace(/\/+$/,""):"")+(t.startsWith("/")?t:"/"+t)),e&&(o+=(o.includes("?")?"&":"?")+e),o}function j(t){const e=g(t);if(/^https?:\/\//i.test(e))try{return new URL(e).pathname}catch(n){return e.split("?")[0]}try{return new URL(e,"http://localhost").pathname}catch(n){return e.split("?")[0]}}let S=null;function kt(t){try{const e=typeof location!="undefined"?location.href:void 0;return new URL(t||"",e).origin}catch(e){return null}}function vt(){S=null}function it(){if(S)return S;const t=I();if(t.mode==="sign"||t.mode==="cookie")return S=t.mode,S;const e=typeof location!="undefined"?location.origin:null;if(!e)return S="sign",S;const n=kt(t.apiBase||e);return S=n&&n===e?"cookie":"sign",S}function b(){return it()==="cookie"}async function h(t,e,n){const o=I(),r=n||o.timeout,s=typeof AbortController!="undefined"?new AbortController:null,u=s?setTimeout(()=>s.abort(),r):null,c={method:"POST",credentials:b()?"same-origin":"omit",signal:s?s.signal:void 0};if(typeof FormData!="undefined"&&e instanceof FormData?c.body=e:e!=null&&(c.headers={"Content-Type":"text/plain"},c.body=JSON.stringify(e)),!m.fetch)throw new E(a.NETWORK,"当前环境不支持 fetch");let f;try{f=await m.fetch(t,c)}catch(y){throw u&&clearTimeout(u),y&&y.name==="AbortError"?new E(a.TIMEOUT,"请求超时,请重试"):new E(a.NETWORK,"网络异常,请检查网络后重试")}u&&clearTimeout(u);const d=await f.text();let l=null;if(d)try{l=JSON.parse(d)}catch(y){l=null}if(!f.ok)throw new E(String(f.status),l&&l.message||d||"请求失败");if(!l||l.code===null||l.code===void 0)throw new E("500","响应格式异常");return l.code=String(l.code),l}const Lt="dplugin-auth",Ft=1,Y="keys",st="sso",ct="domain",Ht={name:"ECDSA",namedCurve:"P-256"},Bt={name:"ECDSA",hash:"SHA-256"},H=new Map;let L=null;function X(){if(!m.subtle){const t=typeof location!="undefined"&&location.origin?location.origin:"(未知 origin)";throw new Error("[dplugin/core] crypto.subtle 不存在:浏览器只在**安全上下文**里提供它(https、http://localhost、http://127.0.0.1、http://[::1]、file://);当前 origin="+t)}return m.subtle}function Gt(){return L||(L=new Promise(t=>{if(!m.idbOpen){t(null);return}let e;try{e=m.idbOpen(Lt,Ft)}catch(n){t(null);return}e.onupgradeneeded=()=>{const n=e.result;n.objectStoreNames.contains(Y)||n.createObjectStore(Y)},e.onsuccess=()=>t(e.result),e.onerror=()=>t(null),e.onblocked=()=>t(null)}),L)}function mt(t,e){return Gt().then(n=>n?new Promise(o=>{let r;try{r=n.transaction(Y,t)}catch(c){o(null);return}const s=r.objectStore(Y);let u=null;try{const c=e(s);c&&(c.onsuccess=()=>{u=c.result},c.onerror=()=>{u=null})}catch(c){u=null}r.oncomplete=()=>o(u),r.onerror=()=>o(null),r.onabort=()=>o(null)}):null)}function Ot(t){return H.has(t)?Promise.resolve(H.get(t)):mt("readonly",e=>e.get(t)).then(e=>(e&&H.set(t,e),e||null))}function yt(t,e){return H.set(t,e),mt("readwrite",n=>n.put(e,t)).then(()=>e)}async function at(t){const n=await X().generateKey(Ht,!1,["sign","verify"]);return yt(t,n)}async function bt(t){return Ot(t)}async function ht(t,e){return e?yt(t,e):null}async function Yt(t){return await Ot(t)||at(t)}function Vt(){return at(st)}function jt(){return at(ct)}function ut(){return bt(st)}function St(){return bt(ct)}async function et(t){const n=await X().exportKey("spki",t.publicKey);let o="";const r=new Uint8Array(n);for(let s=0;s<r.length;s++)o+=String.fromCharCode(r[s]);return btoa(o)}function It(t){const e=new Uint8Array(t);let n="";for(let o=0;o<e.length;o++)n+=String.fromCharCode(e[o]);return btoa(n).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}async function Xt(t,e){const o=await X().sign(Bt,t.privateKey,new TextEncoder().encode(e));return It(o)}async function Tt(t){const e=X(),n=await e.exportKey("spki",t.publicKey),o=await e.digest("SHA-256",n);return It(o)}const nt={SSO:st,DOMAIN:ct},ot="-",Wt="POST",N={BIZ:"BIZ",VERIFY:"VERIFY",VERIFY_SID:"VERIFY_SID",TOKEN:"TOKEN"};function p(t){return t==null||t===""?ot:String(t)}function qt(t,e){const n=p(e.origin);return[p(t),p(e.method),p(e.path),n===ot?ot:n.toLowerCase(),p(e.ds),p(e.ts),p(e.subject),p(e.dna),p(e.redirect)].join(`
2
- `)}function zt(t,e,n){return[p(t),p(e.subject),p(e.ds),p(e.ts),n].join(".")}async function A(t,e,n){const o=n==="sso"?await ut():await St();if(!o)throw Pt(n==="sso"?"缺少登录签名密钥":"缺少业务签名密钥");const r=await Xt(o,qt(t,e));return zt(t,e,r)}async function ft(t,e,n){if(!e||!n)throw Pt("缺少 access 或 ds,无法签名");const o=await A(N.BIZ,{method:Wt,path:j(t),origin:K(),ds:n,ts:Date.now(),subject:e},"domain");return"dsign="+encodeURIComponent(o)}function K(){if(typeof location=="undefined")return"";if(location.origin)return location.origin.toLowerCase();try{return new URL(location.href).origin.toLowerCase()}catch(t){return""}}function Pt(t){const e=new Error(t);return e.code="KEY_MISSING",e}function Zt(){const t=typeof navigator!="undefined"&&navigator.userAgent||"",e={device_brand:$t(t),device_model:Jt(t),browser_name:Qt(t),browser_version:te(t),os_name:ee(t),os_version:ne(t)},n=i.ds;return n&&(e.device_id=n),e}function T(t,e){const n=t.match(e);return n?n[1]:""}function $t(t){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":""}function Jt(t){if(/iPhone/.test(t))return"iPhone";if(/iPad/.test(t))return"iPad";const e=t.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);return e?e[1].trim():/Macintosh/.test(t)?"Mac":""}function Qt(t){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":""}function te(t){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.]+)/):""}function ee(t){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":""}function ne(t){if(/Windows NT 10\.0/.test(t))return"10/11";if(/Windows NT 6\.3/.test(t))return"8.1";if(/Windows NT 6\.1/.test(t))return"7";const e=t.match(/(?:iPhone )?OS (\d+[_.]\d+(?:[_.]\d+)?)/);if(e)return e[1].replace(/_/g,".");if(/Android[ _]([\d.]+)/.test(t))return T(t,/Android[ _]([\d.]+)/);const n=t.match(/Mac OS X (\d+[_.]\d+(?:[_.]\d+)?)/);return n?n[1].replace(/_/g,"."):""}const w={SIGN:{VERIFY:"/api/unsafe/dplugin/login/s/verify",TOKEN:"/api/unsafe/dplugin/login/s/token",REFRESH:"/api/unsafe/dplugin/login/s/refresh"},COOKIE:{VERIFY:"/api/unsafe/dplugin/login/c/verify",TOKEN:"/api/unsafe/dplugin/login/c/token",REFRESH:"/api/unsafe/dplugin/login/c/refresh"},LOGOUT:"/api/safe/dplugin/login/logout"},dt=1800*1e3;let M=null,F=null,D=null,V=!1;function W(){return typeof location!="undefined"?location.href:""}function k(t){return new Promise(e=>setTimeout(e,t))}function q(){const t=I();typeof t.onLogin=="function"&&t.onLogin(v())}function R(t,e){return new E(t.code,t.message||e)}function v(){return{mode:it(),loggedIn:z(),hasAccess:!!i.access,sid:i.sid,ds:i.ds}}function z(){return b()?!!i.cookie:!!(i.access||i.sid)}function lt(){i.clear()}function P(t){const e=z();lt();const n=I();if(e&&typeof n.onLogout=="function"&&n.onLogout(t||"expired"),V||!n.enableAuth||!n.loginUrl||typeof location=="undefined")return;V=!0;const o=n.loginUrl.includes("?")?"&":"?";location.href=n.loginUrl+o+"referer="+encodeURIComponent(location.href)}function Rt(t,e){return D||(D=re(t,e).then(n=>(D=null,n),n=>{throw D=null,oe(n),n}),D)}function oe(t){const e=I();typeof e.onError=="function"&&e.onError({code:t&&t.code,message:t&&t.message,error:t})}async function re(t,e){const n=I(),o=Zt();if(b())return ce(t,e,o,n);const r=await ut(),s=await St();try{return await ie(t,e,o,n)}catch(u){throw await ht(nt.SSO,r),await ht(nt.DOMAIN,s),u}}async function ie(t,e,n,o){const r=await Vt(),s=await et(r),u=await h(g(t),Object.assign({},e,{device:n,pk:s}));if(u.code!==a.OK)throw R(u,"登录失败");const c=u.data&&u.data.token;if(!c)throw R(u,"登录失败");const f=await jt(),d=await Tt(f),l=await se(c,o,d),y=await h(g(w.SIGN.TOKEN),{dsign:await A(N.TOKEN,{origin:K(),ts:Date.now(),subject:l.code,dna:d},"domain"),pk:await et(f)});if(y.code!==a.OK||!y.data)throw R(y,"登录失败");return i.sid=l.sid,i.ds=y.data.ds||l.ds,i.access=y.data.token,i.exp=Date.now()+dt,V=!1,q(),v()}async function se(t,e,n){const o=W(),r=Math.max(1,Number(e.pollMax)||5);for(let s=1;s<=r;s++){const u=i.ds||null,c=Date.now(),f=await A(N.VERIFY,{method:"POST",path:j(w.SIGN.VERIFY),origin:K(),ds:u,ts:c,subject:t,dna:n,redirect:o},"sso");let d;try{d=await h(g(w.SIGN.VERIFY),{dsign:f,dna:n,redirect:o})}catch(l){if((l.code===a.NETWORK||l.code===a.TIMEOUT)&&s<r){await k(e.pollInterval);continue}throw l}if(d.code===a.OK&&d.data)return d.data;if(d.code===a.PENDING){if(s<r){await k(e.pollInterval);continue}break}throw R(d,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function ce(t,e,n,o){const r=await h(g(t),Object.assign({},e,{device:n}));if(r.code!==a.OK||!(r.data&&r.data.token))throw R(r,"登录失败");return await ae(o),i.cookie=!0,V=!1,q(),v()}async function ae(t){const e=W(),n=Math.max(1,Number(t.pollMax)||5);for(let o=1;o<=n;o++){let r;try{r=await h(g(w.COOKIE.VERIFY),{redirect:e})}catch(s){if((s.code===a.NETWORK||s.code===a.TIMEOUT)&&o<n){await k(t.pollInterval);continue}throw s}if(r.code===a.OK)return r.data;if(r.code===a.PENDING){if(o<n){await k(t.pollInterval);continue}break}throw R(r,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function x(t){const e=!!(t&&t.force);return F||(F=(async()=>{try{return b()?await ue(e):await de(e)}catch(n){if(n&&(n.code===a.NETWORK||n.code===a.TIMEOUT))throw n;return!1}finally{F=null}})()),F}async function ue(t){return!t&&i.cookie?!0:(await h(g(w.COOKIE.VERIFY),{redirect:W()})).code!==a.OK?(i.cookie=!1,!1):(i.cookie=!0,q(),!0)}async function fe(){if(!i.sid||!i.ds||!await ut())return!1;const e=i.ds,n=await Yt(nt.DOMAIN),o=await Tt(n),r=W(),s=await A(N.VERIFY_SID,{method:"POST",path:j(w.SIGN.VERIFY),origin:K(),ds:e,ts:Date.now(),subject:i.sid,dna:o,redirect:r},"sso"),u=await h(g(w.SIGN.VERIFY),{dsign:s,dna:o,redirect:r});if(u.code!==a.OK||!u.data)return!1;const c=await h(g(w.SIGN.TOKEN),{dsign:await A(N.TOKEN,{origin:K(),ts:Date.now(),subject:u.data.code,dna:o},"domain"),pk:await et(n)});return c.code!==a.OK||!c.data?!1:(i.access=c.data.token,c.data.ds&&(i.ds=c.data.ds),i.exp=Date.now()+dt,q(),!0)}async function de(t){return!t&&i.access?!0:fe()}function gt(){return M||(M=(b()?le():ge()).then(t=>(M=null,t),t=>{throw M=null,t})),M}async function le(){const t=await h(g(w.COOKIE.TOKEN));if(t.code===a.OK)return!0;const e=await h(g(w.COOKIE.REFRESH));if(e.code===a.OK)return!0;if(e.code===a.AUTH_EXPIRED||t.code===a.AUTH_EXPIRED)throw new O(e.message||t.message);if(e.code==="500"&&/重复/.test(e.message||""))return await k(800),!0;throw new E(e.code,e.message)}async function ge(){const t=i.access,e=i.ds;if(!t||!e)throw new O;const n=await A(N.BIZ,{method:"POST",path:j(w.SIGN.REFRESH),origin:K(),ds:e,ts:Date.now(),subject:t},"domain"),o=await h(g(w.SIGN.REFRESH,"dsign="+encodeURIComponent(n)));if(o.code===a.OK&&o.data)return i.access=o.data.token,o.data.ds&&(i.ds=o.data.ds),i.exp=Date.now()+dt,i.access;const r=await we(t,1200);if(r)return r;throw o.code===a.AUTH_EXPIRED?new O(o.message):new E(o.code,o.message)}function we(t,e){const n=Date.now()+e;return new Promise(o=>{const r=()=>{const s=i.access;if(s&&s!==t){o(s);return}if(Date.now()>=n){o(null);return}setTimeout(r,150)};r()})}async function Dt(){try{let e=await pt();e.code===a.AUTH_EXPIRED&&(await gt(),e=await pt())}catch(e){}lt();const t=I();typeof t.onLogout=="function"&&t.onLogout("logout")}async function pt(){if(b())return h(g(w.LOGOUT));const t=await ft(w.LOGOUT,i.access,i.ds);return h(g(w.LOGOUT,t))}async function he(t,e,n){const o=n||{};return o.auth!==!1&&await Nt(),B(t,e||{},o,!1)}async function Nt(){if(b()){if(i.cookie||await x())return;throw P("expired"),new O}if(!i.access&&!await x())throw P("expired"),new O}async function B(t,e,n,o){const r=n.auth!==!1,s=r&&!b();let u="",c=null;if(s){c=i.access;try{u=await ft(t,c,i.ds)}catch(d){if(!o&&await x({force:!0}))return B(t,e,n,!0);throw P("expired"),new O}}const f=await h(g(t,u),e,n.timeout);if(f.code===a.OK)return f.data;if(r&&f.code===a.AUTH_EXPIRED&&!o){if(s&&c&&i.access!==c)return B(t,e,n,!0);try{await gt()}catch(d){throw d instanceof O&&P("expired"),d}return B(t,e,n,!0)}throw r&&f.code===a.AUTH_EXPIRED?(P("expired"),new O(f.message)):At(f,n)}function At(t,e){const n=new E(t.code,t.message),o=I();return o.onError&&!(e&&e.silent)&&o.onError({code:t.code,message:t.message,error:n}),n}async function pe(t,e,n,o){return(b()?!i.cookie:!i.access)&&await Nt(),rt(t,e,n,o||{},!1)}async function rt(t,e,n,o,r){let s="";if(!b())try{s=await ft(t,i.access,i.ds)}catch(f){if(!r&&await x({force:!0}))return rt(t,e,n,o,!0);throw P("expired"),new O}const u=new FormData;Object.keys(n||{}).forEach(f=>{const d=n[f];d!=null&&u.append(f,d)}),u.append("file",e);const c=await h(g(t,s),u,o.timeout);if(c.code===a.OK)return c.data;if(c.code===a.AUTH_EXPIRED&&!r){try{await gt()}catch(f){throw f instanceof O&&P("expired"),f}return rt(t,e,n,o,!0)}throw At(c,o)}function Ee(t){const e=Kt(t);if(vt(),e.nativeGuard!=="off"){const n=_t();if(n.length){const o="[dplugin/core] 这些原生能力在模块加载前已被替换:"+n.join(", ")+"(签名/请求可能被中间人截获,检查是否有脚本抢跑)";if(e.nativeGuard==="error")throw new Error(o);typeof console!="undefined"&&console.warn(o)}}}const me={login:Rt,logout:Dt,isLogin:z,getState:v,restore:x};exports.AuthError=O;exports.BizError=E;exports.CODE=a;exports.auth=me;exports.clear=lt;exports.expire=P;exports.getMode=it;exports.getState=v;exports.isAuthError=Mt;exports.isLogin=z;exports.login=Rt;exports.logout=Dt;exports.request=he;exports.setup=Ee;exports.store=i;exports.upload=pe;exports.verifyLogin=x;
1
+ "use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const U={apiBase:"",mode:"auto",loginUrl:"/login/",timeout:15e3,pollInterval:2e3,pollMax:5,enableAuth:!1,onError:null,onLogin:null,onLogout:null,nativeGuard:"warn"};function Ke(e){return e&&Object.keys(U).forEach(t=>{e[t]!==void 0&&(U[t]=e[t])}),U}function I(){return U}const m=Ce();function Z(e,t){if(typeof e!="function")return!1;let n;try{n=Function.prototype.toString.call(e)}catch(r){return!1}const o=/^function\s+([\w$]+)\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.exec(n);return o?o[1]===t:/\[native code\]/.test(n)&&!/^function\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.test(n)}function xe(e){try{const t=e();return t&&typeof t.then=="function"&&Promise.resolve(t).catch(()=>{}),!1}catch(t){const n=String(t&&t.message||"");return!!t&&t.name==="TypeError"&&/argument|required|参数/i.test(n)}}function Ce(){const e=globalThis,t=typeof e.window!="undefined"&&e.window&&typeof e.window.document!="undefined",n={fetch:null,idbOpen:null,subtle:null,pristine:{fetch:!1,idbOpen:!1,subtle:!1}};typeof e.fetch=="function"&&(n.pristine.fetch=Z(e.fetch,"fetch"),n.fetch=e.fetch.bind(e)),typeof e.indexedDB!="undefined"&&e.indexedDB&&typeof e.indexedDB.open=="function"&&(n.pristine.idbOpen=Z(e.indexedDB.open,"open")&&(!t||xe(()=>e.indexedDB.open())),n.idbOpen=e.indexedDB.open.bind(e.indexedDB));const o=e.crypto&&e.crypto.subtle?e.crypto.subtle:null;if(o){const r=["generateKey","exportKey","importKey","sign","verify","digest"],s={};let u=!0;r.forEach(c=>{typeof o[c]=="function"&&(s[c]=o[c].bind(o),u=u&&Z(o[c],c))}),n.subtle=s,n.pristine.subtle=u}return n}function _e(){if(typeof window=="undefined"||typeof window.document=="undefined")return[];const e=[];return m.fetch&&!m.pristine.fetch&&e.push("fetch"),m.idbOpen&&!m.pristine.idbOpen&&e.push("indexedDB.open"),m.subtle&&!m.pristine.subtle&&e.push("crypto.subtle"),e}const a={OK:"200",PENDING:"202",AUTH_EXPIRED:"500900",FORBIDDEN:"401001",CORS_DENIED:"403",API_NOT_FOUND:"404",NETWORK:"NETWORK",TIMEOUT:"TIMEOUT"};class E extends Error{constructor(t,n){super(n||"请求失败"),this.name="BizError",this.code=t}}class O extends E{constructor(t){super(a.AUTH_EXPIRED,t||"登录已失效"),this.name="AuthError"}}function Me(e){return e instanceof O||e&&e.code===a.AUTH_EXPIRED}const $="dplugin.access",J="dplugin.sid",we="dplugin.ds",Q="dplugin.exp",ee="dplugin.cookie";function Ue(e){try{return localStorage.getItem(e)}catch(t){return null}}function Ee(e,t){try{t==null||t===""?localStorage.removeItem(e):localStorage.setItem(e,t)}catch(n){}}const G={};function C(e){const t=Ue(e);return t===null?G[e]===void 0?null:G[e]:t}function _(e,t){G[e]=t,Ee(e,t)}const i={get access(){return C($)},set access(e){_($,e)},get sid(){return C(J)},set sid(e){_(J,e)},get ds(){return C(we)},set ds(e){_(we,e)},get exp(){const e=C(Q);return e?Number(e):0},set exp(e){_(Q,e?String(e):null)},get cookie(){return C(ee)==="1"},set cookie(e){_(ee,e?"1":null)},clear(){[$,J,Q,ee].forEach(e=>{G[e]=null,Ee(e,null)})}};function g(e,t){const n=I().apiBase||"";let o=e;return/^https?:\/\//i.test(e)||(o=(n?n.replace(/\/+$/,""):"")+(e.startsWith("/")?e:"/"+e)),t&&(o+=(o.includes("?")?"&":"?")+t),o}function j(e){const t=g(e);if(/^https?:\/\//i.test(t))try{return new URL(t).pathname}catch(n){return t.split("?")[0]}try{return new URL(t,"http://localhost").pathname}catch(n){return t.split("?")[0]}}let S=null;function ke(e){try{const t=typeof location!="undefined"?location.href:void 0;return new URL(e||"",t).origin}catch(t){return null}}function ve(){S=null}function ie(){if(S)return S;const e=I();if(e.mode==="sign"||e.mode==="cookie")return S=e.mode,S;const t=typeof location!="undefined"?location.origin:null;if(!t)return S="sign",S;const n=ke(e.apiBase||t);return S=n&&n===t?"cookie":"sign",S}function b(){return ie()==="cookie"}async function h(e,t,n){const o=I(),r=n||o.timeout,s=typeof AbortController!="undefined"?new AbortController:null,u=s?setTimeout(()=>s.abort(),r):null,c={method:"POST",credentials:b()?"same-origin":"omit",signal:s?s.signal:void 0};if(typeof FormData!="undefined"&&t instanceof FormData?c.body=t:t!=null&&(c.headers={"Content-Type":"text/plain"},c.body=JSON.stringify(t)),!m.fetch)throw new E(a.NETWORK,"当前环境不支持 fetch");let f;try{f=await m.fetch(e,c)}catch(y){throw u&&clearTimeout(u),y&&y.name==="AbortError"?new E(a.TIMEOUT,"请求超时,请重试"):new E(a.NETWORK,"网络异常,请检查网络后重试")}u&&clearTimeout(u);const d=await f.text();let l=null;if(d)try{l=JSON.parse(d)}catch(y){l=null}if(!f.ok)throw new E(String(f.status),l&&l.message||d||"请求失败");if(!l||l.code===null||l.code===void 0)throw new E("500","响应格式异常");return l.code=String(l.code),l}const Le="dplugin-auth",Fe=1,Y="keys",se="sso",ce="domain",He={name:"ECDSA",namedCurve:"P-256"},Be={name:"ECDSA",hash:"SHA-256"},H=new Map;let L=null;function X(){if(!m.subtle){const e=typeof location!="undefined"&&location.origin?location.origin:"(未知 origin)";throw new Error("[dplugin/core] crypto.subtle 不存在:浏览器只在**安全上下文**里提供它(https、http://localhost、http://127.0.0.1、http://[::1]、file://);当前 origin="+e)}return m.subtle}function Ge(){return L||(L=new Promise(e=>{if(!m.idbOpen){e(null);return}let t;try{t=m.idbOpen(Le,Fe)}catch(n){e(null);return}t.onupgradeneeded=()=>{const n=t.result;n.objectStoreNames.contains(Y)||n.createObjectStore(Y)},t.onsuccess=()=>e(t.result),t.onerror=()=>e(null),t.onblocked=()=>e(null)}),L)}function me(e,t){return Ge().then(n=>n?new Promise(o=>{let r;try{r=n.transaction(Y,e)}catch(c){o(null);return}const s=r.objectStore(Y);let u=null;try{const c=t(s);c&&(c.onsuccess=()=>{u=c.result},c.onerror=()=>{u=null})}catch(c){u=null}r.oncomplete=()=>o(u),r.onerror=()=>o(null),r.onabort=()=>o(null)}):null)}function Oe(e){return H.has(e)?Promise.resolve(H.get(e)):me("readonly",t=>t.get(e)).then(t=>(t&&H.set(e,t),t||null))}function ye(e,t){return H.set(e,t),me("readwrite",n=>n.put(t,e)).then(()=>t)}async function ae(e){const n=await X().generateKey(He,!1,["sign","verify"]);return ye(e,n)}async function be(e){return Oe(e)}async function he(e,t){return t?ye(e,t):null}async function Ye(e){return await Oe(e)||ae(e)}function Ve(){return ae(se)}function je(){return ae(ce)}function ue(){return be(se)}function Se(){return be(ce)}async function te(e){const n=await X().exportKey("spki",e.publicKey);let o="";const r=new Uint8Array(n);for(let s=0;s<r.length;s++)o+=String.fromCharCode(r[s]);return btoa(o)}function Ie(e){const t=new Uint8Array(e);let n="";for(let o=0;o<t.length;o++)n+=String.fromCharCode(t[o]);return btoa(n).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}async function Xe(e,t){const o=await X().sign(Be,e.privateKey,new TextEncoder().encode(t));return Ie(o)}async function Te(e){const t=X(),n=await t.exportKey("spki",e.publicKey),o=await t.digest("SHA-256",n);return Ie(o)}const ne={SSO:se,DOMAIN:ce},oe="-",We="POST",N={BIZ:"BIZ",VERIFY:"VERIFY",VERIFY_SID:"VERIFY_SID",TOKEN:"TOKEN"};function p(e){return e==null||e===""?oe:String(e)}function qe(e,t){const n=p(t.origin);return[p(e),p(t.method),p(t.path),n===oe?oe:n.toLowerCase(),p(t.ds),p(t.ts),p(t.subject),p(t.dna),p(t.redirect)].join(`
2
+ `)}function ze(e,t,n){return[p(e),p(t.subject),p(t.ds),p(t.ts),n].join(".")}async function A(e,t,n){const o=n==="sso"?await ue():await Se();if(!o)throw Pe(n==="sso"?"缺少登录签名密钥":"缺少业务签名密钥");const r=await Xe(o,qe(e,t));return ze(e,t,r)}async function fe(e,t,n){if(!t||!n)throw Pe("缺少 access 或 ds,无法签名");const o=await A(N.BIZ,{method:We,path:j(e),origin:K(),ds:n,ts:Date.now(),subject:t},"domain");return"dsign="+encodeURIComponent(o)}function K(){if(typeof location=="undefined")return"";if(location.origin)return location.origin.toLowerCase();try{return new URL(location.href).origin.toLowerCase()}catch(e){return""}}function Pe(e){const t=new Error(e);return t.code="KEY_MISSING",t}function Ze(){const e=typeof navigator!="undefined"&&navigator.userAgent||"",t={device_brand:$e(e),device_model:Je(e),browser_name:Qe(e),browser_version:et(e),os_name:tt(e),os_version:nt(e)},n=i.ds;return n&&(t.device_id=n),t}function T(e,t){const n=e.match(t);return n?n[1]:""}function $e(e){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":""}function Je(e){if(/iPhone/.test(e))return"iPhone";if(/iPad/.test(e))return"iPad";const t=e.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);return t?t[1].trim():/Macintosh/.test(e)?"Mac":""}function Qe(e){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":""}function et(e){return/MicroMessenger\/([\d.]+)/.test(e)?T(e,/MicroMessenger\/([\d.]+)/):/Edg[A-Za-z]*\/([\d.]+)/.test(e)?T(e,/Edg[A-Za-z]*\/([\d.]+)/):/OPR\/([\d.]+)/.test(e)?T(e,/OPR\/([\d.]+)/):/Firefox\/([\d.]+)/.test(e)?T(e,/Firefox\/([\d.]+)/):/CriOS\/([\d.]+)/.test(e)?T(e,/CriOS\/([\d.]+)/):/Chrome\/([\d.]+)/.test(e)?T(e,/Chrome\/([\d.]+)/):/Version\/([\d.]+).*Safari/.test(e)?T(e,/Version\/([\d.]+)/):""}function tt(e){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":""}function nt(e){if(/Windows NT 10\.0/.test(e))return"10/11";if(/Windows NT 6\.3/.test(e))return"8.1";if(/Windows NT 6\.1/.test(e))return"7";const t=e.match(/(?:iPhone )?OS (\d+[_.]\d+(?:[_.]\d+)?)/);if(t)return t[1].replace(/_/g,".");if(/Android[ _]([\d.]+)/.test(e))return T(e,/Android[ _]([\d.]+)/);const n=e.match(/Mac OS X (\d+[_.]\d+(?:[_.]\d+)?)/);return n?n[1].replace(/_/g,"."):""}const w={SIGN:{VERIFY:"/api/unsafe/dplugin/login/s/verify",TOKEN:"/api/unsafe/dplugin/login/s/token",REFRESH:"/api/unsafe/dplugin/login/s/refresh"},COOKIE:{VERIFY:"/api/unsafe/dplugin/login/c/verify",TOKEN:"/api/unsafe/dplugin/login/c/token",REFRESH:"/api/unsafe/dplugin/login/c/refresh"},LOGOUT:"/api/safe/dplugin/login/logout"},de=1800*1e3;let M=null,F=null,R=null,V=!1;function W(){return typeof location!="undefined"?location.href:""}function k(e){return new Promise(t=>setTimeout(t,e))}function q(){const e=I();typeof e.onLogin=="function"&&e.onLogin(v())}function D(e,t){return new E(e.code,e.message||t)}function v(){return{mode:ie(),loggedIn:z(),hasAccess:!!i.access,sid:i.sid,ds:i.ds}}function z(){return b()?!!i.cookie:!!(i.access||i.sid)}function le(){i.clear()}function P(e){const t=z();le();const n=I();if(t&&typeof n.onLogout=="function"&&n.onLogout(e||"expired"),V||!n.enableAuth||!n.loginUrl||typeof location=="undefined")return;V=!0;const o=n.loginUrl.includes("?")?"&":"?";location.href=n.loginUrl+o+"referer="+encodeURIComponent(location.href)}function Re(e,t){return R||(R=rt(e,t).then(n=>(R=null,n),n=>{throw R=null,ot(n),n}),R)}function ot(e){const t=I();typeof t.onError=="function"&&t.onError({code:e&&e.code,message:e&&e.message,error:e})}async function rt(e,t){const n=I(),o=Ze();if(b())return ct(e,t,o,n);const r=await ue(),s=await Se();try{return await it(e,t,o,n)}catch(u){throw await he(ne.SSO,r),await he(ne.DOMAIN,s),u}}async function it(e,t,n,o){const r=await Ve(),s=await te(r),u=await h(g(e),Object.assign({},t,{device:n,pk:s}));if(u.code!==a.OK)throw D(u,"登录失败");const c=u.data&&u.data.token;if(!c)throw D(u,"登录失败");const f=await je(),d=await Te(f),l=await st(c,o,d),y=await h(g(w.SIGN.TOKEN),{dsign:await A(N.TOKEN,{origin:K(),ts:Date.now(),subject:l.code,dna:d},"domain"),pk:await te(f)});if(y.code!==a.OK||!y.data)throw D(y,"登录失败");return i.sid=l.sid,i.ds=y.data.ds||l.ds,i.access=y.data.token,i.exp=Date.now()+de,V=!1,q(),v()}async function st(e,t,n){const o=W(),r=Math.max(1,Number(t.pollMax)||5);for(let s=1;s<=r;s++){const u=i.ds||null,c=Date.now(),f=await A(N.VERIFY,{method:"POST",path:j(w.SIGN.VERIFY),origin:K(),ds:u,ts:c,subject:e,dna:n,redirect:o},"sso");let d;try{d=await h(g(w.SIGN.VERIFY),{dsign:f,dna:n,redirect:o})}catch(l){if((l.code===a.NETWORK||l.code===a.TIMEOUT)&&s<r){await k(t.pollInterval);continue}throw l}if(d.code===a.OK&&d.data)return d.data;if(d.code===a.PENDING){if(s<r){await k(t.pollInterval);continue}break}throw D(d,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function ct(e,t,n,o){const r=await h(g(e),Object.assign({},t,{device:n}));if(r.code!==a.OK)throw D(r,"登录失败");return await at(o),i.cookie=!0,V=!1,q(),v()}async function at(e){const t=W(),n=Math.max(1,Number(e.pollMax)||5);for(let o=1;o<=n;o++){let r;try{r=await h(g(w.COOKIE.VERIFY),{redirect:t})}catch(s){if((s.code===a.NETWORK||s.code===a.TIMEOUT)&&o<n){await k(e.pollInterval);continue}throw s}if(r.code===a.OK)return r.data;if(r.code===a.PENDING){if(o<n){await k(e.pollInterval);continue}break}throw D(r,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function x(e){const t=!!(e&&e.force);return F||(F=(async()=>{try{return b()?await ut(t):await dt(t)}catch(n){if(n&&(n.code===a.NETWORK||n.code===a.TIMEOUT))throw n;return!1}finally{F=null}})()),F}async function ut(e){return!e&&i.cookie?!0:(await h(g(w.COOKIE.VERIFY),{redirect:W()})).code!==a.OK?(i.cookie=!1,!1):(i.cookie=!0,q(),!0)}async function ft(){if(!i.sid||!i.ds||!await ue())return!1;const t=i.ds,n=await Ye(ne.DOMAIN),o=await Te(n),r=W(),s=await A(N.VERIFY_SID,{method:"POST",path:j(w.SIGN.VERIFY),origin:K(),ds:t,ts:Date.now(),subject:i.sid,dna:o,redirect:r},"sso"),u=await h(g(w.SIGN.VERIFY),{dsign:s,dna:o,redirect:r});if(u.code!==a.OK||!u.data)return!1;const c=await h(g(w.SIGN.TOKEN),{dsign:await A(N.TOKEN,{origin:K(),ts:Date.now(),subject:u.data.code,dna:o},"domain"),pk:await te(n)});return c.code!==a.OK||!c.data?!1:(i.access=c.data.token,c.data.ds&&(i.ds=c.data.ds),i.exp=Date.now()+de,q(),!0)}async function dt(e){return!e&&i.access?!0:ft()}function ge(){return M||(M=(b()?lt():gt()).then(e=>(M=null,e),e=>{throw M=null,e})),M}async function lt(){const e=await h(g(w.COOKIE.TOKEN));if(e.code===a.OK)return!0;const t=await h(g(w.COOKIE.REFRESH));if(t.code===a.OK)return!0;if(t.code===a.AUTH_EXPIRED||e.code===a.AUTH_EXPIRED)throw new O(t.message||e.message);if(t.code==="500"&&/重复/.test(t.message||""))return await k(800),!0;throw new E(t.code,t.message)}async function gt(){const e=i.access,t=i.ds;if(!e||!t)throw new O;const n=await A(N.BIZ,{method:"POST",path:j(w.SIGN.REFRESH),origin:K(),ds:t,ts:Date.now(),subject:e},"domain"),o=await h(g(w.SIGN.REFRESH,"dsign="+encodeURIComponent(n)));if(o.code===a.OK&&o.data)return i.access=o.data.token,o.data.ds&&(i.ds=o.data.ds),i.exp=Date.now()+de,i.access;const r=await wt(e,1200);if(r)return r;throw o.code===a.AUTH_EXPIRED?new O(o.message):new E(o.code,o.message)}function wt(e,t){const n=Date.now()+t;return new Promise(o=>{const r=()=>{const s=i.access;if(s&&s!==e){o(s);return}if(Date.now()>=n){o(null);return}setTimeout(r,150)};r()})}async function De(){try{let t=await pe();t.code===a.AUTH_EXPIRED&&(await ge(),t=await pe())}catch(t){}le();const e=I();typeof e.onLogout=="function"&&e.onLogout("logout")}async function pe(){if(b())return h(g(w.LOGOUT));const e=await fe(w.LOGOUT,i.access,i.ds);return h(g(w.LOGOUT,e))}async function ht(e,t,n){const o=n||{};return o.auth!==!1&&await Ne(),B(e,t||{},o,!1)}async function Ne(){if(b()){if(i.cookie||await x())return;throw P("expired"),new O}if(!i.access&&!await x())throw P("expired"),new O}async function B(e,t,n,o){const r=n.auth!==!1,s=r&&!b();let u="",c=null;if(s){c=i.access;try{u=await fe(e,c,i.ds)}catch(d){if(!o&&await x({force:!0}))return B(e,t,n,!0);throw P("expired"),new O}}const f=await h(g(e,u),t,n.timeout);if(f.code===a.OK)return f.data;if(r&&f.code===a.AUTH_EXPIRED&&!o){if(s&&c&&i.access!==c)return B(e,t,n,!0);try{await ge()}catch(d){throw d instanceof O&&P("expired"),d}return B(e,t,n,!0)}throw r&&f.code===a.AUTH_EXPIRED?(P("expired"),new O(f.message)):Ae(f,n)}function Ae(e,t){const n=new E(e.code,e.message),o=I();return o.onError&&!(t&&t.silent)&&o.onError({code:e.code,message:e.message,error:n}),n}async function pt(e,t,n,o){return(b()?!i.cookie:!i.access)&&await Ne(),re(e,t,n,o||{},!1)}async function re(e,t,n,o,r){let s="";if(!b())try{s=await fe(e,i.access,i.ds)}catch(f){if(!r&&await x({force:!0}))return re(e,t,n,o,!0);throw P("expired"),new O}const u=new FormData;Object.keys(n||{}).forEach(f=>{const d=n[f];d!=null&&u.append(f,d)}),u.append("file",t);const c=await h(g(e,s),u,o.timeout);if(c.code===a.OK)return c.data;if(c.code===a.AUTH_EXPIRED&&!r){try{await ge()}catch(f){throw f instanceof O&&P("expired"),f}return re(e,t,n,o,!0)}throw Ae(c,o)}function Et(e){const t=Ke(e);if(ve(),t.nativeGuard!=="off"){const n=_e();if(n.length){const o="[dplugin/core] 这些原生能力在模块加载前已被替换:"+n.join(", ")+"(签名/请求可能被中间人截获,检查是否有脚本抢跑)";if(t.nativeGuard==="error")throw new Error(o);typeof console!="undefined"&&console.warn(o)}}}const mt={login:Re,logout:De,isLogin:z,getState:v,restore:x};exports.AuthError=O;exports.BizError=E;exports.CODE=a;exports.auth=mt;exports.clear=le;exports.expire=P;exports.getMode=ie;exports.getState=v;exports.isAuthError=Me;exports.isLogin=z;exports.login=Re;exports.logout=De;exports.request=ht;exports.setup=Et;exports.store=i;exports.upload=pt;exports.verifyLogin=x;
3
3
  //# sourceMappingURL=index.cjs.map
package/dist/index.mjs CHANGED
@@ -471,7 +471,7 @@ const g = {
471
471
  },
472
472
  LOGOUT: "/api/safe/dplugin/login/logout"
473
473
  }, ut = 1800 * 1e3;
474
- let C = null, L = null, D = null, B = !1;
474
+ let C = null, L = null, R = null, B = !1;
475
475
  function X() {
476
476
  return typeof location != "undefined" ? location.href : "";
477
477
  }
@@ -482,7 +482,7 @@ function W() {
482
482
  const t = I();
483
483
  typeof t.onLogin == "function" && t.onLogin(Z());
484
484
  }
485
- function P(t, e) {
485
+ function D(t, e) {
486
486
  return new m(t.code, t.message || e);
487
487
  }
488
488
  function Z() {
@@ -500,7 +500,7 @@ function ft() {
500
500
  function Pt() {
501
501
  i.clear();
502
502
  }
503
- function R(t) {
503
+ function P(t) {
504
504
  const e = ft();
505
505
  Pt();
506
506
  const n = I();
@@ -511,9 +511,9 @@ function R(t) {
511
511
  location.href = n.loginUrl + o + "referer=" + encodeURIComponent(location.href);
512
512
  }
513
513
  function te(t, e) {
514
- return D || (D = ne(t, e).then((n) => (D = null, n), (n) => {
515
- throw D = null, ee(n), n;
516
- }), D);
514
+ return R || (R = ne(t, e).then((n) => (R = null, n), (n) => {
515
+ throw R = null, ee(n), n;
516
+ }), R);
517
517
  }
518
518
  function ee(t) {
519
519
  const e = I();
@@ -537,10 +537,10 @@ async function ne(t, e) {
537
537
  async function oe(t, e, n, o) {
538
538
  const r = await Gt(), s = await tt(r), a = await h(w(t), Object.assign({}, e, { device: n, pk: s }));
539
539
  if (a.code !== u.OK)
540
- throw P(a, "登录失败");
540
+ throw D(a, "登录失败");
541
541
  const c = a.data && a.data.token;
542
542
  if (!c)
543
- throw P(a, "登录失败");
543
+ throw D(a, "登录失败");
544
544
  const f = await Yt(), d = await It(f), l = await re(c, o, d), y = await h(w(g.SIGN.TOKEN), {
545
545
  dsign: await K(N.TOKEN, {
546
546
  origin: A(),
@@ -551,7 +551,7 @@ async function oe(t, e, n, o) {
551
551
  pk: await tt(f)
552
552
  });
553
553
  if (y.code !== u.OK || !y.data)
554
- throw P(y, "登录失败");
554
+ throw D(y, "登录失败");
555
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
556
  }
557
557
  async function re(t, e, n) {
@@ -586,14 +586,14 @@ async function re(t, e, n) {
586
586
  }
587
587
  break;
588
588
  }
589
- throw P(d, "登录失败");
589
+ throw D(d, "登录失败");
590
590
  }
591
591
  throw new m(u.AUTH_EXPIRED, "登录超时,请重试");
592
592
  }
593
593
  async function ie(t, e, n, o) {
594
594
  const r = await h(w(t), Object.assign({}, e, { device: n }));
595
- if (r.code !== u.OK || !(r.data && r.data.token))
596
- throw P(r, "登录失败");
595
+ if (r.code !== u.OK)
596
+ throw D(r, "登录失败");
597
597
  return await se(o), i.cookie = !0, B = !1, W(), Z();
598
598
  }
599
599
  async function se(t) {
@@ -618,7 +618,7 @@ async function se(t) {
618
618
  }
619
619
  break;
620
620
  }
621
- throw P(r, "登录失败");
621
+ throw D(r, "登录失败");
622
622
  }
623
623
  throw new m(u.AUTH_EXPIRED, "登录超时,请重试");
624
624
  }
@@ -747,10 +747,10 @@ async function Rt() {
747
747
  if (b()) {
748
748
  if (i.cookie || await k())
749
749
  return;
750
- throw R("expired"), new O();
750
+ throw P("expired"), new O();
751
751
  }
752
752
  if (!i.access && !await k())
753
- throw R("expired"), new O();
753
+ throw P("expired"), new O();
754
754
  }
755
755
  async function H(t, e, n, o) {
756
756
  const r = n.auth !== !1, s = r && !b();
@@ -762,7 +762,7 @@ async function H(t, e, n, o) {
762
762
  } catch (d) {
763
763
  if (!o && await k({ force: !0 }))
764
764
  return H(t, e, n, !0);
765
- throw R("expired"), new O();
765
+ throw P("expired"), new O();
766
766
  }
767
767
  }
768
768
  const f = await h(w(t, a), e, n.timeout);
@@ -774,11 +774,11 @@ async function H(t, e, n, o) {
774
774
  try {
775
775
  await dt();
776
776
  } catch (d) {
777
- throw d instanceof O && R("expired"), d;
777
+ throw d instanceof O && P("expired"), d;
778
778
  }
779
779
  return H(t, e, n, !0);
780
780
  }
781
- throw r && f.code === u.AUTH_EXPIRED ? (R("expired"), new O(f.message)) : Dt(f, n);
781
+ throw r && f.code === u.AUTH_EXPIRED ? (P("expired"), new O(f.message)) : Dt(f, n);
782
782
  }
783
783
  function Dt(t, e) {
784
784
  const n = new m(t.code, t.message), o = I();
@@ -795,7 +795,7 @@ async function ot(t, e, n, o, r) {
795
795
  } catch (f) {
796
796
  if (!r && await k({ force: !0 }))
797
797
  return ot(t, e, n, o, !0);
798
- throw R("expired"), new O();
798
+ throw P("expired"), new O();
799
799
  }
800
800
  const a = new FormData();
801
801
  Object.keys(n || {}).forEach((f) => {
@@ -809,7 +809,7 @@ async function ot(t, e, n, o, r) {
809
809
  try {
810
810
  await dt();
811
811
  } catch (f) {
812
- throw f instanceof O && R("expired"), f;
812
+ throw f instanceof O && P("expired"), f;
813
813
  }
814
814
  return ot(t, e, n, o, !0);
815
815
  }
@@ -841,7 +841,7 @@ export {
841
841
  u as CODE,
842
842
  me as auth,
843
843
  Pt as clear,
844
- R as expire,
844
+ P as expire,
845
845
  pt as getMode,
846
846
  Z as getState,
847
847
  ge as isAuthError,
package/dist/index.umd.js CHANGED
@@ -1,3 +1,3 @@
1
- (function(d,I){typeof exports=="object"&&typeof module!="undefined"?I(exports):typeof define=="function"&&define.amd?define(["exports"],I):(d=typeof globalThis!="undefined"?globalThis:d||self,I(d.DPluginCore={}))})(this,(function(d){"use strict";const I={apiBase:"",mode:"auto",loginUrl:"/login/",timeout:15e3,pollInterval:2e3,pollMax:5,enableAuth:!1,onError:null,onLogin:null,onLogout:null,nativeGuard:"warn"};function Ce(e){return e&&Object.keys(I).forEach(t=>{e[t]!==void 0&&(I[t]=e[t])}),I}function T(){return I}const O=Me();function $(e,t){if(typeof e!="function")return!1;let n;try{n=Function.prototype.toString.call(e)}catch(r){return!1}const o=/^function\s+([\w$]+)\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.exec(n);return o?o[1]===t:/\[native code\]/.test(n)&&!/^function\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.test(n)}function _e(e){try{const t=e();return t&&typeof t.then=="function"&&Promise.resolve(t).catch(()=>{}),!1}catch(t){const n=String(t&&t.message||"");return!!t&&t.name==="TypeError"&&/argument|required|参数/i.test(n)}}function Me(){const e=globalThis,t=typeof e.window!="undefined"&&e.window&&typeof e.window.document!="undefined",n={fetch:null,idbOpen:null,subtle:null,pristine:{fetch:!1,idbOpen:!1,subtle:!1}};typeof e.fetch=="function"&&(n.pristine.fetch=$(e.fetch,"fetch"),n.fetch=e.fetch.bind(e)),typeof e.indexedDB!="undefined"&&e.indexedDB&&typeof e.indexedDB.open=="function"&&(n.pristine.idbOpen=$(e.indexedDB.open,"open")&&(!t||_e(()=>e.indexedDB.open())),n.idbOpen=e.indexedDB.open.bind(e.indexedDB));const o=e.crypto&&e.crypto.subtle?e.crypto.subtle:null;if(o){const r=["generateKey","exportKey","importKey","sign","verify","digest"],s={};let u=!0;r.forEach(c=>{typeof o[c]=="function"&&(s[c]=o[c].bind(o),u=u&&$(o[c],c))}),n.subtle=s,n.pristine.subtle=u}return n}function Ue(){if(typeof window=="undefined"||typeof window.document=="undefined")return[];const e=[];return O.fetch&&!O.pristine.fetch&&e.push("fetch"),O.idbOpen&&!O.pristine.idbOpen&&e.push("indexedDB.open"),O.subtle&&!O.pristine.subtle&&e.push("crypto.subtle"),e}const a={OK:"200",PENDING:"202",AUTH_EXPIRED:"500900",FORBIDDEN:"401001",CORS_DENIED:"403",API_NOT_FOUND:"404",NETWORK:"NETWORK",TIMEOUT:"TIMEOUT"};class E extends Error{constructor(t,n){super(n||"请求失败"),this.name="BizError",this.code=t}}class y extends E{constructor(t){super(a.AUTH_EXPIRED,t||"登录已失效"),this.name="AuthError"}}function xe(e){return e instanceof y||e&&e.code===a.AUTH_EXPIRED}const J="dplugin.access",Q="dplugin.sid",he="dplugin.ds",ee="dplugin.exp",te="dplugin.cookie";function ke(e){try{return localStorage.getItem(e)}catch(t){return null}}function pe(e,t){try{t==null||t===""?localStorage.removeItem(e):localStorage.setItem(e,t)}catch(n){}}const F={};function U(e){const t=ke(e);return t===null?F[e]===void 0?null:F[e]:t}function x(e,t){F[e]=t,pe(e,t)}const i={get access(){return U(J)},set access(e){x(J,e)},get sid(){return U(Q)},set sid(e){x(Q,e)},get ds(){return U(he)},set ds(e){x(he,e)},get exp(){const e=U(ee);return e?Number(e):0},set exp(e){x(ee,e?String(e):null)},get cookie(){return U(te)==="1"},set cookie(e){x(te,e?"1":null)},clear(){[J,Q,ee,te].forEach(e=>{F[e]=null,pe(e,null)})}};function w(e,t){const n=T().apiBase||"";let o=e;return/^https?:\/\//i.test(e)||(o=(n?n.replace(/\/+$/,""):"")+(e.startsWith("/")?e:"/"+e)),t&&(o+=(o.includes("?")?"&":"?")+t),o}function H(e){const t=w(e);if(/^https?:\/\//i.test(t))try{return new URL(t).pathname}catch(n){return t.split("?")[0]}try{return new URL(t,"http://localhost").pathname}catch(n){return t.split("?")[0]}}let P=null;function ve(e){try{const t=typeof location!="undefined"?location.href:void 0;return new URL(e||"",t).origin}catch(t){return null}}function Le(){P=null}function ne(){if(P)return P;const e=T();if(e.mode==="sign"||e.mode==="cookie")return P=e.mode,P;const t=typeof location!="undefined"?location.origin:null;if(!t)return P="sign",P;const n=ve(e.apiBase||t);return P=n&&n===t?"cookie":"sign",P}function b(){return ne()==="cookie"}async function h(e,t,n){const o=T(),r=n||o.timeout,s=typeof AbortController!="undefined"?new AbortController:null,u=s?setTimeout(()=>s.abort(),r):null,c={method:"POST",credentials:b()?"same-origin":"omit",signal:s?s.signal:void 0};if(typeof FormData!="undefined"&&t instanceof FormData?c.body=t:t!=null&&(c.headers={"Content-Type":"text/plain"},c.body=JSON.stringify(t)),!O.fetch)throw new E(a.NETWORK,"当前环境不支持 fetch");let f;try{f=await O.fetch(e,c)}catch(S){throw u&&clearTimeout(u),S&&S.name==="AbortError"?new E(a.TIMEOUT,"请求超时,请重试"):new E(a.NETWORK,"网络异常,请检查网络后重试")}u&&clearTimeout(u);const l=await f.text();let g=null;if(l)try{g=JSON.parse(l)}catch(S){g=null}if(!f.ok)throw new E(String(f.status),g&&g.message||l||"请求失败");if(!g||g.code===null||g.code===void 0)throw new E("500","响应格式异常");return g.code=String(g.code),g}const Fe="dplugin-auth",He=1,B="keys",oe="sso",re="domain",Be={name:"ECDSA",namedCurve:"P-256"},Ge={name:"ECDSA",hash:"SHA-256"},G=new Map;let Y=null;function j(){if(!O.subtle){const e=typeof location!="undefined"&&location.origin?location.origin:"(未知 origin)";throw new Error("[dplugin/core] crypto.subtle 不存在:浏览器只在**安全上下文**里提供它(https、http://localhost、http://127.0.0.1、http://[::1]、file://);当前 origin="+e)}return O.subtle}function Ye(){return Y||(Y=new Promise(e=>{if(!O.idbOpen){e(null);return}let t;try{t=O.idbOpen(Fe,He)}catch(n){e(null);return}t.onupgradeneeded=()=>{const n=t.result;n.objectStoreNames.contains(B)||n.createObjectStore(B)},t.onsuccess=()=>e(t.result),t.onerror=()=>e(null),t.onblocked=()=>e(null)}),Y)}function Ee(e,t){return Ye().then(n=>n?new Promise(o=>{let r;try{r=n.transaction(B,e)}catch(c){o(null);return}const s=r.objectStore(B);let u=null;try{const c=t(s);c&&(c.onsuccess=()=>{u=c.result},c.onerror=()=>{u=null})}catch(c){u=null}r.oncomplete=()=>o(u),r.onerror=()=>o(null),r.onabort=()=>o(null)}):null)}function me(e){return G.has(e)?Promise.resolve(G.get(e)):Ee("readonly",t=>t.get(e)).then(t=>(t&&G.set(e,t),t||null))}function Oe(e,t){return G.set(e,t),Ee("readwrite",n=>n.put(t,e)).then(()=>t)}async function ie(e){const n=await j().generateKey(Be,!1,["sign","verify"]);return Oe(e,n)}async function ye(e){return me(e)}async function be(e,t){return t?Oe(e,t):null}async function je(e){return await me(e)||ie(e)}function Ve(){return ie(oe)}function Xe(){return ie(re)}function se(){return ye(oe)}function Se(){return ye(re)}async function ce(e){const n=await j().exportKey("spki",e.publicKey);let o="";const r=new Uint8Array(n);for(let s=0;s<r.length;s++)o+=String.fromCharCode(r[s]);return btoa(o)}function Ie(e){const t=new Uint8Array(e);let n="";for(let o=0;o<t.length;o++)n+=String.fromCharCode(t[o]);return btoa(n).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}async function We(e,t){const o=await j().sign(Ge,e.privateKey,new TextEncoder().encode(t));return Ie(o)}async function Te(e){const t=j(),n=await t.exportKey("spki",e.publicKey),o=await t.digest("SHA-256",n);return Ie(o)}const ae={SSO:oe,DOMAIN:re},ue="-",qe="POST",A={BIZ:"BIZ",VERIFY:"VERIFY",VERIFY_SID:"VERIFY_SID",TOKEN:"TOKEN"};function m(e){return e==null||e===""?ue:String(e)}function ze(e,t){const n=m(t.origin);return[m(e),m(t.method),m(t.path),n===ue?ue:n.toLowerCase(),m(t.ds),m(t.ts),m(t.subject),m(t.dna),m(t.redirect)].join(`
2
- `)}function Ze(e,t,n){return[m(e),m(t.subject),m(t.ds),m(t.ts),n].join(".")}async function K(e,t,n){const o=n==="sso"?await se():await Se();if(!o)throw Pe(n==="sso"?"缺少登录签名密钥":"缺少业务签名密钥");const r=await We(o,ze(e,t));return Ze(e,t,r)}async function fe(e,t,n){if(!t||!n)throw Pe("缺少 access 或 ds,无法签名");const o=await K(A.BIZ,{method:qe,path:H(e),origin:C(),ds:n,ts:Date.now(),subject:t},"domain");return"dsign="+encodeURIComponent(o)}function C(){if(typeof location=="undefined")return"";if(location.origin)return location.origin.toLowerCase();try{return new URL(location.href).origin.toLowerCase()}catch(e){return""}}function Pe(e){const t=new Error(e);return t.code="KEY_MISSING",t}function $e(){const e=typeof navigator!="undefined"&&navigator.userAgent||"",t={device_brand:Je(e),device_model:Qe(e),browser_name:et(e),browser_version:tt(e),os_name:nt(e),os_version:ot(e)},n=i.ds;return n&&(t.device_id=n),t}function R(e,t){const n=e.match(t);return n?n[1]:""}function Je(e){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":""}function Qe(e){if(/iPhone/.test(e))return"iPhone";if(/iPad/.test(e))return"iPad";const t=e.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);return t?t[1].trim():/Macintosh/.test(e)?"Mac":""}function et(e){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":""}function tt(e){return/MicroMessenger\/([\d.]+)/.test(e)?R(e,/MicroMessenger\/([\d.]+)/):/Edg[A-Za-z]*\/([\d.]+)/.test(e)?R(e,/Edg[A-Za-z]*\/([\d.]+)/):/OPR\/([\d.]+)/.test(e)?R(e,/OPR\/([\d.]+)/):/Firefox\/([\d.]+)/.test(e)?R(e,/Firefox\/([\d.]+)/):/CriOS\/([\d.]+)/.test(e)?R(e,/CriOS\/([\d.]+)/):/Chrome\/([\d.]+)/.test(e)?R(e,/Chrome\/([\d.]+)/):/Version\/([\d.]+).*Safari/.test(e)?R(e,/Version\/([\d.]+)/):""}function nt(e){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":""}function ot(e){if(/Windows NT 10\.0/.test(e))return"10/11";if(/Windows NT 6\.3/.test(e))return"8.1";if(/Windows NT 6\.1/.test(e))return"7";const t=e.match(/(?:iPhone )?OS (\d+[_.]\d+(?:[_.]\d+)?)/);if(t)return t[1].replace(/_/g,".");if(/Android[ _]([\d.]+)/.test(e))return R(e,/Android[ _]([\d.]+)/);const n=e.match(/Mac OS X (\d+[_.]\d+(?:[_.]\d+)?)/);return n?n[1].replace(/_/g,"."):""}const p={SIGN:{VERIFY:"/api/unsafe/dplugin/login/s/verify",TOKEN:"/api/unsafe/dplugin/login/s/token",REFRESH:"/api/unsafe/dplugin/login/s/refresh"},COOKIE:{VERIFY:"/api/unsafe/dplugin/login/c/verify",TOKEN:"/api/unsafe/dplugin/login/c/token",REFRESH:"/api/unsafe/dplugin/login/c/refresh"},LOGOUT:"/api/safe/dplugin/login/logout"},de=1800*1e3;let k=null,V=null,_=null,X=!1;function W(){return typeof location!="undefined"?location.href:""}function v(e){return new Promise(t=>setTimeout(t,e))}function q(){const e=T();typeof e.onLogin=="function"&&e.onLogin(L())}function N(e,t){return new E(e.code,e.message||t)}function L(){return{mode:ne(),loggedIn:z(),hasAccess:!!i.access,sid:i.sid,ds:i.ds}}function z(){return b()?!!i.cookie:!!(i.access||i.sid)}function le(){i.clear()}function D(e){const t=z();le();const n=T();if(t&&typeof n.onLogout=="function"&&n.onLogout(e||"expired"),X||!n.enableAuth||!n.loginUrl||typeof location=="undefined")return;X=!0;const o=n.loginUrl.includes("?")?"&":"?";location.href=n.loginUrl+o+"referer="+encodeURIComponent(location.href)}function Re(e,t){return _||(_=it(e,t).then(n=>(_=null,n),n=>{throw _=null,rt(n),n}),_)}function rt(e){const t=T();typeof t.onError=="function"&&t.onError({code:e&&e.code,message:e&&e.message,error:e})}async function it(e,t){const n=T(),o=$e();if(b())return at(e,t,o,n);const r=await se(),s=await Se();try{return await st(e,t,o,n)}catch(u){throw await be(ae.SSO,r),await be(ae.DOMAIN,s),u}}async function st(e,t,n,o){const r=await Ve(),s=await ce(r),u=await h(w(e),Object.assign({},t,{device:n,pk:s}));if(u.code!==a.OK)throw N(u,"登录失败");const c=u.data&&u.data.token;if(!c)throw N(u,"登录失败");const f=await Xe(),l=await Te(f),g=await ct(c,o,l),S=await h(w(p.SIGN.TOKEN),{dsign:await K(A.TOKEN,{origin:C(),ts:Date.now(),subject:g.code,dna:l},"domain"),pk:await ce(f)});if(S.code!==a.OK||!S.data)throw N(S,"登录失败");return i.sid=g.sid,i.ds=S.data.ds||g.ds,i.access=S.data.token,i.exp=Date.now()+de,X=!1,q(),L()}async function ct(e,t,n){const o=W(),r=Math.max(1,Number(t.pollMax)||5);for(let s=1;s<=r;s++){const u=i.ds||null,c=Date.now(),f=await K(A.VERIFY,{method:"POST",path:H(p.SIGN.VERIFY),origin:C(),ds:u,ts:c,subject:e,dna:n,redirect:o},"sso");let l;try{l=await h(w(p.SIGN.VERIFY),{dsign:f,dna:n,redirect:o})}catch(g){if((g.code===a.NETWORK||g.code===a.TIMEOUT)&&s<r){await v(t.pollInterval);continue}throw g}if(l.code===a.OK&&l.data)return l.data;if(l.code===a.PENDING){if(s<r){await v(t.pollInterval);continue}break}throw N(l,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function at(e,t,n,o){const r=await h(w(e),Object.assign({},t,{device:n}));if(r.code!==a.OK||!(r.data&&r.data.token))throw N(r,"登录失败");return await ut(o),i.cookie=!0,X=!1,q(),L()}async function ut(e){const t=W(),n=Math.max(1,Number(e.pollMax)||5);for(let o=1;o<=n;o++){let r;try{r=await h(w(p.COOKIE.VERIFY),{redirect:t})}catch(s){if((s.code===a.NETWORK||s.code===a.TIMEOUT)&&o<n){await v(e.pollInterval);continue}throw s}if(r.code===a.OK)return r.data;if(r.code===a.PENDING){if(o<n){await v(e.pollInterval);continue}break}throw N(r,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function M(e){const t=!!(e&&e.force);return V||(V=(async()=>{try{return b()?await ft(t):await lt(t)}catch(n){if(n&&(n.code===a.NETWORK||n.code===a.TIMEOUT))throw n;return!1}finally{V=null}})()),V}async function ft(e){return!e&&i.cookie?!0:(await h(w(p.COOKIE.VERIFY),{redirect:W()})).code!==a.OK?(i.cookie=!1,!1):(i.cookie=!0,q(),!0)}async function dt(){if(!i.sid||!i.ds||!await se())return!1;const t=i.ds,n=await je(ae.DOMAIN),o=await Te(n),r=W(),s=await K(A.VERIFY_SID,{method:"POST",path:H(p.SIGN.VERIFY),origin:C(),ds:t,ts:Date.now(),subject:i.sid,dna:o,redirect:r},"sso"),u=await h(w(p.SIGN.VERIFY),{dsign:s,dna:o,redirect:r});if(u.code!==a.OK||!u.data)return!1;const c=await h(w(p.SIGN.TOKEN),{dsign:await K(A.TOKEN,{origin:C(),ts:Date.now(),subject:u.data.code,dna:o},"domain"),pk:await ce(n)});return c.code!==a.OK||!c.data?!1:(i.access=c.data.token,c.data.ds&&(i.ds=c.data.ds),i.exp=Date.now()+de,q(),!0)}async function lt(e){return!e&&i.access?!0:dt()}function ge(){return k||(k=(b()?gt():wt()).then(e=>(k=null,e),e=>{throw k=null,e})),k}async function gt(){const e=await h(w(p.COOKIE.TOKEN));if(e.code===a.OK)return!0;const t=await h(w(p.COOKIE.REFRESH));if(t.code===a.OK)return!0;if(t.code===a.AUTH_EXPIRED||e.code===a.AUTH_EXPIRED)throw new y(t.message||e.message);if(t.code==="500"&&/重复/.test(t.message||""))return await v(800),!0;throw new E(t.code,t.message)}async function wt(){const e=i.access,t=i.ds;if(!e||!t)throw new y;const n=await K(A.BIZ,{method:"POST",path:H(p.SIGN.REFRESH),origin:C(),ds:t,ts:Date.now(),subject:e},"domain"),o=await h(w(p.SIGN.REFRESH,"dsign="+encodeURIComponent(n)));if(o.code===a.OK&&o.data)return i.access=o.data.token,o.data.ds&&(i.ds=o.data.ds),i.exp=Date.now()+de,i.access;const r=await ht(e,1200);if(r)return r;throw o.code===a.AUTH_EXPIRED?new y(o.message):new E(o.code,o.message)}function ht(e,t){const n=Date.now()+t;return new Promise(o=>{const r=()=>{const s=i.access;if(s&&s!==e){o(s);return}if(Date.now()>=n){o(null);return}setTimeout(r,150)};r()})}async function De(){try{let t=await Ne();t.code===a.AUTH_EXPIRED&&(await ge(),t=await Ne())}catch(t){}le();const e=T();typeof e.onLogout=="function"&&e.onLogout("logout")}async function Ne(){if(b())return h(w(p.LOGOUT));const e=await fe(p.LOGOUT,i.access,i.ds);return h(w(p.LOGOUT,e))}async function pt(e,t,n){const o=n||{};return o.auth!==!1&&await Ae(),Z(e,t||{},o,!1)}async function Ae(){if(b()){if(i.cookie||await M())return;throw D("expired"),new y}if(!i.access&&!await M())throw D("expired"),new y}async function Z(e,t,n,o){const r=n.auth!==!1,s=r&&!b();let u="",c=null;if(s){c=i.access;try{u=await fe(e,c,i.ds)}catch(l){if(!o&&await M({force:!0}))return Z(e,t,n,!0);throw D("expired"),new y}}const f=await h(w(e,u),t,n.timeout);if(f.code===a.OK)return f.data;if(r&&f.code===a.AUTH_EXPIRED&&!o){if(s&&c&&i.access!==c)return Z(e,t,n,!0);try{await ge()}catch(l){throw l instanceof y&&D("expired"),l}return Z(e,t,n,!0)}throw r&&f.code===a.AUTH_EXPIRED?(D("expired"),new y(f.message)):Ke(f,n)}function Ke(e,t){const n=new E(e.code,e.message),o=T();return o.onError&&!(t&&t.silent)&&o.onError({code:e.code,message:e.message,error:n}),n}async function Et(e,t,n,o){return(b()?!i.cookie:!i.access)&&await Ae(),we(e,t,n,o||{},!1)}async function we(e,t,n,o,r){let s="";if(!b())try{s=await fe(e,i.access,i.ds)}catch(f){if(!r&&await M({force:!0}))return we(e,t,n,o,!0);throw D("expired"),new y}const u=new FormData;Object.keys(n||{}).forEach(f=>{const l=n[f];l!=null&&u.append(f,l)}),u.append("file",t);const c=await h(w(e,s),u,o.timeout);if(c.code===a.OK)return c.data;if(c.code===a.AUTH_EXPIRED&&!r){try{await ge()}catch(f){throw f instanceof y&&D("expired"),f}return we(e,t,n,o,!0)}throw Ke(c,o)}function mt(e){const t=Ce(e);if(Le(),t.nativeGuard!=="off"){const n=Ue();if(n.length){const o="[dplugin/core] 这些原生能力在模块加载前已被替换:"+n.join(", ")+"(签名/请求可能被中间人截获,检查是否有脚本抢跑)";if(t.nativeGuard==="error")throw new Error(o);typeof console!="undefined"&&console.warn(o)}}}const Ot={login:Re,logout:De,isLogin:z,getState:L,restore:M};d.AuthError=y,d.BizError=E,d.CODE=a,d.auth=Ot,d.clear=le,d.expire=D,d.getMode=ne,d.getState=L,d.isAuthError=xe,d.isLogin=z,d.login=Re,d.logout=De,d.request=pt,d.setup=mt,d.store=i,d.upload=Et,d.verifyLogin=M,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
1
+ (function(d,I){typeof exports=="object"&&typeof module!="undefined"?I(exports):typeof define=="function"&&define.amd?define(["exports"],I):(d=typeof globalThis!="undefined"?globalThis:d||self,I(d.DPluginCore={}))})(this,(function(d){"use strict";const I={apiBase:"",mode:"auto",loginUrl:"/login/",timeout:15e3,pollInterval:2e3,pollMax:5,enableAuth:!1,onError:null,onLogin:null,onLogout:null,nativeGuard:"warn"};function Ce(e){return e&&Object.keys(I).forEach(t=>{e[t]!==void 0&&(I[t]=e[t])}),I}function T(){return I}const O=Me();function $(e,t){if(typeof e!="function")return!1;let n;try{n=Function.prototype.toString.call(e)}catch(r){return!1}const o=/^function\s+([\w$]+)\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.exec(n);return o?o[1]===t:/\[native code\]/.test(n)&&!/^function\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.test(n)}function _e(e){try{const t=e();return t&&typeof t.then=="function"&&Promise.resolve(t).catch(()=>{}),!1}catch(t){const n=String(t&&t.message||"");return!!t&&t.name==="TypeError"&&/argument|required|参数/i.test(n)}}function Me(){const e=globalThis,t=typeof e.window!="undefined"&&e.window&&typeof e.window.document!="undefined",n={fetch:null,idbOpen:null,subtle:null,pristine:{fetch:!1,idbOpen:!1,subtle:!1}};typeof e.fetch=="function"&&(n.pristine.fetch=$(e.fetch,"fetch"),n.fetch=e.fetch.bind(e)),typeof e.indexedDB!="undefined"&&e.indexedDB&&typeof e.indexedDB.open=="function"&&(n.pristine.idbOpen=$(e.indexedDB.open,"open")&&(!t||_e(()=>e.indexedDB.open())),n.idbOpen=e.indexedDB.open.bind(e.indexedDB));const o=e.crypto&&e.crypto.subtle?e.crypto.subtle:null;if(o){const r=["generateKey","exportKey","importKey","sign","verify","digest"],s={};let u=!0;r.forEach(c=>{typeof o[c]=="function"&&(s[c]=o[c].bind(o),u=u&&$(o[c],c))}),n.subtle=s,n.pristine.subtle=u}return n}function Ue(){if(typeof window=="undefined"||typeof window.document=="undefined")return[];const e=[];return O.fetch&&!O.pristine.fetch&&e.push("fetch"),O.idbOpen&&!O.pristine.idbOpen&&e.push("indexedDB.open"),O.subtle&&!O.pristine.subtle&&e.push("crypto.subtle"),e}const a={OK:"200",PENDING:"202",AUTH_EXPIRED:"500900",FORBIDDEN:"401001",CORS_DENIED:"403",API_NOT_FOUND:"404",NETWORK:"NETWORK",TIMEOUT:"TIMEOUT"};class E extends Error{constructor(t,n){super(n||"请求失败"),this.name="BizError",this.code=t}}class y extends E{constructor(t){super(a.AUTH_EXPIRED,t||"登录已失效"),this.name="AuthError"}}function xe(e){return e instanceof y||e&&e.code===a.AUTH_EXPIRED}const J="dplugin.access",Q="dplugin.sid",he="dplugin.ds",ee="dplugin.exp",te="dplugin.cookie";function ke(e){try{return localStorage.getItem(e)}catch(t){return null}}function pe(e,t){try{t==null||t===""?localStorage.removeItem(e):localStorage.setItem(e,t)}catch(n){}}const F={};function U(e){const t=ke(e);return t===null?F[e]===void 0?null:F[e]:t}function x(e,t){F[e]=t,pe(e,t)}const i={get access(){return U(J)},set access(e){x(J,e)},get sid(){return U(Q)},set sid(e){x(Q,e)},get ds(){return U(he)},set ds(e){x(he,e)},get exp(){const e=U(ee);return e?Number(e):0},set exp(e){x(ee,e?String(e):null)},get cookie(){return U(te)==="1"},set cookie(e){x(te,e?"1":null)},clear(){[J,Q,ee,te].forEach(e=>{F[e]=null,pe(e,null)})}};function w(e,t){const n=T().apiBase||"";let o=e;return/^https?:\/\//i.test(e)||(o=(n?n.replace(/\/+$/,""):"")+(e.startsWith("/")?e:"/"+e)),t&&(o+=(o.includes("?")?"&":"?")+t),o}function H(e){const t=w(e);if(/^https?:\/\//i.test(t))try{return new URL(t).pathname}catch(n){return t.split("?")[0]}try{return new URL(t,"http://localhost").pathname}catch(n){return t.split("?")[0]}}let P=null;function ve(e){try{const t=typeof location!="undefined"?location.href:void 0;return new URL(e||"",t).origin}catch(t){return null}}function Le(){P=null}function ne(){if(P)return P;const e=T();if(e.mode==="sign"||e.mode==="cookie")return P=e.mode,P;const t=typeof location!="undefined"?location.origin:null;if(!t)return P="sign",P;const n=ve(e.apiBase||t);return P=n&&n===t?"cookie":"sign",P}function b(){return ne()==="cookie"}async function h(e,t,n){const o=T(),r=n||o.timeout,s=typeof AbortController!="undefined"?new AbortController:null,u=s?setTimeout(()=>s.abort(),r):null,c={method:"POST",credentials:b()?"same-origin":"omit",signal:s?s.signal:void 0};if(typeof FormData!="undefined"&&t instanceof FormData?c.body=t:t!=null&&(c.headers={"Content-Type":"text/plain"},c.body=JSON.stringify(t)),!O.fetch)throw new E(a.NETWORK,"当前环境不支持 fetch");let f;try{f=await O.fetch(e,c)}catch(S){throw u&&clearTimeout(u),S&&S.name==="AbortError"?new E(a.TIMEOUT,"请求超时,请重试"):new E(a.NETWORK,"网络异常,请检查网络后重试")}u&&clearTimeout(u);const l=await f.text();let g=null;if(l)try{g=JSON.parse(l)}catch(S){g=null}if(!f.ok)throw new E(String(f.status),g&&g.message||l||"请求失败");if(!g||g.code===null||g.code===void 0)throw new E("500","响应格式异常");return g.code=String(g.code),g}const Fe="dplugin-auth",He=1,B="keys",oe="sso",re="domain",Be={name:"ECDSA",namedCurve:"P-256"},Ge={name:"ECDSA",hash:"SHA-256"},G=new Map;let Y=null;function j(){if(!O.subtle){const e=typeof location!="undefined"&&location.origin?location.origin:"(未知 origin)";throw new Error("[dplugin/core] crypto.subtle 不存在:浏览器只在**安全上下文**里提供它(https、http://localhost、http://127.0.0.1、http://[::1]、file://);当前 origin="+e)}return O.subtle}function Ye(){return Y||(Y=new Promise(e=>{if(!O.idbOpen){e(null);return}let t;try{t=O.idbOpen(Fe,He)}catch(n){e(null);return}t.onupgradeneeded=()=>{const n=t.result;n.objectStoreNames.contains(B)||n.createObjectStore(B)},t.onsuccess=()=>e(t.result),t.onerror=()=>e(null),t.onblocked=()=>e(null)}),Y)}function Ee(e,t){return Ye().then(n=>n?new Promise(o=>{let r;try{r=n.transaction(B,e)}catch(c){o(null);return}const s=r.objectStore(B);let u=null;try{const c=t(s);c&&(c.onsuccess=()=>{u=c.result},c.onerror=()=>{u=null})}catch(c){u=null}r.oncomplete=()=>o(u),r.onerror=()=>o(null),r.onabort=()=>o(null)}):null)}function me(e){return G.has(e)?Promise.resolve(G.get(e)):Ee("readonly",t=>t.get(e)).then(t=>(t&&G.set(e,t),t||null))}function Oe(e,t){return G.set(e,t),Ee("readwrite",n=>n.put(t,e)).then(()=>t)}async function ie(e){const n=await j().generateKey(Be,!1,["sign","verify"]);return Oe(e,n)}async function ye(e){return me(e)}async function be(e,t){return t?Oe(e,t):null}async function je(e){return await me(e)||ie(e)}function Ve(){return ie(oe)}function Xe(){return ie(re)}function se(){return ye(oe)}function Se(){return ye(re)}async function ce(e){const n=await j().exportKey("spki",e.publicKey);let o="";const r=new Uint8Array(n);for(let s=0;s<r.length;s++)o+=String.fromCharCode(r[s]);return btoa(o)}function Ie(e){const t=new Uint8Array(e);let n="";for(let o=0;o<t.length;o++)n+=String.fromCharCode(t[o]);return btoa(n).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}async function We(e,t){const o=await j().sign(Ge,e.privateKey,new TextEncoder().encode(t));return Ie(o)}async function Te(e){const t=j(),n=await t.exportKey("spki",e.publicKey),o=await t.digest("SHA-256",n);return Ie(o)}const ae={SSO:oe,DOMAIN:re},ue="-",qe="POST",N={BIZ:"BIZ",VERIFY:"VERIFY",VERIFY_SID:"VERIFY_SID",TOKEN:"TOKEN"};function m(e){return e==null||e===""?ue:String(e)}function ze(e,t){const n=m(t.origin);return[m(e),m(t.method),m(t.path),n===ue?ue:n.toLowerCase(),m(t.ds),m(t.ts),m(t.subject),m(t.dna),m(t.redirect)].join(`
2
+ `)}function Ze(e,t,n){return[m(e),m(t.subject),m(t.ds),m(t.ts),n].join(".")}async function A(e,t,n){const o=n==="sso"?await se():await Se();if(!o)throw Pe(n==="sso"?"缺少登录签名密钥":"缺少业务签名密钥");const r=await We(o,ze(e,t));return Ze(e,t,r)}async function fe(e,t,n){if(!t||!n)throw Pe("缺少 access 或 ds,无法签名");const o=await A(N.BIZ,{method:qe,path:H(e),origin:K(),ds:n,ts:Date.now(),subject:t},"domain");return"dsign="+encodeURIComponent(o)}function K(){if(typeof location=="undefined")return"";if(location.origin)return location.origin.toLowerCase();try{return new URL(location.href).origin.toLowerCase()}catch(e){return""}}function Pe(e){const t=new Error(e);return t.code="KEY_MISSING",t}function $e(){const e=typeof navigator!="undefined"&&navigator.userAgent||"",t={device_brand:Je(e),device_model:Qe(e),browser_name:et(e),browser_version:tt(e),os_name:nt(e),os_version:ot(e)},n=i.ds;return n&&(t.device_id=n),t}function R(e,t){const n=e.match(t);return n?n[1]:""}function Je(e){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":""}function Qe(e){if(/iPhone/.test(e))return"iPhone";if(/iPad/.test(e))return"iPad";const t=e.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);return t?t[1].trim():/Macintosh/.test(e)?"Mac":""}function et(e){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":""}function tt(e){return/MicroMessenger\/([\d.]+)/.test(e)?R(e,/MicroMessenger\/([\d.]+)/):/Edg[A-Za-z]*\/([\d.]+)/.test(e)?R(e,/Edg[A-Za-z]*\/([\d.]+)/):/OPR\/([\d.]+)/.test(e)?R(e,/OPR\/([\d.]+)/):/Firefox\/([\d.]+)/.test(e)?R(e,/Firefox\/([\d.]+)/):/CriOS\/([\d.]+)/.test(e)?R(e,/CriOS\/([\d.]+)/):/Chrome\/([\d.]+)/.test(e)?R(e,/Chrome\/([\d.]+)/):/Version\/([\d.]+).*Safari/.test(e)?R(e,/Version\/([\d.]+)/):""}function nt(e){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":""}function ot(e){if(/Windows NT 10\.0/.test(e))return"10/11";if(/Windows NT 6\.3/.test(e))return"8.1";if(/Windows NT 6\.1/.test(e))return"7";const t=e.match(/(?:iPhone )?OS (\d+[_.]\d+(?:[_.]\d+)?)/);if(t)return t[1].replace(/_/g,".");if(/Android[ _]([\d.]+)/.test(e))return R(e,/Android[ _]([\d.]+)/);const n=e.match(/Mac OS X (\d+[_.]\d+(?:[_.]\d+)?)/);return n?n[1].replace(/_/g,"."):""}const p={SIGN:{VERIFY:"/api/unsafe/dplugin/login/s/verify",TOKEN:"/api/unsafe/dplugin/login/s/token",REFRESH:"/api/unsafe/dplugin/login/s/refresh"},COOKIE:{VERIFY:"/api/unsafe/dplugin/login/c/verify",TOKEN:"/api/unsafe/dplugin/login/c/token",REFRESH:"/api/unsafe/dplugin/login/c/refresh"},LOGOUT:"/api/safe/dplugin/login/logout"},de=1800*1e3;let k=null,V=null,C=null,X=!1;function W(){return typeof location!="undefined"?location.href:""}function v(e){return new Promise(t=>setTimeout(t,e))}function q(){const e=T();typeof e.onLogin=="function"&&e.onLogin(L())}function _(e,t){return new E(e.code,e.message||t)}function L(){return{mode:ne(),loggedIn:z(),hasAccess:!!i.access,sid:i.sid,ds:i.ds}}function z(){return b()?!!i.cookie:!!(i.access||i.sid)}function le(){i.clear()}function D(e){const t=z();le();const n=T();if(t&&typeof n.onLogout=="function"&&n.onLogout(e||"expired"),X||!n.enableAuth||!n.loginUrl||typeof location=="undefined")return;X=!0;const o=n.loginUrl.includes("?")?"&":"?";location.href=n.loginUrl+o+"referer="+encodeURIComponent(location.href)}function Re(e,t){return C||(C=it(e,t).then(n=>(C=null,n),n=>{throw C=null,rt(n),n}),C)}function rt(e){const t=T();typeof t.onError=="function"&&t.onError({code:e&&e.code,message:e&&e.message,error:e})}async function it(e,t){const n=T(),o=$e();if(b())return at(e,t,o,n);const r=await se(),s=await Se();try{return await st(e,t,o,n)}catch(u){throw await be(ae.SSO,r),await be(ae.DOMAIN,s),u}}async function st(e,t,n,o){const r=await Ve(),s=await ce(r),u=await h(w(e),Object.assign({},t,{device:n,pk:s}));if(u.code!==a.OK)throw _(u,"登录失败");const c=u.data&&u.data.token;if(!c)throw _(u,"登录失败");const f=await Xe(),l=await Te(f),g=await ct(c,o,l),S=await h(w(p.SIGN.TOKEN),{dsign:await A(N.TOKEN,{origin:K(),ts:Date.now(),subject:g.code,dna:l},"domain"),pk:await ce(f)});if(S.code!==a.OK||!S.data)throw _(S,"登录失败");return i.sid=g.sid,i.ds=S.data.ds||g.ds,i.access=S.data.token,i.exp=Date.now()+de,X=!1,q(),L()}async function ct(e,t,n){const o=W(),r=Math.max(1,Number(t.pollMax)||5);for(let s=1;s<=r;s++){const u=i.ds||null,c=Date.now(),f=await A(N.VERIFY,{method:"POST",path:H(p.SIGN.VERIFY),origin:K(),ds:u,ts:c,subject:e,dna:n,redirect:o},"sso");let l;try{l=await h(w(p.SIGN.VERIFY),{dsign:f,dna:n,redirect:o})}catch(g){if((g.code===a.NETWORK||g.code===a.TIMEOUT)&&s<r){await v(t.pollInterval);continue}throw g}if(l.code===a.OK&&l.data)return l.data;if(l.code===a.PENDING){if(s<r){await v(t.pollInterval);continue}break}throw _(l,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function at(e,t,n,o){const r=await h(w(e),Object.assign({},t,{device:n}));if(r.code!==a.OK)throw _(r,"登录失败");return await ut(o),i.cookie=!0,X=!1,q(),L()}async function ut(e){const t=W(),n=Math.max(1,Number(e.pollMax)||5);for(let o=1;o<=n;o++){let r;try{r=await h(w(p.COOKIE.VERIFY),{redirect:t})}catch(s){if((s.code===a.NETWORK||s.code===a.TIMEOUT)&&o<n){await v(e.pollInterval);continue}throw s}if(r.code===a.OK)return r.data;if(r.code===a.PENDING){if(o<n){await v(e.pollInterval);continue}break}throw _(r,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function M(e){const t=!!(e&&e.force);return V||(V=(async()=>{try{return b()?await ft(t):await lt(t)}catch(n){if(n&&(n.code===a.NETWORK||n.code===a.TIMEOUT))throw n;return!1}finally{V=null}})()),V}async function ft(e){return!e&&i.cookie?!0:(await h(w(p.COOKIE.VERIFY),{redirect:W()})).code!==a.OK?(i.cookie=!1,!1):(i.cookie=!0,q(),!0)}async function dt(){if(!i.sid||!i.ds||!await se())return!1;const t=i.ds,n=await je(ae.DOMAIN),o=await Te(n),r=W(),s=await A(N.VERIFY_SID,{method:"POST",path:H(p.SIGN.VERIFY),origin:K(),ds:t,ts:Date.now(),subject:i.sid,dna:o,redirect:r},"sso"),u=await h(w(p.SIGN.VERIFY),{dsign:s,dna:o,redirect:r});if(u.code!==a.OK||!u.data)return!1;const c=await h(w(p.SIGN.TOKEN),{dsign:await A(N.TOKEN,{origin:K(),ts:Date.now(),subject:u.data.code,dna:o},"domain"),pk:await ce(n)});return c.code!==a.OK||!c.data?!1:(i.access=c.data.token,c.data.ds&&(i.ds=c.data.ds),i.exp=Date.now()+de,q(),!0)}async function lt(e){return!e&&i.access?!0:dt()}function ge(){return k||(k=(b()?gt():wt()).then(e=>(k=null,e),e=>{throw k=null,e})),k}async function gt(){const e=await h(w(p.COOKIE.TOKEN));if(e.code===a.OK)return!0;const t=await h(w(p.COOKIE.REFRESH));if(t.code===a.OK)return!0;if(t.code===a.AUTH_EXPIRED||e.code===a.AUTH_EXPIRED)throw new y(t.message||e.message);if(t.code==="500"&&/重复/.test(t.message||""))return await v(800),!0;throw new E(t.code,t.message)}async function wt(){const e=i.access,t=i.ds;if(!e||!t)throw new y;const n=await A(N.BIZ,{method:"POST",path:H(p.SIGN.REFRESH),origin:K(),ds:t,ts:Date.now(),subject:e},"domain"),o=await h(w(p.SIGN.REFRESH,"dsign="+encodeURIComponent(n)));if(o.code===a.OK&&o.data)return i.access=o.data.token,o.data.ds&&(i.ds=o.data.ds),i.exp=Date.now()+de,i.access;const r=await ht(e,1200);if(r)return r;throw o.code===a.AUTH_EXPIRED?new y(o.message):new E(o.code,o.message)}function ht(e,t){const n=Date.now()+t;return new Promise(o=>{const r=()=>{const s=i.access;if(s&&s!==e){o(s);return}if(Date.now()>=n){o(null);return}setTimeout(r,150)};r()})}async function De(){try{let t=await Ne();t.code===a.AUTH_EXPIRED&&(await ge(),t=await Ne())}catch(t){}le();const e=T();typeof e.onLogout=="function"&&e.onLogout("logout")}async function Ne(){if(b())return h(w(p.LOGOUT));const e=await fe(p.LOGOUT,i.access,i.ds);return h(w(p.LOGOUT,e))}async function pt(e,t,n){const o=n||{};return o.auth!==!1&&await Ae(),Z(e,t||{},o,!1)}async function Ae(){if(b()){if(i.cookie||await M())return;throw D("expired"),new y}if(!i.access&&!await M())throw D("expired"),new y}async function Z(e,t,n,o){const r=n.auth!==!1,s=r&&!b();let u="",c=null;if(s){c=i.access;try{u=await fe(e,c,i.ds)}catch(l){if(!o&&await M({force:!0}))return Z(e,t,n,!0);throw D("expired"),new y}}const f=await h(w(e,u),t,n.timeout);if(f.code===a.OK)return f.data;if(r&&f.code===a.AUTH_EXPIRED&&!o){if(s&&c&&i.access!==c)return Z(e,t,n,!0);try{await ge()}catch(l){throw l instanceof y&&D("expired"),l}return Z(e,t,n,!0)}throw r&&f.code===a.AUTH_EXPIRED?(D("expired"),new y(f.message)):Ke(f,n)}function Ke(e,t){const n=new E(e.code,e.message),o=T();return o.onError&&!(t&&t.silent)&&o.onError({code:e.code,message:e.message,error:n}),n}async function Et(e,t,n,o){return(b()?!i.cookie:!i.access)&&await Ae(),we(e,t,n,o||{},!1)}async function we(e,t,n,o,r){let s="";if(!b())try{s=await fe(e,i.access,i.ds)}catch(f){if(!r&&await M({force:!0}))return we(e,t,n,o,!0);throw D("expired"),new y}const u=new FormData;Object.keys(n||{}).forEach(f=>{const l=n[f];l!=null&&u.append(f,l)}),u.append("file",t);const c=await h(w(e,s),u,o.timeout);if(c.code===a.OK)return c.data;if(c.code===a.AUTH_EXPIRED&&!r){try{await ge()}catch(f){throw f instanceof y&&D("expired"),f}return we(e,t,n,o,!0)}throw Ke(c,o)}function mt(e){const t=Ce(e);if(Le(),t.nativeGuard!=="off"){const n=Ue();if(n.length){const o="[dplugin/core] 这些原生能力在模块加载前已被替换:"+n.join(", ")+"(签名/请求可能被中间人截获,检查是否有脚本抢跑)";if(t.nativeGuard==="error")throw new Error(o);typeof console!="undefined"&&console.warn(o)}}}const Ot={login:Re,logout:De,isLogin:z,getState:L,restore:M};d.AuthError=y,d.BizError=E,d.CODE=a,d.auth=Ot,d.clear=le,d.expire=D,d.getMode=ne,d.getState=L,d.isAuthError=xe,d.isLogin=z,d.login=Re,d.logout=De,d.request=pt,d.setup=mt,d.store=i,d.upload=Et,d.verifyLogin=M,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
3
3
  //# sourceMappingURL=index.umd.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dplugin/core",
3
- "version": "2.0.10",
3
+ "version": "2.0.11",
4
4
  "description": "DPlugin 前端登录与请求 SDK(同域 Cookie + 跨域 ECDSA 双模式)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",