微信小程序能唤起APP吗?封装唤起APP组件

微信小程序能唤起APP吗?首先回答:可以的,但是受限使用场景。

实现方式

微信没有提供API,通过button中open-type设置为launchApp来实现跳转APP。

它可以传递的参数:

  • 可以通过app-parameter属性设定向APP传的参数,app根据传的参数跳转APP不同页面;

  • binderror:当使用开放能力时,发生错误的回调,open-type=launchApp时有效

  • bindlaunchapp:打开 APP 成功的回调,open-type=launchApp时有效

限制唤起APP场景

根据官方描述只能实现APP跳转小程序后,小程序才能回调APP,场景值是1069时有效。官方描述为:当小程序从 APP 打开的场景打开时(场景值 1069),小程序会获得返回 APP 的能力,此时用户点击按钮可以打开拉起该小程序的 APP。即小程序不能打开任意 APP,只能 跳回 APP。

实现

JS

const URLSCHEME = 'weixin://';
Component({
  properties: {
    targetAppUrl: {
      type: String,
      value: ''
    },
    textButton: {
      type: Boolean,
      value: true
    },
    width: {
      type: Number,
      value: 600,
    },
    height: {
      type: Number,
      value: 150,
    },
    borderRaius: {
      type: Number,
      value: 200
    },
    textContent: {
      type: String,
      value: '点击打开APP'
    },
    // 文字按钮,按钮背景颜色
    bgColor: {
      type: String,
      value: '#C2BF5E'
    },
    // 文字按钮,按钮文字颜色
    textColor: {
      type: String,
      value: 'rgb(247, 239, 239)'
    },
    // 文字按钮,按钮背景图片
    textBgImg: {
      type: String,
      value: ''
    },
    // 图片按钮,按钮背景图片
    bgImage: {
      type: String,
      value: 'http://t15.baidu.com/it/u=2364316606,2378153540&fm=224&app=112&f=JPEG?w=344&h=500'
    }
  },
  data: {

  },
  lifetimes: {
    attached() {
      this.setData({
        appUrl: `${URLSCHEME}${this.data.targetAppUrl}`
      });
    }
  },
  methods: {
    launchAppError(e) {
      console.log('launchAppError',e.detail.errMsg);
      wx.showToast({
        title: '此场景无法跳转',
        icon: 'none'
      });
    },
    launchAppSucess(e) {
      console.log('launchAppSucess',e);
    }
  }
})

wxml

<view class="comp-root">
  <button class="btn"	hover-class="small-hover" open-type="launchApp" app-parameter="{{appUrl}}" binderror="launchAppError" bindlaunchapp="launchAppSucess" style="width: {{width}}rpx; height: {{height}}rpx; padding: 0; line-height: {{height}}rpx; border-radius: {{borderRaius}}rpx; background:{{bgColor}} url({{textBgImg}});  color: {{textColor}};" >
  <block wx:if="{{textButton}}">
  {{textContent}}
  </block>
  <block wx:else>
  <image class="img" src="{{bgImage}}" mode="aspectFill"/>
  </block>
  </button>
</view>

wxss

.btn {
  padding: 0;
  text-align: center;
}

.img {
  width: 100%;
  height: 100%;
}

.rect-hover{
  position: relative;
  top: 3rpx;
  left: 3rpx;
  box-shadow: 0 0 8px rgba(0,0,0,0.1) inset;
}


.small-hover{
  opacity: 0.9;
  transform: scale(0.95,0.95);
}


.medium-hover{
  opacity: 0.8;
  transform: scale(0.85,0.85);
}

总结

微信小程序能唤起APP吗?首先回答:可以的,但是受限使用1069(APP唤起小程序)场景,本文基于此封装了微信小程序唤起APP组件。

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

前端后花园

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值