微信公众号获取openid

837 阅读1分钟

一.第一步

注意:

1.redirect_uri 需要转 encodeURIComponent,并且地址是全地址 2.通过 window.location.href 3.重定向地址会自动带上 code,state

// 重定向,获取code

let url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${APPID}
&redirect_uri=${encodeURIComponent(REDIRECT_URI)}
&response_type=code
&scope=${SCOPE}
&state=${STATE}#wechat_redirect`;
window.location.href=url;

官方参考:developers.weixin.qq.com/doc/offiacc…