记录一下外部H5如何打开指定微信小程序的指定页面,这次用到的是微信的scheme码。
有言在先:目前仅针对国内非个人主体的小程序开放。
1.获取access_token
后端通过微信接口(https://api.weixin.qq.com/cgi-bin/token)来获取对应的接口
具体的参数注意事项可以看微信文档
微信获取ccess_token官方文档
2.获取小程序跳转链接weixin://dl/business/?t=
这也是有后端通过微信接口(https://api.weixin.qq.com/wxa/generatescheme)
微信官方接口文档
这里用uniapp 前端请求接口来演示
/ 第一步获取token
uni.request({
method: "GET",
// 这里的appid写你需要跳转的小程序的appid,secret秘钥也一样,grant_type=client_credential是固定的参数,不用改
url: "https://api.weixin.qq.com/cgi-bin/token?appid=wxe109dd058as8sdf88&secret=9eb3d6dec783bc55sdfs32sdf2f2aa0&grant_type=client_credential",
success: (res1) => {
// 第2步获取小程序跳转链接
uni.request({
method: "POST",
// access_token就是请求上一个接口拿到的回调结果
url:
"https://api.weixin.qq.com/wxa/generatescheme?access_token=" + res1.data.access_token,
data: {
// path跳转到的小程序目标页面,query跳转需要携带参数,在目标页面onload里面接收options里面,其他参数固定,获取看文档了解
jump_wxa: {
path: "/app/test/test",
query: "url=" + urls,
env_version: "trial", // 正式版为"release",体验版为"trial",开发版为"develop"
},
is_expire: true,
expire_type: 1,
expire_interval: 1,
},
success: (res2) => {
alert("获取小程序跳转链接1");
//这里看你的需求
location.href=data
},
fail:(err) =>{
}
});
},
fail:(err) =>{
}
});
``