uniapp,H5页面接微信开放标签wx-open-launch-app打开app

html代码

<wx-open-launch-app id="launch-btn" @error="handleErrorFn" @launch="handleLaunchFn"
				:extinfo="encodedExtInfo" appid="************">//替换appid
				<script type="text/wxtag-template">
					<style>
						.btn1 {
							padding: 12px;
						}
						.download {
							color: #ffffff;
							background-color: #FF5D34;
							width:100%;
							height:100%;
							display: flex;
							align-items: center;
							justify-content: center;
							font-size: 16px;
							font-family: PingFangSC-Medium, PingFang SC;
							font-weight: 500;
							border-radius: 2px;
						}
					</style>
					<button class="download btn1" style="height:100%;width:343px;border:none; border-radius: 28px;">打开app</button>
				</script>
			</wx-open-launch-app>

需要使用到微信开放标签,要使用jweixin-1.6.0.js。官方地址目录 | 微信开放文档。因为在uniapp中关键字被占用,所以要单独引入

import jweixin from "@/static/jweixin-1.6.0.js";

根据官方文档,所写要使用微信开放标签需要鉴权,所以要后端提供鉴权接口,并把相关数据反回。接收到相关数据,初始化按钮。

postRequest() {
				let params = {
					url: window.location.href.split("#")[0],
				};

				var xhr = new XMLHttpRequest();
				xhr.open(
					"POST",
					"************",//后端提供
					true
				);
				xhr.setRequestHeader("Content-Type", "application/json");
				xhr.onreadystatechange = function() {
					if (xhr.readyState == 4 && xhr.status == 200) {
						var data = JSON.parse(xhr.responseText);
						if (data.code === 1) {
							setTimeout(() => {
								this.authSDK(data);
							}, 200)
						}
					} else if (xhr.readyState == 4) {
						console.error("Error:", xhr.status);
					}
				}.bind(this);
				xhr.send(JSON.stringify(params));
			},

authSDK(data) {
				const { appId, nonceStr, signature, timestamp } = data;
				jweixin.config({
					debug: false,
					appId: appId,
					timestamp: timestamp,
					nonceStr: nonceStr,
					signature: signature,
					jsApiList: ["getLocation"],
					openTagList: ["wx-open-launch-app", "wx-open-launch-weapp"], // 这里要配置一下
				});
				jweixin.ready(function() {
					jweixin.checkJsApi({
						jsApiList: [
							"getLocation",
							"wx-open-launch-app",
							"wx-open-launch-weapp",
						], 
						success: function(res) {
							console.log("wxJsApi success:", res);
						},
						fail: (err) => {
							console.log("wxJsApi fail:", err);
						},
					});
				});
			},

打包项目,放到服务器,下图显示成功了。


也可以运行到网页,用微信开发者工具打开公众号网页项目,把运行到浏览器的地址复制过去就可以了,但是要显示按钮,还必须你是公众号的开发者才能显示,不然你只能放到服务器上才能查看是否实现

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值