開發(fā)前需準(zhǔn)備
1:申請微信公眾號 和 微信小程序蹬敲,這是兩個不同的東西,都需要單獨(dú)申請莺戒、不同的帳號伴嗡;
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 發(fā)送給到后臺瘪校,后臺用小程序的 appId ,secret 和code? 拿到openId
https://api.weixin.qq.com/sns/jscode2session?appid= APPID &secret= APP SECRET &grant_type=authorization_code&js_code= CODE
將登錄憑證轉(zhuǎn)換獲取openid
3.前端通過
https://???/???/weixin/api/getCardSign?cardId=' + cardId;(cardId為固定名段,在制作卡券的時(shí)候會有)
拿到timestamp 時(shí)間戳阱扬,signature簽名值,nonce_str隨機(jī)字符串(后臺人員根據(jù)AccessToken通過https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token="?+?token.getAccess_token()?+?"&type=wx_card吉嫩,然后做簽名价认,再返回給前端)
小程序中通過 wx.addCard 方法 領(lǐng)取卡券
具體微信小程序代碼:
var that = this;?
var service_url = 'https://???/???/weixin/api/getCardSign?cardId=' + cardId;//需要將服務(wù)器域名添加到小程序的request合法域名中,而且必須是https開頭 (拿到timestamp 時(shí)間戳自娩,signature簽名值用踩,nonce_str隨機(jī)字符串)
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參數(shù)的value值是 String類型,不要使用對象發(fā)送忙迁;另外openid如果在創(chuàng)建優(yōu)惠券的時(shí)候沒有指定脐彩,則這邊為空,千萬不要填寫當(dāng)前用戶的openid?
? ? ? ? success: function (result) {?
? ? ? ? ? console.log(res);?
? ? ? ? ? wx.showToast({?
? ? ? ? ? ? title: '領(lǐng)取成功',?
? ? ? ? ? ? icon: 'success',?
? ? ? ? ? ? duration: 2000?
? ? ? ? ? });?
? ? ? ? },?
? ? ? ? fail: function (res) {?
? ? ? ? ? console.log('領(lǐng)取失敗');?
? ? ? ? ? console.log(res);?
? ? ? ? }?
? ? ? })?
? }?
});?
更多關(guān)于后臺的java代碼:https://blog.csdn.net/zhourenfei17/article/details/77714600