JXDetailViewController.m 19 KB

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