@dplugin/core 2.0.9 → 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 M={apiBase:"",mode:"auto",loginUrl:"/login/",timeout:15e3,pollInterval:2e3,pollMax:5,enableAuth:!1,onError:null,onLogin:null,onLogout:null,nativeGuard:"warn"};function Ae(e){return e&&Object.keys(M).forEach(t=>{e[t]!==void 0&&(M[t]=e[t])}),M}function I(){return M}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 Ke(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||Ke(()=>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 ke(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 Me(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=Me(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 b=null;function Ue(e){try{const t=typeof location!="undefined"?location.href:void 0;return new URL(e||"",t).origin}catch(t){return null}}function ve(){b=null}function ie(){if(b)return b;const e=I();if(e.mode==="sign"||e.mode==="cookie")return b=e.mode,b;const t=typeof location!="undefined"?location.origin:null;if(!t)return b="sign",b;const n=Ue(e.apiBase||t);return b=n&&n===t?"cookie":"sign",b}function S(){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:S()?"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 Se(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 Se(se)}function be(){return Se(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 x(e,t,n){const o=n==="sso"?await ue():await be();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 x(N.BIZ,{method:We,path:j(e),origin:A(),ds:n,ts:Date.now(),subject:t},"domain");return"dsign="+encodeURIComponent(o)}function A(){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"},ot="dp_dvt",de=1800*1e3;let k=null,F=null,D=null,V=!1;function W(){return typeof location!="undefined"?location.href:""}function U(e){return new Promise(t=>setTimeout(t,e))}function q(){const e=I();typeof e.onLogin=="function"&&e.onLogin(v())}function R(e,t){return new E(e.code,e.message||t)}function rt(e,t,n){if(typeof document=="undefined")return;let o=e+"="+encodeURIComponent(t)+"; path=/; SameSite=Lax";o+="; max-age="+n,document.cookie=o}function v(){return{mode:ie(),loggedIn:z(),hasAccess:!!i.access,sid:i.sid,ds:i.ds}}function z(){return S()?!!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 D||(D=st(e,t).then(n=>(D=null,n),n=>{throw D=null,it(n),n}),D)}function it(e){const t=I();typeof t.onError=="function"&&t.onError({code:e&&e.code,message:e&&e.message,error:e})}async function st(e,t){const n=I(),o=Ze();if(S())return ut(e,t,o,n);const r=await ue(),s=await be();try{return await ct(e,t,o,n)}catch(u){throw await he(ne.SSO,r),await he(ne.DOMAIN,s),u}}async function ct(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 R(u,"登录失败");const c=u.data&&u.data.token;if(!c)throw R(u,"登录失败");const f=await je(),d=await Te(f),l=await at(c,o,d),y=await h(g(w.SIGN.TOKEN),{dsign:await x(N.TOKEN,{origin:A(),ts:Date.now(),subject:l.code,dna:d},"domain"),pk:await te(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()+de,V=!1,q(),v()}async function at(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 x(N.VERIFY,{method:"POST",path:j(w.SIGN.VERIFY),origin:A(),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 U(t.pollInterval);continue}throw l}if(d.code===a.OK&&d.data)return d.data;if(d.code===a.PENDING){if(s<r){await U(t.pollInterval);continue}break}throw R(d,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function ut(e,t,n,o){const r=await h(g(e),Object.assign({},t,{device:n}));if(r.code!==a.OK)throw R(r,"登录失败");const s=r.data&&r.data.token;if(!s)throw R(r,"登录失败");return rt(ot,s,300),await ft(o),i.cookie=!0,V=!1,q(),v()}async function ft(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 U(e.pollInterval);continue}throw s}if(r.code===a.OK)return r.data;if(r.code===a.PENDING){if(o<n){await U(e.pollInterval);continue}break}throw R(r,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function K(e){const t=!!(e&&e.force);return F||(F=(async()=>{try{return S()?await dt(t):await gt(t)}catch(n){if(n&&(n.code===a.NETWORK||n.code===a.TIMEOUT))throw n;return!1}finally{F=null}})()),F}async function dt(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 lt(){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 x(N.VERIFY_SID,{method:"POST",path:j(w.SIGN.VERIFY),origin:A(),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 x(N.TOKEN,{origin:A(),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 gt(e){return!e&&i.access?!0:lt()}function ge(){return k||(k=(S()?wt():ht()).then(e=>(k=null,e),e=>{throw k=null,e})),k}async function wt(){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 U(800),!0;throw new E(t.code,t.message)}async function ht(){const e=i.access,t=i.ds;if(!e||!t)throw new O;const n=await x(N.BIZ,{method:"POST",path:j(w.SIGN.REFRESH),origin:A(),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 pt(e,1200);if(r)return r;throw o.code===a.AUTH_EXPIRED?new O(o.message):new E(o.code,o.message)}function pt(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(S())return h(g(w.LOGOUT));const e=await fe(w.LOGOUT,i.access,i.ds);return h(g(w.LOGOUT,e))}async function Et(e,t,n){const o=n||{};return o.auth!==!1&&await Ne(),B(e,t||{},o,!1)}async function Ne(){if(S()){if(i.cookie||await K())return;throw P("expired"),new O}if(!i.access&&!await K())throw P("expired"),new O}async function B(e,t,n,o){const r=n.auth!==!1,s=r&&!S();let u="",c=null;if(s){c=i.access;try{u=await fe(e,c,i.ds)}catch(d){if(!o&&await K({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)):xe(f,n)}function xe(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 mt(e,t,n,o){return(S()?!i.cookie:!i.access)&&await Ne(),re(e,t,n,o||{},!1)}async function re(e,t,n,o,r){let s="";if(!S())try{s=await fe(e,i.access,i.ds)}catch(f){if(!r&&await K({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 xe(c,o)}function Ot(e){const t=Ae(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 yt={login:Re,logout:De,isLogin:z,getState:v,restore:K};exports.AuthError=O;exports.BizError=E;exports.CODE=a;exports.auth=yt;exports.clear=le;exports.expire=P;exports.getMode=ie;exports.getState=v;exports.isAuthError=ke;exports.isLogin=z;exports.login=Re;exports.logout=De;exports.request=Et;exports.setup=Ot;exports.store=i;exports.upload=mt;exports.verifyLogin=K;
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
@@ -1,4 +1,4 @@
1
- const k = {
1
+ const U = {
2
2
  // 接口前缀。'' = 同源;也可以是 https://api.company.com
3
3
  apiBase: "",
4
4
  // 运行模式:'auto' 按同源自动判定;'cookie' 强制同域 Cookie;'sign' 强制跨域签名
@@ -23,14 +23,14 @@ const k = {
23
23
  nativeGuard: "warn"
24
24
  };
25
25
  function Nt(t) {
26
- return t && Object.keys(k).forEach((e) => {
27
- t[e] !== void 0 && (k[e] = t[e]);
28
- }), k;
26
+ return t && Object.keys(U).forEach((e) => {
27
+ t[e] !== void 0 && (U[e] = t[e]);
28
+ }), U;
29
29
  }
30
30
  function I() {
31
- return k;
31
+ return U;
32
32
  }
33
- const E = Kt();
33
+ const E = At();
34
34
  function $(t, e) {
35
35
  if (typeof t != "function")
36
36
  return !1;
@@ -43,7 +43,7 @@ function $(t, e) {
43
43
  const o = /^function\s+([\w$]+)\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.exec(n);
44
44
  return o ? o[1] === e : /\[native code\]/.test(n) && !/^function\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.test(n);
45
45
  }
46
- function xt(t) {
46
+ function Kt(t) {
47
47
  try {
48
48
  const e = t();
49
49
  return e && typeof e.then == "function" && Promise.resolve(e).catch(() => {
@@ -53,14 +53,14 @@ function xt(t) {
53
53
  return !!e && e.name === "TypeError" && /argument|required|参数/i.test(n);
54
54
  }
55
55
  }
56
- function Kt() {
56
+ function At() {
57
57
  const t = globalThis, e = typeof t.window != "undefined" && t.window && typeof t.window.document != "undefined", n = {
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 || xt(() => t.indexedDB.open())), n.idbOpen = t.indexedDB.open.bind(t.indexedDB));
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
64
  const o = t.crypto && t.crypto.subtle ? t.crypto.subtle : null;
65
65
  if (o) {
66
66
  const r = ["generateKey", "exportKey", "importKey", "sign", "verify", "digest"], s = {};
@@ -71,7 +71,7 @@ function Kt() {
71
71
  }
72
72
  return n;
73
73
  }
74
- function At() {
74
+ function xt() {
75
75
  if (typeof window == "undefined" || typeof window.document == "undefined")
76
76
  return [];
77
77
  const t = [];
@@ -97,11 +97,11 @@ class O extends m {
97
97
  super(u.AUTH_EXPIRED, e || "登录已失效"), this.name = "AuthError";
98
98
  }
99
99
  }
100
- function pe(t) {
100
+ function ge(t) {
101
101
  return t instanceof O || t && t.code === u.AUTH_EXPIRED;
102
102
  }
103
103
  const q = "dplugin.access", z = "dplugin.sid", lt = "dplugin.ds", J = "dplugin.exp", Q = "dplugin.cookie";
104
- function Ct(t) {
104
+ function _t(t) {
105
105
  try {
106
106
  return localStorage.getItem(t);
107
107
  } catch (e) {
@@ -115,46 +115,46 @@ function ht(t, e) {
115
115
  }
116
116
  }
117
117
  const G = {};
118
- function A(t) {
119
- const e = Ct(t);
118
+ function x(t) {
119
+ const e = _t(t);
120
120
  return e === null ? G[t] === void 0 ? null : G[t] : e;
121
121
  }
122
- function C(t, e) {
122
+ function _(t, e) {
123
123
  G[t] = e, ht(t, e);
124
124
  }
125
125
  const i = {
126
126
  get access() {
127
- return A(q);
127
+ return x(q);
128
128
  },
129
129
  set access(t) {
130
- C(q, t);
130
+ _(q, t);
131
131
  },
132
132
  get sid() {
133
- return A(z);
133
+ return x(z);
134
134
  },
135
135
  set sid(t) {
136
- C(z, t);
136
+ _(z, t);
137
137
  },
138
138
  get ds() {
139
- return A(lt);
139
+ return x(lt);
140
140
  },
141
141
  set ds(t) {
142
- C(lt, t);
142
+ _(lt, t);
143
143
  },
144
144
  /** access 的到期时间(毫秒时间戳)。仅用于界面判断,不要拿它做提前续期 */
145
145
  get exp() {
146
- const t = A(J);
146
+ const t = x(J);
147
147
  return t ? Number(t) : 0;
148
148
  },
149
149
  set exp(t) {
150
- C(J, t ? String(t) : null);
150
+ _(J, t ? String(t) : null);
151
151
  },
152
152
  /** cookie 模式:本地只记「可能已登录」,实际有效性由第一次请求的结果决定 */
153
153
  get cookie() {
154
- return A(Q) === "1";
154
+ return x(Q) === "1";
155
155
  },
156
156
  set cookie(t) {
157
- C(Q, t ? "1" : null);
157
+ _(Q, t ? "1" : null);
158
158
  },
159
159
  /**
160
160
  * 清登录态。
@@ -187,8 +187,8 @@ function V(t) {
187
187
  return e.split("?")[0];
188
188
  }
189
189
  }
190
- let b = null;
191
- function _t(t) {
190
+ let S = null;
191
+ function Ct(t) {
192
192
  try {
193
193
  const e = typeof location != "undefined" ? location.href : void 0;
194
194
  return new URL(t || "", e).origin;
@@ -196,29 +196,29 @@ function _t(t) {
196
196
  return null;
197
197
  }
198
198
  }
199
- function kt() {
200
- b = null;
199
+ function Ut() {
200
+ S = null;
201
201
  }
202
202
  function pt() {
203
- if (b)
204
- return b;
203
+ if (S)
204
+ return S;
205
205
  const t = I();
206
206
  if (t.mode === "sign" || t.mode === "cookie")
207
- return b = t.mode, b;
207
+ return S = t.mode, S;
208
208
  const e = typeof location != "undefined" ? location.origin : null;
209
209
  if (!e)
210
- return b = "sign", b;
211
- const n = _t(t.apiBase || e);
212
- return b = n && n === e ? "cookie" : "sign", b;
210
+ return S = "sign", S;
211
+ const n = Ct(t.apiBase || e);
212
+ return S = n && n === e ? "cookie" : "sign", S;
213
213
  }
214
- function S() {
214
+ function b() {
215
215
  return pt() === "cookie";
216
216
  }
217
217
  async function h(t, e, n) {
218
218
  const o = I(), r = n || o.timeout, s = typeof AbortController != "undefined" ? new AbortController() : null, a = s ? setTimeout(() => s.abort(), r) : null, c = {
219
219
  method: "POST",
220
220
  // cookie 模式必须带上 Cookie(同源不涉及 CORS);sign 模式主动不带,减少暴露面
221
- credentials: S() ? "same-origin" : "omit",
221
+ credentials: b() ? "same-origin" : "omit",
222
222
  signal: s ? s.signal : void 0
223
223
  };
224
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)
@@ -244,7 +244,7 @@ async function h(t, e, n) {
244
244
  throw new m("500", "响应格式异常");
245
245
  return l.code = String(l.code), l;
246
246
  }
247
- const Ut = "dplugin-auth", Mt = 1, Y = "keys", rt = "sso", it = "domain", vt = { name: "ECDSA", namedCurve: "P-256" }, Lt = { name: "ECDSA", hash: "SHA-256" }, F = /* @__PURE__ */ new Map();
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
248
  let v = null;
249
249
  function j() {
250
250
  if (!E.subtle) {
@@ -261,7 +261,7 @@ function Ft() {
261
261
  }
262
262
  let e;
263
263
  try {
264
- e = E.idbOpen(Ut, Mt);
264
+ e = E.idbOpen(Mt, kt);
265
265
  } catch (n) {
266
266
  t(null);
267
267
  return;
@@ -324,7 +324,7 @@ function Yt() {
324
324
  function ct() {
325
325
  return yt(rt);
326
326
  }
327
- function St() {
327
+ function bt() {
328
328
  return yt(it);
329
329
  }
330
330
  async function tt(t) {
@@ -335,7 +335,7 @@ async function tt(t) {
335
335
  o += String.fromCharCode(r[s]);
336
336
  return btoa(o);
337
337
  }
338
- function bt(t) {
338
+ function St(t) {
339
339
  const e = new Uint8Array(t);
340
340
  let n = "";
341
341
  for (let o = 0; o < e.length; o++)
@@ -344,11 +344,11 @@ function bt(t) {
344
344
  }
345
345
  async function Bt(t, e) {
346
346
  const o = await j().sign(Lt, t.privateKey, new TextEncoder().encode(e));
347
- return bt(o);
347
+ return St(o);
348
348
  }
349
349
  async function It(t) {
350
350
  const e = j(), n = await e.exportKey("spki", t.publicKey), o = await e.digest("SHA-256", n);
351
- return bt(o);
351
+ return St(o);
352
352
  }
353
353
  const et = {
354
354
  SSO: rt,
@@ -380,8 +380,8 @@ function jt(t, e) {
380
380
  function Xt(t, e, n) {
381
381
  return [p(t), p(e.subject), p(e.ds), p(e.ts), n].join(".");
382
382
  }
383
- async function x(t, e, n) {
384
- const o = n === "sso" ? await ct() : await St();
383
+ async function K(t, e, n) {
384
+ const o = n === "sso" ? await ct() : await bt();
385
385
  if (!o)
386
386
  throw Tt(n === "sso" ? "缺少登录签名密钥" : "缺少业务签名密钥");
387
387
  const r = await Bt(o, jt(t, e));
@@ -390,17 +390,17 @@ async function x(t, e, n) {
390
390
  async function at(t, e, n) {
391
391
  if (!e || !n)
392
392
  throw Tt("缺少 access 或 ds,无法签名");
393
- const o = await x(N.BIZ, {
393
+ const o = await K(N.BIZ, {
394
394
  method: Vt,
395
395
  path: V(t),
396
- origin: K(),
396
+ origin: A(),
397
397
  ds: n,
398
398
  ts: Date.now(),
399
399
  subject: e
400
400
  }, "domain");
401
401
  return "dsign=" + encodeURIComponent(o);
402
402
  }
403
- function K() {
403
+ function A() {
404
404
  if (typeof location == "undefined")
405
405
  return "";
406
406
  if (location.origin)
@@ -470,27 +470,21 @@ const g = {
470
470
  REFRESH: "/api/unsafe/dplugin/login/c/refresh"
471
471
  },
472
472
  LOGOUT: "/api/safe/dplugin/login/logout"
473
- }, te = "dp_dvt", ut = 1800 * 1e3;
474
- let _ = null, L = null, D = null, B = !1;
473
+ }, ut = 1800 * 1e3;
474
+ let C = null, L = null, R = null, B = !1;
475
475
  function X() {
476
476
  return typeof location != "undefined" ? location.href : "";
477
477
  }
478
- function U(t) {
478
+ function M(t) {
479
479
  return new Promise((e) => setTimeout(e, t));
480
480
  }
481
481
  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
- function ee(t, e, n) {
489
- if (typeof document == "undefined")
490
- return;
491
- let o = t + "=" + encodeURIComponent(e) + "; path=/; SameSite=Lax";
492
- o += "; max-age=" + n, document.cookie = o;
493
- }
494
488
  function Z() {
495
489
  return {
496
490
  mode: pt(),
@@ -501,12 +495,12 @@ function Z() {
501
495
  };
502
496
  }
503
497
  function ft() {
504
- return S() ? !!i.cookie : !!(i.access || i.sid);
498
+ return b() ? !!i.cookie : !!(i.access || i.sid);
505
499
  }
506
500
  function Pt() {
507
501
  i.clear();
508
502
  }
509
- function R(t) {
503
+ function P(t) {
510
504
  const e = ft();
511
505
  Pt();
512
506
  const n = I();
@@ -516,12 +510,12 @@ function R(t) {
516
510
  const o = n.loginUrl.includes("?") ? "&" : "?";
517
511
  location.href = n.loginUrl + o + "referer=" + encodeURIComponent(location.href);
518
512
  }
519
- function ne(t, e) {
520
- return D || (D = re(t, e).then((n) => (D = null, n), (n) => {
521
- throw D = null, oe(n), n;
522
- }), D);
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);
523
517
  }
524
- function oe(t) {
518
+ function ee(t) {
525
519
  const e = I();
526
520
  typeof e.onError == "function" && e.onError({
527
521
  code: t && t.code,
@@ -529,27 +523,27 @@ function oe(t) {
529
523
  error: t
530
524
  });
531
525
  }
532
- async function re(t, e) {
526
+ async function ne(t, e) {
533
527
  const n = I(), o = Wt();
534
- if (S())
535
- return ce(t, e, o, n);
536
- const r = await ct(), s = await St();
528
+ if (b())
529
+ return ie(t, e, o, n);
530
+ const r = await ct(), s = await bt();
537
531
  try {
538
- return await ie(t, e, o, n);
532
+ return await oe(t, e, o, n);
539
533
  } catch (a) {
540
534
  throw await wt(et.SSO, r), await wt(et.DOMAIN, s), a;
541
535
  }
542
536
  }
543
- async function ie(t, e, n, o) {
537
+ async function oe(t, e, n, o) {
544
538
  const r = await Gt(), s = await tt(r), a = await h(w(t), Object.assign({}, e, { device: n, pk: s }));
545
539
  if (a.code !== u.OK)
546
- throw P(a, "登录失败");
540
+ throw D(a, "登录失败");
547
541
  const c = a.data && a.data.token;
548
542
  if (!c)
549
- throw P(a, "登录失败");
550
- const f = await Yt(), d = await It(f), l = await se(c, o, d), y = await h(w(g.SIGN.TOKEN), {
551
- dsign: await x(N.TOKEN, {
552
- origin: K(),
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(),
553
547
  ts: Date.now(),
554
548
  subject: l.code,
555
549
  dna: d
@@ -557,16 +551,16 @@ async function ie(t, e, n, o) {
557
551
  pk: await tt(f)
558
552
  });
559
553
  if (y.code !== u.OK || !y.data)
560
- throw P(y, "登录失败");
554
+ throw D(y, "登录失败");
561
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();
562
556
  }
563
- async function se(t, e, n) {
557
+ async function re(t, e, n) {
564
558
  const o = X(), r = Math.max(1, Number(e.pollMax) || 5);
565
559
  for (let s = 1; s <= r; s++) {
566
- const a = i.ds || null, c = Date.now(), f = await x(N.VERIFY, {
560
+ const a = i.ds || null, c = Date.now(), f = await K(N.VERIFY, {
567
561
  method: "POST",
568
562
  path: V(g.SIGN.VERIFY),
569
- origin: K(),
563
+ origin: A(),
570
564
  ds: a,
571
565
  ts: c,
572
566
  subject: t,
@@ -578,7 +572,7 @@ async function se(t, e, n) {
578
572
  d = await h(w(g.SIGN.VERIFY), { dsign: f, dna: n, redirect: o });
579
573
  } catch (l) {
580
574
  if ((l.code === u.NETWORK || l.code === u.TIMEOUT) && s < r) {
581
- await U(e.pollInterval);
575
+ await M(e.pollInterval);
582
576
  continue;
583
577
  }
584
578
  throw l;
@@ -587,25 +581,22 @@ async function se(t, e, n) {
587
581
  return d.data;
588
582
  if (d.code === u.PENDING) {
589
583
  if (s < r) {
590
- await U(e.pollInterval);
584
+ await M(e.pollInterval);
591
585
  continue;
592
586
  }
593
587
  break;
594
588
  }
595
- throw P(d, "登录失败");
589
+ throw D(d, "登录失败");
596
590
  }
597
591
  throw new m(u.AUTH_EXPIRED, "登录超时,请重试");
598
592
  }
599
- async function ce(t, e, n, o) {
593
+ async function ie(t, e, n, o) {
600
594
  const r = await h(w(t), Object.assign({}, e, { device: n }));
601
595
  if (r.code !== u.OK)
602
- throw P(r, "登录失败");
603
- const s = r.data && r.data.token;
604
- if (!s)
605
- throw P(r, "登录失败");
606
- return ee(te, s, 300), await ae(o), i.cookie = !0, B = !1, W(), Z();
596
+ throw D(r, "登录失败");
597
+ return await se(o), i.cookie = !0, B = !1, W(), Z();
607
598
  }
608
- async function ae(t) {
599
+ async function se(t) {
609
600
  const e = X(), n = Math.max(1, Number(t.pollMax) || 5);
610
601
  for (let o = 1; o <= n; o++) {
611
602
  let r;
@@ -613,7 +604,7 @@ async function ae(t) {
613
604
  r = await h(w(g.COOKIE.VERIFY), { redirect: e });
614
605
  } catch (s) {
615
606
  if ((s.code === u.NETWORK || s.code === u.TIMEOUT) && o < n) {
616
- await U(t.pollInterval);
607
+ await M(t.pollInterval);
617
608
  continue;
618
609
  }
619
610
  throw s;
@@ -622,20 +613,20 @@ async function ae(t) {
622
613
  return r.data;
623
614
  if (r.code === u.PENDING) {
624
615
  if (o < n) {
625
- await U(t.pollInterval);
616
+ await M(t.pollInterval);
626
617
  continue;
627
618
  }
628
619
  break;
629
620
  }
630
- throw P(r, "登录失败");
621
+ throw D(r, "登录失败");
631
622
  }
632
623
  throw new m(u.AUTH_EXPIRED, "登录超时,请重试");
633
624
  }
634
- async function M(t) {
625
+ async function k(t) {
635
626
  const e = !!(t && t.force);
636
627
  return L || (L = (async () => {
637
628
  try {
638
- return S() ? await ue(e) : await de(e);
629
+ return b() ? await ce(e) : await ue(e);
639
630
  } catch (n) {
640
631
  if (n && (n.code === u.NETWORK || n.code === u.TIMEOUT))
641
632
  throw n;
@@ -645,16 +636,16 @@ async function M(t) {
645
636
  }
646
637
  })()), L;
647
638
  }
648
- async function ue(t) {
639
+ async function ce(t) {
649
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);
650
641
  }
651
- async function fe() {
642
+ async function ae() {
652
643
  if (!i.sid || !i.ds || !await ct())
653
644
  return !1;
654
- const e = i.ds, n = await Ht(et.DOMAIN), o = await It(n), r = X(), s = await x(N.VERIFY_SID, {
645
+ const e = i.ds, n = await Ht(et.DOMAIN), o = await It(n), r = X(), s = await K(N.VERIFY_SID, {
655
646
  method: "POST",
656
647
  path: V(g.SIGN.VERIFY),
657
- origin: K(),
648
+ origin: A(),
658
649
  ds: e,
659
650
  ts: Date.now(),
660
651
  subject: i.sid,
@@ -664,8 +655,8 @@ async function fe() {
664
655
  if (a.code !== u.OK || !a.data)
665
656
  return !1;
666
657
  const c = await h(w(g.SIGN.TOKEN), {
667
- dsign: await x(N.TOKEN, {
668
- origin: K(),
658
+ dsign: await K(N.TOKEN, {
659
+ origin: A(),
669
660
  ts: Date.now(),
670
661
  subject: a.data.code,
671
662
  dna: o
@@ -674,15 +665,15 @@ async function fe() {
674
665
  });
675
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);
676
667
  }
677
- async function de(t) {
678
- return !t && i.access ? !0 : fe();
668
+ async function ue(t) {
669
+ return !t && i.access ? !0 : ae();
679
670
  }
680
671
  function dt() {
681
- return _ || (_ = (S() ? le() : we()).then((t) => (_ = null, t), (t) => {
682
- throw _ = null, t;
683
- })), _;
672
+ return C || (C = (b() ? fe() : de()).then((t) => (C = null, t), (t) => {
673
+ throw C = null, t;
674
+ })), C;
684
675
  }
685
- async function le() {
676
+ async function fe() {
686
677
  const t = await h(w(g.COOKIE.TOKEN));
687
678
  if (t.code === u.OK)
688
679
  return !0;
@@ -692,29 +683,29 @@ async function le() {
692
683
  if (e.code === u.AUTH_EXPIRED || t.code === u.AUTH_EXPIRED)
693
684
  throw new O(e.message || t.message);
694
685
  if (e.code === "500" && /重复/.test(e.message || ""))
695
- return await U(800), !0;
686
+ return await M(800), !0;
696
687
  throw new m(e.code, e.message);
697
688
  }
698
- async function we() {
689
+ async function de() {
699
690
  const t = i.access, e = i.ds;
700
691
  if (!t || !e)
701
692
  throw new O();
702
- const n = await x(N.BIZ, {
693
+ const n = await K(N.BIZ, {
703
694
  method: "POST",
704
695
  path: V(g.SIGN.REFRESH),
705
- origin: K(),
696
+ origin: A(),
706
697
  ds: e,
707
698
  ts: Date.now(),
708
699
  subject: t
709
700
  }, "domain"), o = await h(w(g.SIGN.REFRESH, "dsign=" + encodeURIComponent(n)));
710
701
  if (o.code === u.OK && o.data)
711
702
  return i.access = o.data.token, o.data.ds && (i.ds = o.data.ds), i.exp = Date.now() + ut, i.access;
712
- const r = await ge(t, 1200);
703
+ const r = await le(t, 1200);
713
704
  if (r)
714
705
  return r;
715
706
  throw o.code === u.AUTH_EXPIRED ? new O(o.message) : new m(o.code, o.message);
716
707
  }
717
- function ge(t, e) {
708
+ function le(t, e) {
718
709
  const n = Date.now() + e;
719
710
  return new Promise((o) => {
720
711
  const r = () => {
@@ -732,7 +723,7 @@ function ge(t, e) {
732
723
  r();
733
724
  });
734
725
  }
735
- async function he() {
726
+ async function we() {
736
727
  try {
737
728
  let e = await gt();
738
729
  e.code === u.AUTH_EXPIRED && (await dt(), e = await gt());
@@ -743,35 +734,35 @@ async function he() {
743
734
  typeof t.onLogout == "function" && t.onLogout("logout");
744
735
  }
745
736
  async function gt() {
746
- if (S())
737
+ if (b())
747
738
  return h(w(g.LOGOUT));
748
739
  const t = await at(g.LOGOUT, i.access, i.ds);
749
740
  return h(w(g.LOGOUT, t));
750
741
  }
751
- async function Ee(t, e, n) {
742
+ async function he(t, e, n) {
752
743
  const o = n || {};
753
744
  return o.auth !== !1 && await Rt(), H(t, e || {}, o, !1);
754
745
  }
755
746
  async function Rt() {
756
- if (S()) {
757
- if (i.cookie || await M())
747
+ if (b()) {
748
+ if (i.cookie || await k())
758
749
  return;
759
- throw R("expired"), new O();
750
+ throw P("expired"), new O();
760
751
  }
761
- if (!i.access && !await M())
762
- throw R("expired"), new O();
752
+ if (!i.access && !await k())
753
+ throw P("expired"), new O();
763
754
  }
764
755
  async function H(t, e, n, o) {
765
- const r = n.auth !== !1, s = r && !S();
756
+ const r = n.auth !== !1, s = r && !b();
766
757
  let a = "", c = null;
767
758
  if (s) {
768
759
  c = i.access;
769
760
  try {
770
761
  a = await at(t, c, i.ds);
771
762
  } catch (d) {
772
- if (!o && await M({ force: !0 }))
763
+ if (!o && await k({ force: !0 }))
773
764
  return H(t, e, n, !0);
774
- throw R("expired"), new O();
765
+ throw P("expired"), new O();
775
766
  }
776
767
  }
777
768
  const f = await h(w(t, a), e, n.timeout);
@@ -783,28 +774,28 @@ async function H(t, e, n, o) {
783
774
  try {
784
775
  await dt();
785
776
  } catch (d) {
786
- throw d instanceof O && R("expired"), d;
777
+ throw d instanceof O && P("expired"), d;
787
778
  }
788
779
  return H(t, e, n, !0);
789
780
  }
790
- 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);
791
782
  }
792
783
  function Dt(t, e) {
793
784
  const n = new m(t.code, t.message), o = I();
794
785
  return o.onError && !(e && e.silent) && o.onError({ code: t.code, message: t.message, error: n }), n;
795
786
  }
796
- async function me(t, e, n, o) {
797
- return (S() ? !i.cookie : !i.access) && await Rt(), ot(t, e, n, o || {}, !1);
787
+ async function pe(t, e, n, o) {
788
+ return (b() ? !i.cookie : !i.access) && await Rt(), ot(t, e, n, o || {}, !1);
798
789
  }
799
790
  async function ot(t, e, n, o, r) {
800
791
  let s = "";
801
- if (!S())
792
+ if (!b())
802
793
  try {
803
794
  s = await at(t, i.access, i.ds);
804
795
  } catch (f) {
805
- if (!r && await M({ force: !0 }))
796
+ if (!r && await k({ force: !0 }))
806
797
  return ot(t, e, n, o, !0);
807
- throw R("expired"), new O();
798
+ throw P("expired"), new O();
808
799
  }
809
800
  const a = new FormData();
810
801
  Object.keys(n || {}).forEach((f) => {
@@ -818,16 +809,16 @@ async function ot(t, e, n, o, r) {
818
809
  try {
819
810
  await dt();
820
811
  } catch (f) {
821
- throw f instanceof O && R("expired"), f;
812
+ throw f instanceof O && P("expired"), f;
822
813
  }
823
814
  return ot(t, e, n, o, !0);
824
815
  }
825
816
  throw Dt(c, o);
826
817
  }
827
- function Oe(t) {
818
+ function Ee(t) {
828
819
  const e = Nt(t);
829
- if (kt(), e.nativeGuard !== "off") {
830
- const n = At();
820
+ if (Ut(), e.nativeGuard !== "off") {
821
+ const n = xt();
831
822
  if (n.length) {
832
823
  const o = "[dplugin/core] 这些原生能力在模块加载前已被替换:" + n.join(", ") + "(签名/请求可能被中间人截获,检查是否有脚本抢跑)";
833
824
  if (e.nativeGuard === "error")
@@ -836,31 +827,31 @@ function Oe(t) {
836
827
  }
837
828
  }
838
829
  }
839
- const ye = {
840
- login: ne,
841
- logout: he,
830
+ const me = {
831
+ login: te,
832
+ logout: we,
842
833
  isLogin: ft,
843
834
  getState: Z,
844
835
  /** 页面加载时恢复登录态;返回是否已登录 */
845
- restore: M
836
+ restore: k
846
837
  };
847
838
  export {
848
839
  O as AuthError,
849
840
  m as BizError,
850
841
  u as CODE,
851
- ye as auth,
842
+ me as auth,
852
843
  Pt as clear,
853
- R as expire,
844
+ P as expire,
854
845
  pt as getMode,
855
846
  Z as getState,
856
- pe as isAuthError,
847
+ ge as isAuthError,
857
848
  ft as isLogin,
858
- ne as login,
859
- he as logout,
860
- Ee as request,
861
- Oe as setup,
849
+ te as login,
850
+ we as logout,
851
+ he as request,
852
+ Ee as setup,
862
853
  i as store,
863
- me as upload,
864
- M as verifyLogin
854
+ pe as upload,
855
+ k as verifyLogin
865
856
  };
866
857
  //# sourceMappingURL=index.mjs.map
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=xe();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 xe(){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 ke(){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 Me(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 Ue(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 k(e){const t=Ue(e);return t===null?F[e]===void 0?null:F[e]:t}function M(e,t){F[e]=t,pe(e,t)}const i={get access(){return k(J)},set access(e){M(J,e)},get sid(){return k(Q)},set sid(e){M(Q,e)},get ds(){return k(he)},set ds(e){M(he,e)},get exp(){const e=k(ee);return e?Number(e):0},set exp(e){M(ee,e?String(e):null)},get cookie(){return k(te)==="1"},set cookie(e){M(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 S(){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:S()?"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(b){throw u&&clearTimeout(u),b&&b.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(b){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 V(){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 V().generateKey(Be,!1,["sign","verify"]);return Oe(e,n)}async function ye(e){return me(e)}async function Se(e,t){return t?Oe(e,t):null}async function Ve(e){return await me(e)||ie(e)}function je(){return ie(oe)}function Xe(){return ie(re)}function se(){return ye(oe)}function be(){return ye(re)}async function ce(e){const n=await V().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 V().sign(Ge,e.privateKey,new TextEncoder().encode(t));return Ie(o)}async function Te(e){const t=V(),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 be();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"},rt="dp_dvt",de=1800*1e3;let U=null,j=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 it(e,t,n){if(typeof document=="undefined")return;let o=e+"="+encodeURIComponent(t)+"; path=/; SameSite=Lax";o+="; max-age="+n,document.cookie=o}function L(){return{mode:ne(),loggedIn:z(),hasAccess:!!i.access,sid:i.sid,ds:i.ds}}function z(){return S()?!!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 _||(_=ct(e,t).then(n=>(_=null,n),n=>{throw _=null,st(n),n}),_)}function st(e){const t=T();typeof t.onError=="function"&&t.onError({code:e&&e.code,message:e&&e.message,error:e})}async function ct(e,t){const n=T(),o=$e();if(S())return ft(e,t,o,n);const r=await se(),s=await be();try{return await at(e,t,o,n)}catch(u){throw await Se(ae.SSO,r),await Se(ae.DOMAIN,s),u}}async function at(e,t,n,o){const r=await je(),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 ut(c,o,l),b=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(b.code!==a.OK||!b.data)throw N(b,"登录失败");return i.sid=g.sid,i.ds=b.data.ds||g.ds,i.access=b.data.token,i.exp=Date.now()+de,X=!1,q(),L()}async function ut(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 ft(e,t,n,o){const r=await h(w(e),Object.assign({},t,{device:n}));if(r.code!==a.OK)throw N(r,"登录失败");const s=r.data&&r.data.token;if(!s)throw N(r,"登录失败");return it(rt,s,300),await dt(o),i.cookie=!0,X=!1,q(),L()}async function dt(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 x(e){const t=!!(e&&e.force);return j||(j=(async()=>{try{return S()?await lt(t):await wt(t)}catch(n){if(n&&(n.code===a.NETWORK||n.code===a.TIMEOUT))throw n;return!1}finally{j=null}})()),j}async function lt(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 gt(){if(!i.sid||!i.ds||!await se())return!1;const t=i.ds,n=await Ve(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 wt(e){return!e&&i.access?!0:gt()}function ge(){return U||(U=(S()?ht():pt()).then(e=>(U=null,e),e=>{throw U=null,e})),U}async function ht(){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 pt(){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 Et(e,1200);if(r)return r;throw o.code===a.AUTH_EXPIRED?new y(o.message):new E(o.code,o.message)}function Et(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(S())return h(w(p.LOGOUT));const e=await fe(p.LOGOUT,i.access,i.ds);return h(w(p.LOGOUT,e))}async function mt(e,t,n){const o=n||{};return o.auth!==!1&&await Ae(),Z(e,t||{},o,!1)}async function Ae(){if(S()){if(i.cookie||await x())return;throw D("expired"),new y}if(!i.access&&!await x())throw D("expired"),new y}async function Z(e,t,n,o){const r=n.auth!==!1,s=r&&!S();let u="",c=null;if(s){c=i.access;try{u=await fe(e,c,i.ds)}catch(l){if(!o&&await x({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 Ot(e,t,n,o){return(S()?!i.cookie:!i.access)&&await Ae(),we(e,t,n,o||{},!1)}async function we(e,t,n,o,r){let s="";if(!S())try{s=await fe(e,i.access,i.ds)}catch(f){if(!r&&await x({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 yt(e){const t=Ce(e);if(Le(),t.nativeGuard!=="off"){const n=ke();if(n.length){const o="[dplugin/core] 这些原生能力在模块加载前已被替换:"+n.join(", ")+"(签名/请求可能被中间人截获,检查是否有脚本抢跑)";if(t.nativeGuard==="error")throw new Error(o);typeof console!="undefined"&&console.warn(o)}}}const St={login:Re,logout:De,isLogin:z,getState:L,restore:x};d.AuthError=y,d.BizError=E,d.CODE=a,d.auth=St,d.clear=le,d.expire=D,d.getMode=ne,d.getState=L,d.isAuthError=Me,d.isLogin=z,d.login=Re,d.logout=De,d.request=mt,d.setup=yt,d.store=i,d.upload=Ot,d.verifyLogin=x,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.9",
3
+ "version": "2.0.11",
4
4
  "description": "DPlugin 前端登录与请求 SDK(同域 Cookie + 跨域 ECDSA 双模式)",
5
5
  "type": "module",
6
6
  "main": "./dist/index.cjs",