// // InviteRecordsView.m // ShenQi // // Created by Yao on 2024/11/6. // #import "InviteRecordsView.h" #import "MacroDefine.h" #import "InviteRecordsTableViewCell.h" #import "AppDelegate.h" #import "DataModels.h" #import #import #import #import static NSString *identifier = @"InviteRecordsTableViewCell"; @implementation InviteRecordsView -(void)awakeFromNib { [super awakeFromNib]; self.titleLabel.text = NSLocalizedString(@"RECORDS", nil); self.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5]; self.mainView.layer.cornerRadius = 4.f; self.mainView.layer.masksToBounds = YES; self.recordsTableView.delegate = self; self.recordsTableView.dataSource = self; self.recordsTableView.rowHeight = 50; self.recordsTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{ self.page = 1; self.recordsArray = @[].mutableCopy; [self setupDeviceCode]; }]; [self.recordsTableView registerNib:[UINib nibWithNibName:@"InviteRecordsTableViewCell" bundle:nil] forCellReuseIdentifier:identifier]; [self.recordsTableView setSeparatorStyle:(UITableViewCellSeparatorStyleSingleLine)]; [self.recordsTableView setSeparatorInset:UIEdgeInsetsMake(0, 0, 0, 0)]; self.recordsTableView.tableFooterView = [UIView new]; self.page = 1; self.recordsArray = @[].mutableCopy; [self setupDeviceCode]; } -(void)setupDeviceCode { // 我的邀请码 __block NSString *advertisingId = nil; AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate; if (ISNULLSTR(appdelegate.inviteCode)) { if (@available(iOS 14, *)) { [ATTrackingManager requestTrackingAuthorizationWithCompletionHandler:^(ATTrackingManagerAuthorizationStatus status) { advertisingId = [[ASIdentifierManager sharedManager] advertisingIdentifier].UUIDString; [self getCodeWithAdvertisingId:advertisingId]; }]; } else { advertisingId = [[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString]; [self getCodeWithAdvertisingId:advertisingId]; } }else{ advertisingId = appdelegate.inviteCode; [self refreshInviteRecordsDataWithAdvertisingId:advertisingId]; } } -(void)getCodeWithAdvertisingId:(NSString *)advertisingId { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:InviteCodeURL]]; request.HTTPMethod = @"POST"; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSDictionary *parmas = @{@"userId":[NSNumber numberWithInteger:[AMB88UserID integerValue]],@"deviceCode":advertisingId}; NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parmas options:NSJSONWritingPrettyPrinted error:nil]; request.HTTPBody = jsonData; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { dispatch_async(dispatch_get_main_queue(), ^{ if (error == nil) { NSError *error; NSMutableDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; if (error == nil) { if (ISNULL(error) && !ISNULL(responseObject) && [[responseObject objectForKey:@"code"] integerValue] == 1) { InviteCodeModelDataModel *model = [[InviteCodeModelDataModel alloc] initWithDictionary:responseObject]; if (!ISNULLSTR(model.data.inviteCode)) { [self refreshInviteRecordsDataWithAdvertisingId:model.data.inviteCode]; } } } } }); }]; [dataTask resume]; } -(void)refreshInviteRecordsDataWithAdvertisingId:(NSString *)advertisingId { NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?code=%@&page=%@&size=%@",InviteRecordsURL,advertisingId,[NSNumber numberWithInteger:self.page],[NSNumber numberWithInteger:20]]]]; request.HTTPMethod = @"GET"; [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; NSURLSession *session = [NSURLSession sharedSession]; NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { dispatch_async(dispatch_get_main_queue(), ^{ if (error) { [SVProgressHUD showInfoWithStatus:error.localizedDescription]; }else{ NSError *error; NSMutableDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error]; if (error) { [SVProgressHUD showInfoWithStatus:error.localizedDescription]; }else{ if (ISNULL(error) && !ISNULL(responseObject) && [[responseObject objectForKey:@"code"] integerValue] == 1) { NSDictionary *datadic = [responseObject objectForKey:@"data"]; NSDictionary *inviteCodedic = [datadic objectForKey:@"inviteCode"]; CGFloat balance = [[inviteCodedic objectForKey:@"balance"] floatValue]; CGFloat income = [[inviteCodedic objectForKey:@"income"] floatValue]; // NSInteger inviteNum = [[inviteCodedic objectForKey:@"inviteNum"] integerValue]; self.balanceLabel.text = [NSString stringWithFormat:@"%@:%.1f",NSLocalizedString(@"Balance", nil),balance]; self.incomeLabel.text = [NSString stringWithFormat:@"%@:%.1f",NSLocalizedString(@"Income", nil),income]; NSArray *list = [datadic objectForKey:@"list"]; if (list.count < 20) { self.recordsTableView.mj_footer = nil; }else if (list.count == 20) { self.recordsTableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{ self.page++; [self setupDeviceCode]; }]; } if (!ISNULLARRAY(list)) { [self.recordsArray addObjectsFromArray:list]; } } } } if (ISNULLARRAY(self.recordsArray)) { self.emptyLabel.hidden = NO; }else{ self.emptyLabel.hidden = YES; } [self.activityIndicator stopAnimating]; [self.recordsTableView reloadData]; [self.recordsTableView.mj_header endRefreshing]; [self.recordsTableView.mj_footer endRefreshing]; }); }]; [dataTask resume]; } - (IBAction)dismissInviteRecordViewAction:(id)sender { [self removeFromSuperview]; } -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.recordsArray.count; } -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { InviteRecordsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier]; cell.titleLabel.text = [self.recordsArray objectAtIndex:indexPath.row]; return cell; } @end