美文网首页
小程序获取微信公众号的卡券

小程序获取微信公众号的卡券

作者: zx一个胖子 | 来源:发表于2018-05-16 14:20 被阅读0次

    开发前需准备

    1:申请微信公众号 和 微信小程序,这是两个不同的东西,都需要单独申请、不同的帐号;

    2:微信公众号需要开通微信卡券的功能;

    3:在微信公众号里面去绑定小程序;

    注:只针对不懂后端的前端人员

    1.首先要在后台通过微信公众号的 appId 和 secret 

    param = "?grant_type="client_credential "&appid=" + appId + "&secret=" + secret

    https://api.weixin.qq.com/cgi-bin/token" + param

    获取到AccessToken

    2.前端小程序通过wx.login获取到 code 发送给到后台,后台用小程序的 appId ,secret 和code  拿到openId

    https://api.weixin.qq.com/sns/jscode2session?appid= APPID &secret= APP SECRET &grant_type=authorization_code&js_code= CODE

    将登录凭证转换获取openid

    3.前端通过

    https://???/???/weixin/api/getCardSign?cardId=' + cardId;(cardId为固定,在制作卡券的时候会有)

    拿到timestamp 时间戳,signature签名值,nonce_str随机字符串(后台人员根据AccessToken通过https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=" + token.getAccess_token() + "&type=wx_card,然后做签名,再返回给前端)

    小程序中通过 wx.addCard 方法 领取卡券

    具体微信小程序代码:

    var that = this; 

    var service_url = 'https://???/???/weixin/api/getCardSign?cardId=' + cardId;//需要将服务器域名添加到小程序的request合法域名中,而且必须是https开头 (拿到timestamp 时间戳,signature签名值,nonce_str随机字符串)

    wx.request({ 

      url: service_url, 

      data: {}, 

      method: 'GET', 

      success: function (res) { 

        console.log(res); 

          wx.addCard({ 

            cardList: [{ 

              cardId: that.data.cardId, 

              cardExt: '{"code":"","openid":"","timestamp":' + res.data.timestamp + ',"nonce_str":"' + res.data.nonceStr + '","signature":"' + res.data.signature + '"}' 

            }],//这里需要注意的是cardExt参数的value值是 String类型,不要使用对象发送;另外openid如果在创建优惠券的时候没有指定,则这边为空,千万不要填写当前用户的openid 

            success: function (result) { 

              console.log(res); 

              wx.showToast({ 

                title: '领取成功', 

                icon: 'success', 

                duration: 2000 

              }); 

            }, 

            fail: function (res) { 

              console.log('领取失败'); 

              console.log(res); 

            } 

          }) 

      } 

    }); 

    更多关于后台的java代码:https://blog.csdn.net/zhourenfei17/article/details/77714600

    相关文章

      网友评论

          本文标题:小程序获取微信公众号的卡券

          本文链接:https://www.haomeiwen.com/subject/pzypdftx.html