通过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))
            }
          });

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>