通过weixin://dl/business/?t=打开微信小程序

第一步,通过微信接口(https://api.weixin.qq.com/cgi-bin/token)获取小程序access_token

微信官方文档

第二步,通过微信接口(https://api.weixin.qq.com/wxa/generatescheme)获取小程序跳转链接weixin://dl/business/?t=

微信官方文档
我这里用的uni-app,所以请求用的uni.request,其他的同理,比如ajax和axios,不多赘述

// 第一步获取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) => {
              alert("获取token成功");
              console.log(res1.data);
              // 第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,
                  // env_version: "trial",
                },
                success: (res2) => {
                  alert("获取小程序跳转链接1");
                  console.log(res2.data);
                  let a = document.createElement("a"); //创建一个a标签元素
                  a.href = res2.data.openlink; //设置跳转地址
                  document.body.appendChild(a); //加入
                  a.click(); //触发点击跳转
                  document.body.removeChild(a);删除元素
                },
                fail:(err) =>{
              alert('获取微信跳转链接失败')
              alert(JSON.stringify(err))
            }
              });
            },
            fail:(err) =>{
              alert('获取微信token失败')
              alert(JSON.stringify(err))
            }
          });
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端攻城狮路飞

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值