前言
在 微信抢红包(四) 中,我们已经获取了抢红包所必备的所有参数,那接下来拿到参数调用抢红包的方法 OpenRedEnvelopesRequest
即实现了自动抢红包的功能了是吗???
完成自动抢红包
参数的获取:
在 微信抢红包(四) 中我们知道这个方法可以获取到 红包的关键参数 timingIdentifier
— (void)OnWCToHongbaoCommonResponse:(HongBaoRes *)arg1 Request:(HongBaoReq *)arg2
arg1.retText.buffer 为 NSData 类型,那我们直接转换为字典:
NSError *error = nil;
NSDictionary *responseDic = [NSJSONSerialization JSONObjectWithData:arg1.retText.buffer options:(NSJSONReadingMutableContainers) error:&error];
打印结果:
Printing description of responseDic:
{
hbStatus = 4;
hbType = 0;
isSender = 0;
receiveStatus = 2;
retcode = 0;
retmsg = ok;
sendId = 1000039501201808076022437245004;
sendUserName = "wxid_erhumb6yg9yu22";
statusMess = "";
timingIdentifier = 02F382D28FCB7B2DC54F8E1065A8139F;
watermark = "";
wishing = "\U606d\U559c\U53d1\U8d22\Uff0c\U5927\U5409\U5927\U5229";
}
- 部分参数说明:
receiveStatus:0 自己没有抢的未拆开
receiveStatus:2 自己抢过的已拆开
hbStatus:2 未拆开
hbStatus:4 已拆开的
HongBaoRes 类中 的 cgiCmdid 是不停变化的
拆开没被抢过的红包时 cgiCmdid 字段是 3
isSender 判断是不是自己发的红包
自己发的红包 isSender : 1
别人发的红包 isSender : 0
- 获取 timingIdentifier:
if (arg1 != nil && arg2 != nil && arg1.cgiCmdid == 3){
// 判断没有抢过的红包
NSString *timingIdentifier = [responseDic objectForKey:@"timingIdentifier"];
NSLog(@"timingIdentifier--%@",timingIdentifier);
}
拆红包的参数:
CMessageWrap 中的
@property(retain, nonatomic) NSString *m_nsFromUsr; // @synthesize m_nsFromUsr;
收到红包时
- 如果为个人的红包,m_nsFromUsr == sessionUserName
- 如果为群红包,m_nsFromUsr == sessionUserName
但是 sessionUserName == 群id + @chatroom?
接着修改手动拆红包的参数:
并存储红包消息
// 存储红包消息
WeChatRedEnveLopParamQueue *paramQueue = [WeChatRedEnveLopParamQueue sharedQueue];
[paramQueue addParaDic:mutableDic];
接下来就是关键的抢红包了,在 手动调用拆红包的方法响应的方法
- (void)OnWCToHongbaoCommonResponse:(HongBaoRes *)arg1 Request:(HongBaoReq *)arg2
中实现抢红包的逻辑:
测试一下,发送一个红包,控制台输出结果:
抢红包的关键参数:
// 获取 timingIdentifier 前
Printing description of paraDic:
{
channelId = 1;
headImg = "http://wx.qlogo.cn/mmhead/ver_1/kS0Y8uchoAJFSCicXtojNribrR5GJQVH5JwR4Pap49a52vbT9lA4MSLz2nxK9mtUH1UvRQAm9T1YOMgHl0Tx6Ne0Ss0FlYgnEjLibohLOobNBQ/132";
msgType = 1;
nativeUrl = "wxpay://c2cbizmessagehandler/hongbao/receivehongbao?msgtype=1&channelid=1&sendid=1000039501201808096015609992038&sendusername=wxid_erhumb6yg9yu22&ver=6&sign=c6720e58adaccc250dad76bc9336970e2df2bb05a549b68014b5f9196bc8f8b867bf33ab268c678b073b353fa459125bd558aa53e808dfff81115a11b17d3388320fa28ce17cf91c553dc313c7722811";
nickName = UniversalSuperman;
sendId = 1000039501201808096015609992038;
sessionUserName = "wxid_erhumb6yg9yu22";
}
// 添加 timingIdentifier 之后
Printing description of paraDic:
{
channelId = 1;
headImg = "http://wx.qlogo.cn/mmhead/ver_1/kS0Y8uchoAJFSCicXtojNribrR5GJQVH5JwR4Pap49a52vbT9lA4MSLz2nxK9mtUH1UvRQAm9T1YOMgHl0Tx6Ne0Ss0FlYgnEjLibohLOobNBQ/132";
msgType = 1;
nativeUrl = "wxpay://c2cbizmessagehandler/hongbao/receivehongbao?msgtype=1&channelid=1&sendid=1000039501201808096015609992038&sendusername=wxid_erhumb6yg9yu22&ver=6&sign=c6720e58adaccc250dad76bc9336970e2df2bb05a549b68014b5f9196bc8f8b867bf33ab268c678b073b353fa459125bd558aa53e808dfff81115a11b17d3388320fa28ce17cf91c553dc313c7722811";
nickName = UniversalSuperman;
sendId = 1000039501201808096015609992038;
sessionUserName = "wxid_erhumb6yg9yu22";
timingIdentifier = B00D4746BCF13661A0E518ECC1AFEA87;
}
OK,大功告成, 成功实现了自动抢红包功能!!!一遍通过,颇为顺利!!!
完善抢红包插件,细节处理
接收到消息时是否开启插件的判断:
抢红包设置时间优化:
以及功能分类整理,优化等。