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

通过weixin://dl/business/?t=打开微信小程序通过 weixin dl business t 打开微信小程序 我这里用的 uni app 所以请求用的 uni request 其他的同理 比如 ajax 和 axios 不多赘述

第一步,通过微信接口(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))
            }
          });
编程小号
上一篇 2025-01-13 12:30
下一篇 2025-01-13 12:21

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ji-chu/109164.html