vue3——H5页面调起微信订阅通知

<!--
 * @Description: 
 * @Author: Southern Wind
 * @Date: 2024-02-27 19:46:10
 * @LastEditTime: 2024-04-08 11:34:49
 * @FilePath: \h5\src\views\Index\Index.vue
-->
<script lang='ts' setup>
import wx from "weixin-js-sdk";
import { UserService } from "@/api/api";
import { onMounted, reactive, ref } from "vue";

onMounted(() => {
  // login()
  const load = async () => {
    const res: any = await UserService.getjssign({ url: location.href });
    wx.config({
      debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来,若要查看传入的参数,可以在pc端打开,参数信息会通过log打出,仅在pc端时才会打印。
      appId: res.data.data.appId, // 必填,公众号的唯一标识
      timestamp: res.data.data.timestamp, // 必填,生成签名的时间戳
      nonceStr: res.data.data.nonceStr, // 必填,生成签名的随机串
      signature: res.data.data.signature, // 必填,签名
      jsApiList: [],
      openTagList: ["wx-open-subscribe"], // 必填,需要使用的JS接口列表
    });
    console.log(res);

    let btn: any = document.getElementById("subscribe-btn");
    wx.ready(function () {
      btn.addEventListener("success", function (e: any) {
        console.log("success", e.detail);
      });
      btn.addEventListener("error", function (e: any) {
        console.log("fail", e.detail);
        routerBtn()
        
      });
    });
  };
  load();
});

</script>

<template>
  <div class="containers">
    <wx-open-subscribe template="templateID" id="subscribe-btn" >
      <component :is="'script'" type="text/wxtag-template">
        <div class="btn"   style="    text-align: center;
    color: #664502;
    line-height: 45px;
    font-size: 16px;" >
          点击进入
        </div>
      </component>
    </wx-open-subscribe>
  </div>
</template>

<style scoped lang='less'>
.containers {
  width: 100%;
  height: calc(100vh - 92px);
  background: url(../../assets/image/bg1.jpg) no-repeat top center;
  background-size: cover;
  position: relative;

  /*   .cs {
    width: 750px;
    height: 30px;
    background-color: black;
  } */

  #subscribe-btn  {
    position: absolute;
    bottom: 153px;
    left: 50%;
    transform: translate(-50%, 0);
    width: 360px;
    height: 90px;
    text-align: center;
    line-height: 90px;
    background: linear-gradient(-29deg, #ffaf03 22%, #ffde00 100%), #ffc600;
    border-radius: 45px;
    box-shadow: -15px 0px 9px 15px rgba(0, 53, 181, 0.19);
    font-weight: 700;
  }
}

</style>

注意以下几点:

  1. 使用pnpm weuxub-js-jdk安装微信依赖
  2. url需要传递完整路径,可以使用location.href
  3. 标签部分需要后端给templateID
  4. wx-open-subscribe标签中的样式需要用内联样式,或者在标签里面使用style进行包裹,不然无法更改自定义样式
  • 10
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
Vue H5页面中实现微信分享,可以按照以下步骤进行: 第一步,获取微信分享所需的相关配置: 在Vue项目中引入微信开放平台的JS-SDK,然后通过微信开放平台接口获取到当前页面的分享配置信息,包括标题、描述、链接和图片等信息。 第二步,调用微信JS-SDK进行分享: 在Vue的组件中,通过引入微信JS-SDK提供的方法,如`wx.onMenuShareTimeline`和`wx.onMenuShareAppMessage`等,来实现对应的分享功能。可以将这些方法封装成一个单独的util文件,方便在项目中多次调用。 第三步,配置分享参数: 调用微信JS-SDK的分享方法前,需要将获取到的分享配置信息传入相应的参数中,如标题、链接、描述和分享图等。可以根据当前页面的不同内容来动态设置分享的参数。 第四步,设置微信签名: 在Vue页面中,可以使用`vue-router`插件进行路由控制,当进入到需要分享的页面时,通过异步请求来获取当前页面的分享配置信息和签名,然后将签名设置给微信JS-SDK。 第五步,调用分享方法: 在上面的配置完成后,即可通过微信JS-SDK提供的分享方法,如`wx.onMenuShareTimeline`和`wx.onMenuShareAppMessage`等,来实现对应的分享功能。可以按需选择分享到朋友圈、发送给好友或分享到QQ等渠道。 需要注意的是,Vue H5页面在与微信分享时,由于所处的环境和限制会有所不同,可能需要在不同的事件中触发分享,比如在路由切换完成后触发分享方法。 以上是实现Vue H5页面分享微信的一般步骤,具体实现还需要根据项目的需求和微信开放平台的接口文档进行调整。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Southern Wind

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

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

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

打赏作者

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

抵扣说明:

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

余额充值