User.m 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. //
  2. // User.m
  3. // NIMDemo
  4. //
  5. // Created by Fenix Wang on 2017/5/24.
  6. // Copyright © 2017年 Netease. All rights reserved.
  7. //
  8. #import "User.h"
  9. #import "AFHTTPSessionManager.h"
  10. #import "GiftInputContainer.h"
  11. #import "NIMSDK/NIMSDK.h"
  12. @interface User()
  13. @property (nonatomic, strong) NSMutableArray<GiftData *> *giftList;
  14. @end
  15. @implementation User
  16. int newCreateTeamId;
  17. + (UIColor *)greenColor
  18. {
  19. static UIColor *greenColor = nil;
  20. static dispatch_once_t onceToken;
  21. dispatch_once(&onceToken, ^{
  22. greenColor = [UIColor colorWithRed:(51.0 / 255.0) green:(205.0 / 255.0) blue:(153.0 / 255.0) alpha:1.0];
  23. });
  24. return greenColor;
  25. }
  26. + (UIColor *)orangeColor{
  27. static UIColor *orangeColor = nil;
  28. static dispatch_once_t onceToken;
  29. dispatch_once(&onceToken, ^{
  30. orangeColor = [UIColor colorWithRed:(255.0 / 255.0) green:(171.0 / 255.0) blue:(8.0 / 255.0) alpha:1.0];
  31. });
  32. return orangeColor;
  33. }
  34. + (instancetype)sharedInfo
  35. {
  36. static User *sharedInfo = nil;
  37. static dispatch_once_t onceToken;
  38. dispatch_once(&onceToken, ^{
  39. sharedInfo = [[User alloc] init];
  40. sharedInfo.giftGotDict = [[NSMutableDictionary alloc] init];
  41. sharedInfo.tradeGotDict = [[NSMutableDictionary alloc] init];
  42. });
  43. return sharedInfo;
  44. }
  45. + (NSString *_Nonnull)distance:(double)originLat originLng:(double)originLng targetLat:(double)targetLat targetLng:(double)targetLng{
  46. double pow1 = pow(sin((targetLat*M_PI/180 - originLat*M_PI/180)/2), 2);
  47. double pow2 = pow(sin((targetLng*M_PI/180 - originLng*M_PI/180)/2), 2);
  48. double cos2 = cos(targetLat*M_PI/180) * cos(originLat*M_PI/180);
  49. double dis = round(6378.138 * 2 * asin(sqrt(pow1 + cos2 * pow2))*1000);
  50. if(dis > 1000)
  51. return [NSString stringWithFormat:@"%.1FKM", dis/1000];
  52. return [NSString stringWithFormat:@"%.0fM", dis];
  53. }
  54. + (void)showUserInfo:(NSString *)userId{
  55. NSLog(@"showUserInfo %@", userId);
  56. }
  57. - (void)requestGiftList:(void (^_Nullable)(NSMutableArray * _Nullable list))sucCallBack
  58. failure:(void (^_Nullable)(NSError * _Nullable error))failCallBack
  59. {
  60. if(_giftList != nil)
  61. {
  62. sucCallBack(_giftList);
  63. return;
  64. }
  65. NSDictionary *parameters = @{};
  66. NSString *urlString = @"http://whosay.dashgame.com/index.php?m=who&c=index&a=gift_list";
  67. //请求的managers
  68. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  69. manager.responseSerializer = [AFJSONResponseSerializer new];
  70. manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
  71. //请求的方式:POST
  72. NSLog(@"%@", parameters);
  73. [manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
  74. NSLog(@"请求成功,服务器返回的信息%@", responseObject);
  75. NSDictionary * dic = responseObject;
  76. int code = [[dic valueForKey:@"c"] intValue];
  77. NSDictionary *data = (NSDictionary *)[dic objectForKey:@"d"];
  78. NSLog(@"Json解析结果 = %d %@", code, data);
  79. self.giftList = [[NSMutableArray alloc] init];
  80. NSMutableArray *dataList = [data objectForKey:@"list"];
  81. for(int i=0; i<dataList.count; i++)
  82. {
  83. NSDictionary *obj = [dataList objectAtIndex:i];
  84. GiftData *giftData = [[GiftData alloc] init];
  85. giftData.giftId = [obj objectForKey:@"id"];
  86. giftData.name = [obj objectForKey:@"name"];
  87. giftData.price = [obj objectForKey:@"price"];
  88. giftData.iconURL = [obj objectForKey:@"icon"];
  89. giftData.movieURL = [obj objectForKey:@"movie"];
  90. [self.giftList addObject:giftData];
  91. }
  92. if(sucCallBack)
  93. sucCallBack(self.giftList);
  94. } failure:^(NSURLSessionDataTask *task, NSError * error) {
  95. NSLog(@"请求失败,服务器返回的错误信息%@",error);
  96. if(failCallBack)
  97. failCallBack(error);
  98. }];
  99. }
  100. - (BOOL)isNewGiftGot:(NSString *)key dataId:(NSString *)dataId
  101. {
  102. NSString *existDataId = [self.giftGotDict objectForKey:key];
  103. int existId = existDataId ? [existDataId intValue] : 0;
  104. int currentId = [dataId intValue];
  105. if(currentId > existId)
  106. {
  107. [self.giftGotDict setValue:[NSString stringWithFormat:@"%d", currentId] forKey:key];
  108. return YES;
  109. }
  110. return NO;
  111. }
  112. - (void)initGiftGot:(NSString *)key dataId:(NSString *)dataId
  113. {
  114. NSString *existDataId = [self.giftGotDict objectForKey:key];
  115. int existId = existDataId ? [existDataId intValue] : 0;
  116. int currentId = [dataId intValue];
  117. if(currentId > existId)
  118. {
  119. [self.giftGotDict setValue:[NSString stringWithFormat:@"%d", currentId] forKey:key];
  120. }
  121. }
  122. - (BOOL)isNewTradeGot:(NSString *)key dataId:(NSString *)dataId
  123. {
  124. NSString *existDataId = [self.tradeGotDict objectForKey:key];
  125. int existId = existDataId ? [existDataId intValue] : 0;
  126. int currentId = [dataId intValue];
  127. if(currentId > existId)
  128. {
  129. [self.tradeGotDict setValue:[NSString stringWithFormat:@"%d", currentId] forKey:key];
  130. return YES;
  131. }
  132. return NO;
  133. }
  134. - (GiftData *_Nullable)getGiftData:(NSString *)giftId{
  135. if(self.giftList)
  136. {
  137. for(int i=0; i<self.giftList.count; i++)
  138. {
  139. GiftData *data = [self.giftList objectAtIndex:i];
  140. if([data.giftId isEqualToString:giftId])
  141. return data;
  142. }
  143. }
  144. else
  145. {
  146. [self requestGiftList:nil failure:nil];
  147. }
  148. return nil;
  149. }
  150. - (void)sendGif:(GiftData *_Nullable)giftData
  151. count:(int)count
  152. teamId:(NSString *_Nullable)teamId
  153. targetId:(NSString *_Nullable)targetId
  154. msg:(NSString *_Nullable)msg
  155. success:(void (^_Nullable)(NSString * _Nullable giftId, NSString * _Nullable info))sucCallBack
  156. failure:(void (^_Nullable)(NSError * _Nullable error))failCallBack
  157. {
  158. teamId = teamId != nil ? teamId : @"";
  159. targetId = targetId != nil ? targetId : @"";
  160. NSDictionary *parameters = @{@"id":[NSString stringWithFormat:@"%d", self.userId],
  161. @"gift":giftData.giftId,
  162. @"count":[NSString stringWithFormat:@"%d", count],
  163. @"team":teamId,
  164. @"target":targetId,
  165. @"msg":msg != nil ? msg : @""
  166. };
  167. NSString *urlString = @"http://whosay.dashgame.com/index.php?m=who&c=index&a=send_gift";
  168. //请求的managers
  169. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  170. manager.responseSerializer = [AFJSONResponseSerializer new];
  171. manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
  172. //请求的方式:POST
  173. NSLog(@"%@", parameters);
  174. __weak typeof(self) wself = self;
  175. [manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
  176. NSLog(@"请求成功,服务器返回的信息%@", responseObject);
  177. NSDictionary * dic = responseObject;
  178. int code = [[dic valueForKey:@"c"] intValue];
  179. NSDictionary *data = (NSDictionary *)[dic objectForKey:@"d"];
  180. int price = [giftData.price intValue] * count;
  181. //int coin = [[data objectForKey:@"coin"] intValue];
  182. wself.coin -= price;
  183. NSString *info = [data objectForKey:@"info"];
  184. NSString *giftId = [data objectForKey:@"id"];
  185. sucCallBack(giftId, info);
  186. } failure:^(NSURLSessionDataTask *task, NSError * error) {
  187. NSLog(@"请求失败,服务器返回的错误信息%@",error);
  188. failCallBack(error);
  189. }];
  190. }
  191. - (void)requestGif:(NSString *_Nullable)senderId
  192. targetId:(NSString *_Nullable)targetId
  193. teamId:(NSString *_Nullable)teamId
  194. success:(void (^_Nullable)(NSMutableArray * _Nonnull giftArr))sucCallBack
  195. failure:(void (^_Nullable)(NSError * _Nullable error))failCallBack
  196. {
  197. teamId = teamId != nil ? teamId : @"";
  198. targetId = targetId != nil ? targetId : @"";
  199. NSDictionary *parameters = @{@"sender":senderId,
  200. @"team":teamId,
  201. @"target":targetId
  202. };
  203. NSString *urlString = @"http://whosay.dashgame.com/index.php?m=who&c=index&a=request_gift";
  204. //请求的managers
  205. AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
  206. manager.responseSerializer = [AFJSONResponseSerializer new];
  207. manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
  208. //请求的方式:POST
  209. NSLog(@"%@", parameters);
  210. [manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
  211. NSLog(@"请求成功,服务器返回的信息%@", responseObject);
  212. NSDictionary * dic = responseObject;
  213. //int code = [[dic valueForKey:@"c"] intValue];
  214. NSDictionary *data = (NSDictionary *)[dic objectForKey:@"d"];
  215. sucCallBack([data objectForKey:@"list"]);
  216. } failure:^(NSURLSessionDataTask *task, NSError * error) {
  217. NSLog(@"请求失败,服务器返回的错误信息%@",error);
  218. failCallBack(error);
  219. }];
  220. }
  221. @end