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