JXDetailViewController.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. //
  2. // JXDetailViewController.m
  3. // AICity
  4. //
  5. // Created by TogetherWatch on 2025-10-13.
  6. // 短剧详情+播放器页面(对标Android的JuXingPlayerActivity)
  7. //
  8. #import "JXDetailViewController.h"
  9. #import "JXAPIService.h"
  10. #import "JXCacheManager.h"
  11. #import "JXDrama.h"
  12. #import "JXEpisode.h"
  13. #import "JXInteraction.h"
  14. #import "JXAVPlayer.h"
  15. #import "JXSuperPlayer.h"
  16. #import <AVFoundation/AVFoundation.h>
  17. #import "JXCollectionViewController.h"
  18. @interface JXDetailViewController () <JXSuperPlayerDelegate, UICollectionViewDelegate, UICollectionViewDataSource,JXCollectionViewControllerDelegate>
  19. // 播放器
  20. @property (nonatomic, strong) UIView *playerContainer;
  21. @property (nonatomic, strong) JXSuperPlayer *player;
  22. // UI组件
  23. @property (nonatomic, strong) UICollectionView *episodeCollectionView;
  24. @property (nonatomic, strong) UILabel *titleLabel;
  25. @property (nonatomic, strong) UILabel *descLabel;
  26. @property (nonatomic, strong) UILabel *authorLabel;
  27. @property (nonatomic, strong) UIButton *likeButton;
  28. @property (nonatomic, strong) UILabel *likeCountLabel;
  29. @property (nonatomic, strong) UIButton *favoriteButton;
  30. @property (nonatomic, strong) UILabel *favoriteCountLabel;
  31. @property (nonatomic, strong) UIButton *commentButton;
  32. @property (nonatomic, strong) UILabel *commentCountLabel;
  33. // 数据
  34. @property (nonatomic, strong) JXDrama *drama;
  35. @property (nonatomic, strong) NSArray<JXEpisode *> *episodes;
  36. @property (nonatomic, strong) JXInteraction *interaction;
  37. @property (nonatomic, assign) NSInteger currentEpisodeIndex;
  38. @end
  39. @implementation JXDetailViewController
  40. - (instancetype)initWithDramaId:(NSString *)dramaId {
  41. self = [super init];
  42. if (self) {
  43. _dramaId = [dramaId copy];
  44. _currentEpisodeIndex = 0;
  45. }
  46. return self;
  47. }
  48. - (void)viewDidLoad {
  49. [super viewDidLoad];
  50. self.view.backgroundColor = [UIColor blackColor];
  51. // 隐藏导航栏返回按钮
  52. self.navigationItem.hidesBackButton = YES;
  53. [self setupUI];
  54. [self loadData];
  55. }
  56. - (void)setupUI {
  57. CGFloat width = self.view.bounds.size.width;
  58. CGFloat height = self.view.bounds.size.height;
  59. // 创建垂直滚动的CollectionView(每集占满屏幕)
  60. UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
  61. layout.scrollDirection = UICollectionViewScrollDirectionVertical;
  62. layout.itemSize = CGSizeMake(width, height);
  63. layout.minimumLineSpacing = 0;
  64. layout.minimumInteritemSpacing = 0;
  65. self.episodeCollectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds
  66. collectionViewLayout:layout];
  67. self.episodeCollectionView.backgroundColor = [UIColor blackColor];
  68. self.episodeCollectionView.delegate = self;
  69. self.episodeCollectionView.dataSource = self;
  70. self.episodeCollectionView.pagingEnabled = YES;
  71. self.episodeCollectionView.showsVerticalScrollIndicator = NO;
  72. [self.episodeCollectionView registerClass:[UICollectionViewCell class]
  73. forCellWithReuseIdentifier:@"EpisodeCell"];
  74. [self.view addSubview:self.episodeCollectionView];
  75. // 设置约束
  76. self.episodeCollectionView.translatesAutoresizingMaskIntoConstraints = NO;
  77. [NSLayoutConstraint activateConstraints:@[
  78. [self.episodeCollectionView.topAnchor constraintEqualToAnchor:self.view.topAnchor],
  79. [self.episodeCollectionView.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
  80. [self.episodeCollectionView.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor],
  81. [self.episodeCollectionView.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor]
  82. ]];
  83. }
  84. - (void)loadData {
  85. if (!self.dramaId) {
  86. return;
  87. }
  88. // 从网络加载
  89. [[JXAPIService sharedService] getDramaDetailWithDramaId:self.dramaId success:^(id responseObject) {
  90. NSDictionary *response = responseObject;
  91. NSLog(@"📡 getDramaDetail响应代码: %@", response[@"code"]);
  92. NSLog(@"📡 getDramaDetail完整响应: %@", response);
  93. if ([response[@"code"] intValue] == 0) {
  94. NSDictionary *data = response[@"data"];
  95. NSLog(@"✅ 获取短剧数据成功");
  96. NSLog(@"📊 Drama数据: %@", data[@"drama"]);
  97. NSLog(@"📊 Episodes数量: %lu", (unsigned long)[data[@"episodes"] count]);
  98. [self updateUIWithData:data];
  99. // 自动滚动到第一集
  100. NSLog(@"⏯️ 自动滚动检查 - 剧集总数: %lu", (unsigned long)self.episodes.count);
  101. if (self.episodes.count > 0) {
  102. NSLog(@"⏯️ 滚动到第一集");
  103. [self.episodeCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0]
  104. atScrollPosition:UICollectionViewScrollPositionCenteredVertically
  105. animated:NO];
  106. } else {
  107. NSLog(@"❌ 剧集列表为空,无法播放");
  108. }
  109. } else {
  110. NSLog(@"❌ API返回错误代码: %@", response[@"code"]);
  111. }
  112. } failure:^(NSError *error) {
  113. NSLog(@"❌ 加载短剧详情失败: %@", error.localizedDescription);
  114. }];
  115. }
  116. - (void)updateUIWithData:(NSDictionary *)data {
  117. // 解析数据
  118. self.drama = [[JXDrama alloc] initWithDictionary:data[@"drama"]];
  119. NSArray *episodesData = data[@"episodes"];
  120. NSMutableArray *episodesList = [NSMutableArray array];
  121. for (NSDictionary *episodeDict in episodesData) {
  122. JXEpisode *episode = [[JXEpisode alloc] initWithDictionary:episodeDict];
  123. [episodesList addObject:episode];
  124. }
  125. self.episodes = episodesList;
  126. self.interaction = [[JXInteraction alloc] initWithDictionary:data[@"interaction"]];
  127. [self.episodeCollectionView reloadData];
  128. }
  129. #pragma mark - UICollectionViewDataSource
  130. - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
  131. return self.episodes.count;
  132. }
  133. - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
  134. UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"EpisodeCell"
  135. forIndexPath:indexPath];
  136. // 清理之前的子视图
  137. for (UIView *view in cell.contentView.subviews) {
  138. [view removeFromSuperview];
  139. }
  140. cell.contentView.backgroundColor = [UIColor blackColor];
  141. CGFloat width = cell.contentView.bounds.size.width;
  142. CGFloat height = cell.contentView.bounds.size.height;
  143. // 播放器容器(占据上半部分)
  144. CGFloat playerHeight = height;
  145. UIView *playerContainer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, width, playerHeight)];
  146. playerContainer.backgroundColor = [UIColor blackColor];
  147. [cell.contentView addSubview:playerContainer];
  148. [cell.contentView sendSubviewToBack:playerContainer];
  149. // 创建播放器
  150. JXSuperPlayer *player = [[JXSuperPlayer alloc] initWithContainerView:playerContainer];
  151. player.delegate = self;
  152. // 标题
  153. // 右侧互动按钮
  154. // [self setupInteractionButtonsForCell:cell atY:y];
  155. UILabel * seeL = [[UILabel alloc] initWithFrame:CGRectMake(16, kScreenHeight - 14-safebottom, SCREEN_WIDTH - 32, 14)];
  156. seeL.font = [UIFont systemFontOfSize:14 weight:UIFontWeightMedium];
  157. seeL.textColor = [UIColor colorWithRed:173/255.0 green:173/255.0 blue:173/255.0 alpha:1];
  158. [cell.contentView addSubview:seeL];
  159. seeL.text = [NSString stringWithFormat:@"%@次播放",@"30万"];
  160. UIView *bottomView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMinY(seeL.frame) - 58, SCREEN_WIDTH, 38)];
  161. bottomView.backgroundColor = [UIColor colorWithRed:255/255.0 green:255/255.0 blue:255/255.0 alpha:0.1];
  162. [cell.contentView addSubview:bottomView];
  163. UIImageView *sImgView = [[UIImageView alloc] initWithFrame:CGRectMake(16, 10.5, 17, 17)];
  164. sImgView.image = [UIImage imageNamed:@"icon 2.1 拷贝"];
  165. [bottomView addSubview:sImgView];
  166. UILabel *hjL = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(sImgView.frame)+4, 0, 100, 38)];
  167. [bottomView addSubview:hjL];
  168. hjL.font = [UIFont boldSystemFontOfSize:18];
  169. hjL.textColor = UIColor.whiteColor;
  170. hjL.text = @"合集";
  171. UIImageView *mImgView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREEN_WIDTH-27, 13.5, 11, 11)];
  172. mImgView.image = [UIImage imageNamed:@"Frame 9366"];
  173. [bottomView addSubview:mImgView];
  174. UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showHJ:)];
  175. [bottomView addGestureRecognizer:tap];
  176. UILabel *descLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMinY(bottomView.frame)-60, width - 32, 40)];
  177. descLabel.font = [UIFont systemFontOfSize:15 weight:UIFontWeightMedium];
  178. descLabel.textColor = [UIColor whiteColor];
  179. descLabel.numberOfLines = 2;
  180. [cell.contentView addSubview:descLabel];
  181. [cell.contentView bringSubviewToFront:descLabel];
  182. UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, CGRectGetMinY(descLabel.frame) - 50, width - 100, 40)];
  183. titleLabel.font = [UIFont boldSystemFontOfSize:18];
  184. titleLabel.textColor = [UIColor whiteColor];
  185. titleLabel.numberOfLines = 2;
  186. [cell.contentView addSubview:titleLabel];
  187. [cell.contentView bringSubviewToFront:titleLabel];
  188. // // 作者
  189. // UILabel *authorLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, y, width - 32, 20)];
  190. // authorLabel.font = [UIFont systemFontOfSize:13];
  191. // authorLabel.textColor = [UIColor grayColor];
  192. // [cell.contentView addSubview:authorLabel];
  193. //
  194. // [cell.contentView bringSubviewToFront:authorLabel];
  195. // y += 30;
  196. // // 剧集信息区域
  197. // UIView *episodeInfoView = [[UIView alloc] initWithFrame:CGRectMake(0, y, width, height - y)];
  198. // episodeInfoView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:1.0];
  199. // [cell.contentView addSubview:episodeInfoView];
  200. JXEpisode *episode = self.episodes[indexPath.item];
  201. //
  202. // // 更新标签
  203. titleLabel.text = self.drama.title ?: @"";
  204. descLabel.text = self.drama.desc ?: @"";
  205. // authorLabel.text = [NSString stringWithFormat:@"@%@", self.drama.authorInfo ?: @"未知"];
  206. //
  207. // // 剧集编号和标题
  208. // UILabel *episodeTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(16, 20, width - 32, 40)];
  209. // NSString *episodeTitle = [NSString stringWithFormat:@"第%ld集", (long)episode.episodeNumber];
  210. // if (episode.title && episode.title.length > 0) {
  211. // episodeTitle = [NSString stringWithFormat:@"%@ - %@", episodeTitle, episode.title];
  212. // }
  213. // episodeTitleLabel.text = episodeTitle;
  214. // episodeTitleLabel.font = [UIFont boldSystemFontOfSize:16];
  215. // episodeTitleLabel.textColor = [UIColor whiteColor];
  216. // episodeTitleLabel.numberOfLines = 2;
  217. // [episodeInfoView addSubview:episodeTitleLabel];
  218. // 播放视频
  219. [player playWithAppId:episode.appId fileId:episode.fileId psign:episode.psign];
  220. return cell;
  221. }
  222. - (void)showHJ:(UIGestureRecognizer *)gtr{
  223. JXEpisode *episode = self.episodes[self.currentEpisodeIndex];
  224. [self showCollectionDialogWithDramaId:[NSString stringWithFormat:@"%ld",episode.dramaId]];
  225. }
  226. - (void)showCollectionDialogWithDramaId:(NSString *)dramaId {
  227. JXCollectionViewController *collectionVC = [[JXCollectionViewController alloc] initWithDramaId:dramaId];
  228. collectionVC.delegate = self;
  229. // 设置弹窗样式(底部弹出)
  230. if (@available(iOS 15.0, *)) {
  231. UISheetPresentationController *sheet = collectionVC.sheetPresentationController;
  232. sheet.detents = @[
  233. [UISheetPresentationControllerDetent mediumDetent],
  234. [UISheetPresentationControllerDetent largeDetent]
  235. ];
  236. sheet.prefersGrabberVisible = YES;
  237. }
  238. [self presentViewController:collectionVC animated:YES completion:nil];
  239. }
  240. - (void)collectionViewControllerDidSelectEpisode:(NSString *)episodeId {
  241. NSLog(@"[JXShortDrama] 选择播放剧集: %@", episodeId);
  242. // TODO: 切换到选定的剧集播放
  243. // 可以找到对应的短剧,然后滚动到该位置并播放
  244. }
  245. #pragma mark - UICollectionViewDelegate
  246. - (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath {
  247. // 停止不可见的播放器
  248. }
  249. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
  250. // 获取当前可见的cell索引
  251. CGFloat pageWidth = scrollView.frame.size.width;
  252. CGFloat pageHeight = scrollView.frame.size.height;
  253. NSInteger currentPage = (NSInteger)(scrollView.contentOffset.y / pageHeight);
  254. if (currentPage != self.currentEpisodeIndex) {
  255. self.currentEpisodeIndex = currentPage;
  256. NSLog(@"📺 滚动到第 %ld 集", (long)currentPage + 1);
  257. }
  258. }
  259. #pragma mark - Helper Methods
  260. - (void)setupInteractionButtonsForCell:(UICollectionViewCell *)cell atY:(CGFloat)y {
  261. CGFloat width = cell.contentView.bounds.size.width;
  262. UIView *container = [[UIView alloc] initWithFrame:CGRectMake(width - 70, y, 60, 180)];
  263. [cell.contentView addSubview:container];
  264. CGFloat buttonY = 0;
  265. // 点赞
  266. UIButton *likeButton = [UIButton buttonWithType:UIButtonTypeSystem];
  267. likeButton.frame = CGRectMake(10, buttonY, 40, 40);
  268. [likeButton setImage:[UIImage systemImageNamed:@"heart"] forState:UIControlStateNormal];
  269. [likeButton setImage:[UIImage systemImageNamed:@"heart.fill"] forState:UIControlStateSelected];
  270. likeButton.tintColor = [UIColor whiteColor];
  271. [container addSubview:likeButton];
  272. UILabel *likeCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, buttonY + 40, 60, 20)];
  273. likeCountLabel.textAlignment = NSTextAlignmentCenter;
  274. likeCountLabel.font = [UIFont systemFontOfSize:12];
  275. likeCountLabel.textColor = [UIColor whiteColor];
  276. likeCountLabel.text = [self formatCount:self.interaction.likeCount];
  277. [container addSubview:likeCountLabel];
  278. buttonY += 70;
  279. // 收藏
  280. UIButton *favoriteButton = [UIButton buttonWithType:UIButtonTypeSystem];
  281. favoriteButton.frame = CGRectMake(10, buttonY, 40, 40);
  282. [favoriteButton setImage:[UIImage systemImageNamed:@"star"] forState:UIControlStateNormal];
  283. [favoriteButton setImage:[UIImage systemImageNamed:@"star.fill"] forState:UIControlStateSelected];
  284. favoriteButton.tintColor = [UIColor whiteColor];
  285. [container addSubview:favoriteButton];
  286. UILabel *favoriteCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, buttonY + 40, 60, 20)];
  287. favoriteCountLabel.textAlignment = NSTextAlignmentCenter;
  288. favoriteCountLabel.font = [UIFont systemFontOfSize:12];
  289. favoriteCountLabel.textColor = [UIColor whiteColor];
  290. favoriteCountLabel.text = [self formatCount:self.interaction.favoriteCount];
  291. [container addSubview:favoriteCountLabel];
  292. buttonY += 70;
  293. // 评论
  294. UIButton *commentButton = [UIButton buttonWithType:UIButtonTypeSystem];
  295. commentButton.frame = CGRectMake(10, buttonY, 40, 40);
  296. [commentButton setImage:[UIImage systemImageNamed:@"bubble.right"] forState:UIControlStateNormal];
  297. commentButton.tintColor = [UIColor whiteColor];
  298. [container addSubview:commentButton];
  299. // [commentButton addTarget:self action:@selector(commentDidClicked) forControlEvents:UIControlEventTouchUpInside];
  300. UILabel *commentCountLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, buttonY + 40, 60, 20)];
  301. commentCountLabel.textAlignment = NSTextAlignmentCenter;
  302. commentCountLabel.font = [UIFont systemFontOfSize:12];
  303. commentCountLabel.textColor = [UIColor whiteColor];
  304. commentCountLabel.text = [self formatCount:self.interaction.commentCount];
  305. [container addSubview:commentCountLabel];
  306. }
  307. - (NSString *)formatCount:(long long)count {
  308. if (count >= 1000000) {
  309. return [NSString stringWithFormat:@"%.1fM", count / 1000000.0];
  310. } else if (count >= 1000) {
  311. return [NSString stringWithFormat:@"%.1fK", count / 1000.0];
  312. }
  313. return [NSString stringWithFormat:@"%lld", count];
  314. }
  315. - (void)dealloc {
  316. // 停止所有播放器
  317. }
  318. @end