uniapp 微信公众号H5/app/小程序跳转小程序

1.微信公众号H5跳转小程序使用微信标签wx-open-launch-weapp

html 复制代码
a.在init.vue使用标签(要实现跳转的页面)
		<wx-open-launch-weapp
            id="launch-btn"
            style="width: 100%; display: block"
            :appid="pageParam.appId"
            :path="pageParam.path" >
            <script type="text/wxtag-template">
              <view style="border: 2rpx solid rgb(73, 162, 238) !important;border-radius: 34rpx;color: rgb(73, 162, 238);padding: 10rpx 10rpx;text-align: center;font-weight: 200;font-size: 32rpx;" class="realNameAuth">前往实名</view></script>
         </wx-open-launch-weapp>
javascript 复制代码
//b.从接口获取配置信息并调用,
created() {
    let query = {
      url: window.location.href,
      appId: store.state.user.appId,
    };
    if (this.payInfo.operator == 1) {
      getSignature(query)
        .then((res) => {
          let iccidNum = this.iccid.substring(0, 19);
          this.pageParam.path = "/pages/login/index?iccid=" + iccidNum;

          this.handlesWeiXin(res.data);
        })
        .catch((err) => {});
    }
  },
  //配置wx.config
    handlesWeiXin(params) {
      const { appId, nonceStr, signature, timestamp } = params; //从接口获取
      wx.config({
        // debug: true,
        appId: appId, // 必填,公众号的唯一标识
        timestamp: timestamp, // 必填,生成签名的时间戳
        nonceStr: nonceStr, // 必填,生成签名的随机串
        signature: signature, // 必填,签名
        jsApiList: ["checkJsApi"], // 必填,需要使用的JS接口列表
        openTagList: ["wx-open-launch-weapp"], // 可选,需要使用的开放标签列表,例如['wx-open-launch-weapp','wx-open-launch-app']
      });
      wx.ready(function () {
        //config信息验证后会执行ready方法,所有接口调用都必须在config接口获得结果之后,config是一个客户端的异步操作,所以如果需要在页面加载时就调用相关接口,则须把相关接口放在ready函数中调用来确保正确执行。对于用户触发时才调用的接口,则可以直接调用,不需要放在ready函数中
      });
      wx.error(function (res) {
        // uni.showToast({
        //     title:res.err,
        //     duration: 500
        // });
        // config信息验证失败会执行error函数,如签名过期导致验证失败,具体错误信息可以打开config的debug模式查看,也可以在返回的res参数中查看,对于SPA可以在这里更新签名。
      });
    },
xml 复制代码
c.如果上述跳转没成功,那么是因为uni自带的window.wx给污染了js文件的wx全局变量;需要在APP.vue页面的method方法里面加上如下代码,并在onLaunch中调用;然后就可以跳转了
javascript 复制代码
 onLaunch: function () {
    console.log("App Launch");
    this.addScript();
  },
   methods: {
    //wx.config配置无效解决---(原因是uni自带的window.wx给污染了js文件的wx全局变量)
    addScript() {
      const that = this;
      window.wx = null;
      const script1 = document.createElement("script");
      script1.type = "text/javascript";
      script1.src = "https://res.wx.qq.com/open/js/jweixin-1.6.0.js";
      document.head.appendChild(script1);
      script1.onload = function () {
        const script2 = document.createElement("script");
        script2.type = "text/javascript";
        script2.src =
          "https://open.work.weixin.qq.com/wwopen/js/jwxwork-1.0.0.js";
        document.head.appendChild(script2);

        script2.onload = function () {
          // TODO something
        };
      };
    },
  },

2.APP跳转小程序

a.通过uniapp开发的APP跳转小程序实现,首先用到APP分享功能,需要在manifest.json中APP常用配置勾选share分享中的微信分享,填上APPid

b.在需要跳转的按钮绑定如下方法,就可以实现跳转了

javascript 复制代码
appJumpMiniPro() {
      // console.log("isok------", this.pageParam.path);
      let that = this
      // 获取分享服务列表
      plus.share.getServices(
        function (res) {
          var sweixin = null;
          for (var i = 0; i < res.length; i++) {
            var t = res[i];
            if (t.id == "weixin") {
              sweixin = t;
            }
          }
          if (sweixin) {
            sweixin.launchMiniProgram({
              id: "gh_xxxxxx", //这里写你的小程序原始id(以gh开头),可以在需要跳转的小程序中可以找到
              type: 0, //这里是不同的环境(默认0)
              path: that.pageParam.path, //这里是指定页的路径,如需传参直接字符串拼接(首页可以省略)
            });
          }else{
            plus.nativeUI.alert('当前环境不支持微信操作!')
          }
        },
        function (res) {
          console.log(JSON.stringify(res),'---9898989');
        }
      );
    },

3.小程序跳小程序,就简单很多使用下面这个方法就可以实现了

javascript 复制代码
	wx.navigateToMiniProgram({
	    appId: '目标小程序appid',
	    path: '目标小程序页面路径',
	    //develop开发版;trial体验版;release正式版
	    envVersion: 'release', 
	    success(res) {
	      // 打开成功
	      console.log("跳转小程序成功!",res);
	    } 
	})
相关推荐
2501_916008891 小时前
iOS 跨平台开发实战指南,从框架选择到开心上架(Appuploader)跨系统免 Mac 发布全流程解析
android·macos·ios·小程序·uni-app·iphone·webview
一匹电信狗1 小时前
【C++11】右值引用+移动语义+完美转发
服务器·c++·算法·leetcode·小程序·stl·visual studio
Le1Yu2 小时前
微信小程序端服务器接口:全部服务以及实战
微信小程序·小程序
一 乐12 小时前
点餐|智能点餐系统|基于java+ Springboot的动端的点餐系统小程序(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·小程序·论文
微盛AI企微管家17 小时前
企业微信AI聊天agent:优化企业微信客户运营的推荐工具
大数据·人工智能·微信·企业微信
全栈软件开发21 小时前
新版点微同城主题源码34.7+全套插件+小程序前后端 源文件
小程序
毕设源码-朱学姐21 小时前
【开题答辩全过程】以 基于java的民宿管理小程序为例,包含答辩的问题和答案
java·开发语言·小程序
QuantumLeap丶1 天前
《uni-app跨平台开发完全指南》- 06 - 页面路由与导航
前端·vue.js·uni-app
用户9714171814271 天前
uniapp页面路由
vue.js·uni-app
Kingsaj1 天前
uni-app打包app -- 在用户首次启动 App 时,强制弹出一个“用户协议与隐私政策”的确认对话框。
服务器·ubuntu·uni-app