uniapp App跳转微信小程序并互相传递参数、接收微信小程序传递的参数

1、App端跳转微信小程序(注意id一定是小程序的原始ID,在小程序后台设置-基本设置里可以看到)

注意:与微信小程序跳转微信小程序不同,App端不支持 uni.navigateToMiniProgram,App平台打开微信小程序,需要使用plus.share的 launchMiniProgram

javascript 复制代码
// App端跳转微信小程序
const appJumpMiniPro = () => {
    // 获取分享服务列表
    plus.share.getServices(
        res => {
            let sweixin: any = '';
            for (var i = 0; i < res.length; i++) {
                let t: any = res[i];
                if (t.id == 'weixin') {
                    sweixin = t;
                }
            }
            if (sweixin) {
                sweixin.launchMiniProgram(
                    {
                        id: 'gh_11af7705af70',  // 要跳转小程序的原始ID
                        path: ``,  //  可带参数
                        type: 2   // 微信小程序版本类型可取值: 0-正式版; 1-测试版; 2-体验版。 默认值为0。
                    },
                    // 目标小程序点击返回App后执行的回调,在此接收微信小程序传递的参数
                    (res2: any) => {
                        console.log(typeof res2, res2)
                        // res2是微信小程序传递回来的参数 类型为string 需转化为js对象使用
                        let result = JSON.parse(res2)
                        console.log(result)
                        // 拿到参数后执行你需要的逻辑
                    },
                    (err2: any) => {
                        console.log(err2)
                    }
                );
            }
            else {
                uni.showToast({ icon: 'none', title: '当前环境不支持微信操作!' })
            }
        },
        err => {
            console.log(err)
        }
    )
}

2、微信小程序端返回App端

下面的代码基于微信小程序也是uniapp开发的,原生的写法稍微有点不一样,详情见:打开 App | 微信开放文档

javascript 复制代码
<template>
  <view class="page-container">
    <view class="">移动应用:{{appName}}</view>
    <button class="bottom" open-type="launchApp" :app-parameter="JSON.stringify(form)" @error="launchAppError">返回APP</button>
  </view>
</template>
 
<script>
export default {
  data() {
    return {
      appName: '',
      form: {
        cid: 4408111111111,
        libs: ['北京','上海']
      }
    }
  },
  onLoad(option) {
    console.log(option)
    this.appName = option.appName
  },
  methods: {
    launchAppError(e) {
      console.log(e.detail)
      uni.showToast({icon:'none', title:e.detail.errMsg})
    }
  }
}
</script>
相关推荐
iOS阿玮1 小时前
AppStore教你一招免备案的骚操作!
uni-app·app·apple
racerun1 小时前
微信小程序如何实现再多个页面共享数据
微信小程序·小程序
XM-54582 小时前
2025微信小程序wxapkg解包全攻略
linux·运维·小程序
ModyQyW19 小时前
用 AI 驱动 wot-design-uni 开发小程序
前端·uni-app
HERR_QQ1 天前
【unify】unify的微信小程序开发学习 (to be continued)
学习·微信小程序·小程序
耶啵奶膘1 天前
uniapp+firstUI——上传视频组件fui-upload-video
前端·javascript·uni-app
racerun1 天前
小程序导航设置更多内容的实现方法
小程序
说私域1 天前
基于开源AI智能名片链动2+1模式S2B2C商城小程序的超级文化符号构建路径研究
人工智能·小程序·开源
mg6681 天前
微信小程序入门实例_____快速搭建一个快递查询小程序
微信小程序·小程序
程序员柳1 天前
基于微信小程序的校园二手交易平台、微信小程序校园二手商城源代码+数据库+使用说明,layui+微信小程序+Spring Boot
数据库·微信小程序·layui