@dplugin/core 2.0.12 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +2 -2
- package/dist/index.mjs +422 -705
- package/dist/index.umd.js +2 -2
- package/package.json +1 -1
package/dist/index.umd.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
(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 ie():await Se();if(!o)throw Pe(n==="sso"?"缺少登录签名密钥":"缺少业务签名密钥");const r=await qe(o,Ze(e,t));return $e(e,t,r)}async function ue(e,t,n){if(!t||!n)throw Pe("缺少 access 或 ds,无法签名");const o=await A(N.BIZ,{method:ze,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 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"},fe=1800*1e3;let k=null,V=null,C=null,X=!1;function de(){return typeof location!="undefined"?location.href:""}function Re(){if(typeof location=="undefined")return"";try{const e=new URLSearchParams(location.search).get("referer");if(e)return decodeURIComponent(e)}catch(e){}return de()}function L(e){return new Promise(t=>setTimeout(t,e))}function W(){const e=T();typeof e.onLogin=="function"&&e.onLogin(v())}function _(e,t){return new E(e.code,e.message||t)}function v(){return{mode:te(),loggedIn:q(),hasAccess:!!i.access,sid:i.sid,ds:i.ds}}function q(){return b()?!!i.cookie:!!(i.access||i.sid)}function le(){i.clear()}function D(e){const t=q();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 De(e,t){return C||(C=st(e,t).then(n=>(C=null,n),n=>{throw C=null,it(n),n}),C)}function it(e){const t=T();typeof t.onError=="function"&&t.onError({code:e&&e.code,message:e&&e.message,error:e})}async function st(e,t){const n=T(),o=Je();if(b())return ut(e,t,o,n);const r=await ie(),s=await Se();try{return await ct(e,t,o,n)}catch(u){throw await be(ce.SSO,r),await be(ce.DOMAIN,s),u}}async function ct(e,t,n,o){const r=await Xe(),s=await se(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 We(),l=await Te(f),g=await at(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 se(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()+fe,X=!1,W(),v()}async function at(e,t,n){const o=de(),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 L(t.pollInterval);continue}throw g}if(l.code===a.OK&&l.data)return l.data;if(l.code===a.PENDING){if(s<r){await L(t.pollInterval);continue}break}throw _(l,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function ut(e,t,n,o){const r=await h(w(e),Object.assign({},t,{device:n}));if(r.code!==a.OK)throw _(r,"登录失败");return await ft(o),i.cookie=!0,X=!1,W(),v()}async function ft(e){const t=Re(),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 L(e.pollInterval);continue}throw s}if(r.code===a.OK)return r.data;if(r.code===a.PENDING){if(o<n){await L(e.pollInterval);continue}break}throw _(r,"登录失败")}throw new E(a.AUTH_EXPIRED,"登录超时,请重试")}async function U(e){const t=!!(e&&e.force);return V||(V=(async()=>{try{return b()?await dt(t):await gt(t)}catch(n){if(n&&(n.code===a.NETWORK||n.code===a.TIMEOUT))throw n;return!1}finally{V=null}})()),V}async function dt(e){return!e&&i.cookie?!0:(await h(w(p.COOKIE.VERIFY),{redirect:Re()})).code!==a.OK?(i.cookie=!1,!1):(i.cookie=!0,W(),!0)}async function lt(){if(!i.sid||!i.ds||!await ie())return!1;const t=i.ds,n=await Ve(ce.DOMAIN),o=await Te(n),r=de(),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 se(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()+fe,W(),!0)}async function gt(e){return!e&&i.access?!0:lt()}function ge(){return k||(k=(b()?wt():ht()).then(e=>(k=null,e),e=>{throw k=null,e})),k}async function wt(){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 L(800),!0;throw new E(t.code,t.message)}async function ht(){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()+fe,i.access;const r=await pt(e,1200);if(r)return r;throw o.code===a.AUTH_EXPIRED?new y(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 Ne(){try{let t=await Ae();t.code===a.AUTH_EXPIRED&&(await ge(),t=await Ae())}catch(t){}le();const e=T();typeof e.onLogout=="function"&&e.onLogout("logout")}async function Ae(){if(b())return h(w(p.LOGOUT));const e=await ue(p.LOGOUT,i.access,i.ds);return h(w(p.LOGOUT,e))}async function Et(e,t,n){const o=n||{};return o.auth!==!1&&await Ke(),z(e,t||{},o,!1)}async function Ke(){if(b()){if(i.cookie||await U())return;throw D("expired"),new y}if(!i.access&&!await U())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 ue(e,c,i.ds)}catch(l){if(!o&&await U({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)):Ce(f,n)}function Ce(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 mt(e,t,n,o){return(b()?!i.cookie:!i.access)&&await Ke(),we(e,t,n,o||{},!1)}async function we(e,t,n,o,r){let s="";if(!b())try{s=await ue(e,i.access,i.ds)}catch(f){if(!r&&await U({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 Ce(c,o)}function Ot(e){const t=_e(e);if(Fe(),t.nativeGuard!=="off"){const n=xe();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:De,logout:Ne,isLogin:q,getState:v,restore:U};d.AuthError=y,d.BizError=E,d.CODE=a,d.auth=yt,d.clear=le,d.expire=D,d.getMode=te,d.getState=v,d.isAuthError=ke,d.isLogin=q,d.login=De,d.logout=Ne,d.request=Et,d.setup=Ot,d.store=i,d.upload=mt,d.verifyLogin=U,Object.defineProperty(d,Symbol.toStringTag,{value:"Module"})}));
|
|
1
|
+
(function(a,h){typeof exports=="object"&&typeof module!="undefined"?h(exports):typeof define=="function"&&define.amd?define(["exports"],h):(a=typeof globalThis!="undefined"?globalThis:a||self,h(a.DPluginCore={}))})(this,(function(a){"use strict";const h={apiBase:"",mode:"auto",loginUrl:"/login/",timeout:15e3,pollInterval:2e3,pollMax:5,enableAuth:!1,onError:null,onLogin:null,onLogout:null,nativeGuard:"error"};function oe(e){return e&&Object.keys(h).forEach(n=>{e[n]!==void 0&&(h[n]=e[n])}),h}function S(){return h}const f=ce();function L(e,n){if(typeof e!="function")return!1;let t;try{t=Function.prototype.toString.call(e)}catch(i){return!1}const r=/^function\s+([\w$]+)\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.exec(t);return r?r[1]===n:/\[native code\]/.test(t)&&!/^function\s*\(\s*\)\s*\{\s*\[native code\]\s*\}$/.test(t)}function se(e){try{const n=e();return n&&typeof n.then=="function"&&Promise.resolve(n).catch(()=>{}),!1}catch(n){const t=String(n&&n.message||"");return!!n&&n.name==="TypeError"&&/argument|required|参数/i.test(t)}}function ce(){const e=globalThis,n=typeof e.window!="undefined"&&e.window&&typeof e.window.document!="undefined",t={fetch:null,idbOpen:null,subtle:null,pristine:{fetch:!1,idbOpen:!1,subtle:!1}};typeof e.fetch=="function"&&(t.pristine.fetch=L(e.fetch,"fetch"),t.fetch=e.fetch.bind(e)),typeof e.indexedDB!="undefined"&&e.indexedDB&&typeof e.indexedDB.open=="function"&&(t.pristine.idbOpen=L(e.indexedDB.open,"open")&&(!n||se(()=>e.indexedDB.open())),t.idbOpen=e.indexedDB.open.bind(e.indexedDB));const r=e.crypto&&e.crypto.subtle?e.crypto.subtle:null;if(r){const i=["generateKey","exportKey","importKey","sign","verify","digest"],o={};let s=!0;i.forEach(c=>{typeof r[c]=="function"&&(o[c]=r[c].bind(r),s=s&&L(r[c],c))}),t.subtle=o,t.pristine.subtle=s}return t}function ue(){if(typeof window=="undefined"||typeof window.document=="undefined")return[];const e=[];return f.fetch&&!f.pristine.fetch&&e.push("fetch"),f.idbOpen&&!f.pristine.idbOpen&&e.push("indexedDB.open"),f.subtle&&!f.pristine.subtle&&e.push("crypto.subtle"),e}const l={OK:"200",LOGIN_PENDING:"202",LOGIN_TIMEOUT:"g.login.timeout",NETWORK:"NETWORK",TIMEOUT:"TIMEOUT"};class p extends Error{constructor(n,t){super(t||"请求失败"),this.name="BizError",this.code=n}}class ae extends p{constructor(n){super(l.LOGIN_TIMEOUT,n||"登录已失效"),this.name="AuthError"}}const B="dp.token",V="dp.sid",q="dp.ds";function fe(e){try{return localStorage.getItem(e)}catch(n){return null}}function X(e,n){try{n==null||n===""?localStorage.removeItem(e):localStorage.setItem(e,n)}catch(t){}}const R={};function U(e){const n=fe(e);return n===null?R[e]===void 0?null:R[e]:n}function F(e,n){R[e]=n,X(e,n)}const u={get token(){return U(B)},set token(e){F(B,e)},get sid(){return U(V)},set sid(e){F(V,e)},get ds(){return U(q)},set ds(e){F(q,e)},clear(){[B,V].forEach(e=>{R[e]=null,X(e,null)})}},le="dplugin",de=1,K="keys",ge={name:"ECDSA",namedCurve:"P-256"},he={name:"ECDSA",hash:"SHA-256"},C=new Map;let D=null;function M(){if(!f.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 f.subtle}function pe(){return D||(D=new Promise(e=>{if(!f.idbOpen){e(null);return}let n;try{n=f.idbOpen(le,de)}catch(t){e(null);return}n.onupgradeneeded=()=>{const t=n.result;t.objectStoreNames.contains(K)||t.createObjectStore(K)},n.onsuccess=()=>e(n.result),n.onerror=()=>e(null),n.onblocked=()=>e(null)}),D)}function J(e,n){return pe().then(t=>t?new Promise(r=>{let i;try{i=t.transaction(K,e)}catch(c){r(null);return}const o=i.objectStore(K);let s=null;try{const c=n(o);c&&(c.onsuccess=()=>{s=c.result},c.onerror=()=>{s=null})}catch(c){s=null}i.oncomplete=()=>r(s),i.onerror=()=>r(null),i.onabort=()=>r(null)}):null)}async function T(e){return C.has(e)?Promise.resolve(C.get(e)):J("readonly",n=>n.get(e)).then(n=>(n&&C.set(e,n),n||null))}function Q(e){const n=new Uint8Array(e);let t="";for(let r=0;r<n.length;r++)t+=String.fromCharCode(n[r]);return btoa(t).replace(/\+/g,"-").replace(/\//g,"_").replace(/=+$/,"")}async function ee(e){let n=await T(e);return n||(n=await M().generateKey(ge,!1,["sign","verify"]),C.set(e,n),J("readwrite",r=>r.put(n,e)).then(()=>n))}function we(){return ee("sso")}function me(){return ee("domain")}async function ye(){return await T("sso")}async function Oe(){return await T("domain")}async function Ee(){let e=await T("domain");const n=M(),t=await n.exportKey("spki",e.publicKey),r=await n.digest("SHA-256",t);return Q(r)}async function j(e){let n=await T(e);const r=await M().exportKey("spki",n.publicKey);let i="";const o=new Uint8Array(r);for(let s=0;s<o.length;s++)i+=String.fromCharCode(o[s]);return btoa(i)}async function Se(e,n){const r=await M().sign(he,e.privateKey,new TextEncoder().encode(n));return Q(r)}const be="-",_={BIZ:"BIZ",VTT:"VTT",VSD:"VSD",TOK:"TOK"};function O(e){return e==null||e===""?be:String(e)}function Te(e,n){const t=O(n.origin);return[e,n.method,n.path,t.toLowerCase(),O(n.ds),n.ts,O(n.token),O(n.dna),O(n.redirect)].join(`
|
|
2
|
+
`)}function ke(e,n,t){return[e,O(n.token),O(n.ds),n.ts,t].join(".")}function Ie(e){const n=new Error(e);return n.code="KEY_MISSING",n}async function A(e,n,t){const r=t==="sso"?await ye():await Oe();if(!r)throw Ie(t==="sso"?"缺少登录签名密钥":"缺少业务签名密钥");let i=n.path.indexOf("?");i>0&&(n.path=n.path.substring(0,i));const o=await Se(r,Te(e,n));return ke(e,n,o)}let w=null;function Pe(e){try{const n=typeof location!="undefined"?location.href:void 0;return new URL(e||"",n).origin}catch(n){return null}}function Ne(){w=null}function Re(){if(w)return w;const e=S();if(e.mode==="sign"||e.mode==="cookie")return w=e.mode,w;const n=typeof location!="undefined"?location.origin:null;if(!n)return w="sign",w;const t=Pe(e.apiBase||n);return w=t&&t===n?"cookie":"sign",w}function b(){return Re()==="cookie"}function Ke(e,n){let t=S();throw t.onError&&n.showError&&t.onError({code:e.code,message:e.message}),new p(e.code,e.message)}let v,H=null;function Ce(){return v||(v=Promise.resolve().then(()=>H()).finally(()=>{v=null})),v}async function De(e,n,t,r,i){const o=S(),s=i||o.timeout,c=typeof AbortController!="undefined"?new AbortController:null,G=c?setTimeout(()=>c.abort(),s):null,E={method:n};if(e?E.credentials="same-origin":E.credentials="omit",E.signal=c?c.signal:void 0,typeof FormData!="undefined"&&r instanceof FormData?E.body=r:r!=null&&(E.headers={"Content-Type":"text/plain"},E.body=JSON.stringify(r)),!f.fetch)throw new p(l.NETWORK,"当前环境不支持 fetch");let x;try{x=await f.fetch(t,E)}catch(Z){throw G&&clearTimeout(G),Z&&Z.name==="AbortError"?new p(l.TIMEOUT,"请求超时,请重试"):new p(l.NETWORK,"网络异常,请检查网络后重试")}G&&clearTimeout(G);const z=await x.text();let g=null;if(z)try{g=JSON.parse(z)}catch(Z){g=null}if(!x.ok)throw new p(String(x.status),g&&g.message||z||"请求失败");if(!g||g.code===null||g.code===void 0)throw new p("500","响应格式异常");return g.code=String(g.code),g}async function d(e,n=null,t=null){const r=t||{};r.method||(r.method="POST"),r.showError=r.showError!==!1;let i=!0;/^https?:\/\/.+$/.test(e)?e.indexOf(S().apiBase)===-1&&(i=!1):e=S().apiBase+e,i&&!b()&&(i=!1);let o=e;if(!i&&u.ds&&u.token){let c=await A(_.BIZ,{method:r.method,path:new URL(e,location.href).pathname,ds:u.ds,ts:Date.now(),token:u.token},"domain");o.indexOf("?")>0?o=o+"&dsign="+c:o=o+"?dsign="+c}const s=await De(i,r.method,o,n,r.timeout);return s.code===l.LOGIN_TIMEOUT&&!r.skipRefresh&&H&&await Ce()?d(e,n,Object.assign({},r,{skipRefresh:!0})):(/^2.+/.test(s.code)||Ke(s,r),s)}async function Me(e,n,t,r){const i=new FormData;return Object.keys(t||{}).forEach(o=>{const s=t[o];s!=null&&i.append(o,s)}),i.append("file",n),d(e,i,r)}function _e(e){H=e}function Ae(){const e=typeof navigator!="undefined"&&navigator.userAgent||"",n={device_brand:ve(e),device_model:Ge(e),browser_name:xe(e),browser_version:Le(e),os_name:Be(e),os_version:Ve(e)},t=u.ds;return t&&(n.device_id=t),n}function y(e,n){const t=e.match(n);return t?t[1]:""}function ve(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 Ge(e){if(/iPhone/.test(e))return"iPhone";if(/iPad/.test(e))return"iPad";const n=e.match(/Android[^;]*;\s*([^;)]+?)(?:\s+Build[/)]|\))/i);return n?n[1].trim():/Macintosh/.test(e)?"Mac":""}function xe(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 Le(e){return/MicroMessenger\/([\d.]+)/.test(e)?y(e,/MicroMessenger\/([\d.]+)/):/Edg[A-Za-z]*\/([\d.]+)/.test(e)?y(e,/Edg[A-Za-z]*\/([\d.]+)/):/OPR\/([\d.]+)/.test(e)?y(e,/OPR\/([\d.]+)/):/Firefox\/([\d.]+)/.test(e)?y(e,/Firefox\/([\d.]+)/):/CriOS\/([\d.]+)/.test(e)?y(e,/CriOS\/([\d.]+)/):/Chrome\/([\d.]+)/.test(e)?y(e,/Chrome\/([\d.]+)/):/Version\/([\d.]+).*Safari/.test(e)?y(e,/Version\/([\d.]+)/):""}function Be(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 Ve(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 n=e.match(/(?:iPhone )?OS (\d+[_.]\d+(?:[_.]\d+)?)/);if(n)return n[1].replace(/_/g,".");if(/Android[ _]([\d.]+)/.test(e))return y(e,/Android[ _]([\d.]+)/);const t=e.match(/Mac OS X (\d+[_.]\d+(?:[_.]\d+)?)/);return t?t[1].replace(/_/g,"."):""}function ne(){if(location.origin)return location.origin.toLowerCase();try{return new URL(location.href).origin.toLowerCase()}catch(e){return""}}let W,te;function Y(e){const n=typeof location!="undefined"&&location.search||"";return(!W||te!==n)&&(te=n,W=new URLSearchParams(n)),W.get(e)}const m={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"};_e(qe);function Ue(e){return new Promise(n=>setTimeout(n,e))}let k=null;async function Fe(e,n){if(k)return k;const t=Ae();return k=(b()?je(e,n,t):He(e,n,t)).finally(()=>{k=null}),k}async function je(e,n,t){let r=await d(e,Object.assign({},n,{device:t}));return await $(r.data,!0,!0)}async function He(e,n,t){await we();const r=await j("sso"),i=await d(e,Object.assign({},n,{device:t,pk:r}));return await $(i.data,!0,!0)}let I;async function $(e,n=!1,t=!1){return I||(I=We(e,n,t).finally(()=>{I=null}),I)}async function We(e,n,t){if(e||(e={}),t){let r=S(),i;for(let o=0;o<r.pollMax;o++){if(i=b()?await re(e,n):await ie(e,n),i.code!==l.LOGIN_PENDING)return i;await Ue(r.pollInterval)}return i}else return b()?await re(e,n):await ie(e,n)}async function re(e,n){return await d(m.COOKIE.VERIFY,{redirect:Y("referer")},{skipRefresh:!0,showError:n})}async function ie(e,n){let t={method:"POST",path:m.SIGN.VERIFY,origin:ne(),ts:Date.now(),ds:u.ds,dna:Y("dna"),redirect:Y("referer")},r;if(e&&e.token)t.token=e.token,r=await A(_.VTT,t,"sso");else{if(!u.sid||!u.ds)throw new p(l.LOGIN_TIMEOUT,"未登录");t.token=u.sid,r=await A(_.VSD,t,"sso")}const i=await d(m.SIGN.VERIFY,{dsign:r,dna:t.dna,redirect:t.redirect},{skipRefresh:!0,showError:n});return i.code===l.OK&&(u.token=i.data.token,i.data.ds&&(u.ds=i.data.ds)),i}let P;async function Ye(e){return P||(P=$e(e).finally(()=>{P=null}),P)}async function $e(e){return b()?await ze():await Ze(e)}async function ze(e){return await d(m.COOKIE.TOKEN,null,{skipRefresh:!0})}async function Ze(e){let n={method:"POST",path:m.SIGN.TOKEN,origin:ne(),ts:Date.now(),ds:u.ds},t;if(e&&e.code)n.token=e.code,t=await A(_.TOK,n,"domain");else throw new p(l.LOGIN_TIMEOUT,"未登录");let r=await j("domain");const i=await d(m.SIGN.TOKEN,{dsign:t,pk:r},{skipRefresh:!0});return i.code===l.OK&&(u.token=i.data.token,i.data.ds&&(u.ds=i.data.ds)),i}let N;async function qe(){return N||(N=Xe().finally(()=>{N=null}),N)}async function Xe(){return b()?await Je():await Qe()}async function Je(){return await d(m.COOKIE.REFRESH,null,{skipRefresh:!0,showError:!1})}async function Qe(){const e=u.token,n=u.ds;if(!e||!n)throw new ae;const t=await d(m.SIGN.REFRESH,null,{skipRefresh:!0,showError:!1});return t.code===l.OK&&(t.data.ds&&(u.ds=t.data.ds),u.token=t.data.token),t}async function en(){(await d(m.LOGOUT)).code===l.OK&&u.clear()}function nn(e){const n=oe(e);if(Ne(),n.nativeGuard!=="off"){const t=ue();if(t.length){const r="[dplugin/core] 这些原生能力在模块加载前已被替换:"+t.join(", ")+"(签名/请求可能被中间人截获,检查是否有脚本抢跑)";if(n.nativeGuard==="error")throw new Error(r);typeof console!="undefined"&&console.warn(r)}}}a.createDomainPair=me,a.getDna=Ee,a.getPublicKey=j,a.login=Fe,a.logout=en,a.request=d,a.setup=nn,a.token=Ye,a.upload=Me,a.verify=$,Object.defineProperty(a,Symbol.toStringTag,{value:"Module"})}));
|
|
3
3
|
//# sourceMappingURL=index.umd.js.map
|