// // EPMineViewController.m // YuMi // // Created by AI on 2025-10-09. // Copyright © 2025 YuMi. All rights reserved. // #import "EPMineViewController.h" #import "EPMineHeaderView.h" #import "EPMomentCell.h" #import #import "Api+Moments.h" #import "AccountInfoStorage.h" #import "UserInfoModel.h" #import "MomentsInfoModel.h" #import @interface EPMineViewController () // MARK: - UI Components /// 主列表(显示用户动态) @property (nonatomic, strong) UITableView *tableView; /// 顶部个人信息卡片 @property (nonatomic, strong) EPMineHeaderView *headerView; // MARK: - Data /// 用户动态数据源 @property (nonatomic, strong) NSMutableArray *momentsData; /// 当前页码 @property (nonatomic, assign) NSInteger currentPage; /// 是否正在加载 @property (nonatomic, assign) BOOL isLoading; /// 用户信息模型 @property (nonatomic, strong) UserInfoModel *userInfo; @end @implementation EPMineViewController // MARK: - Lifecycle - (void)viewDidLoad { [super viewDidLoad]; self.momentsData = [NSMutableArray array]; self.currentPage = 1; self.isLoading = NO; [self setupUI]; [self loadUserInfo]; [self loadUserMoments]; NSLog(@"[EPMineViewController] 个人主页加载完成"); } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; // 注意:当前 ViewController 没有包装在 NavigationController 中 // 如果未来需要导航栏,应该在 TabBarController 中包装 UINavigationController } // MARK: - Setup - (void)setupGradientBackground { CAGradientLayer *gradientLayer = [CAGradientLayer layer]; gradientLayer.frame = self.view.bounds; gradientLayer.colors = @[ (id)[UIColor colorWithRed:0.3 green:0.2 blue:0.6 alpha:1.0].CGColor, // 深紫 #4C3399 (id)[UIColor colorWithRed:0.2 green:0.3 blue:0.8 alpha:1.0].CGColor // 蓝 #3366CC ]; gradientLayer.startPoint = CGPointMake(0, 0); gradientLayer.endPoint = CGPointMake(1, 1); [self.view.layer insertSublayer:gradientLayer atIndex:0]; } - (void)setupUI { // 先设置纯色背景作为兜底,避免白色闪烁 self.view.backgroundColor = [UIColor colorWithRed:0.95 green:0.95 blue:0.97 alpha:1.0]; UIImageView *bgImageView = [[UIImageView alloc] initWithImage:kImage(@"vc_bg")]; bgImageView.contentMode = UIViewContentModeScaleAspectFill; bgImageView.clipsToBounds = YES; [self.view addSubview:bgImageView]; [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) { make.edges.mas_equalTo(self.view); }]; [self setupHeaderView]; [self setupTableView]; NSLog(@"[EPMineViewController] UI 设置完成"); } - (void)setupHeaderView { self.headerView = [[EPMineHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)]; [self.view addSubview:self.headerView]; [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(20); make.left.right.equalTo(self.view); make.height.equalTo(@300); }]; } - (void)setupTableView { self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain]; self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.backgroundColor = [UIColor clearColor]; self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; self.tableView.showsVerticalScrollIndicator = NO; // 注册动态 cell(复用 EPMomentCell) [self.tableView registerClass:[EPMomentCell class] forCellReuseIdentifier:@"EPMomentCell"]; [self.view addSubview:self.tableView]; [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.equalTo(self.headerView.mas_bottom).offset(10); make.left.right.equalTo(self.view); make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom); }]; // 添加下拉刷新 UIRefreshControl *refreshControl = [[UIRefreshControl alloc] init]; [refreshControl addTarget:self action:@selector(refreshData) forControlEvents:UIControlEventValueChanged]; self.tableView.refreshControl = refreshControl; } // MARK: - Data Loading - (void)loadUserInfo { NSString *uid = [[AccountInfoStorage instance] getUid]; if (!uid.length) { NSLog(@"[EPMineViewController] 未登录,无法获取用户信息"); return; } // 调用真实 API 获取用户信息 [Api getUserInfo:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) { if (code == 200 && data.data) { self.userInfo = [UserInfoModel mj_objectWithKeyValues:data.data]; // 更新头部视图 NSDictionary *userInfoDict = @{ @"nickname": self.userInfo.nick ?: @"未设置昵称", @"avatar": self.userInfo.avatar ?: @"", @"uid": self.userInfo.uid > 0 ? @(self.userInfo.uid).stringValue : @"", @"followers": @(self.userInfo.fansNum), @"following": @(self.userInfo.followNum), }; [self.headerView updateWithUserInfo:userInfoDict]; NSLog(@"[EPMineViewController] 用户信息加载成功: %@", self.userInfo.nick); } else { NSLog(@"[EPMineViewController] 用户信息加载失败: %@", msg); } } uid:uid]; } - (void)refreshUserInfo { [self loadUserInfo]; } - (void)loadUserMoments { if (self.isLoading) return; NSString *uid = [[AccountInfoStorage instance] getUid]; if (!uid.length) { NSLog(@"[EPMineViewController] 未登录,无法获取用户动态"); return; } self.isLoading = YES; NSString *page = [NSString stringWithFormat:@"%ld", (long)self.currentPage]; // 调用获取用户动态的 API(这里先用通用的动态列表 API) [Api momentsRecommendList:^(BaseModel * _Nullable data, NSInteger code, NSString * _Nullable msg) { self.isLoading = NO; [self.refreshControl endRefreshing]; if (code == 200 && data.data) { NSArray *list = [MomentsInfoModel mj_objectArrayWithKeyValuesArray:data.data]; if (list.count > 0) { [self.momentsData addObjectsFromArray:list]; self.currentPage++; [self.tableView reloadData]; NSLog(@"[EPMineViewController] 用户动态加载成功,新增 %lu 条", (unsigned long)list.count); } } else { NSLog(@"[EPMineViewController] 用户动态加载失败: %@", msg); } } page:page pageSize:@"10" types:@"0,2"]; } - (void)refreshData { self.currentPage = 1; [self.momentsData removeAllObjects]; // 手动下拉刷新时才更新用户信息 [self loadUserInfo]; [self loadUserMoments]; } // MARK: - UITableView DataSource - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section { return self.momentsData.count; } - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { EPMomentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"EPMomentCell" forIndexPath:indexPath]; cell.backgroundColor = [UIColor clearColor]; if (indexPath.row < self.momentsData.count) { [cell configureWithModel:self.momentsData[indexPath.row]]; } return cell; } - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { return 200; // 根据实际内容调整 } // MARK: - UITableView Delegate - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath { // 滚动到底部自动加载更多 if (indexPath.row == self.momentsData.count - 1 && !self.isLoading) { [self loadUserMoments]; } } // MARK: - Lazy Loading - (EPMineHeaderView *)headerView { if (!_headerView) { _headerView = [[EPMineHeaderView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 300)]; } return _headerView; } - (UIRefreshControl *)refreshControl { return self.tableView.refreshControl; } @end