123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295 |
- //
- // User.m
- // NIMDemo
- //
- // Created by Fenix Wang on 2017/5/24.
- // Copyright © 2017年 Netease. All rights reserved.
- //
- #import "User.h"
- #import "AFHTTPSessionManager.h"
- #import "GiftInputContainer.h"
- #import "NIMSDK/NIMSDK.h"
- @interface User()
- @property (nonatomic, strong) NSMutableArray<GiftData *> *giftList;
- @end
- @implementation User
- int newCreateTeamId;
- + (UIColor *)greenColor
- {
- static UIColor *greenColor = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- greenColor = [UIColor colorWithRed:(51.0 / 255.0) green:(205.0 / 255.0) blue:(153.0 / 255.0) alpha:1.0];
- });
- return greenColor;
- }
- + (UIColor *)orangeColor{
- static UIColor *orangeColor = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- orangeColor = [UIColor colorWithRed:(255.0 / 255.0) green:(171.0 / 255.0) blue:(8.0 / 255.0) alpha:1.0];
- });
- return orangeColor;
- }
- + (instancetype)sharedInfo
- {
- static User *sharedInfo = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- sharedInfo = [[User alloc] init];
- sharedInfo.giftGotDict = [[NSMutableDictionary alloc] init];
- sharedInfo.tradeGotDict = [[NSMutableDictionary alloc] init];
- });
- return sharedInfo;
- }
- + (NSString *_Nonnull)distance:(double)originLat originLng:(double)originLng targetLat:(double)targetLat targetLng:(double)targetLng{
-
- double pow1 = pow(sin((targetLat*M_PI/180 - originLat*M_PI/180)/2), 2);
- double pow2 = pow(sin((targetLng*M_PI/180 - originLng*M_PI/180)/2), 2);
- double cos2 = cos(targetLat*M_PI/180) * cos(originLat*M_PI/180);
- double dis = round(6378.138 * 2 * asin(sqrt(pow1 + cos2 * pow2))*1000);
-
- if(dis > 1000)
- return [NSString stringWithFormat:@"%.1FKM", dis/1000];
-
- return [NSString stringWithFormat:@"%.0fM", dis];
- }
- + (void)showUserInfo:(NSString *)userId{
-
- NSLog(@"showUserInfo %@", userId);
-
- }
- - (void)requestGiftList:(void (^_Nullable)(NSMutableArray * _Nullable list))sucCallBack
- failure:(void (^_Nullable)(NSError * _Nullable error))failCallBack
- {
- if(_giftList != nil)
- {
- sucCallBack(_giftList);
- return;
- }
-
-
- NSDictionary *parameters = @{};
- NSString *urlString = @"http://whosay.dashgame.com/index.php?m=who&c=index&a=gift_list";
- //请求的managers
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
- manager.responseSerializer = [AFJSONResponseSerializer new];
- manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
-
- //请求的方式:POST
-
- NSLog(@"%@", parameters);
-
- [manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
-
- NSLog(@"请求成功,服务器返回的信息%@", responseObject);
- NSDictionary * dic = responseObject;
- int code = [[dic valueForKey:@"c"] intValue];
- NSDictionary *data = (NSDictionary *)[dic objectForKey:@"d"];
-
- NSLog(@"Json解析结果 = %d %@", code, data);
-
- self.giftList = [[NSMutableArray alloc] init];
- NSMutableArray *dataList = [data objectForKey:@"list"];
- for(int i=0; i<dataList.count; i++)
- {
- NSDictionary *obj = [dataList objectAtIndex:i];
- GiftData *giftData = [[GiftData alloc] init];
- giftData.giftId = [obj objectForKey:@"id"];
- giftData.name = [obj objectForKey:@"name"];
- giftData.price = [obj objectForKey:@"price"];
- giftData.iconURL = [obj objectForKey:@"icon"];
- giftData.movieURL = [obj objectForKey:@"movie"];
- [self.giftList addObject:giftData];
- }
- if(sucCallBack)
- sucCallBack(self.giftList);
-
- } failure:^(NSURLSessionDataTask *task, NSError * error) {
- NSLog(@"请求失败,服务器返回的错误信息%@",error);
- if(failCallBack)
- failCallBack(error);
- }];
-
- }
- - (BOOL)isNewGiftGot:(NSString *)key dataId:(NSString *)dataId
- {
- NSString *existDataId = [self.giftGotDict objectForKey:key];
- int existId = existDataId ? [existDataId intValue] : 0;
- int currentId = [dataId intValue];
- if(currentId > existId)
- {
- [self.giftGotDict setValue:[NSString stringWithFormat:@"%d", currentId] forKey:key];
- return YES;
- }
-
- return NO;
- }
- - (void)initGiftGot:(NSString *)key dataId:(NSString *)dataId
- {
- NSString *existDataId = [self.giftGotDict objectForKey:key];
- int existId = existDataId ? [existDataId intValue] : 0;
- int currentId = [dataId intValue];
- if(currentId > existId)
- {
- [self.giftGotDict setValue:[NSString stringWithFormat:@"%d", currentId] forKey:key];
- }
- }
- - (BOOL)isNewTradeGot:(NSString *)key dataId:(NSString *)dataId
- {
- NSString *existDataId = [self.tradeGotDict objectForKey:key];
- int existId = existDataId ? [existDataId intValue] : 0;
- int currentId = [dataId intValue];
- if(currentId > existId)
- {
- [self.tradeGotDict setValue:[NSString stringWithFormat:@"%d", currentId] forKey:key];
- return YES;
- }
- return NO;
- }
- - (GiftData *_Nullable)getGiftData:(NSString *)giftId{
-
- if(self.giftList)
- {
- for(int i=0; i<self.giftList.count; i++)
- {
- GiftData *data = [self.giftList objectAtIndex:i];
- if([data.giftId isEqualToString:giftId])
- return data;
- }
- }
- else
- {
- [self requestGiftList:nil failure:nil];
- }
-
- return nil;
- }
- - (void)sendGif:(GiftData *_Nullable)giftData
- count:(int)count
- teamId:(NSString *_Nullable)teamId
- targetId:(NSString *_Nullable)targetId
- msg:(NSString *_Nullable)msg
- success:(void (^_Nullable)(NSString * _Nullable giftId, NSString * _Nullable info))sucCallBack
- failure:(void (^_Nullable)(NSError * _Nullable error))failCallBack
- {
-
- teamId = teamId != nil ? teamId : @"";
- targetId = targetId != nil ? targetId : @"";
-
- NSDictionary *parameters = @{@"id":[NSString stringWithFormat:@"%d", self.userId],
- @"gift":giftData.giftId,
- @"count":[NSString stringWithFormat:@"%d", count],
- @"team":teamId,
- @"target":targetId,
- @"msg":msg != nil ? msg : @""
- };
- NSString *urlString = @"http://whosay.dashgame.com/index.php?m=who&c=index&a=send_gift";
- //请求的managers
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
- manager.responseSerializer = [AFJSONResponseSerializer new];
- manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
-
- //请求的方式:POST
-
- NSLog(@"%@", parameters);
-
- __weak typeof(self) wself = self;
-
- [manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
-
- NSLog(@"请求成功,服务器返回的信息%@", responseObject);
- NSDictionary * dic = responseObject;
- int code = [[dic valueForKey:@"c"] intValue];
- NSDictionary *data = (NSDictionary *)[dic objectForKey:@"d"];
-
- int price = [giftData.price intValue] * count;
-
- //int coin = [[data objectForKey:@"coin"] intValue];
- wself.coin -= price;
-
- NSString *info = [data objectForKey:@"info"];
- NSString *giftId = [data objectForKey:@"id"];
-
- sucCallBack(giftId, info);
-
- } failure:^(NSURLSessionDataTask *task, NSError * error) {
- NSLog(@"请求失败,服务器返回的错误信息%@",error);
- failCallBack(error);
- }];
-
- }
- - (void)requestGif:(NSString *_Nullable)senderId
- targetId:(NSString *_Nullable)targetId
- teamId:(NSString *_Nullable)teamId
- success:(void (^_Nullable)(NSMutableArray * _Nonnull giftArr))sucCallBack
- failure:(void (^_Nullable)(NSError * _Nullable error))failCallBack
- {
-
- teamId = teamId != nil ? teamId : @"";
- targetId = targetId != nil ? targetId : @"";
-
- NSDictionary *parameters = @{@"sender":senderId,
- @"team":teamId,
- @"target":targetId
- };
- NSString *urlString = @"http://whosay.dashgame.com/index.php?m=who&c=index&a=request_gift";
- //请求的managers
- AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
- manager.responseSerializer = [AFJSONResponseSerializer new];
- manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
-
- //请求的方式:POST
-
- NSLog(@"%@", parameters);
-
- [manager POST:urlString parameters:parameters progress:nil success:^(NSURLSessionDataTask *task, id responseObject) {
-
- NSLog(@"请求成功,服务器返回的信息%@", responseObject);
- NSDictionary * dic = responseObject;
- //int code = [[dic valueForKey:@"c"] intValue];
- NSDictionary *data = (NSDictionary *)[dic objectForKey:@"d"];
-
- sucCallBack([data objectForKey:@"list"]);
-
- } failure:^(NSURLSessionDataTask *task, NSError * error) {
- NSLog(@"请求失败,服务器返回的错误信息%@",error);
- failCallBack(error);
- }];
-
- }
- @end
|