wx-auth-sdk 1.2.17 → 1.2.18
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.d.ts +1 -0
- package/dist/wx-auth.js +22 -0
- package/dist/wx-auth.umd.js +1 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
package/dist/wx-auth.js
CHANGED
|
@@ -431,6 +431,28 @@ const a = {
|
|
|
431
431
|
clearToken() {
|
|
432
432
|
a.clearToken();
|
|
433
433
|
},
|
|
434
|
+
// 服务端注销(2026-08-26 方案 C):吊销当前 token + 清本地
|
|
435
|
+
// 调用 POST {apiBase}/api/auth/logout(body.token),后端把 token 加入
|
|
436
|
+
// 吊销黑名单。此后任意子域/设备的 localStorage 备份都无法再恢复登录态
|
|
437
|
+
// (根治「A 域退出、B 域还能用」的双写恢复问题)。
|
|
438
|
+
// 幂等安全:网络失败 /* 后端不可达 */ 时静默降级为仅本地清理(clearToken),
|
|
439
|
+
// 不阻塞退出流程;token 无效也会返回 success(本地照常清理)。
|
|
440
|
+
async revoke() {
|
|
441
|
+
const e = a.getToken();
|
|
442
|
+
if (e)
|
|
443
|
+
try {
|
|
444
|
+
const t = r.apiBase && !/\/$/.test(r.apiBase) ? r.apiBase : "https://wx-auth.shenzjd.com", o = await fetch(`${t}/api/auth/logout`, {
|
|
445
|
+
method: "POST",
|
|
446
|
+
headers: { "Content-Type": "application/json" },
|
|
447
|
+
body: JSON.stringify({ token: e }),
|
|
448
|
+
credentials: "include"
|
|
449
|
+
});
|
|
450
|
+
o.ok || console.warn("[WxAuth] revoke 请求失败,降级本地清理", o.status);
|
|
451
|
+
} catch (t) {
|
|
452
|
+
console.warn("[WxAuth] revoke 网络异常,降级本地清理", t);
|
|
453
|
+
}
|
|
454
|
+
return this.clearToken(), !0;
|
|
455
|
+
},
|
|
434
456
|
// 验证成功回调
|
|
435
457
|
onVerified(e) {
|
|
436
458
|
console.log("[WxAuth] 验证成功", e), typeof r.onVerified == "function" && r.onVerified(e), l.resolveAuth && (l.resolveAuth(!0), l.resolveAuth = null);
|
package/dist/wx-auth.umd.js
CHANGED
|
@@ -31,4 +31,4 @@
|
|
|
31
31
|
</div>
|
|
32
32
|
</div>
|
|
33
33
|
</div>
|
|
34
|
-
`,document.body.appendChild(e),this.bindInputEvents(),e},bindInputEvents(){setTimeout(()=>{const e=document.querySelectorAll(".wx-auth-input");e.length&&e.forEach((t,o)=>{t.addEventListener("input",n=>{const a=n.target.value.replace(/\D/g,"");n.target.value=a,a&&o<5&&e[o+1].focus(),o===5&&a&&setTimeout(()=>x.verifyCode(),300)}),t.addEventListener("keydown",n=>{const a=n.target;n.key==="Backspace"&&!a.value&&o>0?e[o-1].focus():n.key==="ArrowLeft"&&o>0?(n.preventDefault(),e[o-1].focus()):n.key==="ArrowRight"&&o<5&&(n.preventDefault(),e[o+1].focus())}),t.addEventListener("paste",n=>{n.preventDefault();const a=n.clipboardData?.getData("text").replace(/\D/g,"").slice(0,6);a&&(a.split("").forEach((p,w)=>{e[w]&&(e[w].value=p)}),a.length===6&&setTimeout(()=>x.verifyCode(),300))})})},100)},show(){let e=document.getElementById("wx-auth-modal");e?(e.classList.remove("wx-auth-required","wx-auth-optional"),r.required?e.classList.add("wx-auth-required"):e.classList.add("wx-auth-optional")):e=this.createModal(),e.style.display="flex",u.isOpen=!0,l.enable({modalId:"wx-auth-modal",getState:()=>u,onRestore:()=>{this.createModal(),r.qrcodeUrl&&this.setQrCode(r.qrcodeUrl);const t=document.querySelector(".wx-auth-wechat-name");t&&r.wechatName&&(t.textContent=`"${r.wechatName}"`),this.bindInputEvents(),setTimeout(()=>{const o=document.querySelector(".wx-auth-input");o&&o.focus()},100)}})},hide(){const e=document.getElementById("wx-auth-modal");e&&(e.style.display="none",u.isOpen=!1,l.disable())},setQrCode(e){if(!e)return;const t=document.querySelector(".wx-auth-qrcode");t&&(t.src=e,t.style.display="block")},showMessage(e,t="info"){const o=document.querySelector(".wx-auth-message");o&&(o.textContent=e,o.className=`wx-auth-message wx-auth-message-${t}`,o.style.display="block",setTimeout(()=>{o.textContent===e&&(o.style.display="none")},3e3))},getVerifyCode(){const e=document.querySelectorAll(".wx-auth-input");return e.length?Array.from(e).map(t=>t.value).join(""):""},clearCodeInputs(){const e=document.querySelectorAll(".wx-auth-input");e.forEach(t=>t.value=""),e[0]&&e[0].focus()}},x={init(e={}){if(r={...m,...e},!r.apiBase&&typeof window<"u"&&(r.apiBase=window.location.origin),!r.apiBase){console.error("[WxAuth] apiBase is required");return}if(typeof window<"u"){const t=document.referrer;if(t)try{c=new URL(t).hostname}catch{}!c&&window.location.hostname&&(c=window.location.hostname),c||(c="default"),console.log("[WxAuth] 自动获取 siteId:",c)}console.log("[WxAuth] SDK initialized",r),typeof window<"u"&&(r.silent?this.silentCheck():this.autoCheck())},async silentCheck(){const e=s.getToken();if(!e)return!1;const t=c?`&siteId=${encodeURIComponent(c)}`:"",o=`token=${encodeURIComponent(e)}`;try{const n=await s.request(`${r.apiBase}/api/auth/check?${o}${t}`);return n.authenticated?(n.token&&s.setToken(n.token),this.onVerified(n.user),!0):(s.clearToken(),!1)}catch(n){return console.error("[WxAuth] silentCheck 请求失败:",n),!1}},async autoCheck(){const e=s.getToken();if(!e)return this.showAuthModal(),!1;const t=c?`&siteId=${encodeURIComponent(c)}`:"",o=`token=${encodeURIComponent(e)}`;try{const n=await s.request(`${r.apiBase}/api/auth/check?${o}${t}`);return n.authenticated?(console.log("[WxAuth] 自动认证成功(凭证)"),n.token&&s.setToken(n.token),this.onVerified(n.user),!0):(s.clearToken(),this.showAuthModal(),!1)}catch(n){return console.error("[WxAuth] 自动验证失败:",n),this.showAuthModal(),!1}},async showAuthModal(){d.show();try{const t=c?`?siteId=${encodeURIComponent(c)}`:"",o=await s.request(`${r.apiBase}/api/sdk/config${t}`);o.wechatName&&!r.wechatName&&(r.wechatName=o.wechatName),o.qrcodeUrl&&!r.qrcodeUrl&&(r.qrcodeUrl=o.qrcodeUrl)}catch(t){console.warn("[WxAuth] 获取后端配置失败,使用默认配置",t)}r.qrcodeUrl&&d.setQrCode(r.qrcodeUrl);const e=document.querySelector(".wx-auth-wechat-name");e&&r.wechatName&&(e.textContent=`"${r.wechatName}"`),setTimeout(()=>{const t=document.querySelector(".wx-auth-input");t&&t.focus()},100)},async requireAuth(){const e=s.getToken();if(e)try{const t=c?`&siteId=${encodeURIComponent(c)}`:"",o=`token=${encodeURIComponent(e)}`,n=await s.request(`${r.apiBase}/api/auth/check?${o}${t}`);if(n.authenticated)return console.log("[WxAuth] 已认证(凭证)"),n.token&&s.setToken(n.token),this.onVerified(n.user),!0;s.clearToken()}catch(t){console.error("[WxAuth] 凭证检查失败:",t)}return await this.showAuthModal(),new Promise(t=>{u.resolveAuth=t})},async verifyCode(){const e=d.getVerifyCode();if(!e||e.length!==6){d.showMessage("请输入6位验证码","error");return}const t=document.querySelector(".wx-auth-btn-primary");t&&(t.disabled=!0,t.textContent="登录中...");try{const o=c?`&siteId=${encodeURIComponent(c)}`:"",n=await s.request(`${r.apiBase}/api/auth/check?authToken=${e}${o}`);n.authenticated?(n.token&&s.setToken(n.token),t&&(t.textContent="✅ 已登录",t.style.background="#07C160"),setTimeout(()=>{this.onVerified(n.user),this.close(!0)},500)):(d.showMessage("验证码错误或已过期","error"),d.clearCodeInputs(),t&&(t.disabled=!1,t.textContent="登录"))}catch{d.showMessage("登录失败,请重试","error"),t&&(t.disabled=!1,t.textContent="登录")}},close(e=!1){d.hide(),u.resolveAuth&&(u.resolveAuth(!1),u.resolveAuth=null),!e&&typeof r.onClose=="function"&&r.onClose()},clearToken(){s.clearToken()},onVerified(e){console.log("[WxAuth] 验证成功",e),typeof r.onVerified=="function"&&r.onVerified(e),u.resolveAuth&&(u.resolveAuth(!0),u.resolveAuth=null)},onError(e){console.error("[WxAuth] 错误",e),typeof r.onError=="function"&&r.onError(e)}};typeof window<"u"&&(window.WxAuth=Object.freeze(x)),h.WxAuth=x,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})}));
|
|
34
|
+
`,document.body.appendChild(e),this.bindInputEvents(),e},bindInputEvents(){setTimeout(()=>{const e=document.querySelectorAll(".wx-auth-input");e.length&&e.forEach((t,o)=>{t.addEventListener("input",n=>{const a=n.target.value.replace(/\D/g,"");n.target.value=a,a&&o<5&&e[o+1].focus(),o===5&&a&&setTimeout(()=>x.verifyCode(),300)}),t.addEventListener("keydown",n=>{const a=n.target;n.key==="Backspace"&&!a.value&&o>0?e[o-1].focus():n.key==="ArrowLeft"&&o>0?(n.preventDefault(),e[o-1].focus()):n.key==="ArrowRight"&&o<5&&(n.preventDefault(),e[o+1].focus())}),t.addEventListener("paste",n=>{n.preventDefault();const a=n.clipboardData?.getData("text").replace(/\D/g,"").slice(0,6);a&&(a.split("").forEach((p,w)=>{e[w]&&(e[w].value=p)}),a.length===6&&setTimeout(()=>x.verifyCode(),300))})})},100)},show(){let e=document.getElementById("wx-auth-modal");e?(e.classList.remove("wx-auth-required","wx-auth-optional"),r.required?e.classList.add("wx-auth-required"):e.classList.add("wx-auth-optional")):e=this.createModal(),e.style.display="flex",u.isOpen=!0,l.enable({modalId:"wx-auth-modal",getState:()=>u,onRestore:()=>{this.createModal(),r.qrcodeUrl&&this.setQrCode(r.qrcodeUrl);const t=document.querySelector(".wx-auth-wechat-name");t&&r.wechatName&&(t.textContent=`"${r.wechatName}"`),this.bindInputEvents(),setTimeout(()=>{const o=document.querySelector(".wx-auth-input");o&&o.focus()},100)}})},hide(){const e=document.getElementById("wx-auth-modal");e&&(e.style.display="none",u.isOpen=!1,l.disable())},setQrCode(e){if(!e)return;const t=document.querySelector(".wx-auth-qrcode");t&&(t.src=e,t.style.display="block")},showMessage(e,t="info"){const o=document.querySelector(".wx-auth-message");o&&(o.textContent=e,o.className=`wx-auth-message wx-auth-message-${t}`,o.style.display="block",setTimeout(()=>{o.textContent===e&&(o.style.display="none")},3e3))},getVerifyCode(){const e=document.querySelectorAll(".wx-auth-input");return e.length?Array.from(e).map(t=>t.value).join(""):""},clearCodeInputs(){const e=document.querySelectorAll(".wx-auth-input");e.forEach(t=>t.value=""),e[0]&&e[0].focus()}},x={init(e={}){if(r={...m,...e},!r.apiBase&&typeof window<"u"&&(r.apiBase=window.location.origin),!r.apiBase){console.error("[WxAuth] apiBase is required");return}if(typeof window<"u"){const t=document.referrer;if(t)try{c=new URL(t).hostname}catch{}!c&&window.location.hostname&&(c=window.location.hostname),c||(c="default"),console.log("[WxAuth] 自动获取 siteId:",c)}console.log("[WxAuth] SDK initialized",r),typeof window<"u"&&(r.silent?this.silentCheck():this.autoCheck())},async silentCheck(){const e=s.getToken();if(!e)return!1;const t=c?`&siteId=${encodeURIComponent(c)}`:"",o=`token=${encodeURIComponent(e)}`;try{const n=await s.request(`${r.apiBase}/api/auth/check?${o}${t}`);return n.authenticated?(n.token&&s.setToken(n.token),this.onVerified(n.user),!0):(s.clearToken(),!1)}catch(n){return console.error("[WxAuth] silentCheck 请求失败:",n),!1}},async autoCheck(){const e=s.getToken();if(!e)return this.showAuthModal(),!1;const t=c?`&siteId=${encodeURIComponent(c)}`:"",o=`token=${encodeURIComponent(e)}`;try{const n=await s.request(`${r.apiBase}/api/auth/check?${o}${t}`);return n.authenticated?(console.log("[WxAuth] 自动认证成功(凭证)"),n.token&&s.setToken(n.token),this.onVerified(n.user),!0):(s.clearToken(),this.showAuthModal(),!1)}catch(n){return console.error("[WxAuth] 自动验证失败:",n),this.showAuthModal(),!1}},async showAuthModal(){d.show();try{const t=c?`?siteId=${encodeURIComponent(c)}`:"",o=await s.request(`${r.apiBase}/api/sdk/config${t}`);o.wechatName&&!r.wechatName&&(r.wechatName=o.wechatName),o.qrcodeUrl&&!r.qrcodeUrl&&(r.qrcodeUrl=o.qrcodeUrl)}catch(t){console.warn("[WxAuth] 获取后端配置失败,使用默认配置",t)}r.qrcodeUrl&&d.setQrCode(r.qrcodeUrl);const e=document.querySelector(".wx-auth-wechat-name");e&&r.wechatName&&(e.textContent=`"${r.wechatName}"`),setTimeout(()=>{const t=document.querySelector(".wx-auth-input");t&&t.focus()},100)},async requireAuth(){const e=s.getToken();if(e)try{const t=c?`&siteId=${encodeURIComponent(c)}`:"",o=`token=${encodeURIComponent(e)}`,n=await s.request(`${r.apiBase}/api/auth/check?${o}${t}`);if(n.authenticated)return console.log("[WxAuth] 已认证(凭证)"),n.token&&s.setToken(n.token),this.onVerified(n.user),!0;s.clearToken()}catch(t){console.error("[WxAuth] 凭证检查失败:",t)}return await this.showAuthModal(),new Promise(t=>{u.resolveAuth=t})},async verifyCode(){const e=d.getVerifyCode();if(!e||e.length!==6){d.showMessage("请输入6位验证码","error");return}const t=document.querySelector(".wx-auth-btn-primary");t&&(t.disabled=!0,t.textContent="登录中...");try{const o=c?`&siteId=${encodeURIComponent(c)}`:"",n=await s.request(`${r.apiBase}/api/auth/check?authToken=${e}${o}`);n.authenticated?(n.token&&s.setToken(n.token),t&&(t.textContent="✅ 已登录",t.style.background="#07C160"),setTimeout(()=>{this.onVerified(n.user),this.close(!0)},500)):(d.showMessage("验证码错误或已过期","error"),d.clearCodeInputs(),t&&(t.disabled=!1,t.textContent="登录"))}catch{d.showMessage("登录失败,请重试","error"),t&&(t.disabled=!1,t.textContent="登录")}},close(e=!1){d.hide(),u.resolveAuth&&(u.resolveAuth(!1),u.resolveAuth=null),!e&&typeof r.onClose=="function"&&r.onClose()},clearToken(){s.clearToken()},async revoke(){const e=s.getToken();if(e)try{const t=r.apiBase&&!/\/$/.test(r.apiBase)?r.apiBase:"https://wx-auth.shenzjd.com",o=await fetch(`${t}/api/auth/logout`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({token:e}),credentials:"include"});o.ok||console.warn("[WxAuth] revoke 请求失败,降级本地清理",o.status)}catch(t){console.warn("[WxAuth] revoke 网络异常,降级本地清理",t)}return this.clearToken(),!0},onVerified(e){console.log("[WxAuth] 验证成功",e),typeof r.onVerified=="function"&&r.onVerified(e),u.resolveAuth&&(u.resolveAuth(!0),u.resolveAuth=null)},onError(e){console.error("[WxAuth] 错误",e),typeof r.onError=="function"&&r.onError(e)}};typeof window<"u"&&(window.WxAuth=Object.freeze(x)),h.WxAuth=x,Object.defineProperty(h,Symbol.toStringTag,{value:"Module"})}));
|