wx-auth-sdk 1.2.14 → 1.2.15

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/wx-auth.js CHANGED
@@ -211,7 +211,7 @@ const c = {
211
211
  <div class="wx-auth-overlay" ${r.required ? 'onclick="event.preventDefault()"' : ""}></div>
212
212
  <div class="wx-auth-content">
213
213
  <div class="wx-auth-header">
214
- <div class="wx-auth-title">微信认证</div>
214
+ <div class="wx-auth-title">微信登录</div>
215
215
  ${t}
216
216
  </div>
217
217
  <div class="wx-auth-body">
@@ -226,7 +226,7 @@ const c = {
226
226
  <!-- 验证码输入区域 -->
227
227
  <div>
228
228
  <div class="wx-auth-hint">2. 向公众号发送"验证码"获取</div>
229
- <div class="wx-auth-note">取消关注公众号后将被取消认证,请保持关注</div>
229
+ <div class="wx-auth-note">保持关注,即可一直自动登录</div>
230
230
  ${c.generateCodeInputs()}
231
231
  </div>
232
232
 
@@ -235,7 +235,7 @@ const c = {
235
235
 
236
236
  <!-- 按钮 -->
237
237
  <div class="wx-auth-actions">
238
- <button class="wx-auth-btn wx-auth-btn-primary" onclick="WxAuth.verifyCode()">验证</button>
238
+ <button class="wx-auth-btn wx-auth-btn-primary" onclick="WxAuth.verifyCode()">登录</button>
239
239
  </div>
240
240
  </div>
241
241
  </div>
@@ -404,16 +404,16 @@ const c = {
404
404
  const t = document.querySelector(
405
405
  ".wx-auth-btn-primary"
406
406
  );
407
- t && (t.disabled = !0, t.textContent = "验证中...");
407
+ t && (t.disabled = !0, t.textContent = "登录中...");
408
408
  try {
409
409
  const o = l ? `&siteId=${encodeURIComponent(l)}` : "", n = await c.request(
410
410
  `${r.apiBase}/api/auth/check?authToken=${e}${o}`
411
411
  );
412
- n.authenticated ? (n.token ? c.setToken(n.token) : n.user && n.user.openid && c.setCookie("wxauth-openid", n.user.openid), t && (t.textContent = "✅ 成功", t.style.background = "#07C160"), setTimeout(() => {
412
+ n.authenticated ? (n.token ? c.setToken(n.token) : n.user && n.user.openid && c.setCookie("wxauth-openid", n.user.openid), t && (t.textContent = "✅ 已登录", t.style.background = "#07C160"), setTimeout(() => {
413
413
  this.onVerified(n.user), this.close(!0);
414
- }, 500)) : (d.showMessage("验证码错误或已过期", "error"), d.clearCodeInputs(), t && (t.disabled = !1, t.textContent = "验证"));
414
+ }, 500)) : (d.showMessage("验证码错误或已过期", "error"), d.clearCodeInputs(), t && (t.disabled = !1, t.textContent = "登录"));
415
415
  } catch {
416
- d.showMessage("验证失败,请重试", "error"), t && (t.disabled = !1, t.textContent = "验证");
416
+ d.showMessage("登录失败,请重试", "error"), t && (t.disabled = !1, t.textContent = "登录");
417
417
  }
418
418
  },
419
419
  // 关闭弹窗
@@ -3,7 +3,7 @@
3
3
  <div class="wx-auth-overlay" ${i.required?'onclick="event.preventDefault()"':""}></div>
4
4
  <div class="wx-auth-content">
5
5
  <div class="wx-auth-header">
6
- <div class="wx-auth-title">微信认证</div>
6
+ <div class="wx-auth-title">微信登录</div>
7
7
  ${t}
8
8
  </div>
9
9
  <div class="wx-auth-body">
@@ -18,7 +18,7 @@
18
18
  <!-- 验证码输入区域 -->
19
19
  <div>
20
20
  <div class="wx-auth-hint">2. 向公众号发送"验证码"获取</div>
21
- <div class="wx-auth-note">取消关注公众号后将被取消认证,请保持关注</div>
21
+ <div class="wx-auth-note">保持关注,即可一直自动登录</div>
22
22
  ${s.generateCodeInputs()}
23
23
  </div>
24
24
 
@@ -27,8 +27,8 @@
27
27
 
28
28
  <!-- 按钮 -->
29
29
  <div class="wx-auth-actions">
30
- <button class="wx-auth-btn wx-auth-btn-primary" onclick="WxAuth.verifyCode()">验证</button>
30
+ <button class="wx-auth-btn wx-auth-btn-primary" onclick="WxAuth.verifyCode()">登录</button>
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((c,w)=>{e[w]&&(e[w].value=c)}),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"),i.required?e.classList.add("wx-auth-required"):e.classList.add("wx-auth-optional")):e=this.createModal(),e.style.display="flex",u.isOpen=!0,h.enable({modalId:"wx-auth-modal",getState:()=>u,onRestore:()=>{this.createModal(),i.qrcodeUrl&&this.setQrCode(i.qrcodeUrl);const t=document.querySelector(".wx-auth-wechat-name");t&&i.wechatName&&(t.textContent=`"${i.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,h.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(i={...m,...e},!i.apiBase&&typeof window<"u"&&(i.apiBase=window.location.origin),!i.apiBase){console.error("[WxAuth] apiBase is required");return}if(typeof window<"u"){const t=document.referrer;if(t)try{d=new URL(t).hostname}catch{}!d&&window.location.hostname&&(d=window.location.hostname),d||(d="default"),console.log("[WxAuth] 自动获取 siteId:",d)}console.log("[WxAuth] SDK initialized",i),typeof window<"u"&&(i.silent?this.silentCheck():this.autoCheck())},async silentCheck(){const e=s.getToken(),t=s.getCookie("wxauth-openid");if(!(e||t))return!1;const n=d?`&siteId=${encodeURIComponent(d)}`:"",a=e?`token=${encodeURIComponent(e)}`:`openid=${encodeURIComponent(t||"")}`;try{const c=await s.request(`${i.apiBase}/api/auth/check?${a}${n}`);return c.authenticated?(c.token&&(s.setToken(c.token),t&&s.deleteCookie("wxauth-openid")),this.onVerified(c.user),!0):(s.clearToken(),!1)}catch(c){return console.error("[WxAuth] silentCheck 请求失败:",c),!1}},async autoCheck(){const e=s.getToken(),t=s.getCookie("wxauth-openid");if(!(e||t))return this.showAuthModal(),!1;const n=d?`&siteId=${encodeURIComponent(d)}`:"",a=e?`token=${encodeURIComponent(e)}`:`openid=${encodeURIComponent(t||"")}`;try{const c=await s.request(`${i.apiBase}/api/auth/check?${a}${n}`);return c.authenticated?(console.log("[WxAuth] 自动认证成功(凭证)"),c.token&&(s.setToken(c.token),t&&s.deleteCookie("wxauth-openid")),this.onVerified(c.user),!0):(s.clearToken(),this.showAuthModal(),!1)}catch(c){return console.error("[WxAuth] 自动验证失败:",c),this.showAuthModal(),!1}},async showAuthModal(){l.show();try{const t=d?`?siteId=${encodeURIComponent(d)}`:"",o=await s.request(`${i.apiBase}/api/sdk/config${t}`);o.wechatName&&!i.wechatName&&(i.wechatName=o.wechatName),o.qrcodeUrl&&!i.qrcodeUrl&&(i.qrcodeUrl=o.qrcodeUrl)}catch(t){console.warn("[WxAuth] 获取后端配置失败,使用默认配置",t)}i.qrcodeUrl&&l.setQrCode(i.qrcodeUrl);const e=document.querySelector(".wx-auth-wechat-name");e&&i.wechatName&&(e.textContent=`"${i.wechatName}"`),setTimeout(()=>{const t=document.querySelector(".wx-auth-input");t&&t.focus()},100)},async requireAuth(){const e=s.getToken(),t=s.getCookie("wxauth-openid");if(e||t)try{const n=d?`&siteId=${encodeURIComponent(d)}`:"",a=e?`token=${encodeURIComponent(e)}`:`openid=${encodeURIComponent(t||"")}`,c=await s.request(`${i.apiBase}/api/auth/check?${a}${n}`);if(c.authenticated)return console.log("[WxAuth] 已认证(凭证)"),c.token&&(s.setToken(c.token),t&&s.deleteCookie("wxauth-openid")),this.onVerified(c.user),!0;s.clearToken()}catch(n){console.error("[WxAuth] 凭证检查失败:",n)}return await this.showAuthModal(),new Promise(n=>{u.resolveAuth=n})},async verifyCode(){const e=l.getVerifyCode();if(!e||e.length!==6){l.showMessage("请输入6位验证码","error");return}const t=document.querySelector(".wx-auth-btn-primary");t&&(t.disabled=!0,t.textContent="验证中...");try{const o=d?`&siteId=${encodeURIComponent(d)}`:"",n=await s.request(`${i.apiBase}/api/auth/check?authToken=${e}${o}`);n.authenticated?(n.token?s.setToken(n.token):n.user&&n.user.openid&&s.setCookie("wxauth-openid",n.user.openid),t&&(t.textContent="✅ 成功",t.style.background="#07C160"),setTimeout(()=>{this.onVerified(n.user),this.close(!0)},500)):(l.showMessage("验证码错误或已过期","error"),l.clearCodeInputs(),t&&(t.disabled=!1,t.textContent="验证"))}catch{l.showMessage("验证失败,请重试","error"),t&&(t.disabled=!1,t.textContent="验证")}},close(e=!1){l.hide(),u.resolveAuth&&(u.resolveAuth(!1),u.resolveAuth=null),!e&&typeof i.onClose=="function"&&i.onClose()},clearToken(){s.clearToken()},onVerified(e){console.log("[WxAuth] 验证成功",e),typeof i.onVerified=="function"&&i.onVerified(e),u.resolveAuth&&(u.resolveAuth(!0),u.resolveAuth=null)},onError(e){console.error("[WxAuth] 错误",e),typeof i.onError=="function"&&i.onError(e)}};typeof window<"u"&&(window.WxAuth=Object.freeze(x)),f.WxAuth=x,Object.defineProperty(f,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((c,w)=>{e[w]&&(e[w].value=c)}),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"),i.required?e.classList.add("wx-auth-required"):e.classList.add("wx-auth-optional")):e=this.createModal(),e.style.display="flex",u.isOpen=!0,h.enable({modalId:"wx-auth-modal",getState:()=>u,onRestore:()=>{this.createModal(),i.qrcodeUrl&&this.setQrCode(i.qrcodeUrl);const t=document.querySelector(".wx-auth-wechat-name");t&&i.wechatName&&(t.textContent=`"${i.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,h.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(i={...m,...e},!i.apiBase&&typeof window<"u"&&(i.apiBase=window.location.origin),!i.apiBase){console.error("[WxAuth] apiBase is required");return}if(typeof window<"u"){const t=document.referrer;if(t)try{d=new URL(t).hostname}catch{}!d&&window.location.hostname&&(d=window.location.hostname),d||(d="default"),console.log("[WxAuth] 自动获取 siteId:",d)}console.log("[WxAuth] SDK initialized",i),typeof window<"u"&&(i.silent?this.silentCheck():this.autoCheck())},async silentCheck(){const e=s.getToken(),t=s.getCookie("wxauth-openid");if(!(e||t))return!1;const n=d?`&siteId=${encodeURIComponent(d)}`:"",a=e?`token=${encodeURIComponent(e)}`:`openid=${encodeURIComponent(t||"")}`;try{const c=await s.request(`${i.apiBase}/api/auth/check?${a}${n}`);return c.authenticated?(c.token&&(s.setToken(c.token),t&&s.deleteCookie("wxauth-openid")),this.onVerified(c.user),!0):(s.clearToken(),!1)}catch(c){return console.error("[WxAuth] silentCheck 请求失败:",c),!1}},async autoCheck(){const e=s.getToken(),t=s.getCookie("wxauth-openid");if(!(e||t))return this.showAuthModal(),!1;const n=d?`&siteId=${encodeURIComponent(d)}`:"",a=e?`token=${encodeURIComponent(e)}`:`openid=${encodeURIComponent(t||"")}`;try{const c=await s.request(`${i.apiBase}/api/auth/check?${a}${n}`);return c.authenticated?(console.log("[WxAuth] 自动认证成功(凭证)"),c.token&&(s.setToken(c.token),t&&s.deleteCookie("wxauth-openid")),this.onVerified(c.user),!0):(s.clearToken(),this.showAuthModal(),!1)}catch(c){return console.error("[WxAuth] 自动验证失败:",c),this.showAuthModal(),!1}},async showAuthModal(){l.show();try{const t=d?`?siteId=${encodeURIComponent(d)}`:"",o=await s.request(`${i.apiBase}/api/sdk/config${t}`);o.wechatName&&!i.wechatName&&(i.wechatName=o.wechatName),o.qrcodeUrl&&!i.qrcodeUrl&&(i.qrcodeUrl=o.qrcodeUrl)}catch(t){console.warn("[WxAuth] 获取后端配置失败,使用默认配置",t)}i.qrcodeUrl&&l.setQrCode(i.qrcodeUrl);const e=document.querySelector(".wx-auth-wechat-name");e&&i.wechatName&&(e.textContent=`"${i.wechatName}"`),setTimeout(()=>{const t=document.querySelector(".wx-auth-input");t&&t.focus()},100)},async requireAuth(){const e=s.getToken(),t=s.getCookie("wxauth-openid");if(e||t)try{const n=d?`&siteId=${encodeURIComponent(d)}`:"",a=e?`token=${encodeURIComponent(e)}`:`openid=${encodeURIComponent(t||"")}`,c=await s.request(`${i.apiBase}/api/auth/check?${a}${n}`);if(c.authenticated)return console.log("[WxAuth] 已认证(凭证)"),c.token&&(s.setToken(c.token),t&&s.deleteCookie("wxauth-openid")),this.onVerified(c.user),!0;s.clearToken()}catch(n){console.error("[WxAuth] 凭证检查失败:",n)}return await this.showAuthModal(),new Promise(n=>{u.resolveAuth=n})},async verifyCode(){const e=l.getVerifyCode();if(!e||e.length!==6){l.showMessage("请输入6位验证码","error");return}const t=document.querySelector(".wx-auth-btn-primary");t&&(t.disabled=!0,t.textContent="登录中...");try{const o=d?`&siteId=${encodeURIComponent(d)}`:"",n=await s.request(`${i.apiBase}/api/auth/check?authToken=${e}${o}`);n.authenticated?(n.token?s.setToken(n.token):n.user&&n.user.openid&&s.setCookie("wxauth-openid",n.user.openid),t&&(t.textContent="✅ 已登录",t.style.background="#07C160"),setTimeout(()=>{this.onVerified(n.user),this.close(!0)},500)):(l.showMessage("验证码错误或已过期","error"),l.clearCodeInputs(),t&&(t.disabled=!1,t.textContent="登录"))}catch{l.showMessage("登录失败,请重试","error"),t&&(t.disabled=!1,t.textContent="登录")}},close(e=!1){l.hide(),u.resolveAuth&&(u.resolveAuth(!1),u.resolveAuth=null),!e&&typeof i.onClose=="function"&&i.onClose()},clearToken(){s.clearToken()},onVerified(e){console.log("[WxAuth] 验证成功",e),typeof i.onVerified=="function"&&i.onVerified(e),u.resolveAuth&&(u.resolveAuth(!0),u.resolveAuth=null)},onError(e){console.error("[WxAuth] 错误",e),typeof i.onError=="function"&&i.onError(e)}};typeof window<"u"&&(window.WxAuth=Object.freeze(x)),f.WxAuth=x,Object.defineProperty(f,Symbol.toStringTag,{value:"Module"})}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wx-auth-sdk",
3
- "version": "1.2.14",
3
+ "version": "1.2.15",
4
4
  "description": "微信订阅号认证 SDK - 极简版",
5
5
  "type": "module",
6
6
  "main": "./dist/wx-auth.umd.js",