@dplugin/core 2.0.6 → 2.0.9
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 +2 -2
- package/dist/index.mjs +109 -112
- package/dist/index.umd.js +2 -2
- package/package.json +1 -1
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
|
|
2
|
-
`)}function Zt(t,e,n){return[p(t),p(e.subject),p(e.ds),p(e.ts),n].join(".")}async function x(t,e,n){const o=n==="sso"?await ut():await bt();if(!o)throw Pt(n==="sso"?"缺少登录签名密钥":"缺少业务签名密钥");const r=await Wt(o,zt(t,e));return Zt(t,e,r)}async function ft(t,e,n){if(!e||!n)throw Pt("缺少 access 或 ds,无法签名");const o=await x(N.BIZ,{method:qt,path:j(t),origin:K(),ds:n,ts:Date.now(),subject:e},"domain");return"dsign="+encodeURIComponent(o)}function K(){if(typeof location=="undefined")return"";if(location.origin)return location.origin.toLowerCase();try{return new URL(location.href).origin.toLowerCase()}catch(t){return""}}function Pt(t){const e=new Error(t);return e.code="KEY_MISSING",e}function $t(){const t=typeof navigator!="undefined"&&navigator.userAgent||"",e={device_brand:Jt(t),device_model:Qt(t),browser_name:te(t),browser_version:ee(t),os_name:ne(t),os_version:oe(t)},n=i.ds;return n&&(e.device_id=n),e}function T(t,e){const n=t.match(e);return n?n[1]:""}function Jt(t){return/iPhone|iPad|iPod/.test(t)?"Apple":/Huawei|HONOR/i.test(t)?"Huawei":/Xiaomi|Redmi|POCO/i.test(t)?"Xiaomi":/OPPO|realme/i.test(t)?"OPPO":/vivo/i.test(t)?"vivo":/Samsung|SM-|SAMSUNG/i.test(t)?"Samsung":/Macintosh|Mac OS X/.test(t)?"Apple":""}function Qt(t){if(/iPhone/.test(t))return"iPhone";if(/iPad/.test(t))return"iPad";const e=t.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);return e?e[1].trim():/Macintosh/.test(t)?"Mac":""}function te(t){return/MicroMessenger/i.test(t)?"WeChat":/Edg[A-Za-z]*\//.test(t)?"Edge":/OPR\/|Opera/.test(t)?"Opera":/Firefox\//.test(t)?"Firefox":/Chrome\/|CriOS\//.test(t)?"Chrome":/Safari\//.test(t)?"Safari":""}function ee(t){return/MicroMessenger\/([\d.]+)/.test(t)?T(t,/MicroMessenger\/([\d.]+)/):/Edg[A-Za-z]*\/([\d.]+)/.test(t)?T(t,/Edg[A-Za-z]*\/([\d.]+)/):/OPR\/([\d.]+)/.test(t)?T(t,/OPR\/([\d.]+)/):/Firefox\/([\d.]+)/.test(t)?T(t,/Firefox\/([\d.]+)/):/CriOS\/([\d.]+)/.test(t)?T(t,/CriOS\/([\d.]+)/):/Chrome\/([\d.]+)/.test(t)?T(t,/Chrome\/([\d.]+)/):/Version\/([\d.]+).*Safari/.test(t)?T(t,/Version\/([\d.]+)/):""}function ne(t){return/Windows NT/.test(t)?"Windows":/iPhone|iPad|iPod/.test(t)?"iOS":/Android/.test(t)?"Android":/Mac OS X|Macintosh/.test(t)?"macOS":/HarmonyOS/.test(t)?"HarmonyOS":/Linux/.test(t)?"Linux":""}function oe(t){if(/Windows NT 10\.0/.test(t))return"10/11";if(/Windows NT 6\.3/.test(t))return"8.1";if(/Windows NT 6\.1/.test(t))return"7";const e=t.match(/(?:iPhone )?OS (\d+[_.]\d+(?:[_.]\d+)?)/);if(e)return e[1].replace(/_/g,".");if(/Android[ _]([\d.]+)/.test(t))return T(t,/Android[ _]([\d.]+)/);const n=t.match(/Mac OS X (\d+[_.]\d+(?:[_.]\d+)?)/);return n?n[1].replace(/_/g,"."):""}const w={SIGN:{VERIFY:"/api/unsafe/dplugin/login/s/verify",TOKEN:"/api/unsafe/dplugin/login/s/token",REFRESH:"/api/unsafe/dplugin/login/s/refresh"},COOKIE:{VERIFY:"/api/unsafe/dplugin/login/c/verify",TOKEN:"/api/unsafe/dplugin/login/c/token",REFRESH:"/api/unsafe/dplugin/login/c/refresh"},LOGOUT:"/api/safe/dplugin/login/logout"},re="dp_dvt",dt=1800*1e3;let k=null,F=null,D=null,V=!1;function W(){return typeof location!="undefined"?location.href:""}function U(t){return new Promise(e=>setTimeout(e,t))}function q(){const t=I();typeof t.onLogin=="function"&&t.onLogin(v())}function P(t,e){return new E(t.code,t.message||e)}function ie(t,e,n){if(typeof document=="undefined")return;let o=t+"="+encodeURIComponent(e)+"; path=/; SameSite=Lax";o+="; max-age="+n,document.cookie=o}function v(){return{mode:it(),loggedIn:z(),hasAccess:!!i.access,sid:i.sid,ds:i.ds}}function z(){return S()?!!i.cookie:!!(i.access||i.sid)}function lt(){i.clear()}function R(t){const e=z();lt();const n=I();if(e&&typeof n.onLogout=="function"&&n.onLogout(t||"expired"),V||!n.enableAuth||!n.loginUrl||typeof location=="undefined")return;V=!0;const o=n.loginUrl.includes("?")?"&":"?";location.href=n.loginUrl+o+"referer="+encodeURIComponent(location.href)}function Rt(t,e){return D||(D=ce(t,e).then(n=>(D=null,n),n=>{throw D=null,se(n),n}),D)}function se(t){const e=I();typeof e.onError=="function"&&e.onError({code:t&&t.code,message:t&&t.message,error:t})}async function ce(t,e){const n=I(),o=$t();if(S())return fe(t,e,o,n);const r=await ut(),s=await bt();try{return await ae(t,e,o,n)}catch(u){throw await ht(nt.SSO,r),await ht(nt.DOMAIN,s),u}}async function ae(t,e,n,o){const r=await jt(),s=await et(r),u=await h(g(t),Object.assign({},e,{device:n,pk:s}));if(u.code!==a.OK)throw P(u,"登录失败");const c=u.data&&u.data.token;if(!c)throw P(u,"登录失败");const f=await Xt(),d=await Tt(f),l=await ue(c,o,d),y=await h(g(w.SIGN.TOKEN),{dsign:await x(N.TOKEN,{origin:K(),ts:Date.now(),subject:l.code,dna:d},"domain"),pk:await et(f)});if(y.code!==a.OK||!y.data)throw P(y,"登录失败");return i.sid=l.sid,i.ds=y.data.ds||l.ds,i.access=y.data.token,i.exp=Date.now()+dt,V=!1,q(),v()}async function ue(t,e,n){const o=W(),r=Math.max(1,Number(e.pollMax)||5);for(let s=1;s<=r;s++){const u=i.ds||null,c=Date.now(),f=await x(N.VERIFY,{method:"POST",path:j(w.SIGN.VERIFY),origin:K(),ds:u,ts:c,subject:t,dna:n,redirect:o},"sso");let d;try{d=await h(g(w.SIGN.VERIFY),{dsign:f,dna:n,redirect:o})}catch(l){if((l.code===a.NETWORK||l.code===a.TIMEOUT)&&s<r){await U(e.pollInterval);continue}throw l}if(d.code===a.OK&&d.data)return d.data;if(d.code===a.PENDING){if(s<r){await U(e.pollInterval);continue}break}throw P(d,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function fe(t,e,n,o){const r=await h(g(t),Object.assign({},e,{device:n}));if(r.code!==a.OK)throw P(r,"登录失败");const s=r.data&&r.data.token;if(!s)throw P(r,"登录失败");return ie(re,s,300),await de(o),await Dt(),i.cookie=!0,V=!1,q(),v()}async function de(t){const e=W(),n=Math.max(1,Number(t.pollMax)||5);for(let o=1;o<=n;o++){let r;try{r=await h(g(w.COOKIE.VERIFY),{redirect:e})}catch(s){if((s.code===a.NETWORK||s.code===a.TIMEOUT)&&o<n){await U(t.pollInterval);continue}throw s}if(r.code===a.OK)return r.data;if(r.code===a.PENDING){if(o<n){await U(t.pollInterval);continue}break}throw P(r,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function Dt(){const t=await h(g(w.COOKIE.TOKEN));if(t.code!==a.OK)throw P(t,"登录失败");return t}async function A(t){const e=!!(t&&t.force);return F||(F=(async()=>{try{return S()?await le(e):await we(e)}catch(n){if(n&&(n.code===a.NETWORK||n.code===a.TIMEOUT))throw n;return!1}finally{F=null}})()),F}async function le(t){return!t&&i.cookie?!0:(await h(g(w.COOKIE.VERIFY),{redirect:W()})).code!==a.OK||(await Dt()).code!==a.OK?(i.cookie=!1,!1):(i.cookie=!0,q(),!0)}async function ge(){if(!i.sid||!i.ds||!await ut())return!1;const e=i.ds,n=await Vt(nt.DOMAIN),o=await Tt(n),r=W(),s=await x(N.VERIFY_SID,{method:"POST",path:j(w.SIGN.VERIFY),origin:K(),ds:e,ts:Date.now(),subject:i.sid,dna:o,redirect:r},"sso"),u=await h(g(w.SIGN.VERIFY),{dsign:s,dna:o,redirect:r});if(u.code!==a.OK||!u.data)return!1;const c=await h(g(w.SIGN.TOKEN),{dsign:await x(N.TOKEN,{origin:K(),ts:Date.now(),subject:u.data.code,dna:o},"domain"),pk:await et(n)});return c.code!==a.OK||!c.data?!1:(i.access=c.data.token,c.data.ds&&(i.ds=c.data.ds),i.exp=Date.now()+dt,q(),!0)}async function we(t){return!t&&i.access?!0:ge()}function gt(){return k||(k=(S()?he():pe()).then(t=>(k=null,t),t=>{throw k=null,t})),k}async function he(){const t=await h(g(w.COOKIE.REFRESH));if(t.code===a.OK)return!0;if(t.code===a.AUTH_EXPIRED)throw new O(t.message);if(t.code==="500"&&/重复/.test(t.message||""))return await U(800),!0;throw new E(t.code,t.message)}async function pe(){const t=i.access,e=i.ds;if(!t||!e)throw new O;const n=await x(N.BIZ,{method:"POST",path:j(w.SIGN.REFRESH),origin:K(),ds:e,ts:Date.now(),subject:t},"domain"),o=await h(g(w.SIGN.REFRESH,"dsign="+encodeURIComponent(n)));if(o.code===a.OK&&o.data)return i.access=o.data.token,o.data.ds&&(i.ds=o.data.ds),i.exp=Date.now()+dt,i.access;const r=await Ee(t,1200);if(r)return r;throw o.code===a.AUTH_EXPIRED?new O(o.message):new E(o.code,o.message)}function Ee(t,e){const n=Date.now()+e;return new Promise(o=>{const r=()=>{const s=i.access;if(s&&s!==t){o(s);return}if(Date.now()>=n){o(null);return}setTimeout(r,150)};r()})}async function Nt(){try{let e=await pt();e.code===a.AUTH_EXPIRED&&(await gt(),e=await pt())}catch(e){}lt();const t=I();typeof t.onLogout=="function"&&t.onLogout("logout")}async function pt(){if(S())return h(g(w.LOGOUT));const t=await ft(w.LOGOUT,i.access,i.ds);return h(g(w.LOGOUT,t))}async function me(t,e,n){const o=n||{};return o.auth!==!1&&await xt(),G(t,e||{},o,!1)}async function xt(){if(S()){if(i.cookie||await A())return;throw R("expired"),new O}if(!i.access&&!await A())throw R("expired"),new O}async function G(t,e,n,o){const r=n.auth!==!1,s=r&&!S();let u="",c=null;if(s){c=i.access;try{u=await ft(t,c,i.ds)}catch(d){if(!o&&await A({force:!0}))return G(t,e,n,!0);throw R("expired"),new O}}const f=await h(g(t,u),e,n.timeout);if(f.code===a.OK)return f.data;if(r&&f.code===a.AUTH_EXPIRED&&!o){if(s&&c&&i.access!==c)return G(t,e,n,!0);try{await gt()}catch(d){throw d instanceof O&&R("expired"),d}return G(t,e,n,!0)}throw r&&f.code===a.AUTH_EXPIRED?(R("expired"),new O(f.message)):Kt(f)}function Kt(t){const e=new E(t.code,t.message),n=I();return n.onError&&n.onError({code:t.code,message:t.message,error:e}),e}async function Oe(t,e,n,o){return(S()?!i.cookie:!i.access)&&await xt(),rt(t,e,n,o||{},!1)}async function rt(t,e,n,o,r){let s="";if(!S())try{s=await ft(t,i.access,i.ds)}catch(f){if(!r&&await A({force:!0}))return rt(t,e,n,o,!0);throw R("expired"),new O}const u=new FormData;Object.keys(n||{}).forEach(f=>{const d=n[f];d!=null&&u.append(f,d)}),u.append("file",e);const c=await h(g(t,s),u,o.timeout);if(c.code===a.OK)return c.data;if(c.code===a.AUTH_EXPIRED&&!r){try{await gt()}catch(f){throw f instanceof O&&R("expired"),f}return rt(t,e,n,o,!0)}throw Kt(c)}function ye(t){const e=At(t);if(Lt(),e.nativeGuard!=="off"){const n=kt();if(n.length){const o="[dplugin/core] 这些原生能力在模块加载前已被替换:"+n.join(", ")+"(签名/请求可能被中间人截获,检查是否有脚本抢跑)";if(e.nativeGuard==="error")throw new Error(o);typeof console!="undefined"&&console.warn(o)}}}const Se={login:Rt,logout:Nt,isLogin:z,getState:v,restore:A};exports.AuthError=O;exports.BizError=E;exports.CODE=a;exports.auth=Se;exports.clear=lt;exports.expire=R;exports.getMode=it;exports.getState=v;exports.isAuthError=Mt;exports.isLogin=z;exports.login=Rt;exports.logout=Nt;exports.request=me;exports.setup=ye;exports.store=i;exports.upload=Oe;exports.verifyLogin=A;
|
|
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;
|
|
3
3
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.mjs
CHANGED
|
@@ -22,7 +22,7 @@ const k = {
|
|
|
22
22
|
// 原生能力体检:'warn' 只告警 / 'error' 直接抛错 / 'off' 不检查
|
|
23
23
|
nativeGuard: "warn"
|
|
24
24
|
};
|
|
25
|
-
function
|
|
25
|
+
function Nt(t) {
|
|
26
26
|
return t && Object.keys(k).forEach((e) => {
|
|
27
27
|
t[e] !== void 0 && (k[e] = t[e]);
|
|
28
28
|
}), k;
|
|
@@ -30,7 +30,7 @@ function xt(t) {
|
|
|
30
30
|
function I() {
|
|
31
31
|
return k;
|
|
32
32
|
}
|
|
33
|
-
const E =
|
|
33
|
+
const E = Kt();
|
|
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
|
|
46
|
+
function xt(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 Kt(t) {
|
|
|
53
53
|
return !!e && e.name === "TypeError" && /argument|required|参数/i.test(n);
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
|
-
function
|
|
56
|
+
function Kt() {
|
|
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 ||
|
|
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));
|
|
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 At() {
|
|
|
71
71
|
}
|
|
72
72
|
return n;
|
|
73
73
|
}
|
|
74
|
-
function
|
|
74
|
+
function At() {
|
|
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
|
|
100
|
+
function pe(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
|
|
104
|
+
function Ct(t) {
|
|
105
105
|
try {
|
|
106
106
|
return localStorage.getItem(t);
|
|
107
107
|
} catch (e) {
|
|
@@ -114,13 +114,13 @@ function ht(t, e) {
|
|
|
114
114
|
} catch (n) {
|
|
115
115
|
}
|
|
116
116
|
}
|
|
117
|
-
const
|
|
117
|
+
const G = {};
|
|
118
118
|
function A(t) {
|
|
119
|
-
const e =
|
|
120
|
-
return e === null ?
|
|
119
|
+
const e = Ct(t);
|
|
120
|
+
return e === null ? G[t] === void 0 ? null : G[t] : e;
|
|
121
121
|
}
|
|
122
122
|
function C(t, e) {
|
|
123
|
-
|
|
123
|
+
G[t] = e, ht(t, e);
|
|
124
124
|
}
|
|
125
125
|
const i = {
|
|
126
126
|
get access() {
|
|
@@ -164,7 +164,7 @@ const i = {
|
|
|
164
164
|
*/
|
|
165
165
|
clear() {
|
|
166
166
|
[q, z, J, Q].forEach((t) => {
|
|
167
|
-
|
|
167
|
+
G[t] = null, ht(t, null);
|
|
168
168
|
});
|
|
169
169
|
}
|
|
170
170
|
};
|
|
@@ -188,7 +188,7 @@ function V(t) {
|
|
|
188
188
|
}
|
|
189
189
|
}
|
|
190
190
|
let b = null;
|
|
191
|
-
function
|
|
191
|
+
function _t(t) {
|
|
192
192
|
try {
|
|
193
193
|
const e = typeof location != "undefined" ? location.href : void 0;
|
|
194
194
|
return new URL(t || "", e).origin;
|
|
@@ -196,7 +196,7 @@ function kt(t) {
|
|
|
196
196
|
return null;
|
|
197
197
|
}
|
|
198
198
|
}
|
|
199
|
-
function
|
|
199
|
+
function kt() {
|
|
200
200
|
b = null;
|
|
201
201
|
}
|
|
202
202
|
function pt() {
|
|
@@ -208,7 +208,7 @@ function pt() {
|
|
|
208
208
|
const e = typeof location != "undefined" ? location.origin : null;
|
|
209
209
|
if (!e)
|
|
210
210
|
return b = "sign", b;
|
|
211
|
-
const n =
|
|
211
|
+
const n = _t(t.apiBase || e);
|
|
212
212
|
return b = n && n === e ? "cookie" : "sign", b;
|
|
213
213
|
}
|
|
214
214
|
function S() {
|
|
@@ -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
|
|
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();
|
|
248
248
|
let v = null;
|
|
249
249
|
function j() {
|
|
250
250
|
if (!E.subtle) {
|
|
@@ -253,7 +253,7 @@ function j() {
|
|
|
253
253
|
}
|
|
254
254
|
return E.subtle;
|
|
255
255
|
}
|
|
256
|
-
function
|
|
256
|
+
function Ft() {
|
|
257
257
|
return v || (v = new Promise((t) => {
|
|
258
258
|
if (!E.idbOpen) {
|
|
259
259
|
t(null);
|
|
@@ -261,7 +261,7 @@ function Gt() {
|
|
|
261
261
|
}
|
|
262
262
|
let e;
|
|
263
263
|
try {
|
|
264
|
-
e = E.idbOpen(
|
|
264
|
+
e = E.idbOpen(Ut, Mt);
|
|
265
265
|
} catch (n) {
|
|
266
266
|
t(null);
|
|
267
267
|
return;
|
|
@@ -273,7 +273,7 @@ function Gt() {
|
|
|
273
273
|
}), v);
|
|
274
274
|
}
|
|
275
275
|
function Et(t, e) {
|
|
276
|
-
return
|
|
276
|
+
return Ft().then((n) => n ? new Promise((o) => {
|
|
277
277
|
let r;
|
|
278
278
|
try {
|
|
279
279
|
r = n.transaction(Y, t);
|
|
@@ -303,7 +303,7 @@ function Ot(t, e) {
|
|
|
303
303
|
return F.set(t, e), Et("readwrite", (n) => n.put(e, t)).then(() => e);
|
|
304
304
|
}
|
|
305
305
|
async function st(t) {
|
|
306
|
-
const n = await j().generateKey(
|
|
306
|
+
const n = await j().generateKey(vt, !1, ["sign", "verify"]);
|
|
307
307
|
return Ot(t, n);
|
|
308
308
|
}
|
|
309
309
|
async function yt(t) {
|
|
@@ -315,10 +315,10 @@ async function wt(t, e) {
|
|
|
315
315
|
async function Ht(t) {
|
|
316
316
|
return await mt(t) || st(t);
|
|
317
317
|
}
|
|
318
|
-
function
|
|
318
|
+
function Gt() {
|
|
319
319
|
return st(rt);
|
|
320
320
|
}
|
|
321
|
-
function
|
|
321
|
+
function Yt() {
|
|
322
322
|
return st(it);
|
|
323
323
|
}
|
|
324
324
|
function ct() {
|
|
@@ -342,8 +342,8 @@ function bt(t) {
|
|
|
342
342
|
n += String.fromCharCode(e[o]);
|
|
343
343
|
return btoa(n).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
|
|
344
344
|
}
|
|
345
|
-
async function
|
|
346
|
-
const o = await j().sign(
|
|
345
|
+
async function Bt(t, e) {
|
|
346
|
+
const o = await j().sign(Lt, t.privateKey, new TextEncoder().encode(e));
|
|
347
347
|
return bt(o);
|
|
348
348
|
}
|
|
349
349
|
async function It(t) {
|
|
@@ -353,7 +353,7 @@ async function It(t) {
|
|
|
353
353
|
const et = {
|
|
354
354
|
SSO: rt,
|
|
355
355
|
DOMAIN: it
|
|
356
|
-
}, nt = "-",
|
|
356
|
+
}, nt = "-", Vt = "POST", N = {
|
|
357
357
|
BIZ: "BIZ",
|
|
358
358
|
VERIFY: "VERIFY",
|
|
359
359
|
VERIFY_SID: "VERIFY_SID",
|
|
@@ -362,7 +362,7 @@ const et = {
|
|
|
362
362
|
function p(t) {
|
|
363
363
|
return t == null || t === "" ? nt : String(t);
|
|
364
364
|
}
|
|
365
|
-
function
|
|
365
|
+
function jt(t, e) {
|
|
366
366
|
const n = p(e.origin);
|
|
367
367
|
return [
|
|
368
368
|
p(t),
|
|
@@ -377,21 +377,21 @@ function Xt(t, e) {
|
|
|
377
377
|
].join(`
|
|
378
378
|
`);
|
|
379
379
|
}
|
|
380
|
-
function
|
|
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
383
|
async function x(t, e, n) {
|
|
384
384
|
const o = n === "sso" ? await ct() : await St();
|
|
385
385
|
if (!o)
|
|
386
386
|
throw Tt(n === "sso" ? "缺少登录签名密钥" : "缺少业务签名密钥");
|
|
387
|
-
const r = await
|
|
388
|
-
return
|
|
387
|
+
const r = await Bt(o, jt(t, e));
|
|
388
|
+
return Xt(t, e, r);
|
|
389
389
|
}
|
|
390
390
|
async function at(t, e, n) {
|
|
391
391
|
if (!e || !n)
|
|
392
392
|
throw Tt("缺少 access 或 ds,无法签名");
|
|
393
393
|
const o = await x(N.BIZ, {
|
|
394
|
-
method:
|
|
394
|
+
method: Vt,
|
|
395
395
|
path: V(t),
|
|
396
396
|
origin: K(),
|
|
397
397
|
ds: n,
|
|
@@ -415,14 +415,14 @@ function Tt(t) {
|
|
|
415
415
|
const e = new Error(t);
|
|
416
416
|
return e.code = "KEY_MISSING", e;
|
|
417
417
|
}
|
|
418
|
-
function
|
|
418
|
+
function Wt() {
|
|
419
419
|
const t = typeof navigator != "undefined" && navigator.userAgent || "", e = {
|
|
420
|
-
device_brand:
|
|
421
|
-
device_model:
|
|
422
|
-
browser_name:
|
|
423
|
-
browser_version:
|
|
424
|
-
os_name:
|
|
425
|
-
os_version:
|
|
420
|
+
device_brand: Zt(t),
|
|
421
|
+
device_model: $t(t),
|
|
422
|
+
browser_name: qt(t),
|
|
423
|
+
browser_version: zt(t),
|
|
424
|
+
os_name: Jt(t),
|
|
425
|
+
os_version: Qt(t)
|
|
426
426
|
}, n = i.ds;
|
|
427
427
|
return n && (e.device_id = n), e;
|
|
428
428
|
}
|
|
@@ -430,25 +430,25 @@ function T(t, e) {
|
|
|
430
430
|
const n = t.match(e);
|
|
431
431
|
return n ? n[1] : "";
|
|
432
432
|
}
|
|
433
|
-
function
|
|
433
|
+
function Zt(t) {
|
|
434
434
|
return /iPhone|iPad|iPod/.test(t) ? "Apple" : /Huawei|HONOR/i.test(t) ? "Huawei" : /Xiaomi|Redmi|POCO/i.test(t) ? "Xiaomi" : /OPPO|realme/i.test(t) ? "OPPO" : /vivo/i.test(t) ? "vivo" : /Samsung|SM-|SAMSUNG/i.test(t) ? "Samsung" : /Macintosh|Mac OS X/.test(t) ? "Apple" : "";
|
|
435
435
|
}
|
|
436
|
-
function
|
|
436
|
+
function $t(t) {
|
|
437
437
|
if (/iPhone/.test(t)) return "iPhone";
|
|
438
438
|
if (/iPad/.test(t)) return "iPad";
|
|
439
439
|
const e = t.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);
|
|
440
440
|
return e ? e[1].trim() : /Macintosh/.test(t) ? "Mac" : "";
|
|
441
441
|
}
|
|
442
|
-
function
|
|
442
|
+
function qt(t) {
|
|
443
443
|
return /MicroMessenger/i.test(t) ? "WeChat" : /Edg[A-Za-z]*\//.test(t) ? "Edge" : /OPR\/|Opera/.test(t) ? "Opera" : /Firefox\//.test(t) ? "Firefox" : /Chrome\/|CriOS\//.test(t) ? "Chrome" : /Safari\//.test(t) ? "Safari" : "";
|
|
444
444
|
}
|
|
445
|
-
function
|
|
445
|
+
function zt(t) {
|
|
446
446
|
return /MicroMessenger\/([\d.]+)/.test(t) ? T(t, /MicroMessenger\/([\d.]+)/) : /Edg[A-Za-z]*\/([\d.]+)/.test(t) ? T(t, /Edg[A-Za-z]*\/([\d.]+)/) : /OPR\/([\d.]+)/.test(t) ? T(t, /OPR\/([\d.]+)/) : /Firefox\/([\d.]+)/.test(t) ? T(t, /Firefox\/([\d.]+)/) : /CriOS\/([\d.]+)/.test(t) ? T(t, /CriOS\/([\d.]+)/) : /Chrome\/([\d.]+)/.test(t) ? T(t, /Chrome\/([\d.]+)/) : /Version\/([\d.]+).*Safari/.test(t) ? T(t, /Version\/([\d.]+)/) : "";
|
|
447
447
|
}
|
|
448
|
-
function
|
|
448
|
+
function Jt(t) {
|
|
449
449
|
return /Windows NT/.test(t) ? "Windows" : /iPhone|iPad|iPod/.test(t) ? "iOS" : /Android/.test(t) ? "Android" : /Mac OS X|Macintosh/.test(t) ? "macOS" : /HarmonyOS/.test(t) ? "HarmonyOS" : /Linux/.test(t) ? "Linux" : "";
|
|
450
450
|
}
|
|
451
|
-
function
|
|
451
|
+
function Qt(t) {
|
|
452
452
|
if (/Windows NT 10\.0/.test(t)) return "10/11";
|
|
453
453
|
if (/Windows NT 6\.3/.test(t)) return "8.1";
|
|
454
454
|
if (/Windows NT 6\.1/.test(t)) return "7";
|
|
@@ -470,7 +470,7 @@ const g = {
|
|
|
470
470
|
REFRESH: "/api/unsafe/dplugin/login/c/refresh"
|
|
471
471
|
},
|
|
472
472
|
LOGOUT: "/api/safe/dplugin/login/logout"
|
|
473
|
-
},
|
|
473
|
+
}, te = "dp_dvt", ut = 1800 * 1e3;
|
|
474
474
|
let _ = null, L = null, D = null, B = !1;
|
|
475
475
|
function X() {
|
|
476
476
|
return typeof location != "undefined" ? location.href : "";
|
|
@@ -485,7 +485,7 @@ function W() {
|
|
|
485
485
|
function P(t, e) {
|
|
486
486
|
return new m(t.code, t.message || e);
|
|
487
487
|
}
|
|
488
|
-
function
|
|
488
|
+
function ee(t, e, n) {
|
|
489
489
|
if (typeof document == "undefined")
|
|
490
490
|
return;
|
|
491
491
|
let o = t + "=" + encodeURIComponent(e) + "; path=/; SameSite=Lax";
|
|
@@ -516,12 +516,12 @@ function R(t) {
|
|
|
516
516
|
const o = n.loginUrl.includes("?") ? "&" : "?";
|
|
517
517
|
location.href = n.loginUrl + o + "referer=" + encodeURIComponent(location.href);
|
|
518
518
|
}
|
|
519
|
-
function
|
|
520
|
-
return D || (D =
|
|
521
|
-
throw D = null,
|
|
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
522
|
}), D);
|
|
523
523
|
}
|
|
524
|
-
function
|
|
524
|
+
function oe(t) {
|
|
525
525
|
const e = I();
|
|
526
526
|
typeof e.onError == "function" && e.onError({
|
|
527
527
|
code: t && t.code,
|
|
@@ -529,25 +529,25 @@ function re(t) {
|
|
|
529
529
|
error: t
|
|
530
530
|
});
|
|
531
531
|
}
|
|
532
|
-
async function
|
|
533
|
-
const n = I(), o =
|
|
532
|
+
async function re(t, e) {
|
|
533
|
+
const n = I(), o = Wt();
|
|
534
534
|
if (S())
|
|
535
|
-
return
|
|
535
|
+
return ce(t, e, o, n);
|
|
536
536
|
const r = await ct(), s = await St();
|
|
537
537
|
try {
|
|
538
|
-
return await
|
|
538
|
+
return await ie(t, e, o, n);
|
|
539
539
|
} catch (a) {
|
|
540
540
|
throw await wt(et.SSO, r), await wt(et.DOMAIN, s), a;
|
|
541
541
|
}
|
|
542
542
|
}
|
|
543
|
-
async function
|
|
544
|
-
const r = await
|
|
543
|
+
async function ie(t, e, n, o) {
|
|
544
|
+
const r = await Gt(), s = await tt(r), a = await h(w(t), Object.assign({}, e, { device: n, pk: s }));
|
|
545
545
|
if (a.code !== u.OK)
|
|
546
546
|
throw P(a, "登录失败");
|
|
547
547
|
const c = a.data && a.data.token;
|
|
548
548
|
if (!c)
|
|
549
549
|
throw P(a, "登录失败");
|
|
550
|
-
const f = await
|
|
550
|
+
const f = await Yt(), d = await It(f), l = await se(c, o, d), y = await h(w(g.SIGN.TOKEN), {
|
|
551
551
|
dsign: await x(N.TOKEN, {
|
|
552
552
|
origin: K(),
|
|
553
553
|
ts: Date.now(),
|
|
@@ -560,7 +560,7 @@ async function se(t, e, n, o) {
|
|
|
560
560
|
throw P(y, "登录失败");
|
|
561
561
|
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
562
|
}
|
|
563
|
-
async function
|
|
563
|
+
async function se(t, e, n) {
|
|
564
564
|
const o = X(), r = Math.max(1, Number(e.pollMax) || 5);
|
|
565
565
|
for (let s = 1; s <= r; s++) {
|
|
566
566
|
const a = i.ds || null, c = Date.now(), f = await x(N.VERIFY, {
|
|
@@ -596,16 +596,16 @@ async function ce(t, e, n) {
|
|
|
596
596
|
}
|
|
597
597
|
throw new m(u.AUTH_EXPIRED, "登录超时,请重试");
|
|
598
598
|
}
|
|
599
|
-
async function
|
|
599
|
+
async function ce(t, e, n, o) {
|
|
600
600
|
const r = await h(w(t), Object.assign({}, e, { device: n }));
|
|
601
601
|
if (r.code !== u.OK)
|
|
602
602
|
throw P(r, "登录失败");
|
|
603
603
|
const s = r.data && r.data.token;
|
|
604
604
|
if (!s)
|
|
605
605
|
throw P(r, "登录失败");
|
|
606
|
-
return
|
|
606
|
+
return ee(te, s, 300), await ae(o), i.cookie = !0, B = !1, W(), Z();
|
|
607
607
|
}
|
|
608
|
-
async function
|
|
608
|
+
async function ae(t) {
|
|
609
609
|
const e = X(), n = Math.max(1, Number(t.pollMax) || 5);
|
|
610
610
|
for (let o = 1; o <= n; o++) {
|
|
611
611
|
let r;
|
|
@@ -631,17 +631,11 @@ async function ue(t) {
|
|
|
631
631
|
}
|
|
632
632
|
throw new m(u.AUTH_EXPIRED, "登录超时,请重试");
|
|
633
633
|
}
|
|
634
|
-
async function Rt() {
|
|
635
|
-
const t = await h(w(g.COOKIE.TOKEN));
|
|
636
|
-
if (t.code !== u.OK)
|
|
637
|
-
throw P(t, "登录失败");
|
|
638
|
-
return t;
|
|
639
|
-
}
|
|
640
634
|
async function M(t) {
|
|
641
635
|
const e = !!(t && t.force);
|
|
642
636
|
return L || (L = (async () => {
|
|
643
637
|
try {
|
|
644
|
-
return S() ? await
|
|
638
|
+
return S() ? await ue(e) : await de(e);
|
|
645
639
|
} catch (n) {
|
|
646
640
|
if (n && (n.code === u.NETWORK || n.code === u.TIMEOUT))
|
|
647
641
|
throw n;
|
|
@@ -651,10 +645,10 @@ async function M(t) {
|
|
|
651
645
|
}
|
|
652
646
|
})()), L;
|
|
653
647
|
}
|
|
654
|
-
async function
|
|
655
|
-
return !t && i.cookie ? !0 : (await h(w(g.COOKIE.VERIFY), { redirect: X() })).code !== u.OK
|
|
648
|
+
async function ue(t) {
|
|
649
|
+
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);
|
|
656
650
|
}
|
|
657
|
-
async function
|
|
651
|
+
async function fe() {
|
|
658
652
|
if (!i.sid || !i.ds || !await ct())
|
|
659
653
|
return !1;
|
|
660
654
|
const e = i.ds, n = await Ht(et.DOMAIN), o = await It(n), r = X(), s = await x(N.VERIFY_SID, {
|
|
@@ -680,25 +674,28 @@ async function de() {
|
|
|
680
674
|
});
|
|
681
675
|
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);
|
|
682
676
|
}
|
|
683
|
-
async function
|
|
684
|
-
return !t && i.access ? !0 :
|
|
677
|
+
async function de(t) {
|
|
678
|
+
return !t && i.access ? !0 : fe();
|
|
685
679
|
}
|
|
686
680
|
function dt() {
|
|
687
|
-
return _ || (_ = (S() ?
|
|
681
|
+
return _ || (_ = (S() ? le() : we()).then((t) => (_ = null, t), (t) => {
|
|
688
682
|
throw _ = null, t;
|
|
689
683
|
})), _;
|
|
690
684
|
}
|
|
691
|
-
async function
|
|
692
|
-
const t = await h(w(g.COOKIE.
|
|
685
|
+
async function le() {
|
|
686
|
+
const t = await h(w(g.COOKIE.TOKEN));
|
|
693
687
|
if (t.code === u.OK)
|
|
694
688
|
return !0;
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
689
|
+
const e = await h(w(g.COOKIE.REFRESH));
|
|
690
|
+
if (e.code === u.OK)
|
|
691
|
+
return !0;
|
|
692
|
+
if (e.code === u.AUTH_EXPIRED || t.code === u.AUTH_EXPIRED)
|
|
693
|
+
throw new O(e.message || t.message);
|
|
694
|
+
if (e.code === "500" && /重复/.test(e.message || ""))
|
|
698
695
|
return await U(800), !0;
|
|
699
|
-
throw new m(
|
|
696
|
+
throw new m(e.code, e.message);
|
|
700
697
|
}
|
|
701
|
-
async function
|
|
698
|
+
async function we() {
|
|
702
699
|
const t = i.access, e = i.ds;
|
|
703
700
|
if (!t || !e)
|
|
704
701
|
throw new O();
|
|
@@ -712,12 +709,12 @@ async function ge() {
|
|
|
712
709
|
}, "domain"), o = await h(w(g.SIGN.REFRESH, "dsign=" + encodeURIComponent(n)));
|
|
713
710
|
if (o.code === u.OK && o.data)
|
|
714
711
|
return i.access = o.data.token, o.data.ds && (i.ds = o.data.ds), i.exp = Date.now() + ut, i.access;
|
|
715
|
-
const r = await
|
|
712
|
+
const r = await ge(t, 1200);
|
|
716
713
|
if (r)
|
|
717
714
|
return r;
|
|
718
715
|
throw o.code === u.AUTH_EXPIRED ? new O(o.message) : new m(o.code, o.message);
|
|
719
716
|
}
|
|
720
|
-
function
|
|
717
|
+
function ge(t, e) {
|
|
721
718
|
const n = Date.now() + e;
|
|
722
719
|
return new Promise((o) => {
|
|
723
720
|
const r = () => {
|
|
@@ -735,7 +732,7 @@ function he(t, e) {
|
|
|
735
732
|
r();
|
|
736
733
|
});
|
|
737
734
|
}
|
|
738
|
-
async function
|
|
735
|
+
async function he() {
|
|
739
736
|
try {
|
|
740
737
|
let e = await gt();
|
|
741
738
|
e.code === u.AUTH_EXPIRED && (await dt(), e = await gt());
|
|
@@ -751,11 +748,11 @@ async function gt() {
|
|
|
751
748
|
const t = await at(g.LOGOUT, i.access, i.ds);
|
|
752
749
|
return h(w(g.LOGOUT, t));
|
|
753
750
|
}
|
|
754
|
-
async function
|
|
751
|
+
async function Ee(t, e, n) {
|
|
755
752
|
const o = n || {};
|
|
756
|
-
return o.auth !== !1 && await
|
|
753
|
+
return o.auth !== !1 && await Rt(), H(t, e || {}, o, !1);
|
|
757
754
|
}
|
|
758
|
-
async function
|
|
755
|
+
async function Rt() {
|
|
759
756
|
if (S()) {
|
|
760
757
|
if (i.cookie || await M())
|
|
761
758
|
return;
|
|
@@ -764,7 +761,7 @@ async function Dt() {
|
|
|
764
761
|
if (!i.access && !await M())
|
|
765
762
|
throw R("expired"), new O();
|
|
766
763
|
}
|
|
767
|
-
async function
|
|
764
|
+
async function H(t, e, n, o) {
|
|
768
765
|
const r = n.auth !== !1, s = r && !S();
|
|
769
766
|
let a = "", c = null;
|
|
770
767
|
if (s) {
|
|
@@ -773,7 +770,7 @@ async function G(t, e, n, o) {
|
|
|
773
770
|
a = await at(t, c, i.ds);
|
|
774
771
|
} catch (d) {
|
|
775
772
|
if (!o && await M({ force: !0 }))
|
|
776
|
-
return
|
|
773
|
+
return H(t, e, n, !0);
|
|
777
774
|
throw R("expired"), new O();
|
|
778
775
|
}
|
|
779
776
|
}
|
|
@@ -782,22 +779,22 @@ async function G(t, e, n, o) {
|
|
|
782
779
|
return f.data;
|
|
783
780
|
if (r && f.code === u.AUTH_EXPIRED && !o) {
|
|
784
781
|
if (s && c && i.access !== c)
|
|
785
|
-
return
|
|
782
|
+
return H(t, e, n, !0);
|
|
786
783
|
try {
|
|
787
784
|
await dt();
|
|
788
785
|
} catch (d) {
|
|
789
786
|
throw d instanceof O && R("expired"), d;
|
|
790
787
|
}
|
|
791
|
-
return
|
|
788
|
+
return H(t, e, n, !0);
|
|
792
789
|
}
|
|
793
|
-
throw r && f.code === u.AUTH_EXPIRED ? (R("expired"), new O(f.message)) :
|
|
790
|
+
throw r && f.code === u.AUTH_EXPIRED ? (R("expired"), new O(f.message)) : Dt(f, n);
|
|
794
791
|
}
|
|
795
|
-
function
|
|
796
|
-
const
|
|
797
|
-
return
|
|
792
|
+
function Dt(t, e) {
|
|
793
|
+
const n = new m(t.code, t.message), o = I();
|
|
794
|
+
return o.onError && !(e && e.silent) && o.onError({ code: t.code, message: t.message, error: n }), n;
|
|
798
795
|
}
|
|
799
|
-
async function
|
|
800
|
-
return (S() ? !i.cookie : !i.access) && await
|
|
796
|
+
async function me(t, e, n, o) {
|
|
797
|
+
return (S() ? !i.cookie : !i.access) && await Rt(), ot(t, e, n, o || {}, !1);
|
|
801
798
|
}
|
|
802
799
|
async function ot(t, e, n, o, r) {
|
|
803
800
|
let s = "";
|
|
@@ -825,12 +822,12 @@ async function ot(t, e, n, o, r) {
|
|
|
825
822
|
}
|
|
826
823
|
return ot(t, e, n, o, !0);
|
|
827
824
|
}
|
|
828
|
-
throw
|
|
825
|
+
throw Dt(c, o);
|
|
829
826
|
}
|
|
830
|
-
function
|
|
831
|
-
const e =
|
|
832
|
-
if (
|
|
833
|
-
const n =
|
|
827
|
+
function Oe(t) {
|
|
828
|
+
const e = Nt(t);
|
|
829
|
+
if (kt(), e.nativeGuard !== "off") {
|
|
830
|
+
const n = At();
|
|
834
831
|
if (n.length) {
|
|
835
832
|
const o = "[dplugin/core] 这些原生能力在模块加载前已被替换:" + n.join(", ") + "(签名/请求可能被中间人截获,检查是否有脚本抢跑)";
|
|
836
833
|
if (e.nativeGuard === "error")
|
|
@@ -839,9 +836,9 @@ function ye(t) {
|
|
|
839
836
|
}
|
|
840
837
|
}
|
|
841
838
|
}
|
|
842
|
-
const
|
|
843
|
-
login:
|
|
844
|
-
logout:
|
|
839
|
+
const ye = {
|
|
840
|
+
login: ne,
|
|
841
|
+
logout: he,
|
|
845
842
|
isLogin: ft,
|
|
846
843
|
getState: Z,
|
|
847
844
|
/** 页面加载时恢复登录态;返回是否已登录 */
|
|
@@ -851,19 +848,19 @@ export {
|
|
|
851
848
|
O as AuthError,
|
|
852
849
|
m as BizError,
|
|
853
850
|
u as CODE,
|
|
854
|
-
|
|
851
|
+
ye as auth,
|
|
855
852
|
Pt as clear,
|
|
856
853
|
R as expire,
|
|
857
854
|
pt as getMode,
|
|
858
855
|
Z as getState,
|
|
859
|
-
|
|
856
|
+
pe as isAuthError,
|
|
860
857
|
ft as isLogin,
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
858
|
+
ne as login,
|
|
859
|
+
he as logout,
|
|
860
|
+
Ee as request,
|
|
861
|
+
Oe as setup,
|
|
865
862
|
i as store,
|
|
866
|
-
|
|
863
|
+
me as upload,
|
|
867
864
|
M as verifyLogin
|
|
868
865
|
};
|
|
869
866
|
//# 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
|
|
2
|
-
`)}function $e(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 be();if(!o)throw Pe(n==="sso"?"缺少登录签名密钥":"缺少业务签名密钥");const r=await qe(o,Ze(e,t));return $e(e,t,r)}async function fe(e,t,n){if(!t||!n)throw Pe("缺少 access 或 ds,无法签名");const o=await A(K.BIZ,{method:ze,path:B(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 Je(){const e=typeof navigator!="undefined"&&navigator.userAgent||"",t={device_brand:Qe(e),device_model:et(e),browser_name:tt(e),browser_version:nt(e),os_name:ot(e),os_version:rt(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 Qe(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 et(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 tt(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 nt(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 ot(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 rt(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"},it="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 D(e,t){return new E(e.code,e.message||t)}function st(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 N(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 _||(_=at(e,t).then(n=>(_=null,n),n=>{throw _=null,ct(n),n}),_)}function ct(e){const t=T();typeof t.onError=="function"&&t.onError({code:e&&e.code,message:e&&e.message,error:e})}async function at(e,t){const n=T(),o=Je();if(S())return dt(e,t,o,n);const r=await se(),s=await be();try{return await ut(e,t,o,n)}catch(u){throw await Se(ae.SSO,r),await Se(ae.DOMAIN,s),u}}async function ut(e,t,n,o){const r=await Xe(),s=await ce(r),u=await h(w(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 We(),l=await Te(f),g=await ft(c,o,l),b=await h(w(p.SIGN.TOKEN),{dsign:await A(K.TOKEN,{origin:C(),ts:Date.now(),subject:g.code,dna:l},"domain"),pk:await ce(f)});if(b.code!==a.OK||!b.data)throw D(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 ft(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(K.VERIFY,{method:"POST",path:B(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 D(l,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function dt(e,t,n,o){const r=await h(w(e),Object.assign({},t,{device:n}));if(r.code!==a.OK)throw D(r,"登录失败");const s=r.data&&r.data.token;if(!s)throw D(r,"登录失败");return st(it,s,300),await lt(o),await De(),i.cookie=!0,X=!1,q(),L()}async function lt(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 D(r,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function De(){const e=await h(w(p.COOKIE.TOKEN));if(e.code!==a.OK)throw D(e,"登录失败");return e}async function x(e){const t=!!(e&&e.force);return j||(j=(async()=>{try{return S()?await gt(t):await ht(t)}catch(n){if(n&&(n.code===a.NETWORK||n.code===a.TIMEOUT))throw n;return!1}finally{j=null}})()),j}async function gt(e){return!e&&i.cookie?!0:(await h(w(p.COOKIE.VERIFY),{redirect:W()})).code!==a.OK||(await De()).code!==a.OK?(i.cookie=!1,!1):(i.cookie=!0,q(),!0)}async function wt(){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(K.VERIFY_SID,{method:"POST",path:B(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 A(K.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 ht(e){return!e&&i.access?!0:wt()}function ge(){return U||(U=(S()?pt():Et()).then(e=>(U=null,e),e=>{throw U=null,e})),U}async function pt(){const e=await h(w(p.COOKIE.REFRESH));if(e.code===a.OK)return!0;if(e.code===a.AUTH_EXPIRED)throw new y(e.message);if(e.code==="500"&&/重复/.test(e.message||""))return await v(800),!0;throw new E(e.code,e.message)}async function Et(){const e=i.access,t=i.ds;if(!e||!t)throw new y;const n=await A(K.BIZ,{method:"POST",path:B(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 mt(e,1200);if(r)return r;throw o.code===a.AUTH_EXPIRED?new y(o.message):new E(o.code,o.message)}function mt(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 Ne(){try{let t=await Ke();t.code===a.AUTH_EXPIRED&&(await ge(),t=await Ke())}catch(t){}le();const e=T();typeof e.onLogout=="function"&&e.onLogout("logout")}async function Ke(){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 Ot(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 N("expired"),new y}if(!i.access&&!await x())throw N("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 N("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&&N("expired"),l}return Z(e,t,n,!0)}throw r&&f.code===a.AUTH_EXPIRED?(N("expired"),new y(f.message)):Ce(f)}function Ce(e){const t=new E(e.code,e.message),n=T();return n.onError&&n.onError({code:e.code,message:e.message,error:t}),t}async function yt(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 N("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&&N("expired"),f}return we(e,t,n,o,!0)}throw Ce(c)}function St(e){const t=_e(e);if(Fe(),t.nativeGuard!=="off"){const n=Me();if(n.length){const o="[dplugin/core] 这些原生能力在模块加载前已被替换:"+n.join(", ")+"(签名/请求可能被中间人截获,检查是否有脚本抢跑)";if(t.nativeGuard==="error")throw new Error(o);typeof console!="undefined"&&console.warn(o)}}}const bt={login:Re,logout:Ne,isLogin:z,getState:L,restore:x};d.AuthError=y,d.BizError=E,d.CODE=a,d.auth=bt,d.clear=le,d.expire=N,d.getMode=ne,d.getState=L,d.isAuthError=Ue,d.isLogin=z,d.login=Re,d.logout=Ne,d.request=Ot,d.setup=St,d.store=i,d.upload=yt,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=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"})}));
|
|
3
3
|
//# sourceMappingURL=index.umd.js.map
|