Skip to content

Commit 213bcde

Browse files
committed
提供跨应用单聊,修改会话id编码方式
1 parent b722bb5 commit 213bcde

5 files changed

Lines changed: 129 additions & 35 deletions

File tree

android/build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ apply plugin: 'com.android.library'
22

33
android {
44
compileSdkVersion 23
5-
buildToolsVersion "23.0.3"
5+
buildToolsVersion "23.0.1"
66

77
defaultConfig {
88
minSdkVersion 16
@@ -30,6 +30,7 @@ android {
3030

3131
dependencies {
3232
compile 'com.facebook.react:react-native:+'
33+
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.0'
3334
testCompile 'junit:junit:4.12'
3435
compile fileTree(include: ['*.jar'], dir: 'libs')
3536
}

android/src/main/java/com/xsdlr/rnjmessage/JMessageModule.java

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@
1313
import com.facebook.react.bridge.WritableArray;
1414
import com.facebook.react.bridge.WritableMap;
1515
import com.facebook.react.modules.core.DeviceEventManagerModule;
16+
import com.google.gson.Gson;
17+
import com.google.gson.JsonParseException;
18+
import com.google.gson.reflect.TypeToken;
19+
import com.xsdlr.rnjmessage.model.ConversationIDJSONModel;
20+
1621
import java.io.File;
1722
import java.io.FileNotFoundException;
1823
import java.util.HashMap;
@@ -152,8 +157,10 @@ public void myInfo(final Promise promise) {
152157
* @param promise
153158
*/
154159
@ReactMethod
155-
public void sendSingleMessage(String username, String type, ReadableMap data, final Promise promise) {
156-
Conversation conversation = Conversation.createSingleConversation(username);
160+
public void sendSingleMessage(String appkey, String username, String type, ReadableMap data, final Promise promise) {
161+
Conversation conversation = Utils.isEmpty(appkey)
162+
? Conversation.createSingleConversation(username)
163+
: Conversation.createSingleConversation(username, appkey);
157164
sendMessage(conversation, type, data, promise);
158165
}
159166
/**
@@ -329,20 +336,27 @@ private WritableMap transformToWritableMap(Conversation conversation) {
329336

330337
result.putString("laseMessage", getLastMessageContent(conversation));
331338
result.putInt("unreadCount", conversation.getUnReadMsgCnt());
339+
340+
ConversationIDJSONModel conversationIDJSONModel = new ConversationIDJSONModel();
341+
conversationIDJSONModel.setAppkey(conversation.getTargetAppKey());
342+
conversationIDJSONModel.setType(messagePropsToInt(conversation.getType()));
343+
332344
switch (conversation.getType()) {
333345
case single:
334346
UserInfo userInfo = (UserInfo)conversation.getTargetInfo();
335347
result.putString("username", userInfo.getUserName());
336-
result.putString("id", String.format("1|%s", userInfo.getUserName()));
348+
conversationIDJSONModel.setId(userInfo.getUserName());
337349
break;
338350
case group:
339351
GroupInfo groupInfo = (GroupInfo)conversation.getTargetInfo();
340352
result.putDouble("groupId", groupInfo.getGroupID());
341-
result.putString("id", String.format("2|%s", groupInfo.getGroupID()));
353+
conversationIDJSONModel.setId(String.valueOf(groupInfo.getGroupID()));
342354
break;
343355
default:
344356
break;
345357
}
358+
Gson gson = new Gson();
359+
result.putString("id", gson.toJson(conversationIDJSONModel));
346360
return result;
347361
}
348362

@@ -532,17 +546,28 @@ private Conversation getConversation(String cid) throws JMessageException {
532546
if (Utils.isEmpty(cid)) {
533547
throw JMessageException.CONVERSATION_ID_EMPTY;
534548
}
535-
if (cid.length() < 2 || cid.charAt(1) != '|') {
549+
ConversationIDJSONModel conversationIDJSONModel;
550+
try {
551+
Gson gson = new Gson();
552+
conversationIDJSONModel = gson.fromJson(cid, ConversationIDJSONModel.class);
553+
} catch (JsonParseException ex) {
554+
throw JMessageException.CONVERSATION_INVALID;
555+
}
556+
String appkey = conversationIDJSONModel.getAppkey();
557+
String nameOrGID = conversationIDJSONModel.getId();
558+
Integer type = conversationIDJSONModel.getType();
559+
560+
if (Utils.isEmpty(nameOrGID) || type == null) {
536561
throw JMessageException.CONVERSATION_INVALID;
537562
}
538-
String type = cid.substring(0, 1);
539-
String nameOrGID = cid.substring(2);
540563
Conversation conversation = null;
541564
switch (type) {
542-
case "1":
543-
conversation = Conversation.createSingleConversation(nameOrGID);
565+
case 1:
566+
conversation = Utils.isEmpty(appkey)
567+
? Conversation.createSingleConversation(nameOrGID)
568+
: Conversation.createSingleConversation(nameOrGID, appkey);
544569
break;
545-
case "2":
570+
case 2:
546571
conversation = Conversation.createGroupConversation(Long.valueOf(nameOrGID));
547572
break;
548573
default:
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.xsdlr.rnjmessage.model;
2+
3+
/**
4+
* Created by xsdlr on 2017/3/8.
5+
*/
6+
7+
public class ConversationIDJSONModel {
8+
public String id;
9+
public Integer type;
10+
public String appkey;
11+
12+
public String getId() {
13+
return id;
14+
}
15+
16+
public void setId(String id) {
17+
this.id = id;
18+
}
19+
20+
public Integer getType() {
21+
return type;
22+
}
23+
24+
public void setType(Integer type) {
25+
this.type = type;
26+
}
27+
28+
public String getAppkey() {
29+
return appkey;
30+
}
31+
32+
public void setAppkey(String appkey) {
33+
this.appkey = appkey;
34+
}
35+
}

ios/RCTJMessage/RCTJMessageModule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#import <UIKit/UIKit.h>
1111
#import <JMessage/JMessage.h>
1212
#import <JMessage/JMessageDelegate.h>
13+
#import "NSJSONSerialization+JSONString.h"
1314

1415
#if __has_include("RCTEventEmitter.h")
1516
#import "RCTEventEmitter.h"

ios/RCTJMessage/RCTJMessageModule.m

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,7 @@ - (void)onReceiveMessageDownloadFailed:(JMSGMessage *)message {
204204
* image为{image: ''}
205205
*/
206206
RCT_EXPORT_METHOD(sendSingleMessage
207+
:(NSString*)appkey
207208
:(NSString*)username
208209
:(NSString*)type
209210
:(NSDictionary*)data
@@ -217,8 +218,9 @@ - (void)onReceiveMessageDownloadFailed:(JMSGMessage *)message {
217218
reject([@(error.code) stringValue], error.localizedDescription, error);
218219
return;
219220
}
220-
[self sendMessageWithUserNameOrGID:username
221-
isSingle:@YES
221+
[self sendMessageWithUserNameOrGID:appkey
222+
name:username
223+
isSingle:YES
222224
contentType:type
223225
data:data
224226
timeout:0
@@ -250,8 +252,9 @@ - (void)onReceiveMessageDownloadFailed:(JMSGMessage *)message {
250252
reject([@(error.code) stringValue], error.localizedDescription, error);
251253
return;
252254
}
253-
[self sendMessageWithUserNameOrGID:groupId
254-
isSingle:@NO
255+
[self sendMessageWithUserNameOrGID:nil
256+
name:groupId
257+
isSingle:NO
255258
contentType:type
256259
data:data
257260
timeout:0
@@ -302,14 +305,15 @@ - (void)onReceiveMessageDownloadFailed:(JMSGMessage *)message {
302305
switch (conversation.conversationType) {
303306
case kJMSGConversationTypeSingle:
304307
username = ((JMSGUser*) conversation.target).username;
305-
isSingle = @YES;
308+
isSingle = YES;
306309
break;
307310
case kJMSGConversationTypeGroup:
308311
username = ((JMSGGroup*) conversation.target).gid;
309-
isSingle = @NO;
312+
isSingle = NO;
310313
break;
311314
}
312-
[self sendMessageWithUserNameOrGID:username
315+
[self sendMessageWithUserNameOrGID:nil
316+
name:username
313317
isSingle:isSingle
314318
contentType:type
315319
data:data
@@ -346,21 +350,35 @@ - (void)onReceiveMessageDownloadFailed:(JMSGMessage *)message {
346350
{
347351
JMSGUser *userInfo = conversation.target;
348352
username = userInfo.username;
349-
cid = [NSString stringWithFormat:@"%@|%@", @(conversation.conversationType), username];
353+
NSData *data = [NSJSONSerialization dataWithJSONObject:@{
354+
@"appkey": OPTION_NULL(conversation.targetAppKey),
355+
@"type": @(conversation.conversationType),
356+
@"id": username
357+
}
358+
options:NSJSONWritingPrettyPrinted
359+
error:nil];
360+
cid = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
350361
}
351362
break;
352363
case kJMSGConversationTypeGroup:
353364
{
354365
JMSGGroup *groupInfo = conversation.target;
355366
groupId = groupInfo.gid;
356-
cid = [NSString stringWithFormat:@"%@|%@", @(conversation.conversationType), groupId];
367+
NSData *data = [NSJSONSerialization dataWithJSONObject:@{
368+
@"appkey": OPTION_NULL(conversation.targetAppKey),
369+
@"type": @(conversation.conversationType),
370+
@"id": groupId
371+
}
372+
options:NSJSONWritingPrettyPrinted
373+
error:nil];
374+
cid = [data base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
357375
}
358376
break;
359377
default:
360378
break;
361379
}
362380

363-
[result addObject:@{@"id": cid,
381+
[result addObject:@{@"id": OPTION_NULL(cid),
364382
@"type": @(conversation.conversationType),
365383
@"typeDesc": typeDesc,
366384
@"username": OPTION_NULL(username),
@@ -593,7 +611,8 @@ - (NSDictionary*) getTargetWithMessage:(JMSGMessage *)message {
593611
@param resolve 成功回调
594612
@param reject 失败回调
595613
*/
596-
- (void) sendMessageWithUserNameOrGID:(NSString *)name
614+
- (void) sendMessageWithUserNameOrGID:(NSString*)appkey
615+
name:(NSString*)name
597616
isSingle:(BOOL)isSingle
598617
contentType:(NSString*)type
599618
data:(NSDictionary*)data
@@ -610,7 +629,8 @@ - (void) sendMessageWithUserNameOrGID:(NSString *)name
610629
reject([@(error.code) stringValue], error.localizedDescription, error);
611630
return;
612631
}
613-
[self createConversationIsSingle:isSingle
632+
[self createConversationWithAppKey:appkey
633+
isSingle:isSingle
614634
nameOrGID:name
615635
completionHandler:^(id resultObject, NSError *error) {
616636
if (!error) {
@@ -636,7 +656,8 @@ - (void) sendMessageWithUserNameOrGID:(NSString *)name
636656
reject([@(error.code) stringValue], error.localizedDescription, error);
637657
return;
638658
}
639-
[self createConversationIsSingle:isSingle
659+
[self createConversationWithAppKey:appkey
660+
isSingle:isSingle
640661
nameOrGID:name
641662
completionHandler:^(id resultObject, NSError *error) {
642663
if (!error) {
@@ -654,7 +675,6 @@ - (void) sendMessageWithUserNameOrGID:(NSString *)name
654675
reject([@(error.code) stringValue], error.localizedDescription, error);
655676
}
656677
}];
657-
NSLog(@"%@", imageURL);
658678
} else {
659679
NSError *error = [[NSError alloc] initWithDomain:@""
660680
code:kJMSGErrorRNMessageProtocolContentTypeNotSupport
@@ -671,11 +691,16 @@ - (void) sendMessageWithUserNameOrGID:(NSString *)name
671691
@param name 对方用户名(群号)
672692
@param handler 回调
673693
*/
674-
- (void) createConversationIsSingle:(BOOL)isSingle
694+
- (void) createConversationWithAppKey:(NSString*)appkey
695+
isSingle:(BOOL)isSingle
675696
nameOrGID:(NSString*)name
676697
completionHandler:(JMSGCompletionHandler JMSG_NULLABLE)handler {
677698
if (isSingle) {
678-
[JMSGConversation createSingleConversationWithUsername:name completionHandler:handler];
699+
if(appkey) {
700+
[JMSGConversation createSingleConversationWithUsername:name appKey:appkey completionHandler:handler];
701+
} else {
702+
[JMSGConversation createSingleConversationWithUsername:name completionHandler:handler];
703+
}
679704
} else {
680705
[JMSGConversation createGroupConversationWithGroupId:name completionHandler:handler];
681706
}
@@ -724,26 +749,33 @@ - (void) detectConversationValidById:(NSString*)cid
724749
userInfo:@{NSLocalizedDescriptionKey: @"会话无效"
725750
}];
726751

727-
NSArray<NSString*> *params = [cid componentsSeparatedByString:@"|"];
728-
if (params.count < 2) {
752+
NSData *data = [[NSData alloc] initWithBase64EncodedString:cid options:NSDataBase64DecodingIgnoreUnknownCharacters];
753+
NSDictionary* dataDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
754+
755+
NSString *appkey = [dataDic valueForKey:@"appkey"];
756+
NSNumber *type = [dataDic valueForKey:@"type"];
757+
NSString *nameOrGID = [dataDic valueForKey:@"id"];
758+
759+
if (!nameOrGID) {
729760
handler(nil, conversationInvalidError);
730761
return;
731762
}
732763
BOOL isSingle;
733-
NSInteger type = [[params objectAtIndex:0] integerValue];
734-
NSString *nameOrGID = [[params subarrayWithRange:(NSMakeRange(1, [params count] - 1))] componentsJoinedByString:@"|"];
735-
switch (type) {
764+
switch ([type integerValue]) {
736765
case kJMSGConversationTypeSingle:
737-
isSingle = @YES;
766+
isSingle = YES;
738767
break;
739768
case kJMSGConversationTypeGroup:
769+
isSingle = NO;
740770
break;
741-
isSingle = @NO;
742771
default:
743772
handler(nil, conversationInvalidError);
744773
return;
745774
}
746-
[self createConversationIsSingle:isSingle nameOrGID:nameOrGID completionHandler:^(id resultObject, NSError *error) {
775+
[self createConversationWithAppKey:appkey
776+
isSingle:isSingle
777+
nameOrGID:nameOrGID
778+
completionHandler:^(id resultObject, NSError *error) {
747779
if (!error) {
748780
JMSGConversation *conversation = resultObject;
749781
handler(conversation, nil);

0 commit comments

Comments
 (0)