uni-app / vue 使用微信开发标签标签wx-open-subscribe

字多 耐心 看完 别慌张 仔细仔细看


经过N久得爬坑,终于做出来了,废话不多说,直接上代码,样例为uniapp vue 同理,直接新建一个文件打开即可 ,同时把所有得wx. 换成uni.;


效果:

在这里插入图片描述
在这里插入图片描述


有人会问你这一看就是小程序啊,非也非也,我写得是uni,但是呢,是从小程序逆转过来实现得,也支持微信得写法;
前提:
1.模板id必须有,没有跟运维要跟后端要; 即代码中得‘ templateIds’
2.如果出现点击不好使得问题 确保id是初始化就加载,隐藏起来不好使,别问为什么,就是那么回事
在这里插入图片描述
3.如果电脑上报错
addEventListener is undefined 啊 啥的暴多,就是id 没有加载出来的问题;

4.保持必须在线上环境测试,硬性要求
5.如果本地微信开发者工具出现甜甜甜,不要慌张,那是系统默认给的,手机上是正常的;
6.点击事件 用的是原生的vue 的点击事件;
7.有人会问,为什么不用vue的success和error方法呀,因为我没调通,不会用,所以就用了原生的点击事件了;
8.如果解决了,可以告知我哈


!!!上代码

<template>
  <view>
    <view class="subscribe">
      <template type="text/wxtag-template">
        <view class="subscribe-icon">
          <image class="subscribe-icon-one" src="/static/static/img/finger_00.png"></image>
          <image class="subscribe-icon-two" src="/static/static/img/finger_01.png"></image>
        </view>
        <view class="subscribe-cont">
          <wx-open-subscribe :template="templateIds[0]" id="subscribe-btn" ref="subscribeBtn">
            <script type="text/wxtag-template" slot="style">
              <style>
               .subscribe-txt{
                 color: #89663f;
                 font-weight: bold;
                -webkit-animation: subscribeCont 1s linear infinite;
                  animation: subscribeCont 1s linear infinite;
                  display: inline-block;
                  height: 40px;
                  width: 160px;
                  text-align: center;
                  line-height: 40px;
                }
                @keyframes subscribeCont {
                  25% {
                    transform: scale(.98);
                  }
                
                  50%,
                  100% {
                    transform: scale(1);
                  }
                
                  75% {
                    transform: scale(1.05);
                  }
                }
              </style>
            </script>
            <script type="text/wxtag-template">
              <view class="subscribe-txt">点击消息提醒</view>
            </script>
          </wx-open-subscribe>
        </view>
      </template>
    </view>
  </view>
</template>

<script>
  const jweixin = require('jweixin-module');
  export default {
    data() {
      return {
        templateIds: ['R9LbtDjg9sC-o3xUS2kDcSQ9MS4I67LnacAS8Fsmxp0']
      }
    },
    onReady() {
      this.getInfo()
    },
    methods: {
      getInfo() {
      // 这里是后去config得基本信息(跟后端要接口 wx.$api.index.wpaySubscribeH5Info  别看不懂,这是封装的请求接口而已换成你自己的就行了)
        wx.$api.index.wpaySubscribeH5Info(location.href.split('#')[0], ['chooseWXPay', ]).then(res1 => {
          let w_pay = JSON.parse(res1.data)
          jweixin.config({
            debug: false,
            appId: w_pay.appId,
            timestamp: w_pay.timestamp,
            nonceStr: w_pay.nonceStr,
            signature: w_pay.signature,
            jsApiList: ['wx-open-subscribe', 'wx-open-launch-app', 'launchAppLication'],
            openTagList: ['wx-open-subscribe', 'wx-open-launch-app', 'wx-open-subscribe-dialog']
          })
          // config 准备好了那么就开始操作了,
          jweixin.ready(() => {
          // wx-open-subscribe 原生绑定点击事件
            this.$nextTick(() => {
              const btn = this.$refs.subscribeBtn;
              btn.addEventListener('success', ({
                detail
              }) => {
                this.content2 = JSON.parse(detail.subscribeDetails)
                for (var i in this.content2) {
                  if (i === this.templateIds[0] || i ===
                    'EZcWzqrLdU-2Ax6KILdv9uSf6ANbHka7CbYG2KGahPk') {
                    let status = JSON.parse(this.content2[i]).status
                    console.log(status)
                    console.log(this.content2)
                    if (status === 'accept') {
                    // 这里跟后端要个接口,绑定;
                      wx.$api.index.subscribeH5(1).then(res => {
                        wx.showLoading({
                          title: res.data,
                          icon: 'none',
                        })
                        // 获取卡权益
                        // this.indexInfo();
                        setTimeout(function() {
                          wx.hideLoading();
                        }, 1000)
                      })
                    } else if (status === 'reject') {
                      alert('拒绝授权')
                    }
                  }
                }
              });
              btn.addEventListener('error', (e) => {
                alert('授权失败了,请重试')
              });
            })
          })
        }).catch(err => {
          wx.showToast({
            title: err.message,
            icon: 'none'
          })
        })
      }

    }
  }
