H5利用微信开放标签唤起用户手机APP

APP壳子分享网页到微信,被分享人在微信打开网页后,利用公众号配置微信开放标签['wx-open-launch-app'],实现唤起APP

一、Vue2.x(2.6.11)

1. main.js

// main.js

import Vue from 'vue';

Vue.config.ignoredElements = ['wx-open-launch-app'];

2. awakeByWeChat.vue

// awakeByWeChat.vue
<template>
  <wx-open-launch-app
    id="launch-btn"
    :appid="公众号ID"
    :extinfo="唤起app传递的参数"
    @error="onListenLaunchError"
  >
    <script type="text/wxtag-template">
      <style>
          * {
            margin: 0;
            padding: 0;
          }

          button:focus {
            outline: none;
          }

          .defaultBtn {
            border: 0;
            height: 500px;
            outline: none;
            margin: 0 auto;
            font-weight: 500;
          }

          {{ btnStyle }}
        </style>

        <button class="defaultBtn openBtn">{{ btnName }}</button>
    </script>
  </wx-open-launch-app>
</template>


<script>
export default {
  name: 'aweakAppByWeChat',
  props: {
    btnStyle: String, // 自定义按钮样式 `.btnClass{xx}`
    btnName: {
      type: String,
      default: '立即打开' // 自定义按钮名称
    }
  },
  data() {
    return {
      wxAppId: '',
      targetInfo: null, // 传递参数,格式需为字符串
    };
  },
  methods: {
    // wx 唤起失败异常处理, 降级处理跳转
    onListenLaunchError(e) {
      console.log('wx唤醒失败 | error', e.detail || e);
      this.$emit('launchError');
    }
  }
};
</script>

<style lang="less" scoped>
#launch-btn {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  width: 100%;
  height: 100%;
  z-index: 99999;
}
</style>

二、Vue3.x(3.4.21)

1.main.js

// main.js

import { createApp } from 'vue'

const app = createApp(App)
app.config.compilerOptions.isCustomElement = (tag) => (tag.includes('wx-open-launch-app'))

2.vite.config.js

// vite.config.js
import { defineConfig } from 'vite'

export default defineConfig({
  base: './',
  define: {
    __VUE_OPTIONS_API__: true // 关闭vue2中的 api属性
  },
  plugins: [
    vue({
      template: {
        compilerOptions: {
          isCustomElement: (tag:any) => tag.includes('wx-open-launch-app')
        }
      }
    })
  ]
})

3.awakeByWeChat.vue

//awakeByWeChat.vue

<template>
  <wx-open-launch-app
    id="launch-btn"
    :appid="props.wxId"
    :extinfo="props.extinfo"
    @error="onListenLaunchError"
  >
    <component :is="'script'" type="text/wxtag-template">
      <button
        style="
          display: block;
          border: 0;
          width: 100%;
          height: 500px;
          outline: none;
          margin: 0 auto;
          font-weight: 500;
        "
      >
        {{ customName || "打开APP查看" }}
      </button>
    </component>
  </wx-open-launch-app>
</template>

<script lang="ts" setup>
const props = defineProps({
  wxId: {
    type: String,
    required: true,
  },
  extinfo: String,
  customName: String
});

const emit = defineEmits(["launchError"]);

const onListenLaunchError = (e) => {
  emit("launchError");
  console.log("WX唤起失败:", e.detail || e);

  // 执行降级操作
};
</script>

<style scoped lang="scss">
#launch-btn {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  opacity: 0;
  width: 100%;
  height: 100%;
  z-index: 99999;
}
</style>

### 如何在H5页面中实现调用微信App的功能及所需微信配置 #### 微信JS-SDK简介 为了使H5页面能够与微信客户端交互,开发者可以利用微信提供的JavaScript SDK(简称JS-SDK)。该工具包允许网页通过API访问微信内置功能,如分享、支付、获取地理位置等。 #### 获取应用ID和密钥 要使用这些API,在微信公众平台注册账号并创建一个移动Web应用程序是必要的。这一步骤会提供给开发者一对用于身份验证的应用ID(AppID) 和 应用密钥(AppSecret)[^1]。 #### 配置服务器端逻辑 对于处在开发阶段的项目来说,如果遇到无法设置正式环境下的`JS接口安全域名`的情况,则可以通过申请接口测试号来绕过这一限制。此测试号支持大部分开放能力,并且不需要绑定备案网站即可完成调试工作。 #### 初始化JS-SDK 当准备就绪之后,需按照官方文档指引引入JS文件并在页面加载完成后执行初始化操作: ```html <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js"></script> <script type="text/javascript"> wx.config({ debug: true, // 开启调试模式, appId: '', // 必填,公众号的唯一标识 timestamp: , // 必填,生成签名的时间戳 nonceStr: '', // 必填,生成签名的随机串 signature: '',// 必填,签名 jsApiList: [] // 必填,需要使用的JS接口列表 }); </script> ``` 上述代码中的参数值应该由后端服务动态计算得出;其中signature字段尤其重要因为它涉及到安全性问题。 #### 调用具体功能 一旦成功注入权限验证配置,就可以调用各种API了。例如想要打开摄像头扫描条形码/二维码可参照如下方式: ```javascript wx.ready(function(){ wx.scanQRCode({ needResult: 1, // 默认为0,扫描结果由微信处理,1则直接返回扫描结果, scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码,默认二者都扫 success: function (res) { var result = res.resultStr; // 当needResult 为 1 时,扫码返回的结果 } }); }); ``` 另外值得注意的是,在实际部署前还需要考虑不同版本间的兼容性和异常情况处理等问题[^3]。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值