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>