UNPKG

wechat-oauth-by-frontend

Version:

a simple function for wechat oauth by frontend

2 lines (1 loc) 1.33 kB
(function(r,c){typeof exports=="object"&&typeof module<"u"?c(exports):typeof define=="function"&&define.amd?define(["exports"],c):(r=typeof globalThis<"u"?globalThis:r||self,c(r["wechat-oa-auth"]={}))})(this,function(r){"use strict";let[,c]=location.search.split("?");const l=URLSearchParams?new URLSearchParams(c):null,a={},u=e=>{if(e){const t=n=>l?l.has(n)?l.get(n):null:(s=>{const f=new RegExp("(^|&)"+s+"=([^&]*)(&|$)"),i=c.match(f);return i!=null?unescape(i[2]):null})(n),o=n=>(n.forEach(s=>a[s]=decodeURIComponent(t(s))),a);return Array.isArray(e)?o(e):t(e)}else return c.split("&").forEach(t=>{const[o,n]=t.split("=");a[o]=decodeURIComponent(n)}),a},h=u("code")||"",d=u("state")||"";async function p({appid:e,redirect_uri:t=encodeURIComponent(location.href),scope:o="snsapi_userinfo"}){const{default:n}=await Promise.resolve().then(()=>g),i="https://open.weixin.qq.com/connect/oauth2/authorize"+n({appid:e,redirect_uri:t,response_type:"code",scope:o,state:d})+"#wechat_redirect";location.href=i}const g=Object.freeze(Object.defineProperty({__proto__:null,default:e=>{if(!Object.keys(e).length)return"";let t="";for(const o in e)t+=`&${o}=${e[o]}`;return t.replace(/^&/,"?")}},Symbol.toStringTag,{value:"Module"}));r.code=h,r.getCode=p,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});