1、App端跳转微信小程序(注意id一定是小程序的原始ID,在小程序后台设置-基本设置里可以看到)
注意:与微信小程序跳转微信小程序不同,App端不支持 uni.navigateToMiniProgram,App平台打开微信小程序,需要使用plus.share的 launchMiniProgram。
// 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', // 要跳转小程序的原始IDpath: ``, // 可带参数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 | 微信开放文档。
<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>