目录
H5唤醒小程序
官方API参考:
其他文章参考:
const appid = '' // appid
const secret = '' // secret
const granttype = 'client_credential' // 固定值
// 获取Token
axios.get(`https://api.weixin.qq.com/cgi-bin/token?grant_type=${granttype}&appid=${appid}&secret=${secret}`).then(res => {
// 获取 urlscheme
axios.post(`https://api.weixin.qq.com/wxa/generatescheme?access_token=${(res.data as { 'access_token': string }).access_token}`, {
jump_wxa: {
path: 'pages/home/home'
}
}).then(response => {
// 唤醒小程序
window.open((response.data as { 'openlink': string }).openlink) // weixin://xxxxxxxxx
}).catch(() => { /* */ })
}).catch(() => { /* */ })
最终效果:
踩坑:
第二步生成urlscheme时,必须填写path参数,即唤醒小程序要跳转的页面。
H5唤醒小程序只支持企业小程序,个人小程序无权限生成urlscheme。
H5唤醒小程序只能在微信端打开,因为生成的urlscheme是微信生态中的连接。
调接口过程最好也有必要做在服务端,一是为了安全,二是调接口时的跨域问题。
微信公众号-H5微信登录授权
在微信公众号中,跳转到第三方H5网站,第三方H5网站可以获取微信用户信息。微信公众号网页授权https://developers.weixin.qq.com/doc/offiaccount/OA_Web_Apps/Wechat_webpage_authorization.html 微信公众平台中设置要授权的第三方H5网站,只有设置了,第三方H5网站才有权限获取微信用户信息。
设置与开发---->公众号设置---->功能设置---->网页授权域名
- 步骤一:微信用户授权,获取code
https://open.weixin.qq.com/connect/oauth2/authorize?appid=微信公众号appid&redirect_uri=要跳转的H5网站&response_type=code&scope=snsapi_userinfo#wechat_redirect
- 步骤二:通过第一步获取的code,来获取access_token 和 openid
https://api.weixin.qq.com/sns/oauth2/access_token?appid=微信公众号appid&secret=微信公众号的secret&code=第一步获取的code&grant_type=authorization_code
- 步骤三:获取用户信息
https://api.weixin.qq.com/sns/userinfo?access_token=第二步获取的Token&openid=第二步获取的openid&lang=zh_CN