红包接入文档
红包功能是基于容联云通讯的即时通讯,接入金融魔方的 IM 红包SDK,实现App快速便捷地集成发单聊红包,发群聊红包,拆红包并查看交易记录等功能。
进行如下集成之前,请确认项目中已经导入了金融魔方红包SDK。如果还想集成钱包功能,请集成钱包SDK。
1、准备工作
1. 启动Xcode,把iOS包中jrmfLib 文件夹拷贝到项目文件夹下,并导入到项目工程中。
2.在Xcode中的Target -> Build Phase Link Binary With Libraries 下添加如下依赖库:
3. 在需要的地方导入头文件。
- #import <JrmfPacketKit/JrmfPacketKit.h> #红包功能
- #import <JrmfWalletKit/JrmfWalletKit.h> #钱包功能
4.在 AppDelegate中添加如下配置:
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
- {
- // 微信支付
- [[SPayClient sharedInstance] application:application didFinishLaunchingWithOptions:launchOptions;
- }
- - (BOOL)application:( UIApplication*)application openURL:(NSURL*)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
- {
- if ([url.host isEqualToString:@"safepay"]) {
- // 跳转支付宝钱包进行支付,处理支付结果
- [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
- if ([[resultDic objectForKey:@"resultStatus"] isEqualToString:@"9000"])
- {
- [JrmfPacket doActionAlipayDone];
- }
- }];
- }
- // 微信支付
- [[SPayClient sharedInstance] application:application openURL:url sourceApplication:sourceApplication:sourceApplication annotation:annotation;
- return YES;
- }
- // NOTE: 9.0 以后使用新API接口
- - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
- {
- if ([url.host isEqualToString:@"safepay"]) {
- // 跳转支付宝钱包进行支付,处理支付结果
- [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
- if ([[resultDic objectForKey:@"resultStatus"] isEqualToString:@"9000"])
- {
- [JrmfPacket doActionAlipayDone];
- }
- }];
- }
- // 微信支付
- [[SPayClient sharedInstance]application:app openURL:url options:options];
- return YES;
- }
- - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL*)url {
- // 微信支付
- return [[SPayClient sharedInstance] application:application handleOpenURL:url];
- }
2、集成流程
2.1 获取token
红包token通过服务器接口获取,参考文档地址:rest接口地址
- //登录SDK成功后获取红包校验token
- [[ECDevice sharedInstance] login:loginInfo completion:^(ECError *error){
- if (error.errorCode == ECErrorType_NoError) {
- [[AppDelegate sharedInstance] ec_configRedpacket];//具体获取流程查看demo Appdelegate+RedpacketConfig.h
- }
- }];
2.2 发红包
- /**
- * 发红包
- * @param viewController 当前视图
- * @param thirdToken 三方签名令牌,即第一步获取的token
- * @param isGroup 是否为群组红包
- * @param reveiveID 接受者ID(单人红包:接受者 用户唯一标识;群红包:群组ID,唯一标识)
- * @param userName 发送者昵称
- * @param userHeadLink 发送者头像链接
- * @param userId 发送者ID
- * @param groupNum 群人数 (个人红包可不传)
- A. 三方签名令牌
- ( 服务端计算后给到app ,服务端算法为 md5 【 custUid+appsecret 】 )
- B. 用户头像字符串限制在 260 个字符内【外网可访问】【下同】
- */
- - (void)doActionPresentSendRedEnvelopeViewController:(UIViewController *)vie wController thirdToken:(NSString *)thirdToken withGroup:(BOOL)isGroup receiv eID:(NSString *)receiveID sendUserName:(NSString *)userName sendUserHead:(NS String *)userHeadLink sendUserID:(NSString *)userId groupNumber:(NSString *) groupNum;
- // 调用实现
- - (void)sendRedpacketMessage {
- JrmfPacket * jrmf = [[Jrmf JrmfPacket ;
- jrmf.delegate = self;
- [jrmf doActionPresentSendRedEnvelopeViewController:vc thirdToken:thirdToken withGroup:isGroup receiveID:@"receiverId" sendUserName:@"nickName" sendUserHead:@"发送者头像" sendUserID:@“senderId” groupNumber:groupNum];
- }
2.3 发送红包成功回调接口
遵循金融魔方协议 jrmfManagerDelegate ,实现对应回调方法。
在回调方法中给接收红包方发送消息,SDK暂未支持红包消息,通过在文本消息中添加userdata,标识是否是红包消息,接收方对应解析(具体参考demo)
- - (void)dojrmfActionDidSendEnvelopedWithID:(NSString *)envId Name:(NSString*)envName Message:(NSString *)envMsg Stat:(jrmfSendStatus)jrmfStat packType:(JrmfRedPacketType)type {
- //jrmfStat 红包发送状态
- if(jrmfStat == kjrmfStatSucess){
- NSString *text = envMsg;
- NSDictionary *userDic =@{@"check_rp_msg":@(YES), @"rp_name":envName,
- @"rp_msg":envMsg, @"rp_id":envId, @"send_rp_id":@"senderId", @"send_rp_name
- ":@"senderName"};
- NSString *userData = [[NSString alloc] initWithData:[NSJSONSerializa
- tion dataWithJSONObject:userDic options:0 error:nil] encoding:NSUTF8StringEn
- coding];
- ECTextMessageBody *messageBody = [[ECTextMessageBody alloc] initWith
- Text:text];
- ECMessage *message = [[ECMessage alloc] initWithReceiver:[ECAppInfo
- sharedInstanced].sessionMgr.session.sessionId body:messageBody];
- message.userData = userData;
- [[ECDevice sharedInstance].messageManager sendMessage:message progre
- ss:nil completion:^(ECError *error, ECMessage *amessage) {
- if (error.errorCode == ECErrorType_NoError) {
- // 消息发送成功回调处理
- } else if(error.errorCode == ECErrorType_Have_Forbid || error.err
- orCode == ECErrorType_File_Have_Forbid){//禁言处理
- // 您已被群组禁言
- } else{// 其他错误处理
- // 发送失败
- }
- }];
- }
- }
2.4 拆红包
- /**
- * 拆红包
- * @param viewController 当前视图
- * @param thirdToken 三方签名令牌,即第一步获取的token
- * @param userName 当前操作用户姓名
- * @param userHeadLink 发送者头像链接
- * @param userId 当前操作用户ID
- * @param envelopeId 红包Id
- * @param isGroup 是否为群组红包
- */
- - (void)doActionPresentOpenViewController:(UIViewController *)viewController thirdToken:(NSString *)thirdToken withUserName:(NSString *)userName userHead:(NSString *)userHeadLink userID:(NSString *)userId envelopeID:(NSString *)envelopeId isGroup:(BOOL)isGroup;
- // 调用实现
- - (void)openRedpacket:(ECMessage *)msg{
- JrmfPacket * jrmf = [[Jrmf JrmfPacket ;
- jrmf.delegate = self;
- // 红包相关信息放在userData里
- NSData *data = [self.userData dataUsingEncoding:NSUTF8StringEncoding];
- if (data.length < 1) {
- return nil;
- }
- NSDictionary *redPacketDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
- NSString *envId = redPacketDic[@"rp_id"];
- self.redpacketMessage = msg;
- [jrmf doActionPresentOpenViewController:vc thirdToken:redpackageToken withUserName:@"拆红包用户名" userHead:@"头像链接" userID:@"用户id" envelopeID:envId isGroup:isGroup];
- }
2.5 拆红包成功/失败回调
实现jrmfManagerDelegate对应的拆红包回调方法。
- //成功领取一个红包的回调
- - (void)dojrmfActionOpenPacketSuccessWith:(NSInteger)hasLeft total:(NSIntege r)total totalMoney:(NSString *)totalMoney grabMoney:(NSString *)grabMoney{
- NSString *receiver = [ECAppInfo sharedInstanced].sessionMgr.session.sessionId;
- NSDictionary *msgDic = [self.redpacketMessage redPacketDic];
- NSString *text = [NSString stringWithFormat:@"[容联云红包]"];
- NSDictionary *userDic =@{EC_rp_is_finish:hasLeft <= 0?@(YES):@(NO), EC_c
- heck_rp_ack_msg:@(YES), EC_send_rp_name:msgDic[EC_send_rp_name], EC_send_rp_id:msgDic[EC_send_rp_id], EC_receive_rp_name:[ECDevicePersonInfo sharedInstanced].nickName, EC_receive_rp_id:[ECDevicePersonInfo sharedInstanced].userName};
- // NSDictionary *userDic =@{EC_rp_is_finish:hasLeft <= 0?@(YES):@(NO), EC_check_rp_ack_msg:@(YES), EC_send_rp_name:msgDic[EC_send_rp_name], EC_send_rp_id:msgDic[EC_send_rp_id], EC_receive_rp_name:[ECAppInfo sharedInstanced].sessionMgr.session.sessionName, EC_receive_rp_id:receiver};
- NSString *userData = [[NSString alloc] initWithData:[NSJSONSerialization dataWithJSONObject:userDic options:0 error:nil] encoding:NSUTF8StringEncoding];
- ECTextMessageBody *messageBody = [[ECTextMessageBody alloc] initWithText:text];
- ECMessage *message = [[ECMessage alloc] initWithReceiver:receiver body:messageBody];
- message.userData = userData;
- [[ECDevice sharedInstance].messageManager sendMessage:message progress:nil completion:^(ECError *error, ECMessage *amessage) {
- if (error.errorCode == ECErrorType_NoError) {//消息发送成功回调处理
- }else if(error.errorCode == ECErrorType_Have_Forbid || error.errorCode == ECErrorType_File_Have_Forbid){//禁言处理
- // 您已被群组禁言
- }else{// 其他错误处理
- // 发送失败
- }
- }];
- }
- // 领取红包状态的回调
- - (void)dojrmfActionGetPacketStatus:(JRMFPacketStatusType)type{
- switch (type) {
- case JRMFPacketCanGet:
- break;
- case JRMFPacketIsGet:
- break;
- case JRMFPacketIsDue:
- [MBProgressHUD toast:@" "];
- break;
- case JRMFPacketIsNull:
- [MBProgressHUD toast:@" "];
- break;
- case JRMFPacketIsCommon:
- break;
- default:
- break;
- }
- }
- // 对应状态定义
- typedef NS_ENUM(NSUInteger, JRMFPacketStatusType) {
- JRMFPacketCanGet = 0, //可领取
- JRMFPacketIsGet, //已领取
- JRMFPacketIsDue, //红包失效
- JRMFPacketIsNull, //手慢l ,红包被抢完了
- JRMFPacketIsCommon, //普通红包,自己不可抢
- JRMFPacketIsUn, //未知,获取红包信息失败
2.6 钱包
引入头文件:
- #import <JrmfWalletKit/JrmfWalletKit.h>
查看钱包页面
- /**
- 调用钱包页面
- @param baseViewController 基础视图
- @param userId 当前用户ID(接入方APP用户的唯一标识)
- @param userName 用户昵称
- @param avatarLink 用户头像链接(用户头像字符串限制在260个字符内)
- @param thirdToken 第三方签名令牌,即第一步获取的token
- */
- - (void)doPresentJrmfWalletPageWithBaseViewController:(UIViewController *)baseViewController userId:(NSString *)userId userName:(NSString *)userName userHeadLink:(NSString *)avatarLink thirdToken:(NSString *)thirdToken;
- JrmfWalletSDK * jrmf = [[JrmfWalletSDK alloc] init];
- [jrmf doPresentJrmfWalletPageWithBaseViewController:self userId:@"用户id" userName:@"用户名" userHeadLink:@"用户头像链接" thirdToken:redpackageToken];
2.7 更新信息
当用户信息更新时,及时更新金融魔方对应的信息。
- /**
- * 用户信息更更新
- * @param userId 用户ID(app用户的唯一标识)
- * @param userName 用户昵称
- * @param userHeadLink 用户头像
- * @param thirdToken 三方签名令牌,即第一步获取的token
- * @param completionAction 回调函数
- * @discussion
- A. 用户昵称、头像可单独更新,非更新传 nil 即可,但不可两者同时为 nil ;
- B. 头像的 URL 连接字符不要过长,不超过 256 个字符为宜。(所有头像链接都需要限制)【注 : 外网可访问】【下同】
- */
- + (void)updateUserMsgWithUserId:(NSString *)userId userName:(NSString *)userName userHead:(NSString *)userHeadLink thirdToken:(NSString *)thirdToken completion:(void (^)(NSError *error, NSDictionary *resultDic))completionAction;
- - (void)updateRedpacketUserInfo{
- [JrmfPacket updateUserMsgWithUserId:@"用户id" userName:@"用户名" userHead:@"用户头像" thirdToken:redpackageToken completion:^(NSError *error, NSDiction
- ary *resultDic) {
- }];
- }