微信小程序添加卡券到微信卡包,使用wx.addCard()方法传参及整体流程

一、准备:

1.经微信认证过的微信公众号。

2.经微信认证过的微信小程序号。

 

先来看看微信小程序官方的文档,https://developers.weixin.qq.com/miniprogram/dev/api/wx.addCard.html

在看看微信介入卡券的流程,https://mp.weixin.qq.com/cgi-bin/announce?action=getannouncement&key=1490190158&version=1&lang=zh_CN&platform=2

二、开始

小程序端:

点击事件调用wx.addCard()方法,其中需要重点了解的是cardExt里面的参数

wx.addCard({
      cardList: [
        {
          cardId: card_id,
          cardExt: JSON.stringify(cardExt)
        }
      ],
      success: (res) => {
        console.log(res)
        this.addCardSuccess(res.cardList[0].code)
      },
      fail: (err) => {
        console.log(err)
      }
    })

 

下面是官方文档的介绍

有个fixed_begintimestamp时间戳需要注意,这个字段是用户实际领取的优惠券时间,所以必须是用户领取完毕之后才可以添加到微信卡券。

这写参数都是由后台传过来的,放在了this.data.wxCardData里面

addToWeixinCard () {
    // this.data.wxCardData为从后台获取的一些参数,包括下面这些参数
    let { card_id, code, timestamp, openid, nonce_str, signature, fixed_begintimestamp } = this.data.wxCardData
    let cardExt = {
      code,
      openid,
      timestamp,
      nonce_str,
      fixed_begintimestamp,
      signature,
      outer_str: 'miniProgram'
    }
    console.log(card_id)
    wx.addCard({
      cardList: [
        {
          cardId: card_id,
          cardExt: JSON.stringify(cardExt)
        }
      ],
      success: (res) => {
        console.log(res)
        this.addCardSuccess(res.cardList[0].code)
      },
      fail: (err) => {
        console.log(err)
      }
    })
  }

 最后调用该方法就可以了,如果调用成功则会跳转到下面的页面

到这里就完成了添加到微信卡券的所有步骤了

posted @ 2019-05-10 14:16  林子lxl  阅读(17030)  评论(1编辑  收藏  举报