BGIMPrivateMsgHandler.m 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091
  1. //
  2. // BGIMPrivateMsgHandler.m
  3. // BuguLive
  4. //
  5. // Created by 朱庆彬 on 2017/8/2.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "BGIMPrivateMsgHandler.h"
  9. #import "AFURLSessionManager.h"
  10. #import "AVIMAble.h"
  11. #import "FMDB.h"
  12. #import "IMAMsg.h"
  13. #import "Mwxpay.h"
  14. #import "NSObject+myobj.h"
  15. #import "TPAACAudioConverter.h"
  16. #import "Util.h"
  17. #import <ImSDK/TIMManager.h>
  18. #import <QMapKit/QMapKit.h>
  19. #import <StoreKit/StoreKit.h>
  20. #import <objc/message.h>
  21. #import <ImSDK_Plus/V2TIMManager+Conversation.h>
  22. #import "TRTCCallingUtils.h"
  23. NSString *g_notif_chatmsg = @"g_notif_chatmsg";
  24. @implementation BGIMPrivateMsgHandler
  25. {
  26. TIMConversation *_tcConv;
  27. }
  28. BogoSingletonM(Instance);
  29. @end
  30. @interface SIMMsgObj () <TPAACAudioConverterDelegate>
  31. @property (nonatomic, assign) AVIMCommand mTMsgType;
  32. - (id)initWithTMsg:(V2TIMMessage *)TMsg;
  33. @end
  34. @implementation SIMMsgObj
  35. {
  36. BOOL _bfetching;
  37. TPAACAudioConverter *_msgacccconvert;
  38. MYNSCondition *_condit;
  39. NSString *_converterr;
  40. }
  41. + (NSString *)makeMsgDesc:(SIMMsgObj *)msg
  42. {
  43. if (msg.mMsgType == 1)
  44. {
  45. return [msg.mTextMsg copy];
  46. }
  47. else if (msg.mMsgType == 2)
  48. {
  49. return ASLocalizedString(@"[图片]");
  50. }
  51. else if (msg.mMsgType == 3)
  52. {
  53. return ASLocalizedString(@"[语音]");
  54. }
  55. else if (msg.mMsgType == 4)
  56. {
  57. return ASLocalizedString(@"[礼物]");
  58. }
  59. return ASLocalizedString(@"未知消息");
  60. }
  61. + (NSString *)makeMsgDescv2:(SIMMsgObj *)msg
  62. {
  63. if (msg.mMsgType == 1)
  64. {
  65. return [msg.mTextMsg copy];
  66. }
  67. else if (msg.mMsgType == 2)
  68. {
  69. return ASLocalizedString(@"[图片]");
  70. }
  71. else if (msg.mMsgType == 3)
  72. {
  73. return ASLocalizedString(@"[语音]");
  74. }
  75. else if (msg.mMsgType == 4)
  76. {
  77. return ASLocalizedString(@"[礼物]");
  78. }else if(msg.mMsgType == 666){
  79. return ASLocalizedString(@"语音消息");
  80. }else if (msg.mMsgType == 667){
  81. return ASLocalizedString(@"视频消息");
  82. }
  83. return ASLocalizedString(@"未知消息");
  84. }
  85. + (BOOL)maybeIMChatMsg:(V2TIMMessage *)itmsg
  86. {
  87. SIMMsgObj *maybemsg = [[SIMMsgObj alloc] initWithTMsg:itmsg];
  88. if (maybemsg.mMsgType == -1)
  89. {
  90. return NO;
  91. }
  92. [[NSNotificationCenter defaultCenter] postNotificationName:g_notif_chatmsg object:maybemsg];
  93. return NO;
  94. }
  95. + (BOOL)saveData:(id)data forkey:(NSString *)key
  96. {
  97. if (data == nil || key == nil)
  98. return NO;
  99. NSString *ss = [NSString stringWithFormat:@"%@_%@", key, [IMAPlatform sharedInstance].host.imUserId];
  100. NSUserDefaults *st = [NSUserDefaults standardUserDefaults];
  101. NSData *dat = [NSKeyedArchiver archivedDataWithRootObject:data];
  102. [st setObject:dat forKey:ss];
  103. return [st synchronize];
  104. }
  105. - (id)init
  106. {
  107. self = [super init];
  108. if (self)
  109. { //如果创建信息消息,那么默认的发送ID就是我自己.
  110. self.mSenderId = [[IMAPlatform sharedInstance].host.imUserId intValue];
  111. self.is_robot = [IMAPlatform sharedInstance].host.is_robot;
  112. }
  113. return self;
  114. }
  115. - (id)initWithTMsg:(V2TIMMessage *)TMsg
  116. {
  117. self = [super init];
  118. if (self)
  119. {
  120. [self fetchIt:TMsg];
  121. }
  122. return self;
  123. }
  124. - (void)fetchIt:(V2TIMMessage *)msg
  125. {
  126. self.mCoreTMsg = msg;
  127. // V2TIMElem *elem = msg.;
  128. //
  129. // if (msg.elemCount == 0)
  130. // {
  131. // self.mMsgType = -1;
  132. // NSLog(@"not have msg data");
  133. // return;
  134. // }
  135. // NSMutableArray *nextElem = [NSMutableArray array];
  136. V2TIMCustomElem *customel = (V2TIMCustomElem *) msg.customElem;
  137. V2TIMElem *customel2 = customel.nextElem;
  138. NSLog(@"customel className %@",[customel className]);
  139. V2TIMElem *elem = customel.nextElem;
  140. // while (elem != nil) {
  141. // // 判断 elem 类型
  142. // [nextElem addObject:elem];
  143. // elem = customel.nextElem;
  144. // }
  145. V2TIMImageElem *imageElem = (V2TIMImageElem *) msg.imageElem;
  146. if(imageElem != nil)
  147. {
  148. customel = imageElem.nextElem;
  149. }
  150. //判断是不是语音消息
  151. V2TIMSoundElem *soundElem = (V2TIMSoundElem *) msg.soundElem;
  152. if(soundElem != nil)
  153. {
  154. customel = soundElem.nextElem;
  155. }
  156. // if (![customel isKindOfClass:[V2TIMCustomElem class]])
  157. // {
  158. // customel = (V2TIMCustomElem *) customel.nextElem;
  159. // if (![customel isKindOfClass:[V2TIMCustomElem class]])
  160. // {
  161. // self.mMsgType = -1;
  162. // return;
  163. // }
  164. // }
  165. NSData *customdata = customel.data;
  166. if (customdata == nil)
  167. {
  168. self.mMsgType = -1;
  169. return;
  170. }
  171. NSDictionary *getdata = [NSJSONSerialization JSONObjectWithData:customdata options:NSJSONReadingMutableContainers error:nil];
  172. // NSLog(@"%@",getdata);
  173. NSString *senderheadimg;
  174. int itsendid;
  175. if ([getdata objectForKey:@"sender"] && [getdata objectForKey:@"sender"] != nil && [[getdata objectForKey:@"sender"] count])
  176. {
  177. senderheadimg = [[getdata objectForKey:@"sender"] objectForKey:@"head_image"];
  178. itsendid = [[[getdata objectForKey:@"sender"] objectForKey:@"user_id"] intValue];
  179. }
  180. self.mSenderId = itsendid;
  181. self.is_robot = [[[getdata objectForKey:@"sender"] objectForKey:@"is_robot"] boolValue];
  182. NSString *senderid = [NSString stringWithFormat:@"%d", itsendid];
  183. NSString *nowuserid = [NSString stringWithFormat:@"%d", [[[IMAPlatform sharedInstance].host.profile.customInfo objectForKey:@"user_id"] intValue]];
  184. self.mIsSendOut = [senderid isEqualToString:nowuserid];
  185. self.mTMsgType = [[getdata objectForKey:@"type"] intValue];
  186. if (self.mTMsgType == MSG_PRIVATE_TEXT)
  187. {
  188. self.mTextMsg = [getdata objectForKey:@"text"];
  189. self.mMsgType = 1;
  190. }
  191. else if (self.mTMsgType == MSG_PRIVATE_VOICE)
  192. {
  193. V2TIMSoundElem *soundel = (V2TIMSoundElem *) soundElem;
  194. // if (![soundel isKindOfClass:[V2TIMSoundElem class]])
  195. // {
  196. // soundel = (V2TIMSoundElem *) [msg getElem:0];
  197. // }
  198. // if (![soundel isKindOfClass:[V2TIMSoundElem class]])
  199. // {
  200. // self.mMsgType = -1;
  201. // return;
  202. // }
  203. self.mMsgType = 3;
  204. id duration = [getdata objectForKey:@"duration"];
  205. if (duration != nil)
  206. {
  207. self.mDurlong = [duration floatValue] / 1000.0f;
  208. }
  209. else
  210. {
  211. self.mDurlong = self.mIsSendOut ? soundel.duration : soundel.duration / 1000.0f;
  212. // self.mDurlong = soundel.duration;
  213. }
  214. self.mVoiceData = [NSData dataWithContentsOfFile:soundel.path];
  215. self.mIsPlaying = NO;
  216. }
  217. else if (self.mTMsgType == MSG_PRIVATE_IMAGE)
  218. {
  219. V2TIMImageElem *imgel = imageElem;
  220. // if (![imgel isKindOfClass:[V2TIMImageElem class]])
  221. // {
  222. // imgel = (V2TIMImageElem *) [msg getElem:0];
  223. // }
  224. // if (![imgel isKindOfClass:[V2TIMImageElem class]])
  225. // {
  226. // self.mMsgType = -1;
  227. // return;
  228. // }
  229. self.mMsgType = 2;
  230. if (imgel.imageList.count != 0)
  231. {
  232. V2TIMImage *theimg = nil;
  233. // V2TIM_IMAGE_TYPE_ORIGIN = 0x01, ///< 原图
  234. // V2TIM_IMAGE_TYPE_THUMB = 0x02, ///< 缩略图
  235. // V2TIM_IMAGE_TYPE_LARGE = 0x04, ///< 大图
  236. ///
  237. for (V2TIMImage *one in imgel.imageList)
  238. {
  239. if (one.type == V2TIM_IMAGE_TYPE_ORIGIN)
  240. {
  241. theimg = one;
  242. break;
  243. }
  244. }
  245. if (theimg == nil)
  246. {
  247. self.mMsgType = -1;
  248. return;
  249. }
  250. self.mPicURL = theimg.url;
  251. self.mPicW = theimg.width;
  252. self.mPicH = theimg.height;
  253. self.mImgObj = nil;
  254. }
  255. else
  256. {
  257. self.mMsgType = -1;
  258. return;
  259. }
  260. NSLog(@"图片消息");
  261. }
  262. else if (self.mTMsgType == MSG_PRIVATE_GIFT)
  263. {
  264. int gifid = [[getdata objectForKey:@"prop_id"] intValue];
  265. self.mGiftId = [NSString stringWithFormat:@"%d", gifid];
  266. self.mGiftIconURL = [getdata objectForKey:@"prop_icon"];
  267. self.mJyStr = [getdata objectForKey:@"from_score"];
  268. self.mMsgType = 4;
  269. } else if (self.mTMsgType == MSG_PRIVATE_CallAudio || self.mTMsgType == MSG_PRIVATE_CallVideo) {
  270. self.mMsgType = (int)self.mTMsgType;
  271. self.callAudioType = [[getdata objectForKey:@"callAudioType"] integerValue];
  272. self.isCall = [[getdata objectForKey:@"isCall"] boolValue];
  273. }
  274. else
  275. {
  276. self.mMsgType = -1; //无效的
  277. return;
  278. }
  279. self.mMsgID = msg.msgID;
  280. self.mMsgStatus = 0;
  281. self.mMsgDate = msg.timestamp;
  282. self.mHeadImgUrl = senderheadimg;
  283. if (self.mTMsgType == MSG_PRIVATE_CallAudio || self.mTMsgType == MSG_PRIVATE_CallVideo) {
  284. self.mMsgDate = nil;
  285. } else {
  286. self.mMsgDate = msg.timestamp;
  287. }
  288. if (self.mTMsgType == MSG_PRIVATE_GIFT)
  289. {
  290. if (self.mIsSendOut)
  291. {
  292. self.mGiftDesc = [getdata objectForKey:@"from_msg"];
  293. }
  294. else
  295. {
  296. self.mGiftDesc = [getdata objectForKey:@"to_msg"];
  297. }
  298. }
  299. }
  300. #define T_VOICE_NAME @"voice_recode"
  301. - (void)fetchMsgData:(void (^)(NSString *errmsg))block
  302. {
  303. if (self.mMsgType == 3)
  304. { //语音消息
  305. if (_bfetching)
  306. return;
  307. _bfetching = YES;
  308. V2TIMSoundElem *soundel = (V2TIMSoundElem *) self.mCoreTMsg.soundElem;
  309. if (![soundel isKindOfClass:[V2TIMSoundElem class]])
  310. {
  311. // soundel = (V2TIMSoundElem *) [self.mCoreTMsg getElem:0];
  312. if (![soundel isKindOfClass:[V2TIMSoundElem class]])
  313. {
  314. block(ASLocalizedString(@"无效的数据"));
  315. _bfetching = NO;
  316. return;
  317. }
  318. }
  319. NSString *soundFileName = @"";
  320. if (soundel.uuid == nil)
  321. {
  322. return;
  323. }
  324. NSString *soundName = [NSString stringWithFormat:@"%@_%@", soundel.uuid, [IMAPlatform sharedInstance].host.imUserId];
  325. NSUserDefaults *st = [NSUserDefaults standardUserDefaults];
  326. NSData *dat = [st objectForKey:soundName];
  327. if (dat != nil)
  328. {
  329. soundFileName = [NSKeyedUnarchiver unarchiveObjectWithData:dat];
  330. if (soundFileName && soundFileName.length)
  331. {
  332. self.mVoiceData = [NSData dataWithContentsOfFile:[SFriendObj makevoicesavepath:soundFileName]];
  333. if (self.mVoiceData)
  334. {
  335. block(nil);
  336. _bfetching = NO;
  337. return;
  338. }
  339. }
  340. }
  341. // if (![soundel.path isEqualToString:@""] || soundel.path != nil)
  342. // {
  343. // NSString *uuid = soundElem.uuid;
  344. // NSString *soundPath = [NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithFormat: @"IMSound%@",uuid]];
  345. //
  346. //
  347. NSString *savePath = [SFriendObj makevoicesavepath:soundFileName];
  348. [soundel downloadSound:savePath progress:^(NSInteger curSize, NSInteger totalSize) {
  349. // 下载进度
  350. NSLog(@"下载语音进度:curSize:%lu,totalSize:%lu",curSize,totalSize);
  351. } succ:^{
  352. // 下载成功
  353. NSString *newfilename = [SFriendObj makevoicefilename];
  354. // NSString *newfullpath = [SFriendObj makevoicesavepath:newfilename];
  355. // if (soundel.path == nil || [soundel.path isEqualToString:@""])
  356. // {
  357. // NSLog(@"path is null");
  358. // }
  359. // else
  360. // {
  361. // NSData *data = [NSData dataWithContentsOfFile:soundel.path];
  362. //
  363. // [data writeToFile:newfullpath atomically:YES];
  364. // }
  365. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  366. NSString *desconvertfile = [savePath stringByAppendingString:@".out"];
  367. _condit = MYNSCondition.new;
  368. _msgacccconvert = [[TPAACAudioConverter alloc] initWithDelegate:self
  369. source:savePath
  370. destination:desconvertfile];
  371. [_msgacccconvert start];
  372. [_condit lock];
  373. [_condit wait];
  374. [_condit unlock];
  375. _msgacccconvert = nil;
  376. _condit = nil;
  377. if (_converterr == nil)
  378. {
  379. self.mVoiceData = [NSData dataWithContentsOfFile:desconvertfile];
  380. [SIMMsgObj saveData:[NSString stringWithFormat:@"%@_%@", soundel.uuid, [IMAPlatform sharedInstance].host.imUserId] forkey:soundel.uuid];
  381. }
  382. dispatch_async(dispatch_get_main_queue(), ^{
  383. _bfetching = NO;
  384. block(_converterr);
  385. _converterr = nil;
  386. });
  387. });
  388. NSLog(@"下载语音完成");
  389. } fail:^(int code, NSString *msg) {
  390. // 下载失败
  391. NSLog(@"下载语音失败:code:%d,msg:%@",code,msg);
  392. if (code == 10017 || code == 20012)
  393. {
  394. [FanweMessage alertHUD:ASLocalizedString(@"您已被禁言")];
  395. return;
  396. }
  397. _bfetching = NO;
  398. block(msg);
  399. }];
  400. // [soundel getSound:soundel.path
  401. // succ:^{
  402. //
  403. // NSString *newfilename = [SFriendObj makevoicefilename];
  404. // NSString *newfullpath = [SFriendObj makevoicesavepath:newfilename];
  405. //
  406. // if (soundel.path == nil || [soundel.path isEqualToString:@""])
  407. // {
  408. // NSLog(@"path is null");
  409. // }
  410. // else
  411. // {
  412. // NSData *data = [NSData dataWithContentsOfFile:soundel.path];
  413. //
  414. // [data writeToFile:newfullpath atomically:YES];
  415. // }
  416. //
  417. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  418. //
  419. // NSString *desconvertfile = [newfullpath stringByAppendingString:@".out"];
  420. //
  421. // _condit = MYNSCondition.new;
  422. //
  423. // _msgacccconvert = [[TPAACAudioConverter alloc] initWithDelegate:self
  424. // source:newfullpath
  425. // destination:desconvertfile];
  426. //
  427. // [_msgacccconvert start];
  428. // [_condit lock];
  429. // [_condit wait];
  430. // [_condit unlock];
  431. //
  432. // _msgacccconvert = nil;
  433. // _condit = nil;
  434. //
  435. // if (_converterr == nil)
  436. // {
  437. // self.mVoiceData = [NSData dataWithContentsOfFile:desconvertfile];
  438. //
  439. // [SIMMsgObj saveData:newfilename forkey:soundel.uuid];
  440. // }
  441. //
  442. // dispatch_async(dispatch_get_main_queue(), ^{
  443. //
  444. // _bfetching = NO;
  445. // block(_converterr);
  446. // _converterr = nil;
  447. //
  448. // });
  449. //
  450. // });
  451. //
  452. // }
  453. // fail:^(int code, NSString *msg) {
  454. // if (code == 10017 || code == 20012)
  455. // {
  456. // [FanweMessage alertHUD:ASLocalizedString(@"您已被禁言")];
  457. // return;
  458. // }
  459. // _bfetching = NO;
  460. // block(msg);
  461. //
  462. // }];
  463. }
  464. // else
  465. // {
  466. //
  467. // NSString *newfilename = [SFriendObj makevoicefilename];
  468. // NSString *newfullpath = [SFriendObj makevoicesavepath:newfilename];
  469. // [soundel getSound:newfullpath
  470. // succ:^{
  471. //
  472. // NSData *data = [NSData dataWithContentsOfFile:newfullpath];
  473. //
  474. // [data writeToFile:newfullpath atomically:YES];
  475. //
  476. // dispatch_async(dispatch_get_global_queue(0, 0), ^{
  477. //
  478. // NSString *desconvertfile = [newfullpath stringByAppendingString:@".out"];
  479. //
  480. // _condit = MYNSCondition.new;
  481. //
  482. // _msgacccconvert = [[TPAACAudioConverter alloc] initWithDelegate:self
  483. // source:newfullpath
  484. // destination:desconvertfile];
  485. //
  486. // [_msgacccconvert start];
  487. // [_condit lock];
  488. // [_condit wait];
  489. // [_condit unlock];
  490. //
  491. // _msgacccconvert = nil;
  492. // _condit = nil;
  493. //
  494. // if (_converterr == nil)
  495. // {
  496. // self.mVoiceData = [NSData dataWithContentsOfFile:desconvertfile];
  497. //
  498. // NSString *soundName = [NSString stringWithFormat:@"%@_%@", soundel.uuid, [IMAPlatform sharedInstance].host.imUserId];
  499. // NSUserDefaults *st = [NSUserDefaults standardUserDefaults];
  500. // NSData *dat = [NSKeyedArchiver archivedDataWithRootObject:newfilename];
  501. // [st setObject:dat forKey:soundName];
  502. // }
  503. //
  504. // dispatch_async(dispatch_get_main_queue(), ^{
  505. //
  506. // _bfetching = NO;
  507. // block(_converterr);
  508. // _converterr = nil;
  509. //
  510. // });
  511. //
  512. // });
  513. //
  514. // }
  515. // fail:^(int code, NSString *msg) {
  516. // if (code == 10017 || code == 20012)
  517. // {
  518. // [FanweMessage alertHUD:ASLocalizedString(@"您已被禁言")];
  519. // return;
  520. // }
  521. // _bfetching = NO;
  522. // block(msg);
  523. //
  524. // }];
  525. // }
  526. // }
  527. /*else
  528. { //其他消息 没有需要填充的
  529. block(ASLocalizedString(@"获取消息数据失败"));
  530. }*/
  531. }
  532. - (void)AACAudioConverterDidFinishConversion:(TPAACAudioConverter *)converter
  533. {
  534. NSLog(@"AACAudioConverterDidFinishConversion ok");
  535. _converterr = nil;
  536. [_condit lock];
  537. [_condit signal];
  538. [_condit unlock];
  539. }
  540. - (void)AACAudioConverter:(TPAACAudioConverter *)converter didFailWithError:(NSError *)error
  541. {
  542. NSLog(@"didFailWithError :%@", error);
  543. _converterr = ASLocalizedString(@"转换语音数据错误");
  544. [_condit lock];
  545. [_condit signal];
  546. [_condit unlock];
  547. }
  548. @end
  549. @interface SFriendObj () <TPAACAudioConverterDelegate>
  550. @end
  551. @implementation SFriendObj
  552. {
  553. V2TIMConversation *_tcConv;
  554. TPAACAudioConverter *_aacconvert;
  555. MYNSCondition *_condit;
  556. NSString *_converterrstr;
  557. }
  558. - (id)initWithUserId:(int)userid
  559. {
  560. self = [super init];
  561. if (self)
  562. {
  563. self.mUser_id = userid;
  564. // _tcConv = [[TIMManager sharedInstance] getConversation:TIM_C2C receiver:[NSString stringWithFormat:@"%d", userid]];
  565. [[V2TIMManager sharedInstance] getConversation:[NSString stringWithFormat:@"%d", userid] succ:^(V2TIMConversation *conv) {
  566. _tcConv = conv;
  567. } fail:^(int code, NSString *desc) {
  568. }];
  569. }
  570. return self;
  571. }
  572. - (void)ignoreThisUnReadCount
  573. {
  574. [[V2TIMManager sharedInstance] markC2CMessageAsRead:StringFromInt(self.mUser_id) succ:^{
  575. NSLog(@"设置已读成功");
  576. } fail:^(int code, NSString *desc) {
  577. }];
  578. // [[V2TIMManager sharedInstance] markC2CMessageAsRead:_tcConv];
  579. // [_tcConv setReadMessage:nil succ:nil fail:nil];
  580. }
  581. - (int)getUnReadCount
  582. {
  583. return [_tcConv unreadCount];
  584. }
  585. - (TIMConversation *)getConv
  586. {
  587. return _tcConv;
  588. }
  589. - (void)setConv:(V2TIMConversation *)conv
  590. {
  591. _tcConv = conv;
  592. }
  593. - (void)setLMsg:(V2TIMMessage *)msg
  594. {
  595. SIMMsgObj *sMsgObj = [[SIMMsgObj alloc] initWithTMsg:msg];
  596. sMsgObj.is_robot = _is_robot;
  597. self.mLastMsg = [SIMMsgObj makeMsgDesc:sMsgObj];
  598. self.mLastMsgDate = [msg.timestamp copy];
  599. }
  600. - (NSString *)getTimeStr
  601. {
  602. return [BGUtils formatTime:self.mLastMsgDate];
  603. }
  604. - (NSAttributedString *)getLastMsg
  605. {
  606. NSMutableAttributedString *ats = [[NSMutableAttributedString alloc] initWithString:ASLocalizedString(@"最近消息")];
  607. return ats;
  608. }
  609. + (NSArray *)getFollowIds
  610. {
  611. // 如果没有登录,就不需要后续操作
  612. if (![IMAPlatform isAutoLogin])
  613. {
  614. return [NSArray array];
  615. }
  616. NSLog(@"[][][][][]");
  617. NSMutableArray *retall = NSMutableArray.new;
  618. NSDictionary *dic = [SResBase postReqSync:@"my_follow" ctl:@"user" parm:nil];
  619. SResBase *ret = [[SResBase alloc] initWithObj:dic];
  620. NSLog(@"ioioioioiio");
  621. if (ret.msuccess)
  622. {
  623. NSArray *t = [ret.mdata objectForKey:@"list"];
  624. for (NSDictionary *one in t)
  625. {
  626. int userId = [[one objectForKeyMy:@"user_id"] intValue];
  627. if (userId)
  628. [retall addObject:[NSString stringWithFormat:@"%d", userId]];
  629. }
  630. }
  631. return retall;
  632. }
  633. // 获取我的列表
  634. // bFollowed 2 好友 3 非好友
  635. + (void)getMyFriendMsgList:(int)bFollowed lastObj:(SFriendObj *)lastObj block:(void (^)(SResBase *resb, NSArray *all, int unReadNum))block
  636. {
  637. // 如果没有登录,就不需要后续操作
  638. if (![IMAPlatform isAutoLogin])
  639. {
  640. return;
  641. }
  642. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  643. NSMutableArray *conversationList = NSMutableArray.new;
  644. [[V2TIMManager sharedInstance] getConversationList:0 count:50 succ:^(NSArray<V2TIMConversation *> *list, uint64_t nextSeq, BOOL isFinished) {
  645. NSLog(@"-----11111");
  646. if(list.count == 0)
  647. {
  648. dispatch_async(dispatch_get_main_queue(), ^{
  649. block([SResBase infoWithString:ASLocalizedString(@"暂无会话")], nil, 0);
  650. });
  651. }
  652. NSLog(@"-----22222");
  653. for (int i=0; i<list.count; i++) {
  654. V2TIMConversation *item = list[i];
  655. if([item type] == V2TIM_C2C)
  656. {
  657. NSLog(@"-----23232323");
  658. [conversationList addObject:item];
  659. }
  660. }
  661. NSLog(@"-----vvffwesfwefw");
  662. // NSArray *follows = [self getFollowIds];
  663. NSMutableArray *array = NSMutableArray.new;
  664. NSMutableString *string = NSMutableString.new;
  665. int i = 0;
  666. for (V2TIMConversation *conversation in conversationList)
  667. {
  668. NSLog(@"-----2434343423");
  669. if ([conversation type] == V2TIM_C2C)
  670. {
  671. NSString *thisuserid = [conversation conversationID];
  672. if (bFollowed == 1)
  673. {
  674. continue;
  675. }
  676. if (string.length)
  677. [string appendString:@","];
  678. [string appendString:[conversation conversationID]];
  679. i++;
  680. if (i == 20)
  681. {
  682. break;
  683. }
  684. }
  685. if (i == 100)
  686. {
  687. [array addObject:string];
  688. string = NSMutableString.new;
  689. i = 0;
  690. }
  691. }
  692. if (string.length)
  693. [array addObject:string];
  694. if (0 == array.count)
  695. { // 没有
  696. dispatch_async(dispatch_get_main_queue(), ^{
  697. block([SResBase infoWithString:ASLocalizedString(@"暂无会话")], nil, 0);
  698. });
  699. return;
  700. }
  701. // 获取用户信息
  702. NSMutableDictionary *allusers = NSMutableDictionary.new;
  703. SResBase *sResBase = nil;
  704. NSString *user_ids = [array componentsJoinedByString:@","];
  705. user_ids = [user_ids stringByReplacingOccurrencesOfString:@"c2c_" withString:@""];
  706. NSMutableDictionary * parmDict = [NSMutableDictionary dictionary];
  707. [parmDict setObject:@"user" forKey:@"ctl"];
  708. [parmDict setObject:@"baseinfo" forKey:@"act"];
  709. [parmDict setObject:user_ids forKey:@"user_ids"];
  710. [[NetHttpsManager manager]POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson) {
  711. if ([[responseJson valueForKey:@"status"]integerValue] == 1) {
  712. SResBase *sResBases = [SResBase modelWithJSON:responseJson];
  713. sResBases.msuccess = YES;
  714. // [_userArray removeAllObjects];
  715. NSArray *listArr = [responseJson valueForKey:@"list"];
  716. for (NSDictionary *onedic in [responseJson valueForKey:@"list"])
  717. {
  718. SFriendObj *oneobj = [[SFriendObj alloc] initWithObj:onedic];
  719. oneobj.is_robot = [[onedic objectForKey:@"is_robot"] boolValue];
  720. [allusers setObject:oneobj forKey:[NSString stringWithFormat:@"%d", oneobj.mUser_id]];
  721. }
  722. NSLog(@"-----333333");
  723. NSMutableArray *retusers = NSMutableArray.new;
  724. int msgNum = 0;
  725. // 获取最后一条消息
  726. for (int j = 0; j < conversationList.count;j++)
  727. {
  728. V2TIMConversation *conversation = conversationList[j];
  729. NSString *user_ids2 = [[conversation conversationID] stringByReplacingOccurrencesOfString:@"c2c_" withString:@""];
  730. SFriendObj *oneobj = [allusers objectForKey:user_ids2];
  731. if([user_ids2 isEqualToString:@"administrator"])
  732. {
  733. [[V2TIMManager sharedInstance] markC2CMessageAsRead:@"administrator" succ:^{
  734. NSLog(@"设置已读成功");
  735. } fail:^(int code, NSString *desc) {
  736. }];
  737. }
  738. SIMMsgObj *sMsgObj = [[SIMMsgObj alloc] initWithTMsg:[conversation lastMessage]];
  739. oneobj.mLastMsg = [SIMMsgObj makeMsgDescv2:sMsgObj];
  740. [[V2TIMManager sharedInstance] getC2CHistoryMessageList:user_ids2 count:20 lastMsg:nil succ:^(NSArray<V2TIMMessage *> *msgs) {
  741. NSLog(@"用户未读--%@----%@---%ld",user_ids2,conversation.showName,[conversation unreadCount]);
  742. for (V2TIMMessage *msg in msgs)
  743. {
  744. V2TIMTextElem *textElem = msg.textElem;
  745. V2TIMCustomElem *customElem = nil;
  746. V2TIMCustomElem *Elem = (V2TIMCustomElem *) textElem.nextElem;
  747. if(![msg isRead])
  748. {
  749. NSLog(@"%@",[msg.customElem.data jsonValueDecoded]);
  750. }
  751. }
  752. } fail:^(int code, NSString *desc) {
  753. }];
  754. if (oneobj == nil)
  755. continue;
  756. int unreadCount = [conversation unreadCount];
  757. oneobj.unread_count = StringFromInt(unreadCount);
  758. [retusers addObject:oneobj];
  759. msgNum = msgNum + unreadCount;
  760. }
  761. NSLog(@"-----444444");
  762. dispatch_async(dispatch_get_main_queue(), ^{
  763. block(sResBases, retusers, msgNum);
  764. });
  765. }
  766. } FailureBlock:^(NSError *error) {
  767. }];
  768. } fail:^(int code, NSString *desc) {
  769. }];
  770. });
  771. }
  772. // 删除消息
  773. - (void)delThis:(void (^)(SResBase *resb))block
  774. {
  775. [[V2TIMManager sharedInstance] deleteConversation:StringFromInt(self.mUser_id) succ:^{
  776. NSLog(@"删除消息成功");
  777. } fail:^(int code, NSString *desc) {
  778. }];
  779. // [[TIMManager sharedInstance] deleteConversation:TIM_C2C receiver:[_tcConv getReceiver]];
  780. block([SResBase infoWithOKString:ASLocalizedString(@"删除会话成功")]);
  781. }
  782. - (NSArray *)sortMsg:(NSArray *)aaaMsg
  783. {
  784. return [aaaMsg sortedArrayUsingComparator:^NSComparisonResult(id _Nonnull obj1, id _Nonnull obj2) {
  785. SIMMsgObj *objm1 = obj1;
  786. SIMMsgObj *objm2 = obj2;
  787. NSTimeInterval f1 = [objm1.mMsgDate timeIntervalSince1970];
  788. NSTimeInterval f2 = [objm2.mMsgDate timeIntervalSince1970];
  789. if (f1 < f2)
  790. return NSOrderedAscending;
  791. else if (f1 == f2)
  792. return NSOrderedSame;
  793. return NSOrderedDescending;
  794. }];
  795. }
  796. - (void)getMsgList:(BOOL)before posmsg:(SIMMsgObj *)posmsg block:(void (^)(SResBase *resb, NSArray *all))block
  797. {
  798. NSString *user_id = [NSString stringWithFormat:@"%@",StringFromInt(self.mUser_id)];
  799. [[V2TIMManager sharedInstance] getC2CHistoryMessageList:user_id count:20 lastMsg:posmsg.mCoreTMsg succ:^(NSArray<V2TIMMessage *> *msgs) {
  800. NSMutableArray *all = NSMutableArray.new;
  801. ;
  802. for (V2TIMMessage *one in msgs)
  803. {
  804. SIMMsgObj *sMsgObj = [[SIMMsgObj alloc] initWithTMsg:one];
  805. if (sMsgObj.mMsgType < 0)
  806. continue;
  807. SIMMsgObj *obj = [[SIMMsgObj alloc] initWithTMsg:one];
  808. // mMsgStatus;//0 正常,1发送中,2发送失败,
  809. if (self.is_robot)
  810. {
  811. obj.mMsgStatus = 0;
  812. }
  813. else
  814. {
  815. obj.mMsgStatus = one.status - 1;
  816. if (one.status == 1)
  817. {
  818. obj.mMsgStatus = 1;
  819. }
  820. if (one.status == 2)
  821. {
  822. obj.mMsgStatus = 0;
  823. }
  824. if (one.status == 3)
  825. {
  826. obj.mMsgStatus = 2;
  827. }
  828. }
  829. if (obj.mTMsgType == MSG_PRIVATE_CallAudio || obj.mTMsgType == MSG_PRIVATE_CallVideo){
  830. continue;
  831. }
  832. [all addObject:obj];
  833. }
  834. block([SResBase infoWithOKString:ASLocalizedString(@"获取消息成功")], [self sortMsg:all]);
  835. } fail:^(int code, NSString *desc) {
  836. if (code == 10017 || code == 20012)
  837. {
  838. [FanweMessage alertHUD:ASLocalizedString(@"您已被禁言")];
  839. return;
  840. }
  841. block([SResBase infoWithString:desc], nil);
  842. }];
  843. /*
  844. [_tcConv getMessage:20
  845. last:posmsg.mCoreTMsg
  846. succ:^(NSArray *msgs) {
  847. NSMutableArray *all = NSMutableArray.new;
  848. ;
  849. for (V2TIMMessage *one in msgs)
  850. {
  851. SIMMsgObj *sMsgObj = [[SIMMsgObj alloc] initWithTMsg:one];
  852. if (sMsgObj.mMsgType < 0)
  853. continue;
  854. SIMMsgObj *obj = [[SIMMsgObj alloc] initWithTMsg:one];
  855. // mMsgStatus;//0 正常,1发送中,2发送失败,
  856. if (self.is_robot)
  857. {
  858. obj.mMsgStatus = 0;
  859. }
  860. else
  861. {
  862. obj.mMsgStatus = one.status - 1;
  863. if (one.status == 1)
  864. {
  865. obj.mMsgStatus = 1;
  866. }
  867. if (one.status == 2)
  868. {
  869. obj.mMsgStatus = 0;
  870. }
  871. if (one.status == 3)
  872. {
  873. obj.mMsgStatus = 2;
  874. }
  875. }
  876. [all addObject:obj];
  877. }
  878. block([SResBase infoWithOKString:ASLocalizedString(@"获取消息成功")], [self sortMsg:all]);
  879. }
  880. fail:^(int code, NSString *msg) {
  881. if (code == 10017 || code == 20012)
  882. {
  883. [FanweMessage alertHUD:ASLocalizedString(@"您已被禁言")];
  884. return;
  885. }
  886. block([SResBase infoWithString:msg], nil);
  887. }];*/
  888. }
  889. // 发送文字消息
  890. - (SIMMsgObj *)sendTextMsg:(NSString *)text block:(void (^)(SResBase *resb, SIMMsgObj *thatmsg))block
  891. {
  892. __block SIMMsgObj *itmsg = SIMMsgObj.new;
  893. NSDictionary *makedat = @{
  894. @"type": @(MSG_PRIVATE_TEXT),
  895. @"text": text,
  896. @"sender": [IMAPlatform sharedInstance].host.customInfoDict,
  897. };
  898. V2TIMCustomElem *text_elem = [[V2TIMCustomElem alloc] init];
  899. text_elem.data = [NSJSONSerialization dataWithJSONObject:makedat options:NSJSONWritingPrettyPrinted error:nil];
  900. V2TIMMessage *msgforsend = [[V2TIMManager sharedInstance] createCustomMessage:text_elem.data];
  901. // V2TIMMessage *msgforsend = [[V2TIMMessage alloc] init];
  902. // msgforsend.customElem = text_elem;
  903. // [msgforsend addElem:text_elem];
  904. itmsg.mMsgID = msgforsend.msgID;
  905. itmsg.mMsgStatus = 1;
  906. itmsg.mHeadImgUrl = [[IMAPlatform sharedInstance].host.customInfoDict objectForKey:@"head_image"];
  907. itmsg.mIsSendOut = YES;
  908. itmsg.mMsgType = 1;
  909. itmsg.mMsgDate = msgforsend.timestamp;
  910. itmsg.mTextMsg = text;
  911. itmsg.mCoreTMsg = msgforsend;
  912. NSString *groupId = nil;
  913. NSString *receiver = nil;
  914. // if([_tcConv getType] == TIM_C2C)
  915. // {
  916. // receiver = [_tcConv getReceiver];
  917. // }
  918. // else
  919. // {
  920. // groupId = [_tcConv getReceiver];
  921. // }
  922. [[V2TIMManager sharedInstance] sendMessage:msgforsend receiver:StringFromInt(self.mUser_id) groupID:nil priority:V2TIM_PRIORITY_DEFAULT onlineUserOnly:NO offlinePushInfo:[self getofflinePushInfo] progress:^(uint32_t progress) {
  923. } succ:^{
  924. itmsg.mMsgStatus = 0;
  925. block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  926. } fail:^(int code, NSString *desc) {
  927. block([SResBase infoWithString:desc], itmsg);
  928. }];
  929. // [_tcConv sendMessage:msgforsend
  930. // succ:^{
  931. //
  932. // itmsg.mMsgStatus = 0;
  933. // block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  934. //
  935. // }
  936. // fail:^(int code, NSString *msg) {
  937. // if (code == 10017 || code == 20012)
  938. // {
  939. // [FanweMessage alertHUD:ASLocalizedString(@"您已被禁言")];
  940. // return;
  941. // }
  942. // itmsg.mMsgStatus = 2;
  943. // if (_is_robot)
  944. // {
  945. // itmsg.mMsgStatus = 0;
  946. // block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  947. // }
  948. // else
  949. // {
  950. // block([SResBase infoWithString:msg], itmsg);
  951. // }
  952. // }];
  953. //
  954. // if (_is_robot)
  955. // {
  956. // itmsg.mMsgStatus = 0;
  957. // }
  958. //
  959. return itmsg;
  960. }
  961. // 发送自定义语音通话消息
  962. - (SIMMsgObj *)sendCustomCallAudioTextMsg:(NSString *)text callAudioType:(AudioChatType)callAudioType isCall:(BOOL)isCall block:(void (^)(SResBase *resb, SIMMsgObj *thatmsg))block
  963. {
  964. __block SIMMsgObj *itmsg = SIMMsgObj.new;
  965. NSDictionary *makedat = @{
  966. @"type": @(MSG_PRIVATE_CallAudio),
  967. @"text": text,
  968. @"sender": [IMAPlatform sharedInstance].host.customInfoDict,
  969. @"callAudioType":@(callAudioType),
  970. @"isCall":@(isCall),
  971. };
  972. V2TIMCustomElem *text_elem = [[V2TIMCustomElem alloc] init];
  973. text_elem.data = [NSJSONSerialization dataWithJSONObject:makedat options:NSJSONWritingPrettyPrinted error:nil];
  974. V2TIMMessage *msgforsend = [[V2TIMManager sharedInstance] createCustomMessage:text_elem.data];
  975. msgforsend.isExcludedFromUnreadCount = YES;
  976. itmsg.mMsgID = msgforsend.msgID;
  977. itmsg.mMsgStatus = 1;
  978. itmsg.mHeadImgUrl = [[IMAPlatform sharedInstance].host.customInfoDict objectForKey:@"head_image"];
  979. itmsg.mIsSendOut = YES;
  980. itmsg.mMsgType = 0;
  981. itmsg.mMsgDate = msgforsend.timestamp;
  982. itmsg.mTextMsg = text;
  983. itmsg.mCoreTMsg = msgforsend;
  984. NSString *groupId = nil;
  985. NSString *receiver = nil;
  986. [[V2TIMManager sharedInstance] sendMessage:msgforsend receiver:StringFromInt(self.mUser_id) groupID:nil priority:V2TIM_PRIORITY_DEFAULT onlineUserOnly:NO offlinePushInfo:nil progress:^(uint32_t progress) {
  987. } succ:^{
  988. itmsg.mMsgStatus = 0;
  989. block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  990. } fail:^(int code, NSString *desc) {
  991. block([SResBase infoWithString:desc], itmsg);
  992. }];
  993. return itmsg;
  994. }
  995. - (SIMMsgObj *)sendCustomCallAudioTextMsgWithPush:(NSString *)text callAudioType:(AudioChatType)callAudioType isCall:(BOOL)isCall block:(void (^)(SResBase *resb, SIMMsgObj *thatmsg))block
  996. {
  997. __block SIMMsgObj *itmsg = SIMMsgObj.new;
  998. NSDictionary *makedat = @{
  999. @"type": @(MSG_PRIVATE_CallAudio),
  1000. @"text": text,
  1001. @"sender": [IMAPlatform sharedInstance].host.customInfoDict,
  1002. @"callAudioType":@(callAudioType),
  1003. @"isCall":@(isCall),
  1004. @"callUserID":[BogoNetwork shareInstance].uid,
  1005. @"callType":@(MSG_PRIVATE_CallAudio),
  1006. @"callNickName":[GlobalVariables sharedInstance].userModel.nick_name,
  1007. @"callHeaderImg":[[IMAPlatform sharedInstance].host.customInfoDict objectForKey:@"head_image"]
  1008. };
  1009. V2TIMCustomElem *text_elem = [[V2TIMCustomElem alloc] init];
  1010. text_elem.data = [NSJSONSerialization dataWithJSONObject:makedat options:NSJSONWritingPrettyPrinted error:nil];
  1011. V2TIMMessage *msgforsend = [[V2TIMManager sharedInstance] createCustomMessage:text_elem.data];
  1012. msgforsend.isExcludedFromUnreadCount = YES;
  1013. itmsg.mMsgID = msgforsend.msgID;
  1014. itmsg.mMsgStatus = 1;
  1015. itmsg.mHeadImgUrl = [[IMAPlatform sharedInstance].host.customInfoDict objectForKey:@"head_image"];
  1016. itmsg.mIsSendOut = YES;
  1017. itmsg.mMsgType = 0;
  1018. itmsg.mMsgDate = msgforsend.timestamp;
  1019. itmsg.mTextMsg = text;
  1020. itmsg.mCoreTMsg = msgforsend;
  1021. NSString *groupId = nil;
  1022. NSString *receiver = nil;
  1023. V2TIMOfflinePushInfo *pushInfo = [[V2TIMOfflinePushInfo alloc] init];
  1024. pushInfo.title = ASLocalizedString(@"发来了一条消息");
  1025. pushInfo.desc = ASLocalizedString(@"消息");
  1026. //V2TIMIOSOfflinePushType
  1027. pushInfo.iOSPushType = V2TIM_IOS_OFFLINE_PUSH_TYPE_VOIP;
  1028. pushInfo.enableIOSBackgroundNotification = YES;
  1029. pushInfo.iOSSound = @"phone_ringing.mp3";
  1030. NSDictionary *Voipdic = @{
  1031. @"aps" : @{
  1032. @"alert" : @"This is some fancy message.",
  1033. @"Extras" :@{
  1034. @"callUserID" : @(167720),
  1035. @"callAudioType" : @(1),
  1036. @"callNickName" : @"1111五",
  1037. @"callHeaderImg" : @"https:\/\/mapi.dynasty168.com\/public\/attachment\/202412\/05\/17\/origin\/1733391394167721.jpg",
  1038. @"callType" : @(667)
  1039. },
  1040. @"badge" : @(6),
  1041. @"sound" : @"default"
  1042. }
  1043. };
  1044. NSDictionary *dic = @{@"entity":@{@"version":@(1),@"type":@(MSG_PRIVATE_CallVideo)},
  1045. @"makedat":makedat,
  1046. @"voipdic":Voipdic,
  1047. @"user_im_id":[BGIMLoginManager sharedInstance].loginParam.identifier,
  1048. @"mHead_image":[[IMAPlatform sharedInstance].host.customInfoDict objectForKey:@"head_image"]};
  1049. pushInfo.ext = [TRTCCallingUtils dictionary2JsonStr:dic];
  1050. [[V2TIMManager sharedInstance] sendMessage:msgforsend receiver:StringFromInt(self.mUser_id) groupID:nil priority:V2TIM_PRIORITY_DEFAULT onlineUserOnly:NO offlinePushInfo:pushInfo progress:^(uint32_t progress) {
  1051. } succ:^{
  1052. itmsg.mMsgStatus = 0;
  1053. block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  1054. } fail:^(int code, NSString *desc) {
  1055. block([SResBase infoWithString:desc], itmsg);
  1056. }];
  1057. return itmsg;
  1058. }
  1059. // 发送自定义视频通话消息
  1060. - (SIMMsgObj *)sendCustomCallVideoTextMsg:(NSString *)text callAudioType:(AudioChatType)callAudioType isCall:(BOOL)isCall block:(void (^)(SResBase *resb, SIMMsgObj *thatmsg))block
  1061. {
  1062. __block SIMMsgObj *itmsg = SIMMsgObj.new;
  1063. NSDictionary *makedat = @{
  1064. @"type": @(MSG_PRIVATE_CallVideo),
  1065. @"text": text,
  1066. @"sender": [IMAPlatform sharedInstance].host.customInfoDict,
  1067. @"callAudioType":@(callAudioType),
  1068. @"isCall":@(isCall),
  1069. };
  1070. V2TIMCustomElem *text_elem = [[V2TIMCustomElem alloc] init];
  1071. text_elem.data = [NSJSONSerialization dataWithJSONObject:makedat options:NSJSONWritingPrettyPrinted error:nil];
  1072. V2TIMMessage *msgforsend = [[V2TIMManager sharedInstance] createCustomMessage:text_elem.data];
  1073. msgforsend.isExcludedFromUnreadCount = YES;
  1074. itmsg.mMsgID = msgforsend.msgID;
  1075. itmsg.mMsgStatus = 1;
  1076. itmsg.mHeadImgUrl = [[IMAPlatform sharedInstance].host.customInfoDict objectForKey:@"head_image"];
  1077. itmsg.mIsSendOut = YES;
  1078. itmsg.mMsgType = 0;
  1079. itmsg.mMsgDate = msgforsend.timestamp;
  1080. itmsg.mTextMsg = text;
  1081. itmsg.mCoreTMsg = msgforsend;
  1082. NSString *groupId = nil;
  1083. NSString *receiver = nil;
  1084. [[V2TIMManager sharedInstance] sendMessage:msgforsend receiver:StringFromInt(self.mUser_id) groupID:nil priority:V2TIM_PRIORITY_DEFAULT onlineUserOnly:NO offlinePushInfo:nil progress:^(uint32_t progress) {
  1085. } succ:^{
  1086. itmsg.mMsgStatus = 0;
  1087. block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  1088. } fail:^(int code, NSString *desc) {
  1089. block([SResBase infoWithString:desc], itmsg);
  1090. }];
  1091. return itmsg;
  1092. }
  1093. - (SIMMsgObj *)sendCustomCallVideoTextMsgWithPush:(NSString *)text callAudioType:(AudioChatType)callAudioType isCall:(BOOL)isCall block:(void (^)(SResBase *resb, SIMMsgObj *thatmsg))block
  1094. {
  1095. __block SIMMsgObj *itmsg = SIMMsgObj.new;
  1096. NSDictionary *makedat = @{
  1097. @"type": @(MSG_PRIVATE_CallVideo),
  1098. @"text": text,
  1099. @"sender": [IMAPlatform sharedInstance].host.customInfoDict,
  1100. @"callAudioType":@(callAudioType),
  1101. @"isCall":@(isCall),
  1102. @"callUserID":[BogoNetwork shareInstance].uid,
  1103. @"callType":@(MSG_PRIVATE_CallVideo),
  1104. @"callNickName":[GlobalVariables sharedInstance].userModel.nick_name,
  1105. @"callHeaderImg":[[IMAPlatform sharedInstance].host.customInfoDict objectForKey:@"head_image"]
  1106. };
  1107. V2TIMCustomElem *text_elem = [[V2TIMCustomElem alloc] init];
  1108. text_elem.data = [NSJSONSerialization dataWithJSONObject:makedat options:NSJSONWritingPrettyPrinted error:nil];
  1109. V2TIMMessage *msgforsend = [[V2TIMManager sharedInstance] createCustomMessage:text_elem.data];
  1110. msgforsend.isExcludedFromUnreadCount = YES;
  1111. itmsg.mMsgID = msgforsend.msgID;
  1112. itmsg.mMsgStatus = 1;
  1113. itmsg.mHeadImgUrl = [[IMAPlatform sharedInstance].host.customInfoDict objectForKey:@"head_image"];
  1114. itmsg.mIsSendOut = YES;
  1115. itmsg.mMsgType = 0;
  1116. itmsg.mMsgDate = msgforsend.timestamp;
  1117. itmsg.mTextMsg = text;
  1118. itmsg.mCoreTMsg = msgforsend;
  1119. NSString *groupId = nil;
  1120. NSString *receiver = nil;
  1121. V2TIMOfflinePushInfo *pushInfo = [[V2TIMOfflinePushInfo alloc] init];
  1122. pushInfo.title = ASLocalizedString(@"发来了一条消息");
  1123. pushInfo.desc = ASLocalizedString(@"消息");
  1124. //V2TIMIOSOfflinePushType
  1125. pushInfo.iOSPushType = V2TIM_IOS_OFFLINE_PUSH_TYPE_VOIP;
  1126. pushInfo.enableIOSBackgroundNotification = YES;
  1127. pushInfo.iOSSound = @"phone_ringing.mp3";
  1128. NSDictionary *Voipdic = @{
  1129. @"aps" : @{
  1130. @"alert" : @"This is some fancy message.",
  1131. @"Extras" :@{
  1132. @"callUserID" : @(167720),
  1133. @"callAudioType" : @(1),
  1134. @"callNickName" : @"1111五",
  1135. @"callHeaderImg" : @"https:\/\/mapi.dynasty168.com\/public\/attachment\/202412\/05\/17\/origin\/1733391394167721.jpg",
  1136. @"callType" : @(667)
  1137. },
  1138. @"badge" : @(6),
  1139. @"sound" : @"default"
  1140. }
  1141. };
  1142. NSDictionary *dic = @{@"entity":@{@"version":@(1),@"type":@(MSG_PRIVATE_CallVideo)},
  1143. @"makedat":makedat,
  1144. @"voipdic":Voipdic,
  1145. @"user_im_id":[BGIMLoginManager sharedInstance].loginParam.identifier,
  1146. @"mHead_image":[[IMAPlatform sharedInstance].host.customInfoDict objectForKey:@"head_image"]};
  1147. pushInfo.ext = [TRTCCallingUtils dictionary2JsonStr:dic];
  1148. [[V2TIMManager sharedInstance] sendMessage:msgforsend receiver:StringFromInt(self.mUser_id) groupID:nil priority:V2TIM_PRIORITY_DEFAULT onlineUserOnly:NO offlinePushInfo:pushInfo progress:^(uint32_t progress) {
  1149. } succ:^{
  1150. itmsg.mMsgStatus = 0;
  1151. block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  1152. } fail:^(int code, NSString *desc) {
  1153. block([SResBase infoWithString:desc], itmsg);
  1154. }];
  1155. return itmsg;
  1156. }
  1157. // 记住:文件不能记住全路径,每次APP运行中间的有一截路径不同
  1158. + (NSString *)makepicsavepath
  1159. {
  1160. NSString *urlStr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  1161. urlStr = [urlStr stringByAppendingPathComponent:[NSString stringWithFormat:@"picfile_%ld", (NSInteger)[[NSDate date] timeIntervalSince1970]]];
  1162. return urlStr;
  1163. }
  1164. + (NSString *)makevoicesavepath:(NSString *)filename
  1165. {
  1166. NSString *urlStr = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
  1167. NSString *ssss = filename;
  1168. if (ssss.length == 0)
  1169. {
  1170. ssss = [NSString stringWithFormat:@"vociefile_%ld", (NSInteger)[[NSDate date] timeIntervalSince1970]];
  1171. }
  1172. urlStr = [urlStr stringByAppendingPathComponent:ssss];
  1173. return urlStr;
  1174. }
  1175. + (NSString *)makevoicefilename
  1176. {
  1177. return [NSString stringWithFormat:@"vociefile_%ld", (NSInteger)[[NSDate date] timeIntervalSince1970]];
  1178. }
  1179. - (SIMMsgObj *)sendPicMsg:(UIImage *)img block:(void (^)(SResBase *resb, SIMMsgObj *thatmsg))block
  1180. {
  1181. __block SIMMsgObj *itmsg = SIMMsgObj.new;
  1182. NSString *iimgsavepath = [SFriendObj makepicsavepath];
  1183. NSData *ddd = UIImageJPEGRepresentation(img, 0.9f);
  1184. if (![ddd writeToFile:iimgsavepath atomically:YES])
  1185. {
  1186. block([SResBase infoWithString:ASLocalizedString(@"上传图片失败")], nil);
  1187. return nil;
  1188. }
  1189. NSDictionary *makedat = @{
  1190. @"type": @(MSG_PRIVATE_IMAGE),
  1191. @"sender": [IMAPlatform sharedInstance].host.customInfoDict,
  1192. };
  1193. V2TIMCustomElem *text_elem = [[V2TIMCustomElem alloc] init];
  1194. text_elem.data = [NSJSONSerialization dataWithJSONObject:makedat options:NSJSONWritingPrettyPrinted error:nil];
  1195. // V2TIMMessage *msgforsend = [[V2TIMMessage alloc] init];
  1196. V2TIMMessage *msgforsend = [[V2TIMManager sharedInstance] createImageMessage:iimgsavepath];
  1197. // [msg ];
  1198. // V2TIMImageElem *image_elem = [[V2TIMImageElem alloc] init];
  1199. // image_elem.path = iimgsavepath;
  1200. // image_elem.level = TIM_IMAGE_COMPRESS_LOW;
  1201. // [msgforsend addElem:image_elem];
  1202. [msgforsend.imageElem appendElem:text_elem];
  1203. itmsg.mMsgID = msgforsend.msgID;
  1204. itmsg.mMsgStatus = 1;
  1205. itmsg.mHeadImgUrl = [[IMAPlatform sharedInstance].host.customInfoDict objectForKey:@"head_image"];
  1206. itmsg.mIsSendOut = YES;
  1207. itmsg.mMsgType = 2;
  1208. itmsg.mMsgDate = msgforsend.timestamp;
  1209. itmsg.mImgObj = img;
  1210. itmsg.mPicURL = [NSURL fileURLWithPath:iimgsavepath].absoluteString;
  1211. itmsg.mCoreTMsg = msgforsend;
  1212. // [[V2TIMManager sharedInstance] sendMessage:msgforsend receiver:StringFromInt(self.mUser_id) groupID:nil priority:V2TIM_PRIORITY_DEFAULT onlineUserOnly:NO offlinePushInfo:nil progress:^(uint32_t progress) {
  1213. //
  1214. // } succ:^{
  1215. // itmsg.mMsgStatus = 0;
  1216. // block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  1217. // } fail:^(int code, NSString *desc) {
  1218. // block([SResBase infoWithString:desc], itmsg);
  1219. // }];
  1220. [[V2TIMManager sharedInstance] sendMessage:msgforsend receiver:StringFromInt(self.mUser_id) groupID:nil priority:V2TIM_PRIORITY_DEFAULT onlineUserOnly:NO offlinePushInfo:[self getofflinePushInfo] progress:^(uint32_t progress) {
  1221. } succ:^{
  1222. itmsg.mMsgStatus = 0;
  1223. block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  1224. } fail:^(int code, NSString *desc) {
  1225. if (code == 10017 || code == 20012)
  1226. {
  1227. [FanweMessage alertHUD:ASLocalizedString(@"您已被禁言")];
  1228. return;
  1229. }
  1230. itmsg.mMsgStatus = 2;
  1231. if (_is_robot)
  1232. {
  1233. itmsg.mMsgStatus = 0;
  1234. block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  1235. }
  1236. else
  1237. {
  1238. block([SResBase infoWithString:desc], itmsg);
  1239. }
  1240. }];
  1241. // [_tcConv sendMessage:msgforsend
  1242. // succ:^{
  1243. //
  1244. // itmsg.mMsgStatus = 0;
  1245. // block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  1246. //
  1247. // }
  1248. // fail:^(int code, NSString *msg) {
  1249. // if (code == 10017 || code == 20012)
  1250. // {
  1251. // [FanweMessage alertHUD:ASLocalizedString(@"您已被禁言")];
  1252. // return;
  1253. // }
  1254. // itmsg.mMsgStatus = 2;
  1255. // if (_is_robot)
  1256. // {
  1257. // itmsg.mMsgStatus = 0;
  1258. // block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  1259. // }
  1260. // else
  1261. // {
  1262. // block([SResBase infoWithString:msg], itmsg);
  1263. // }
  1264. //
  1265. // }];
  1266. if (_is_robot)
  1267. {
  1268. itmsg.mMsgStatus = 0;
  1269. }
  1270. return itmsg;
  1271. }
  1272. - (SIMMsgObj *)sendVoiceMsg:(NSURL *)voicepath duration:(NSTimeInterval)duration block:(void (^)(SResBase *resb, SIMMsgObj *thatmsg))block
  1273. {
  1274. __block SIMMsgObj *itmsg = SIMMsgObj.new;
  1275. NSDictionary *makedat = @{
  1276. @"type": @(MSG_PRIVATE_VOICE),
  1277. @"sender": [IMAPlatform sharedInstance].host.customInfoDict,
  1278. @"duration": @(duration * 1000.0f)
  1279. };
  1280. V2TIMCustomElem *text_elem = [[V2TIMCustomElem alloc] init];
  1281. text_elem.data = [NSJSONSerialization dataWithJSONObject:makedat options:NSJSONWritingPrettyPrinted error:nil];
  1282. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  1283. V2TIMMessage *msgforsend = [[V2TIMManager sharedInstance] createSoundMessage:voicepath.path duration:duration];
  1284. // [msgforsend addElem:text_elem];
  1285. itmsg.mMsgID = msgforsend.msgID;
  1286. itmsg.mMsgStatus = 1;
  1287. itmsg.mHeadImgUrl = [[IMAPlatform sharedInstance].host.customInfoDict objectForKey:@"head_image"];
  1288. itmsg.mIsSendOut = YES;
  1289. itmsg.mMsgType = 3;
  1290. itmsg.mMsgDate = msgforsend.timestamp;
  1291. [msgforsend.soundElem appendElem:text_elem];
  1292. itmsg.mVoiceData = [NSData dataWithContentsOfURL:voicepath];
  1293. itmsg.mDurlong = duration;
  1294. itmsg.mCoreTMsg = msgforsend;
  1295. NSString *srcfilepath = [NSString stringWithFormat:@"%s", voicepath.fileSystemRepresentation];
  1296. NSString *desfilepath = [srcfilepath stringByAppendingString:@".out"];
  1297. _condit = MYNSCondition.new;
  1298. _aacconvert = [[TPAACAudioConverter alloc] initWithDelegate:self
  1299. source:[NSString stringWithFormat:@"%s", voicepath.fileSystemRepresentation]
  1300. destination:desfilepath];
  1301. [_aacconvert start];
  1302. [_condit lock];
  1303. [_condit wait];
  1304. [_condit unlock];
  1305. _aacconvert = nil;
  1306. _condit = nil;
  1307. if (_converterrstr)
  1308. {
  1309. dispatch_async(dispatch_get_main_queue(), ^{
  1310. block([SResBase infoWithString:_converterrstr], nil);
  1311. _converterrstr = nil;
  1312. });
  1313. }
  1314. else
  1315. {
  1316. [[V2TIMManager sharedInstance] sendMessage:msgforsend receiver:StringFromInt(self.mUser_id) groupID:nil priority:V2TIM_PRIORITY_DEFAULT onlineUserOnly:NO offlinePushInfo:[self getofflinePushInfo] progress:^(uint32_t progress) {
  1317. } succ:^{
  1318. // V2TIMSoundElem *sound_elem = (V2TIMSoundElem *) [itmsg.mCoreTMsg getElem:1];
  1319. NSString *ssfile = [voicepath lastPathComponent];
  1320. [SIMMsgObj saveData:ssfile forkey:msgforsend.soundElem.uuid];
  1321. itmsg.mMsgStatus = 0;
  1322. block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  1323. } fail:^(int code, NSString *desc) {
  1324. if (code == 10017 || code == 20012)
  1325. {
  1326. [FanweMessage alertHUD:ASLocalizedString(@"您已被禁言")];
  1327. return;
  1328. }
  1329. itmsg.mMsgStatus = 2;
  1330. if (_is_robot)
  1331. {
  1332. itmsg.mMsgStatus = 0;
  1333. block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  1334. }
  1335. else
  1336. {
  1337. block([SResBase infoWithString:desc], itmsg);
  1338. } }];
  1339. /*V2TIMSoundElem *sound_elem = [[V2TIMSoundElem alloc] init];
  1340. [sound_elem setPath:desfilepath];
  1341. [sound_elem setSecond:duration];
  1342. [msgforsend addElem:sound_elem];
  1343. [_tcConv sendMessage:msgforsend
  1344. succ:^{
  1345. V2TIMSoundElem *sound_elem = (V2TIMSoundElem *) [itmsg.mCoreTMsg getElem:1];
  1346. NSString *ssfile = [voicepath lastPathComponent];
  1347. [SIMMsgObj saveData:ssfile forkey:sound_elem.uuid];
  1348. itmsg.mMsgStatus = 0;
  1349. block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  1350. }
  1351. fail:^(int code, NSString *msg) {
  1352. if (code == 10017 || code == 20012)
  1353. {
  1354. [FanweMessage alertHUD:ASLocalizedString(@"您已被禁言")];
  1355. return;
  1356. }
  1357. itmsg.mMsgStatus = 2;
  1358. if (_is_robot)
  1359. {
  1360. itmsg.mMsgStatus = 0;
  1361. block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  1362. }
  1363. else
  1364. {
  1365. block([SResBase infoWithString:msg], itmsg);
  1366. }
  1367. }];*/
  1368. }
  1369. });
  1370. if (_is_robot)
  1371. {
  1372. itmsg.mMsgStatus = 0;
  1373. }
  1374. return itmsg;
  1375. }
  1376. - (void)AACAudioConverterDidFinishConversion:(TPAACAudioConverter *)converter
  1377. {
  1378. NSLog(@"AACAudioConverterDidFinishConversion ok");
  1379. _converterrstr = nil;
  1380. [_condit lock];
  1381. [_condit signal];
  1382. [_condit unlock];
  1383. }
  1384. - (void)AACAudioConverter:(TPAACAudioConverter *)converter didFailWithError:(NSError *)error
  1385. {
  1386. NSLog(@"didFailWithError :%@", error);
  1387. _converterrstr = ASLocalizedString(@"语音数据格式转换失败");
  1388. [_condit lock];
  1389. [_condit signal];
  1390. [_condit unlock];
  1391. }
  1392. - (void)sendGiftMsg:(GiftModel *)findgifobj block:(void (^)(SResBase *resb, SIMMsgObj *thatmsg))block
  1393. {
  1394. [SResBase postReq:@"send_prop"
  1395. ctl:@"deal"
  1396. parm:@{ @"prop_id": @(findgifobj.ID),
  1397. @"num": @(1),
  1398. @"to_user_id": @(self.mUser_id) }
  1399. block:^(SResBase *resb) {
  1400. if (resb.msuccess)
  1401. {
  1402. [self realSendGift:resb block:block];
  1403. }
  1404. else
  1405. {
  1406. block(resb, nil);
  1407. }
  1408. }];
  1409. }
  1410. - (void)sendCoinMsgWithCoin:(NSString *)coin block:(void (^)(SResBase *resb, SIMMsgObj *thatmsg))block
  1411. {
  1412. [SResBase postReq:@"sendCoin"
  1413. ctl:@"games"
  1414. parm:@{ @"to_user_id": @(self.mUser_id),
  1415. @"coin": coin }
  1416. block:^(SResBase *resb) {
  1417. if (resb.msuccess)
  1418. {
  1419. [self realSendGift:resb block:block];
  1420. }
  1421. else
  1422. {
  1423. block(resb, nil);
  1424. }
  1425. }];
  1426. }
  1427. - (void)sendDiamondsMsgWithDiamonds:(NSString *)diamonds block:(void (^)(SResBase *resb, SIMMsgObj *thatmsg))block;
  1428. {
  1429. [SResBase postReq:@"sendDiamonds"
  1430. ctl:@"games"
  1431. parm:@{@"to_user_id": @(self.mUser_id), @"diamonds": diamonds}
  1432. block:^(SResBase *resb) {
  1433. if (resb.msuccess)
  1434. {
  1435. [self realSendGift:resb block:block];
  1436. }
  1437. else
  1438. {
  1439. block(resb, nil);
  1440. }
  1441. }];
  1442. }
  1443. - (SIMMsgObj *)realSendGift:(SResBase *)resb block:(void (^)(SResBase *resb, SIMMsgObj *thatmsg))block
  1444. {
  1445. __block SIMMsgObj *itmsg = SIMMsgObj.new;
  1446. NSMutableDictionary *makedat = NSMutableDictionary.new;
  1447. [makedat setDictionary:resb.mdata];
  1448. [makedat setObject:@"conversationDesc" forKey:ASLocalizedString(@"[礼物]")];
  1449. [makedat setObject:[IMAPlatform sharedInstance].host.customInfoDict forKey:@"sender"];
  1450. [makedat setObject:@(MSG_PRIVATE_GIFT) forKey:@"type"];
  1451. V2TIMCustomElem *text_elem = [[V2TIMCustomElem alloc] init];
  1452. text_elem.data = [NSJSONSerialization dataWithJSONObject:makedat options:NSJSONWritingPrettyPrinted error:nil];
  1453. V2TIMMessage *msgforsend = [[V2TIMManager sharedInstance] createCustomMessage:text_elem.data];
  1454. // V2TIMMessage *msgforsend = [[V2TIMMessage alloc] init];
  1455. // [msgforsend addElem:text_elem];
  1456. itmsg.mMsgID = msgforsend.msgID;
  1457. itmsg.mMsgStatus = 1;
  1458. itmsg.mHeadImgUrl = [[IMAPlatform sharedInstance].host.customInfoDict objectForKey:@"head_image"];
  1459. itmsg.mIsSendOut = YES;
  1460. itmsg.mMsgType = 4;
  1461. itmsg.mMsgDate = msgforsend.timestamp;
  1462. itmsg.mGiftId = [NSString stringWithFormat:@"%d", [[makedat objectForKey:@"prop_id"] intValue]];
  1463. //礼物消息的数据
  1464. itmsg.mGiftIconURL = [makedat objectForKey:@"prop_icon"]; //礼物小图标URL
  1465. itmsg.mGiftDesc = [makedat objectForKey:@"from_msg"];
  1466. itmsg.mJyStr = [makedat objectForKey:@"from_score"];
  1467. itmsg.mCoreTMsg = msgforsend;
  1468. [[V2TIMManager sharedInstance] sendMessage:msgforsend receiver:StringFromInt(self.mUser_id) groupID:nil priority:V2TIM_PRIORITY_DEFAULT onlineUserOnly:NO offlinePushInfo:nil progress:^(uint32_t progress) {
  1469. } succ:^{
  1470. itmsg.mMsgStatus = 0;
  1471. block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  1472. } fail:^(int code, NSString *desc) {
  1473. if (code == 10017 || code == 20012)
  1474. {
  1475. [FanweMessage alertHUD:ASLocalizedString(@"您已被禁言")];
  1476. // return ;
  1477. }
  1478. itmsg.mMsgStatus = 2;
  1479. block([SResBase infoWithString:desc], itmsg);
  1480. }];
  1481. // [_tcConv sendMessage:msgforsend
  1482. // succ:^{
  1483. //
  1484. // itmsg.mMsgStatus = 0;
  1485. // block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], itmsg);
  1486. //
  1487. // }
  1488. // fail:^(int code, NSString *msg) {
  1489. // if (code == 10017 || code == 20012)
  1490. // {
  1491. // [FanweMessage alertHUD:ASLocalizedString(@"您已被禁言")];
  1492. // // return ;
  1493. // }
  1494. // itmsg.mMsgStatus = 2;
  1495. // block([SResBase infoWithString:msg], itmsg);
  1496. //
  1497. // }];
  1498. return itmsg;
  1499. }
  1500. - (void)reSendMsg:(SIMMsgObj *)resmsg block:(void (^)(SResBase *resb, SIMMsgObj *thatmsg))block
  1501. {
  1502. __block SIMMsgObj *neeresend = resmsg;
  1503. // neeresend.mMsgStatus = 1;
  1504. // [_tcConv sendMessage:neeresend.mCoreTMsg
  1505. // succ:^{
  1506. //
  1507. // neeresend.mMsgStatus = 0;
  1508. // block([SResBase infoWithOKString:ASLocalizedString(@"发送消息成功")], neeresend);
  1509. //
  1510. // }
  1511. // fail:^(int code, NSString *msg) {
  1512. // if (code == 10017 || code == 20012)
  1513. // {
  1514. // [FanweMessage alertHUD:ASLocalizedString(@"您已被禁言")];
  1515. // return;
  1516. // }
  1517. // neeresend.mMsgStatus = 2;
  1518. // block([SResBase infoWithString:msg], neeresend);
  1519. //
  1520. // }];
  1521. }
  1522. -(V2TIMOfflinePushInfo*)getofflinePushInfo{
  1523. V2TIMOfflinePushInfo *pushInfo = [[V2TIMOfflinePushInfo alloc] init];
  1524. pushInfo.title = ASLocalizedString(@"发来了一条消息");
  1525. pushInfo.desc = ASLocalizedString(@"消息");
  1526. NSDictionary *dic = @{@"entity":@{@"version":@(1)},@"type":@(1), @"user_im_id":[BGIMLoginManager sharedInstance].loginParam.identifier,@"mHead_image":[[IMAPlatform sharedInstance].host.customInfoDict objectForKey:@"head_image"]};
  1527. NSDictionary *extParam = @{@"entity" : dic};
  1528. pushInfo.ext = [TRTCCallingUtils dictionary2JsonStr:extParam];
  1529. return pushInfo;
  1530. }
  1531. // 忽略未读
  1532. + (void)ignoreMsg:(NSArray *)allFriends block:(void (^)(SResBase *resb))block
  1533. {
  1534. for (SFriendObj *one in allFriends)
  1535. {
  1536. [one ignoreThisUnReadCount];
  1537. }
  1538. block([SResBase infoWithOKString:ASLocalizedString(@"操作成功")]);
  1539. }
  1540. // 废弃
  1541. + (int)getAllUnReadCount
  1542. {
  1543. int retcount = 0;
  1544. int cnt = [[TIMManager sharedInstance] conversationCount];
  1545. NSArray *follows = [self getFollowIds];
  1546. for (int index = 0; index < cnt; index++)
  1547. {
  1548. //TIMConversation *conversation = [[TIMManager sharedInstance] getConversationByIndex:index];
  1549. TIMConversation *conversation = [[[TIMManager sharedInstance] getConversationList] objectAtIndex:index];
  1550. if ([conversation getType] == TIM_C2C && [follows containsObject:[conversation getReceiver]])
  1551. {
  1552. int ii = [conversation getUnReadMessageNum];
  1553. if (ii)
  1554. {
  1555. int immsgcount = 0;
  1556. NSArray *array = [conversation getLastMsgs:20];
  1557. for (int j = 0; j < array.count && j < ii; j++)
  1558. {
  1559. V2TIMMessage *msg = array[j];
  1560. if (msg.status != TIM_MSG_STATUS_HAS_DELETED)
  1561. {
  1562. SIMMsgObj *tttmsgss = [[SIMMsgObj alloc] initWithTMsg:msg];
  1563. if (tttmsgss.mMsgType == -1) //0 时间消息, 1 文字消息, 2,图片消息,3 语音消息,4 礼物
  1564. {
  1565. continue;
  1566. }
  1567. //ykk bug 4
  1568. immsgcount = [conversation getUnReadMessageNum];
  1569. }
  1570. }
  1571. retcount += immsgcount;
  1572. }
  1573. }
  1574. }
  1575. return retcount;
  1576. }
  1577. + (void)getAllUnReadCountComplete:(void (^)(int num))block
  1578. {
  1579. [self getMyFocusFriendUnReadMsgNumIsFriend:0
  1580. block:^(int unReadNum) {
  1581. if (block)
  1582. {
  1583. block(unReadNum);
  1584. }
  1585. }];
  1586. }
  1587. /**
  1588. 获取好友/未关注 消息个数
  1589. 0 查询总消息 1 好友 2 非好友
  1590. cnt 会话数量
  1591. num 记录未读数目
  1592. follows 获取好友id (注意)(这是个耗时操作。最好修改这个方法,放在gcd里处理)
  1593. array 将符合的会话放入数组(包含 好友/未关注)
  1594. num += 区分数组里 好友/未关注,根据isFriend 确定返回的数据
  1595. @param isFriend 获取all会话,获取好友id数组,isFriend筛选
  1596. @param block 回调
  1597. */
  1598. + (void)getMyFocusFriendUnReadMsgNumIsFriend:(int)isFriend block:(void (^)(int unReadNum))block
  1599. {
  1600. // 如果没有登录,就不需要后续操作
  1601. if (![IMAPlatform isAutoLogin])
  1602. {
  1603. return;
  1604. }
  1605. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  1606. NSMutableArray *conversationList = NSMutableArray.new;
  1607. int cnt = [[TIMManager sharedInstance] conversationCount];
  1608. TIMConversation *startfind = nil;
  1609. BOOL bfind = startfind == nil; // 如果是空的就从开始
  1610. [[V2TIMManager sharedInstance] getConversationList:0 count:50 succ:^(NSArray<V2TIMConversation *> *list, uint64_t nextSeq, BOOL isFinished) {
  1611. for (int index = 0; index < list.count; index++)
  1612. {
  1613. V2TIMConversation *conversation = list[index];
  1614. if ([conversation type] == V2TIM_C2C)
  1615. {
  1616. [conversationList addObject:conversation];
  1617. }
  1618. }
  1619. if (0 == list.count)
  1620. { // 没有
  1621. dispatch_async(dispatch_get_main_queue(), ^{
  1622. block(0);
  1623. });
  1624. return;
  1625. }
  1626. // NSArray *follows = [self getFollowIds];
  1627. NSMutableArray *array = NSMutableArray.new;
  1628. NSMutableString *string = NSMutableString.new;
  1629. int i = 0;
  1630. for (V2TIMConversation *conversation in conversationList)
  1631. {
  1632. if ([conversation type] == V2TIM_C2C)
  1633. {
  1634. NSString *thisuserid = [conversation conversationID];
  1635. if (isFriend == 0)
  1636. {
  1637. }
  1638. // if (isFriend == 1)
  1639. // { //如果是查询关注的,,,
  1640. // if (![follows containsObject:thisuserid])
  1641. // continue;
  1642. // }
  1643. // if (isFriend == 2)
  1644. // { //如果是查询没有关注的,
  1645. // if ([follows containsObject:thisuserid])
  1646. // continue;
  1647. // }
  1648. if (string.length)
  1649. [string appendString:@","];
  1650. [string appendString:[conversation conversationID]];
  1651. i++;
  1652. if (i == 20)
  1653. {
  1654. break;
  1655. }
  1656. }
  1657. if (i == 100)
  1658. {
  1659. [array addObject:string];
  1660. string = NSMutableString.new;
  1661. i = 0;
  1662. }
  1663. }
  1664. if (string.length)
  1665. [array addObject:string];
  1666. if (0 == array.count)
  1667. { // 没有
  1668. dispatch_async(dispatch_get_main_queue(), ^{
  1669. block(0);
  1670. });
  1671. return;
  1672. }
  1673. // 获取用户信息
  1674. /*NSMutableDictionary *allusers = NSMutableDictionary.new;
  1675. SResBase *sResBase = nil;
  1676. for (NSString *one in array)
  1677. {
  1678. NSString *one2 = [one stringByReplacingOccurrencesOfString:@"c2c_" withString:@""];
  1679. NSDictionary *retdic = [SResBase postReqSync:@"baseinfo" ctl:@"user" parm:@{ @"user_ids": one2 }];
  1680. if (retdic)
  1681. {
  1682. sResBase = [[SResBase alloc] initWithObj:retdic];
  1683. NSArray *tlist = [sResBase.mdata objectForKeyMy:@"list"];
  1684. for (NSDictionary *onedic in tlist)
  1685. {
  1686. SFriendObj *oneobj = [[SFriendObj alloc] initWithObj:onedic];
  1687. oneobj.is_robot = [[onedic objectForKey:@"is_robot"] boolValue];
  1688. [allusers setObject:oneobj forKey:[NSString stringWithFormat:@"%d", oneobj.mUser_id]];
  1689. }
  1690. }
  1691. else
  1692. {
  1693. sResBase = [SResBase infoWithString:ASLocalizedString(@"获取用户信息失败")];
  1694. break;
  1695. }
  1696. }*/
  1697. int msgNum = 0;
  1698. //获取最后一条消息
  1699. for (int j = 0; j < conversationList.count; j++)
  1700. {
  1701. V2TIMConversation *conversation = conversationList[j];
  1702. // SFriendObj *oneobj = [allusers objectForKey:[conversation conversationID]];
  1703. // if (oneobj == nil)
  1704. // continue;
  1705. int uncount = [conversation unreadCount];
  1706. msgNum = msgNum + uncount;
  1707. // }
  1708. }
  1709. dispatch_async(dispatch_get_main_queue(), ^{
  1710. block(msgNum);
  1711. });
  1712. } fail:^(int code, NSString *desc) {
  1713. }];
  1714. });
  1715. }
  1716. @end