</script>

<style>
  /* 订阅消息 */
  .subscribe {
    color: #89663f;
    width: 100%;
    padding: 0 100rpx;
    display: inline-block;
    box-sizing: border-box;
    margin-top: 50rpx;
    display: flex;
    /* -webkit-animation: Tada 3s both infinite;
              -moz-animation: Tada 3s both infinite;
              -ms-animation: Tada 3s both infinite;
              animation: Tada 3s both infinite; */
  }

  .subscribe-icon,
  .subscribe-cont {
    height: 80rpx;
    text-align: center;
    line-height: 80rpx;
    position: relative;
  }

  .subscribe-icon {
    width: 150rpx;
    padding-left: 30rpx;
    box-sizing: border-box;
    background: linear-gradient(to right, #f3c988, #f9e0b8);
    border-radius: 40rpx 0 0 40rpx;
  }

  .subscribe-icon::after {
    position: absolute;
    content: '';
    right: -50rpx;
    top: 0;
    width: 0;
    height: 0;
    border-bottom: 80rpx solid #f9e0b8;
    border-right: 50rpx solid transparent;
    z-index: 9;
  }

  .subscribe-cont {
    width: calc(100% - 150rpx);
    background: linear-gradient(to right, #f3c988, #f9e0b8);
    border-radius: 0 40rpx 40rpx 0;
    font-weight: 600;
  }

  .subscribe-cont text {
    display: inline-block;
    -webkit-animation: subscribeCont 1s linear infinite;
    animation: subscribeCont 1s linear infinite;
  }

  @keyframes subscribeCont {
    25% {
      transform: scale(.98);
    }

    50%,
    100% {
      transform: scale(1);
    }

    75% {
      transform: scale(1.05);
    }
  }

  .subscribe-icon image {
    width: 54rpx;
    height: 54rpx;
    margin-top: 14rpx;
  }

  @keyframes Tada {
    0% {
      transform: scale(1);
      transform: scale(1)
    }

    70%,
    73% {
      transform: scale(1) rotate(-3deg);
      transform: scale(1) rotate(-3deg)
    }

    77%,
    83%,
    90%,
    97% {
      transform: scale(1) rotate(3deg);
      transform: scale(1) rotate(3deg)
    }

    80%,
    87%,
    93% {
      transform: scale(1) rotate(-3deg);
      transform: scale(1) rotate(-3deg)
    }

    100% {
      transform: scale(1) rotate(0);
      transform: scale(1) rotate(0)
    }
  }

  .subscribe-icon-one {
    -webkit-animation: bounce-right 1s linear infinite;
    animation: bounce-right 1s linear infinite;
  }

  @keyframes bounce-right {
    25% {
      transform: translateX(-3px);
    }

    50%,
    100% {
      transform: translateX(0);
    }

    75% {
      transform: translateX(3px);
    }
  }

  .subscribe-icon-two {
    -webkit-animation: bounce-two 1s linear infinite;
    animation: bounce-two 1s linear infinite;
  }

  @keyframes bounce-two {
    25% {
      opacity: 0;
      transform: translateX(-3px);
    }

    75% {
      opacity: 1;
      transform: translateX(3px);
    }
  }

  subscribe-btn {
    width: 100%;
    height: 200rpx;
    text-align: center;
    line-height: 150rpx;
    font-size: 30rpx;
    font-weight: 500;
    color: #ffffff;
    background-color: green;
    position: relative;
    overflow: hidden;
  }
</style>





//


备注:图片 自己下载去

图片1:图片名:finger_00在这里插入图片描述
图片2:图片名:finger_01
在这里插入图片描述


**现在能想起来的我遇到的问题就这么多,若果有更多问题,欢迎提问,收藏点赞啊 **

在这里插入图片描述
看到最后了么?别人遇到的问题,你也可能遇到,对照下
1.全局记得安装~有人遇到问题
npm i jweixin-module -S

  • 5
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
在TP5中,可以使用PHP的内置函数`simplexml_load_string()`来解析XML字符串。具体步骤如下: 1. 将XML字符串转换为SimpleXMLElement对象; 2. 使用对象的属性或方法获取节点的值。 示例代码如下: ```php // XML字符串 $xml_string = '<xml><ToUserName><![CDATA[toUser]]></ToUserName><FromUserName><![CDATA[FromUser]]></FromUserName><CreateTime>123456789</CreateTime><MsgType><![CDATA[event]]></MsgType><Event><![CDATA[subscribe]]></Event><EventKey><![CDATA[qrscene_123123]]></EventKey><Ticket><![CDATA[TICKET]]></Ticket></xml>'; // 将XML字符串转换为SimpleXMLElement对象 $xml = simplexml_load_string($xml_string, 'SimpleXMLElement', LIBXML_NOCDATA); // 使用对象的属性或方法获取节点的值 $toUserName = $xml->ToUserName; $fromUserName = $xml->FromUserName; $createTime = $xml->CreateTime; $msgType = $xml->MsgType; $event = $xml->Event; $eventKey = $xml->EventKey; $ticket = $xml->Ticket; ``` 需要注意的是,如果XML字符串中有命名空间,需要使用`children()`方法的第一个参数来指定命名空间。例如: ```php // XML字符串 $xml_string = '<ns:xml xmlns:ns="http://example.com"><ns:ToUserName><![CDATA[toUser]]></ns:ToUserName><ns:FromUserName><![CDATA[FromUser]]></ns:FromUserName><ns:CreateTime>123456789</ns:CreateTime><ns:MsgType><![CDATA[event]]></ns:MsgType><ns:Event><![CDATA[subscribe]]></ns:Event><ns:EventKey><![CDATA[qrscene_123123]]></ns:EventKey><ns:Ticket><![CDATA[TICKET]]></ns:Ticket></ns:xml>'; // 将XML字符串转换为SimpleXMLElement对象 $xml = simplexml_load_string($xml_string, 'SimpleXMLElement', LIBXML_NOCDATA); // 指定命名空间 $ns = $xml->getNamespaces(true); $children = $xml->children($ns['ns']); // 使用对象的属性或方法获取节点的值 $toUserName = $children->ToUserName; $fromUserName = $children->FromUserName; $createTime = $children->CreateTime; $msgType = $children->MsgType; $event = $children->Event; $eventKey = $children->EventKey; $ticket = $children->Ticket; ``` 需要注意的是,如果XML字符串中包含CDATA,需要将第二个参数设置为`'SimpleXMLElement', LIBXML_NOCDATA`。这样才能正确解析CDATA。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值