// // UserService.m // BabyAlbum // // Created by mambaxie on 2021/7/27. // #import "UserService.h" #import "PYHTTPManager.h" #import "MTCacheManager.h" #import "MTActionSheet.h" //#import "LoginViewController.h" //#import static NSString * const kCurrentUserCacheKey = @"kCurrentUserCacheKey"; static NSString * const kInviteCodeCacheKey = @"kInviteCodeCacheKey"; static NSString * const kRegisterListenCacheKey = @"kRegisterListenCacheKey"; NSNotificationName const UpdatedUserInfoNotification = @"UpdatedUserInfoNotification"; static NSString * const kDidReportLoginActionCacheKey = @"kDidReportLoginActionCacheKey"; NSNotificationName const LoadedUserOtherInfosNotification = @"LoadedUserOtherInfosNotification"; static User *gCurrentUser; NSString *gIdfa = @""; int gRegisterBeListener = 0; @implementation UserService + (BOOL)isLogined { User *user = [self currentUser]; return user.ID > 0; } + (User *)currentUser { if (gCurrentUser) return gCurrentUser; gCurrentUser = [[MTCacheManager defaultCacheManager] objectOfClass:User.class forKey:kCurrentUserCacheKey]; return gCurrentUser; } + (int)currentUserID { return [self currentUser].ID; } + (void)saveInviteCode:(NSString *)invite { [[NSUserDefaults standardUserDefaults] setObject:invite forKey:kInviteCodeCacheKey]; } + (int)registerListenerSucceed { NSNumber *rs = [[NSUserDefaults standardUserDefaults] objectForKey:kRegisterListenCacheKey]; if (rs == nil) { return 0; } return rs.intValue; } + (void)setRegisterListenerSucceed:(int)succeed { [[NSUserDefaults standardUserDefaults] setObject:@(succeed) forKey:kRegisterListenCacheKey]; } //idfa + (NSString *)idfa { if (gIdfa == nil || [gIdfa isEqual: @""]) { return @"00000000-0000-0000-0000-000000000000"; } return gIdfa; } + (void)setIdfa:(NSString *)idfa { gIdfa = idfa; } //listen + (int )registerBeListener { return gRegisterBeListener; } + (void)setRegisterBeListener:(int)registerBeListener { gRegisterBeListener = registerBeListener; } // 更新用户 + (void)updateUser:(User *)user { gCurrentUser = user; [[MTCacheManager defaultCacheManager] setObject:user forKey:kCurrentUserCacheKey]; [[NSNotificationCenter defaultCenter] postNotificationName:UpdatedUserInfoNotification object:nil]; } + (void)updateUserToRemoteWithUser:(User *)user completion:(void(^)(BOOL succeed))completion { [PYHTTPManager postWithPath:@"userupdate" params:[user mj_keyValues] callback:^(id _Nullable rsp, NSError * _Nullable error) { if (completion) { completion(!error); } }]; } + (void)updateUserToRemoteWithParams:(NSDictionary *)params completion:(PYHTTPManagerCallback)completion { [SVProgressHUD showWithStatus:nil]; [PYHTTPManager postWithPath:@"userupdate" params:params callback:^(id _Nullable rsp, NSError * _Nullable error) { [SVProgressHUD dismiss]; if (completion) { completion(rsp, error); } }]; } + (void)updateUserSecurityToRemote:(NSDictionary *)params completion:(PYHTTPManagerCallback)completion { [SVProgressHUD showWithStatus:nil]; [PYHTTPManager postWithPath:@"updatesecurity" params:params callback:^(id _Nullable rsp, NSError * _Nullable error) { [SVProgressHUD dismiss]; if (completion) { completion(rsp, error); } }]; } // 清除用户 + (void)clearUser { gCurrentUser = nil; [[MTCacheManager defaultCacheManager] removeValueForKey:kCurrentUserCacheKey]; } + (void)getUserInfoWithCompletion:(void(^)(User *user))completion { [PYHTTPManager postWithPath:@"userprofile" params:nil callback:^(id _Nullable rsp, NSError * _Nullable error) { if (!error) { User *user = [User mj_objectWithKeyValues:rsp]; [self updateUser:user]; if (completion) { completion(user); } } else { if (completion) { completion(nil); } } }]; } + (void)getUserListWithScene:(UserListScene)scene pageIndex:(NSInteger)index completion:(void(^)(NSArray *users, int totalSize))completion { NSString *serverPath = @""; switch (scene) { case UserListSceneFriends: serverPath = @"friendslist"; break; case UserListSceneFollowers: serverPath = @"followmeusers"; break; case UserListSceneFollowing: serverPath = @"mefollowusers"; break; default: break; } [PYHTTPManager postWithPath:serverPath params:@{ @"page" : @(index), // @"page_size" : @(20) } callback:^(id _Nullable rsp, NSError * _Nullable error) { if (!completion) { return; } if (!error) { NSArray *users = [User mj_objectArrayWithKeyValuesArray:rsp[@"data"]]; int totalSize = [rsp[@"total_size"] intValue]; completion(users, totalSize); } else { completion(nil, 0); } }]; } + (void)getUserInfoWithUserID:(int)userID completion:(void(^)(User *user))completion { if (userID == [UserService currentUser].ID) { // 自己 [self getUserInfoWithCompletion:completion]; return; } [PYHTTPManager postWithPath:@"otheruserprofile" params:@{ @"user_id": @(userID) } callback:^(id _Nullable rsp, NSError * _Nullable error) { if (!completion) { return; } if (!error) { User *user = [User mj_objectWithKeyValues:rsp]; completion(user); } else { completion(nil); } }]; } + (void)setUserBlackWithUserID:(int)userID isBlack:(BOOL)isBlack completion:(PYHTTPManagerCallback)completion { [PYHTTPManager postWithPath:@"userblack" params:@{ @"black_user_id": @(userID), @"type": isBlack ? @(1) : @(2) } callback:completion]; } + (void)getUserBlackWithUserID:(int)userID completion:(void(^)(BOOL isBlack))completion { [PYHTTPManager postWithPath:@"userblackcheck" params:@{ @"black_user_id": @(userID), } callback:^(id _Nullable rsp, NSError * _Nullable error) { if (!completion) { return; } if (!error) { completion([rsp intValue] == 0); return; } completion(NO); }]; } + (void)getUserBlackListWithStartDate:(NSString *)startDate completion:(void(^)(NSArray *users, NSString *nextStartDate))completion { [PYHTTPManager postWithPath:@"userblacklist" params:@{ @"page_size": @(20), @"start_date": startDate ?: @"" } callback:^(id _Nullable rsp, NSError * _Nullable error) { if (!completion) { return; } if (!error) { completion([User mj_objectArrayWithKeyValuesArray:rsp[@"black_users"]], rsp[@"next_start_date"]); } else { completion(nil, nil); } }]; } + (void)followUserWIthUserID:(int)userID completion:(PYHTTPManagerCallback)completion { [PYHTTPManager postWithPath:@"userfollow" params:@{ @"user_id": @(userID) } callback:completion]; } + (void)unfollowUserWIthUserID:(int)userID completion:(PYHTTPManagerCallback)completion { [PYHTTPManager postWithPath:@"userfollowdel" params:@{ @"user_id": @(userID) } callback:completion]; } + (void)modifyPhoneWhshCode:(NSString *)code newPhone:(NSString *)newPhone completion:(PYHTTPManagerCallback)completion { [PYHTTPManager postWithPath:@"modifyphone" params:@{ @"new_phone": newPhone ?: @"", @"code": code ?: @"" } callback:completion]; } static NSMutableDictionary *gUserOtherInfoDicM; + (void)loadUserOtherInfosWithUserIDs:(NSArray *)userIDs userCache:(BOOL)userCache comoletion:(PYHTTPManagerCallback)completion { if (userIDs.count <= 0) { if (completion) { completion(nil, nil); } return; } NSMutableArray *userIDsM = [NSMutableArray array]; for (NSString *imUserID in userIDs) { if (userCache && [self userOtherInfoForUserID:imUserID]) { // 已存在 continue; } [userIDsM addObject:[self userIDFromIMUserID:imUserID]]; } if (userIDsM.count <= 0) { if (completion) { completion(nil, nil); } [[NSNotificationCenter defaultCenter] postNotificationName:LoadedUserOtherInfosNotification object:nil]; return; } [PYHTTPManager postWithPath:@"getuservinfos" params:@{ @"user_ids": [userIDsM componentsJoinedByString:@","] ?: @"" } callback:^(id _Nullable rsp, NSError * _Nullable error) { if (!error) { if (!gUserOtherInfoDicM) { gUserOtherInfoDicM = [NSMutableDictionary dictionary]; } NSDictionary *data = rsp[@"user_vip_infos"]; if ([data isKindOfClass:NSDictionary.class]) { for (NSString *key in data.allKeys) { [gUserOtherInfoDicM removeObjectForKey:key]; [gUserOtherInfoDicM setObject:[UserOtherInfo mj_objectWithKeyValues:data[key]] forKey:key]; } } } if (completion) { completion(rsp, error); } [[NSNotificationCenter defaultCenter] postNotificationName:LoadedUserOtherInfosNotification object:nil]; }]; } + (UserOtherInfo *)userOtherInfoForUserID:(NSString *)userID { return [gUserOtherInfoDicM objectForKey:[self userIDFromIMUserID:userID]]; } + (NSString *)userIDFromIMUserID:(NSString *)imUserID { return [imUserID stringByReplacingOccurrencesOfString:@"hole_" withString:@""]; } + (BOOL)isSecurityCodeOpen { if ([UserService currentUser].security_code.length != SecurityCodeLength) { return false; } return [UserService currentUser].is_security_on > 0 ? true : false; } /// 返回主账号 + (void)backToMainWithCompletion:(PYHTTPManagerCallback)completion { [PYHTTPManager getWithPath:@"backtomain" params:nil callback:^(id _Nullable rsp, NSError * _Nullable error) { if (error) { if (completion) { completion(nil, error); } } else { [LoginService loginByPhoneWithPhone:rsp[@"main_phone"] code:rsp[@"main_code"] completion:completion]; } }]; } /// 进入分身账户 + (void)changeToCloneWithCompletion:(PYHTTPManagerCallback)completion { [PYHTTPManager getWithPath:@"changetoclone" params:nil callback:^(id _Nullable rsp, NSError * _Nullable error) { if (error) { if (completion) { completion(nil, error); } } else { [LoginService loginByPhoneWithPhone:rsp[@"clone_phone"] code:rsp[@"clone_code"] completion:completion]; } }]; } + (void)getC2CUnreadMsgCountWithCompletion:(PYHTTPManagerCallback)completion { if ([LoginService token].length > 0) { [PYHTTPManager postWithPath:@"c2cunreadmessagenum" params:@{ @"account_type": [LoginService isClone] ? @(1) : @(2) // 1获取主账户(默认),2获取分身账户 } callback:completion]; } } /// 获取用户相册 + (void)getUserPhotosWithUserID:(int)userID completion:(void(^)(NSArray *))completion; { [PYHTTPManager postWithPath:@"usermedias" params:@{ @"user_id": @(userID) } callback:^(id _Nullable rsp, NSError * _Nullable error) { if (!completion) { return; } if (!error) { completion([ProfilePhoto mj_objectArrayWithKeyValuesArray:rsp[@"medias"]]); } else { completion(nil); } }]; } + (void)uploadUserPhotoWithImages:(NSArray *)images completion:(PYHTTPManagerCallback)completion { NSMutableArray *imageDatas = [NSMutableArray array]; for (UIImage *photo in images) { NSData *imageData = compressImageToDataIfNeed(photo); if (imageData) { [imageDatas addObject:imageData]; } } [PYHTTPManager uploadFileWithScene:AliOSSUploadSceneProfilePhoto datas:imageDatas completion:^(id _Nullable rsp, NSError * _Nullable error) { if (!completion) { return; } if (!error && rsp) { NSArray *urls = rsp; [PYHTTPManager postWithPath:@"updatemedia" params:@{ @"media_urls": rsp, } callback:^(id _Nullable rsp, NSError * _Nullable error) { dispatch_async(dispatch_get_main_queue(), ^{ if (error) { completion(nil, error); } else { completion(urls, nil); } }); }]; } else { dispatch_async(dispatch_get_main_queue(), ^{ completion(nil, error); }); } }]; } + (void)topUserPhotoWithImageURL:(NSString *)imageURL isTop:(BOOL)isTop completion:(PYHTTPManagerCallback)completion { [PYHTTPManager postWithPath:@"topmedia" params:@{ @"media_url": imageURL, @"is_top": isTop ? @(1) : @(0) } callback:completion]; } + (void)deleteUserPhotoWithImageURL:(NSString *)imageURL completion:(PYHTTPManagerCallback)completion { [PYHTTPManager postWithPath:@"deletemedia" params:@{ @"media_url": imageURL } callback:completion]; } /// 把用户标记为se(仅限管理员账户操作) + (void)tagUserAccount:(int)userId accountType:(NSString *)accountType completion:(PYHTTPManagerCallback)completion{ [PYHTTPManager postWithPath:@"tagaccounttype" params:@{ @"user_id": @(userId), @"account_type": accountType } callback:completion]; } /// 封号(仅限管理员账户操作) + (void)banAccount:(int)uid hours:(int)hours completion:(PYHTTPManagerCallback)completion { [PYHTTPManager postWithPath:@"banaccount" params:@{ @"user_id": @(uid), @"ban_hour": @(hours) } callback:completion]; } /// 仅限管理员账户操作 + (void)modifyAccount:(int)uid param:(NSString *)param completion:(PYHTTPManagerCallback)completion { [PYHTTPManager postWithPath:@"modifyaccountinfo" params:@{ @"user_id": @(uid), @"modify_param": param } callback:completion]; } //管理账户(管理员功能) + (void)tagUserAccount:(int)userId accountType:(NSString *)accountType{ [UserService tagUserAccount:userId accountType:accountType completion:^(id _Nullable rsp, NSError * _Nullable error) { if (!error) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [ToastUtil showToast:[NSString stringWithFormat:@"标记(%d)用户为%@号成功\nTa的normal的瓶子也被标记为%@", userId, accountType, accountType]]; }); } }]; } + (void)dealUser:(int)userId{ MTActionSheet *actionSheet = [[MTActionSheet alloc] initWithTitle:@"处理账户(ad/se/porn会被限制行为且聊天需VIP)" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"封号" otherButtonTitles:@"解封该账户", @"账户信息违规",@"标记为普通号", @"标记为good号", @"标记为se号", @"标记为porn号",@"标记为ad号", @"标记为adapp号", nil]; actionSheet.onClickAction = ^(MTActionSheet *sheet, NSInteger buttonIndex){ if (buttonIndex == 0) { //封号 MTActionSheet *actionSheet = [[MTActionSheet alloc] initWithTitle:@"封禁账户(无法发帖、回帖和聊天)" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"1天" otherButtonTitles:@"2天", @"3天", @"7天", @"14天", @"30天", @"90天",@"永久封禁", nil]; actionSheet.onClickAction = ^(MTActionSheet *sheet, NSInteger buttonIndex){ int banDay = 0; switch (buttonIndex) { case 0: banDay = 1; break; case 1: banDay = 2; break; case 2: banDay = 3; break; case 3: banDay = 7; break; case 4: banDay = 14; break; case 5: banDay = 30; break; case 6: banDay = 90; break; case 7: banDay = 9999; break; default: break; } if (banDay > 0) { if(banDay == 9999){ // banDay = banDay*24; }else{ banDay = banDay*24; } [UserService banAccount:userId hours:banDay completion:^(id _Nullable rsp, NSError * _Nullable error) { if (!error) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"用户(%d)被封号(%d天)", userId, banDay]]; }); } }]; } }; [actionSheet show]; }else if(buttonIndex == 1){ //解封 [UserService banAccount:userId hours:-1 completion:^(id _Nullable rsp, NSError * _Nullable error) { if (!error) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"用户(%d)已经被解封", userId]]; }); } }]; }else if(buttonIndex == 2){ MTActionSheet *actionSheet = [[MTActionSheet alloc] initWithTitle:@"修改账户信息" delegate:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"取消" otherButtonTitles:@"昵称违规", @"头像违规", @"个人介绍违规", @"背景图违规", nil]; actionSheet.onClickAction = ^(MTActionSheet *sheet, NSInteger buttonIndex){ NSString *paramName = 0; switch (buttonIndex) { //avatar nickname introduction cover_img case 1: paramName = @"nickname"; break; case 2: paramName = @"avatar"; break; case 3: paramName = @"introduction"; break; case 4: paramName = @"cover_img"; break; default: break; } if (paramName != nil) { [UserService modifyAccount:userId param:paramName completion:^(id _Nullable rsp, NSError * _Nullable error) { if (!error) { dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [SVProgressHUD showSuccessWithStatus:[NSString stringWithFormat:@"用户(%d)%@重置成功", userId, paramName]]; }); } }]; } }; [actionSheet show]; }else { //标记账户类型 NSString *dealString = nil; switch (buttonIndex) { case 3: dealString = @"normal"; break; case 4: dealString = @"good"; break; case 5: dealString = @"se"; break; case 6: dealString = @"porn"; break; case 7: dealString = @"ad"; break; case 8: dealString = @"adapp"; break; default: break; } if (dealString != nil) { [UserService tagUserAccount:userId accountType:dealString]; }else{ } } }; [actionSheet show]; } @end