官方api:https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html#1
需求:静默授权,获取用户openid
实现:
第一步:用户同意授权,获取code
注意:要在前端获取到code,传给后端
useEffect(() => {
getCode()
}, [])
const getCode = () => {
//第一步:用户同意授权,获取code
let searchParams = new URLSearchParams(window.location.search);
let urlCode = searchParams.get('code');
if (!urlCode) {
const appid = 'XXXXXXXXXXXXX'; // 微信授权的公众号的AppId
const redirectUrl = encodeURIComponent(window.location.href);
window.location.href = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appid}&redirect_uri