FlutterQiyuPlugin.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. #import <UIKit/UIKit.h>
  2. #import <NIMSDK/NIMSDK.h>
  3. #import <QYSDK/QYSDK.h>
  4. #import "FlutterQiyuPlugin.h"
  5. #import "UIBarButtonItem+blocks.h"
  6. @interface FlutterQiyuPlugin () <QYConversationManagerDelegate>
  7. @end
  8. @implementation FlutterQiyuPlugin
  9. + (void)registerWithRegistrar:(NSObject <FlutterPluginRegistrar> *)registrar {
  10. // 高版本ios需要配置以下两行才能发送图片
  11. [NIMSDK sharedSDK].serverSetting.nosUploadAddress = @"https://nosup-hz1.127.net";
  12. [NIMSDK sharedSDK].serverSetting.nosUploadHost = @"nosup-hz1.127.net";
  13. FlutterMethodChannel *channel = [FlutterMethodChannel
  14. methodChannelWithName:@"flutter_qiyu"
  15. binaryMessenger:[registrar messenger]];
  16. UIViewController *viewController = [UIApplication sharedApplication].delegate.window.rootViewController;
  17. FlutterQiyuPlugin *instance = [[FlutterQiyuPlugin alloc] initWithViewController:viewController];
  18. [registrar addMethodCallDelegate:instance channel:channel];
  19. [registrar addApplicationDelegate:instance];
  20. instance.channel = channel;
  21. [[[QYSDK sharedSDK] conversationManager] setDelegate:instance];
  22. [[QYSDK sharedSDK] registerPushMessageNotification:^(QYPushMessage *message) {
  23. // TODO:
  24. }];
  25. }
  26. - (instancetype)initWithViewController:(UIViewController *)viewController {
  27. self = [super init];
  28. if (self) {
  29. self.viewController = viewController;
  30. }
  31. return self;
  32. }
  33. - (void)handleMethodCall:(FlutterMethodCall *)call result:(FlutterResult)result {
  34. NSDictionary *options = call.arguments;
  35. if ([@"getPlatformVersion" isEqualToString:call.method]) {
  36. result([@"iOS " stringByAppendingString:[[UIDevice currentDevice] systemVersion]]);
  37. } else if ([@"registerApp" isEqualToString:call.method]) {
  38. NSString *appKey = call.arguments[@"appKey"];
  39. NSString *appName = call.arguments[@"appName"];
  40. [self registerApp:appKey appName:appName];
  41. result([NSNumber numberWithBool:YES]);
  42. } else if ([@"openServiceWindow" isEqualToString:call.method]) {
  43. [self openServiceWindow:options];
  44. result([NSNumber numberWithBool:YES]);
  45. } else if ([@"setCustomUIConfig" isEqualToString:call.method]) {
  46. [self setCustomUIConfig:options];
  47. result([NSNumber numberWithBool:YES]);
  48. } else if ([@"getUnreadCount" isEqualToString:call.method]) {
  49. NSInteger *unreadCount = [self getUnreadCount];
  50. result([NSNumber numberWithInteger:unreadCount]);
  51. } else if ([@"setUserInfo" isEqualToString:call.method]) {
  52. [self setUserInfo:options];
  53. result([NSNumber numberWithBool:YES]);
  54. } else if ([@"logout" isEqualToString:call.method]) {
  55. [self logout];
  56. result([NSNumber numberWithBool:YES]);
  57. } else if ([@"cleanCache" isEqualToString:call.method]) {
  58. [self cleanCache];
  59. result([NSNumber numberWithBool:YES]);
  60. } else {
  61. result(FlutterMethodNotImplemented);
  62. }
  63. }
  64. - (void)registerApp:(NSString *)appKey
  65. appName:(NSString *)appName {
  66. [[QYSDK sharedSDK] registerAppId:appKey appName:appName];
  67. }
  68. - (void)openServiceWindow:(NSDictionary *)options {
  69. NSDictionary *paramDict = options;
  70. QYSessionViewController *sessionVC = [[QYSDK sharedSDK] sessionViewController];
  71. QYSource *source = nil;
  72. if ([paramDict objectForKey:@"source"]) {
  73. NSDictionary *sourceDict = [paramDict objectForKey:@"source"];
  74. if ([sourceDict objectForKey:@"sourceTitle"] || [sourceDict objectForKey:@"sourceUrl"]
  75. || [sourceDict objectForKey:@"sourceCustomInfo"]) {
  76. source = [[QYSource alloc] init];
  77. if ([sourceDict objectForKey:@"sourceTitle"]) {
  78. source.title = [sourceDict objectForKey:@"sourceTitle"];
  79. }
  80. if ([sourceDict objectForKey:@"sourceUrl"]) {
  81. source.urlString = [sourceDict objectForKey:@"sourceUrl"];
  82. }
  83. if ([sourceDict objectForKey:@"sourceCustomInfo"]) {
  84. source.customInfo = [sourceDict objectForKey:@"sourceCustomInfo"];
  85. }
  86. }
  87. }
  88. QYCommodityInfo *commodityInfo = nil;
  89. if ([paramDict objectForKey:@"commodityInfo"]) {
  90. NSDictionary *commodityInfoDict = [paramDict objectForKey:@"commodityInfo"];
  91. if ([commodityInfoDict objectForKey:@"commodityInfoTitle"] ||
  92. [commodityInfoDict objectForKey:@"commodityInfoDesc"]
  93. || [commodityInfoDict objectForKey:@"pictureUrl"] ||
  94. [commodityInfoDict objectForKey:@"commodityInfoUrl"]
  95. || [commodityInfoDict objectForKey:@"note"] || [commodityInfoDict objectForKey:@"show"]
  96. || [commodityInfoDict objectForKey:@"sendByUser"]) {
  97. commodityInfo = [[QYCommodityInfo alloc] init];
  98. if ([commodityInfoDict objectForKey:@"commodityInfoTitle"]) {
  99. commodityInfo.title = [commodityInfoDict objectForKey:@"commodityInfoTitle"];
  100. }
  101. if ([commodityInfoDict objectForKey:@"commodityInfoDesc"]) {
  102. commodityInfo.desc = [commodityInfoDict objectForKey:@"commodityInfoDesc"];
  103. }
  104. if ([commodityInfoDict objectForKey:@"pictureUrl"]) {
  105. commodityInfo.pictureUrlString = [commodityInfoDict objectForKey:@"pictureUrl"];
  106. }
  107. if ([commodityInfoDict objectForKey:@"commodityInfoUrl"]) {
  108. commodityInfo.urlString = [commodityInfoDict objectForKey:@"commodityInfoUrl"];
  109. }
  110. if ([commodityInfoDict objectForKey:@"note"]) {
  111. commodityInfo.note = [commodityInfoDict objectForKey:@"note"];
  112. }
  113. if ([commodityInfoDict objectForKey:@"show"]) {
  114. commodityInfo.show = [[commodityInfoDict objectForKey:@"show"] boolValue];
  115. }
  116. if ([commodityInfoDict objectForKey:@"sendByUser"]) {
  117. commodityInfo.sendByUser = [[commodityInfoDict objectForKey:@"sendByUser"] boolValue];
  118. }
  119. }
  120. }
  121. if (source) {
  122. sessionVC.source = source;
  123. }
  124. if (commodityInfo) {
  125. sessionVC.commodityInfo = commodityInfo;
  126. }
  127. if ([paramDict objectForKey:@"sessionTitle"]) {
  128. sessionVC.sessionTitle = [paramDict objectForKey:@"sessionTitle"];
  129. }
  130. if ([paramDict objectForKey:@"groupId"]) {
  131. sessionVC.groupId = [[paramDict objectForKey:@"groupId"] intValue];
  132. }
  133. if ([paramDict objectForKey:@"staffId"]) {
  134. sessionVC.staffId = [[paramDict objectForKey:@"staffId"] intValue];
  135. }
  136. if ([paramDict objectForKey:@"robotId"]) {
  137. sessionVC.robotId = [[paramDict objectForKey:@"robotId"] intValue];
  138. }
  139. if ([paramDict objectForKey:@"vipLevel"]) {
  140. sessionVC.vipLevel = [[paramDict objectForKey:@"vipLevel"] intValue];
  141. }
  142. if ([paramDict objectForKey:@"robotFirst"]) {
  143. sessionVC.openRobotInShuntMode = [paramDict objectForKey:@"robotFirst"];
  144. }
  145. if ([paramDict objectForKey:@"faqTemplateId"]) {
  146. sessionVC.commonQuestionTemplateId = [[paramDict objectForKey:@"faqTemplateId"] intValue];
  147. }
  148. [sessionVC.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain actionHandler:^{
  149. [self.viewController dismissViewControllerAnimated:true completion:nil];
  150. }]];
  151. UINavigationController *rootNavigationController = [[UINavigationController alloc] initWithRootViewController:sessionVC];
  152. [rootNavigationController setNavigationBarHidden:YES];
  153. rootNavigationController.modalPresentationStyle = UIModalPresentationFullScreen;
  154. [self.viewController presentViewController:rootNavigationController animated:YES completion:nil];
  155. }
  156. - (void)setCustomUIConfig:(NSDictionary *)options {
  157. NSDictionary *paramDict = options;
  158. if ([paramDict objectForKey:@"sessionTipTextColor"]) {
  159. [[QYSDK sharedSDK] customUIConfig].sessionTipTextColor = [self colorFromHexString:[paramDict objectForKey:@"sessionTipTextColor"]];
  160. }
  161. if ([paramDict objectForKey:@"sessionTipTextFontSize"]) {
  162. [[QYSDK sharedSDK] customUIConfig].sessionTipTextFontSize = [[paramDict objectForKey:@"sessionTipTextFontSize"] floatValue];
  163. }
  164. if ([paramDict objectForKey:@"customMessageTextColor"]) {
  165. [[QYSDK sharedSDK] customUIConfig].customMessageTextColor = [self colorFromHexString:[paramDict objectForKey:@"customMessageTextColor"]];
  166. }
  167. if ([paramDict objectForKey:@"messageTextFontSize"]) {
  168. [[QYSDK sharedSDK] customUIConfig].customMessageTextFontSize = [[paramDict objectForKey:@"messageTextFontSize"] floatValue];
  169. }
  170. if ([paramDict objectForKey:@"serviceMessageTextColor"]) {
  171. [[QYSDK sharedSDK] customUIConfig].serviceMessageTextColor = [self colorFromHexString:[paramDict objectForKey:@"serviceMessageTextColor"]];
  172. }
  173. if ([paramDict objectForKey:@"messageTextFontSize"]) {
  174. [[QYSDK sharedSDK] customUIConfig].serviceMessageTextFontSize = [[paramDict objectForKey:@"messageTextFontSize"] floatValue];
  175. }
  176. if ([paramDict objectForKey:@"tipMessageTextColor"]) {
  177. [[QYSDK sharedSDK] customUIConfig].tipMessageTextColor = [self colorFromHexString:[paramDict objectForKey:@"tipMessageTextColor"]];
  178. }
  179. if ([paramDict objectForKey:@"tipMessageTextFontSize"]) {
  180. [[QYSDK sharedSDK] customUIConfig].tipMessageTextFontSize = [[paramDict objectForKey:@"tipMessageTextFontSize"] floatValue];
  181. }
  182. if ([paramDict objectForKey:@"inputTextColor"]) {
  183. [[QYSDK sharedSDK] customUIConfig].inputTextColor = [self colorFromHexString:[paramDict objectForKey:@"inputTextColor"]];
  184. }
  185. if ([paramDict objectForKey:@"inputTextFontSize"]) {
  186. [[QYSDK sharedSDK] customUIConfig].inputTextFontSize = [[paramDict objectForKey:@"inputTextFontSize"] floatValue];
  187. }
  188. NSString *imageName = nil;
  189. if ([paramDict objectForKey:@"sessionBackgroundImage"]) {
  190. imageName = [paramDict objectForKey:@"sessionBackgroundImage"];
  191. dispatch_async(dispatch_get_main_queue(), ^{
  192. [[QYSDK sharedSDK] customUIConfig].sessionBackground = [[UIImageView alloc] initWithImage:[self getResourceImage:imageName]];
  193. });
  194. }
  195. if ([paramDict objectForKey:@"sessionTipBackgroundColor"]) {
  196. [[QYSDK sharedSDK] customUIConfig].sessionTipBackgroundColor = [self colorFromHexString:[paramDict objectForKey:@"sessionTipBackgroundColor"]];
  197. }
  198. if ([paramDict objectForKey:@"customerHeadImage"]) {
  199. imageName = [paramDict objectForKey:@"customerHeadImage"];
  200. [[QYSDK sharedSDK] customUIConfig].customerHeadImage = [self getResourceImage:imageName];
  201. }
  202. if ([paramDict objectForKey:@"serviceHeadImage"]) {
  203. imageName = [paramDict objectForKey:@"serviceHeadImage"];
  204. [[QYSDK sharedSDK] customUIConfig].serviceHeadImage = [self getResourceImage:imageName];
  205. }
  206. if ([paramDict objectForKey:@"customerMessageBubbleNormalImage"]) {
  207. imageName = [paramDict objectForKey:@"customerMessageBubbleNormalImage"];
  208. [[QYSDK sharedSDK] customUIConfig].customerMessageBubbleNormalImage = [self getResourceImage:imageName];
  209. }
  210. if ([paramDict objectForKey:@"serviceMessageBubbleNormalImage"]) {
  211. imageName = [paramDict objectForKey:@"serviceMessageBubbleNormalImage"];
  212. [[QYSDK sharedSDK] customUIConfig].serviceMessageBubbleNormalImage = [self getResourceImage:imageName];
  213. }
  214. if ([paramDict objectForKey:@"customerMessageBubblePressedImage"]) {
  215. imageName = [paramDict objectForKey:@"customerMessageBubblePressedImage"];
  216. [[QYSDK sharedSDK] customUIConfig].customerMessageBubblePressedImage = [self getResourceImage:imageName];
  217. }
  218. if ([paramDict objectForKey:@"serviceMessageBubblePressedImage"]) {
  219. imageName = [paramDict objectForKey:@"serviceMessageBubblePressedImage"];
  220. [[QYSDK sharedSDK] customUIConfig].serviceMessageBubblePressedImage = [self getResourceImage:imageName];
  221. }
  222. if ([paramDict objectForKey:@"sessionMessageSpacing"]) {
  223. [[QYSDK sharedSDK] customUIConfig].sessionMessageSpacing = [[paramDict objectForKey:@"sessionMessageSpacing"] floatValue];
  224. }
  225. if ([paramDict objectForKey:@"showHeadImage"]) {
  226. [[QYSDK sharedSDK] customUIConfig].showHeadImage = [paramDict objectForKey:@"showHeadImage"];
  227. }
  228. // if ([paramDict objectForKey:@"naviBarColor"]) {
  229. // self.naviBarColor = [self colorFromString:[paramDict objectForKey:@"naviBarColor"]];
  230. // }
  231. // if ([paramDict objectForKey:@"naviBarStyleDark"]) {
  232. // [[QYSDK sharedSDK] customUIConfig].rightBarButtonItemColorBlackOrWhite = [[paramDict objectForKey:@"naviBarStyleDark"] boolValue];
  233. // }
  234. if ([paramDict objectForKey:@"showAudioEntry"]) {
  235. [[QYSDK sharedSDK] customUIConfig].showAudioEntry = [[paramDict objectForKey:@"showAudioEntry"] boolValue];
  236. }
  237. if ([paramDict objectForKey:@"showEmoticonEntry"]) {
  238. [[QYSDK sharedSDK] customUIConfig].showEmoticonEntry = [[paramDict objectForKey:@"showEmoticonEntry"] boolValue];
  239. }
  240. if ([paramDict objectForKey:@"autoShowKeyboard"]) {
  241. [[QYSDK sharedSDK] customUIConfig].autoShowKeyboard = [[paramDict objectForKey:@"autoShowKeyboard"] boolValue];
  242. }
  243. if ([paramDict objectForKey:@"bottomMargin"]) {
  244. [[QYSDK sharedSDK] customUIConfig].bottomMargin = [[paramDict objectForKey:@"bottomMargin"] floatValue];
  245. }
  246. // if ([paramDict objectForKey:@"showCloseSessionEntry"]) {
  247. // [[QYSDK sharedSDK] customUIConfig].showCloseSessionEntry = [RCTConvert BOOL:[paramDict objectForKey:@"showCloseSessionEntry"]];
  248. // }
  249. }
  250. - (NSInteger *)getUnreadCount {
  251. NSInteger count = [[[QYSDK sharedSDK] conversationManager] allUnreadCount];
  252. return count;
  253. }
  254. - (void)setUserInfo:(NSDictionary *)options {
  255. NSDictionary *paramDict = options;
  256. QYUserInfo *userInfo = nil;
  257. if ([paramDict objectForKey:@"userId"] || [paramDict objectForKey:@"data"]) {
  258. userInfo = [[QYUserInfo alloc] init];
  259. if ([paramDict objectForKey:@"userId"]) {
  260. userInfo.userId = [paramDict objectForKey:@"userId"];
  261. }
  262. if ([paramDict objectForKey:@"data"]) {
  263. userInfo.data = [paramDict objectForKey:@"data"];
  264. }
  265. }
  266. if (userInfo) {
  267. [[QYSDK sharedSDK] setUserInfo:userInfo];
  268. }
  269. }
  270. - (void)logout {
  271. [[QYSDK sharedSDK] logout:nil];
  272. }
  273. - (void)cleanCache {
  274. [[QYSDK sharedSDK] cleanResourceCacheWithBlock:nil];
  275. }
  276. - (UIColor *)colorFromHexString:(NSString *)hexString {
  277. NSString *cleanString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
  278. if ([cleanString length] == 3) {
  279. cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@",
  280. [cleanString substringWithRange:NSMakeRange(0,
  281. 1)], [cleanString substringWithRange:NSMakeRange(
  282. 0, 1)],
  283. [cleanString substringWithRange:NSMakeRange(1,
  284. 1)], [cleanString substringWithRange:NSMakeRange(
  285. 1, 1)],
  286. [cleanString substringWithRange:NSMakeRange(2,
  287. 1)], [cleanString substringWithRange:NSMakeRange(
  288. 2, 1)]];
  289. }
  290. if ([cleanString length] == 6) {
  291. cleanString = [cleanString stringByAppendingString:@"ff"];
  292. }
  293. unsigned int baseValue;
  294. [[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];
  295. float red = ((baseValue >> 24) & 0xFF) / 255.0f;
  296. float green = ((baseValue >> 16) & 0xFF) / 255.0f;
  297. float blue = ((baseValue >> 8) & 0xFF) / 255.0f;
  298. float alpha = ((baseValue >> 0) & 0xFF) / 255.0f;
  299. return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
  300. }
  301. - (UIImage *)getResourceImage:(NSString *)imageFilePath {
  302. NSString *localImagePath = [imageFilePath substringFromIndex:1];
  303. NSString *bundlePath = [NSBundle mainBundle].bundlePath;
  304. bundlePath = [[bundlePath stringByAppendingPathComponent:@"assets"] stringByAppendingPathComponent:localImagePath];
  305. UIImage *image = [[UIImage imageWithContentsOfFile:bundlePath] resizableImageWithCapInsets:UIEdgeInsetsMake(
  306. 15, 15, 30,
  307. 30) resizingMode:UIImageResizingModeStretch];
  308. if (image) {
  309. return image;
  310. }
  311. return nil;
  312. }
  313. - (void)onUnreadCountChanged:(NSInteger)count {
  314. [_channel invokeMethod:@"onUnreadCountChange" arguments:@{
  315. @"unreadCount": [NSNumber numberWithInteger:count]}];
  316. }
  317. - (void) application:(UIApplication *)app
  318. didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken {
  319. [[QYSDK sharedSDK] updateApnsToken:deviceToken];
  320. }
  321. @end