前段时间公司有一个微信小程序的项目,其中有与卡券打通的功能,但是微信的官方文档实在是一言难尽。。。找了很多资料才解决这个问题,其中涉及到卡券的领取,卡券的核销等,在这里做一个小结方便使用。
用户可以在小程序内查看卡券,核销卡券。
微信的卡券事件是发生在公众号里的,由公众号发出卡券,用户领取时微信会推送事件给开发者,开发者可以通过该事件获取到用户的信息和卡券的信息。这一步可以将用户领取过的所有卡券记录下来,方便之后和用户一一对应,以便在小程序中找到用户拥有什么卡券。
//可以将获得的参数写入数据库以便后续操作
string eventType = rootElement.SelectSingleNode("Event") == null ? "" : rootElement.SelectSingleNode("Event").InnerText;
if (eventType == "user_get_card") {
HttpContext.Current.Response.Write("");
string FromUserName = rootElement.SelectSingleNode("FromUserName");
string CardId = rootElement.SelectSingleNode("CardId");
string IsGiveByFriend = rootElement.SelectSingleNode("IsGiveByFriend");
string UserCardCode = rootElement.SelectSingleNode("UserCardCode");
string FriendUserName = rootElement.SelectSingleNode("FriendUserName");
string OuterId = rootElement.SelectSingleNode("OuterId");
string OldUserCardCode = rootElement.SelectSingleNode("OldUserCardCode");
string OuterStr = rootElement.SelectSingleNode("OuterStr");
string IsRestoreMemberCard = rootElement.SelectSingleNode("IsRestoreMemberCard");
string IsRecommendByFriend = rootElement.SelectSingleNode("IsRecommendByFriend");
string UnionId = rootElement.SelectSingleNode("UnionId");
}
if (eventType == "user_del_card") {
HttpContext.Current.Response.Write("");
string FromUserName = rootElement.SelectSingleNode("FromUserName");
string CardId = rootElement.SelectSingleNode("CardId");
string UserCardCode = rootElement.SelectSingleNode("UserCardCode");
}
if (eventType == "user_consume_card") {
HttpContext.Current.Response.Write("");
string FromUserName = rootElement.SelectSingleNode("FromUserName");
string CardId = rootElement.SelectSingleNode("CardId");
string UserCardCode = rootElement.SelectSingleNode("UserCardCode");
string ConsumeSource = rootElement.SelectSingleNode("ConsumeSource");
}
之所以先把卡券推送事件写出来的原因是:微信公众号与微信小程序之间是靠 UnionId 来关联的,在绑定了同一个开放平台(微信开放平台)下的公众号和小程序,每一个用户都有一个独立的UnionId,这个UnionId在这些公众号和小程序下是相同的,因此可以通过UnionId来匹配到用户在公众号内领取到的卡券。
在得到卡券信息后,我们就可以通过小程序内用户授权拿到的UnionId,来判断用户有什么卡券了。
第一步需要先获取到用户的UnionId。
//1.先调用wx.login获取到登录的Code
wx.login({
success: function (res) {
let code = res.code;
}
})
//2.把这个Code作为参数,去获取登录态的Session_key
string code = "******";
string appid = "******";
string appsecret = "******";
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = Encoding.UTF8;
try {
string url = string.Format("https://api.weixin.qq.com/sns/jscode2session?appid={0}&secret={1}&js_code={2}&grant_type=authorization_code", appid, appsecret, code);
request = WebRequest.Create(url) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "GET";
request.ContentType = "application/x-www-form-urlencoded";
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
return content;
} catch (Exception ex) {
return ex;
}
//接着将返回的SessionKey和通过wx.getUserInfo接口获得的加密信息encryptedData和iv,通过解密得到UnionId。
string session_key = "******";
string encryptedData = "******";
string iv = "******";
try {
byte[] iv2 = Convert.FromBase64String(iv);
if (string.IsNullOrEmpty(encryptedData)) return“ 解密失败”;
Byte[] toEncryptArray = Convert.FromBase64String(encryptedData);
System.Security.Cryptography.RijndaelManaged rm = new System.Security.Cryptography.RijndaelManaged {
Key = Convert.FromBase64String(session_key),
IV = iv2,
Mode = System.Security.Cryptography.CipherMode.CBC,
Padding = System.Security.Cryptography.PaddingMode.PKCS7
};
System.Security.Cryptography.ICryptoTransform cTransform = rm.CreateDecryptor();
Byte[] resultArray = cTransform.TransformFinalBlock(toEncryptArray, 0, toEncryptArray.Length);
string ra = Encoding.UTF8.GetString(resultArray); //解密成功字符串,其中包括UnionId
return ra;
} catch (Exception ex) {
return ex;
}
在我们获取到UnionId之后,就已经完成了在小程序内查看卡包的功能。由于在之前的卡券推送事件中获取又到了卡券的Code和用户的unionid,又在小程序里得到了用户的unionid,在绑定同一开放平台的情况下,我们不难得出用户拥有哪些卡券,再调用核销接口,就可以核销掉用户的卡券了。
由于某些卡券是有条件的,所以我们可以调用接口查看卡券的详细信息进行筛选。
string cardid = "******";
string code = "******";
try {
string access_token = GetAccessToken();
string url = string.Format("https://api.weixin.qq.com/card/membercard/userinfo/get?access_token={0}", access_token);
string result = "";
HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder builder = new StringBuilder();
int i = 0;
string postData = "{\"card_id\":\"" + cardid + "\",\"code\":\"" + code + "\"}";
byte[] data = Encoding.UTF8.GetBytes(postData);
req.ContentLength = data.Length;
using(Stream reqStream = req.GetRequestStream()) {
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取响应内容
using(StreamReader reader = new StreamReader(stream, Encoding.UTF8)) {
result = reader.ReadToEnd();
}
return result;
} catch (Exception ex) {
return ex;
}
try {
string code = "******";
string unionid = "******";
string storeId = "******";
string token = GetAccessToken();
string url = string.Format("https://api.weixin.qq.com/card/code/consume?access_token={0}", token);
string result = "";
HttpWebRequest req = (HttpWebRequest) WebRequest.Create(url);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
StringBuilder builder = new StringBuilder();
int i = 0;
string postData = "{\"code\":\"" + code + "\"}";
byte[] data = Encoding.UTF8.GetBytes(postData);
req.ContentLength = data.Length;
using(Stream reqStream = req.GetRequestStream()) {
reqStream.Write(data, 0, data.Length);
reqStream.Close();
}
HttpWebResponse resp = (HttpWebResponse) req.GetResponse();
Stream stream = resp.GetResponseStream();
//获取响应内容
using(StreamReader reader = new StreamReader(stream, Encoding.UTF8)) {
result = reader.ReadToEnd();
}
return result;
} else {
return false;
}
} catch (Exception ex) {
return false;
}
至此,我们便完成了如何在小程序内查看并使用微信公众号领取的卡券。但是还有一种情况便是我们在小程序内领取会员卡,这里也一并写上。
//首先需要获取到会员卡的必要参数(这里用到了Senparc的DLL)
string access_token = "******";
string ticket = “******”; //只需要将access_token传入接口后便可获得,这里不再列出方法
string nonceStr = TenPayV3Util.GetNoncestr();
string timeStamp = TenPayV3Util.GetTimestamp();
string card_id = "******";
string signature = GetCardSignature(ticket, card_id, nonceStr, timeStamp, out signature);
string jsonStr = "{\"Ticket\":\"" + ticket + "\",\"NonceStr\":\"" + nonceStr + "\",\"TimeStamp\":\"" + timeStamp + "\",\"CardId\":\"" + card_id + "\",\"Signature\":\"" + signature.ToLower() + "\"}";
return jsonStr;
//获取签名
public string GetCardSignature(string api_ticket, string card_id, string noncestr, string timestamp, out string string1) {
List < string > ss = new List < string > () {
api_ticket,
timestamp,
noncestr,
card_id
};
var list = ss.OrderBy(x => x, StringComparer.Ordinal).ToArray();
string1 = string.Join("", list);
return SHA1(string1, Encoding.UTF8);
}
//SHA1解密
public static string SHA1(string content, Encoding encode) {
try {
SHA1 sha1 = new SHA1CryptoServiceProvider();
byte[] bytes_in = encode.GetBytes(content);
byte[] bytes_out = sha1.ComputeHash(bytes_in);
sha1.Dispose();
string result = BitConverter.ToString(bytes_out);
result = result.Replace("-", "");
return result;
} catch (Exception ex) {
throw new Exception("SHA1加密出错:" + ex.Message);
}
}
//将返回的参数传入wx.addCard接口即可
wx.addCard({
cardList: [
{
cardId: '',
cardExt: '{"code": "", "openid": "", "timestamp": "", "signature":""}'
}, {
cardId: '',
cardExt: '{"code": "", "openid": "", "timestamp": "", "signature":""}'
}
],
success (res) {
console.log(res.cardList) // 卡券添加结果
}
})
希望大家在完成卡券功能时都能迎刃而解~觉得有帮助的麻烦点个赞哈!