微信支付宝网页应用授权 获取用户openid、昵称、头像
微信文档支付宝文档!DOCTYPE html html langzh-CN head meta charsetutf-8 meta http-equivX-UA-Compatible contentIEedge meta http-equivExpires content0 / meta http-equivPragma contentno-cache / meta http-equivCache-Control contentno-cache, no-store, must-revalidate / title会员授权/title /head body script // 跳转示例 // const url https://xxx/AUTH/?returnUrl${encodeURIComponent(window.location.href)}; // window.location.href url; // 统一接口域名 const API_BASE https://xxx.com; // 统一KEY const AUTH_KEY 123; window.onload function () { getOfficial(); }; /** * 会员授权主方法微信 支付宝 */ async function getOfficial() { const url window.location.href; if (!url.includes(openid) !url.includes(code)) { const returnUrl url.split(returnUrl)[1]; window.localStorage.setItem(auth-redirect-url, decodeURIComponent(returnUrl)); } // // 1. 如果已经拿到 openid直接存并跳转 // if (url.includes(openid)) { try { const openid url.split(openid)[1]; window.localStorage.setItem(xls_vf_Token, openid); // 读取跳转地址 const redirectUrl window.localStorage.getItem(auth-redirect-url); if (redirectUrl) { const list redirectUrl.split(#); return window.location.href ${list[0]}?openid${openid}#${list[1] || }; } alert(授权成功); } catch (e) { alert(授权失败 e.message); } return; } // // 2. 微信环境授权 // if (/MicroMessenger/.test(navigator.userAgent)) { // 已拿到微信 code换取信息 if (url.includes(code)) { const query url.split(?)[1]; window.location.href ${API_BASE}/member/api/v1/admin/MemberPayWechat/userInfo?${query}; } // 无 code跳微信授权页 else { const uri window.location.origin window.location.pathname; window.location.href ${API_BASE}/member/api/v1/admin/MemberPayWechat/authorize?returnUrl${encodeURIComponent(uri)}key${AUTH_KEY}; } return; } // // 3. 支付宝环境授权 // if (/AlipayClient|AliApp/i.test(navigator.userAgent)) { if (url.includes(auth_code)) { // 自己把 auth_code 丢到后端换 token const authCode new URL(url).searchParams.get(auth_code); const apiUrl ${API_BASE}/api/xx/aliInfo?code${authCode}key${AUTH_KEY}; const result await (await fetch(apiUrl)).json(); const token result?.data; if (token) { window.localStorage.setItem(xls_vf_Token, token); const redirectUrl window.localStorage.getItem(auth-redirect-url); if (redirectUrl) { const list redirectUrl.split(#); return window.location.href ${list[0]}?openid${token}#${list[1] || }; } } alert(授权失败); return; } const appId 2021xxx; const uri window.location.href.split(returnUrl)[0]; const redirectUri encodeURIComponent(uri); // 回到当前页 window.location.href https://openauth.alipay.com/oauth2/publicAppAuthorize.htm?app_id${appId}scopeauth_userredirect_uri${redirectUri}; return; } // // 4. 非微信/支付宝环境 // alert(授权环境不支持请使用微信或支付宝扫码访问); } /script /body /html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2569384.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!