杨锴
2025-04-16 09a372bc45fde16fd42257ab6f78b8deeecf720b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//
//  CourseVCTeacherSpecialVC.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/15.
//
 
import UIKit
import JQTools
 
let CourseVCTeacherSpecial_Update = Notification.Name.init("CourseVCTeacherSpecial_Update")
 
class CourseVCTeacherSpecialVC: BaseVC {
                private var tableView:UITableView?
                private var headerView:VideoView!
                private var model:TutorInfoModel?
 
                private var cell0:CourseDetail_1_TCell?
                private var cell1:CourseDetail_3_TCell?
 
                override func viewDidDisappear(_ animated: Bool) {
                                super.viewDidDisappear(animated)
                                headerView.pauseVideo()
                }
 
    override func viewDidLoad() {
        super.viewDidLoad()
 
                                cell0    = (tableView!.dequeueReusableCell(withIdentifier: "_CourseDetail_1_TCell") as! CourseDetail_1_TCell)
                                cell1 = (tableView!.dequeueReusableCell(withIdentifier: "_CourseDetail_3_TCell") as! CourseDetail_3_TCell)
 
                                cell0!.backgroundColor = UIColor(hexString: "#f6f6f6")
                                cell1!.backgroundColor = UIColor(hexString: "#f6f6f6")
 
 
        getData()
//        LoginSuccess_Noti
 
        NotificationCenter.default.rx.notification(LoginSuccess_Noti).take(until: self.rx.deallocated).subscribe {[unowned self] _ in
            self.getData()
        }.disposed(by: disposeBag)
 
 
        self.cell0?.webView.scrollView.rx.observe(CGSize.self, "contentSize").map { (size) -> CGFloat? in
            if let size = size{
                return size.height
            }
            return nil
        }.subscribe(onNext: { [unowned self](height) in
            if let height = height{
                if height > self.cell0?.cons_webHei.constant ?? 0{
                    self.cell0?.cons_webHei.constant = height
                    self.tableView?.reloadData()
                }
            }
        }).disposed(by: disposeBag)
    }
 
    private func getData(){
        Services.tutorInfo().subscribe(onNext: {[weak self] data in
            guard let weakSelf = self else { return }
            if let m = data.data{
                weakSelf.model = m
                weakSelf.cell0!.setContent(title: "导师简介", content: m.tutorIntroduction)
                weakSelf.cell1!.setItems(m.list)
                weakSelf.tableView?.reloadData()
                if let videoUrl = weakSelf.model?.videoUrl.jq_urlEncoded(),videoUrl.jq_isVideo{
                    weakSelf.checkCacheAudio(from: URL(string:videoUrl)!) { state, url in
                        weakSelf.headerView.updateVideoUrl(videoUrl,autoPlay: false,placeHoderImageUrl: m.coverUrl.jq_urlEncoded(),delegate: self)
                    }
                }
            }
        }).disposed(by: disposeBag)
    }
 
                override func setUI() {
                                super.setUI()
                                tableView = UITableView(frame: .zero, style: .plain)
                                tableView!.contentInsetAdjustmentBehavior = .never
                                tableView!.separatorStyle = .none
                                tableView!.delegate = self
                                tableView!.dataSource = self
                                tableView?.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 20, right: 0)
                                tableView!.showsVerticalScrollIndicator = false
                                if #available(iOS 15.0, *) {
                                                tableView!.sectionHeaderTopPadding = 0
                                }
                                tableView!.backgroundColor = UIColor(hexString: "#f6f6f6")
                                tableView!.register(UINib(nibName: "CourseDetail_1_TCell", bundle: nil), forCellReuseIdentifier: "_CourseDetail_1_TCell")
                                tableView!.register(UINib(nibName: "CourseDetail_3_TCell", bundle: nil), forCellReuseIdentifier: "_CourseDetail_3_TCell")
 
                                view.addSubview(tableView!)
                                tableView!.snp.makeConstraints { make in
                                                make.edges.equalToSuperview()
                                }
 
                                DispatchQueue.main.async {
                                                self.headerView = VideoView(url: nil)
                                                self.tableView!.tableHeaderView = self.headerView
                                                self.headerView.frame = CGRect(x: 0, y: 0, width: JQ_ScreenW, height: JQ_ScreenW * 0.56)
                                }
                }
 
                override func setRx() {
 
                }
 
                override var shouldAutorotate: Bool{
                                return  true
                }
 
    internal func checkCacheAudio(from url: URL, completion: @escaping (Bool,URL) -> Void) {
 
        let cacheDirectory = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent("Masters")
 
        let videoCacheUrl = cacheDirectory.appendingPathComponent(url.lastPathComponent.jq_md5String() + "." + url.pathExtension)
 
        // 检查缓存中是否已存在文件
        if FileManager.default.fileExists(atPath: videoCacheUrl.path) {
            print("启用缓存")
            completion(true,videoCacheUrl)
            return
        }else{
            completion(false,url)
            print("没有缓存:执行下载")
        }
 
        // 使用URLSession下载视频
        let downloadTask = URLSession.shared.downloadTask(with: url) {
            tempLocalUrl, response, error in
            print("执行下载任务")
            if let tempLocalUrl = tempLocalUrl, error == nil {
                do {
                    let temp = videoCacheUrl
 
                    if !(FileManager.default.fileExists(atPath: cacheDirectory.path)){
                        try FileManager.default.createDirectory(at: cacheDirectory, withIntermediateDirectories: true)
                    }
 
                    try FileManager.default.moveItem(at: tempLocalUrl, to: temp)
                } catch let e {
                    print("视频缓存失败:catch:\(e)")
                }
            } else {
                print("视频缓存失败:\(error?.localizedDescription ?? "")")
            }
        }
        downloadTask.resume()
    }
}
extension CourseVCTeacherSpecialVC:UITableViewDataSource{
                func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
                            return 2
                }
 
                func numberOfSections(in tableView: UITableView) -> Int {
                                return 1
                }
 
                func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
                                if indexPath.row == 0 || indexPath.row == 1{
                                                return UITableView.automaticDimension
                                }
                                return 770.736
                }
 
                func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
 
                                if indexPath.row == 0{
                                                return cell0!
                                }
 
                                if indexPath.row == 1{
                                                return cell1!
                                }
 
                                var cell = tableView.dequeueReusableCell(withIdentifier: "cell")
                                if cell == nil{
                                                cell = UITableViewCell(style: .value1, reuseIdentifier: "cell")
                                }
                                return cell!
                }
 
                func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
                                return 73.5
                }
}
 
extension CourseVCTeacherSpecialVC:UITableViewDelegate{
 
 
}
 
extension CourseVCTeacherSpecialVC:CLPlayerDelegate{
    func player(_ player: CLPlayer, playProgressChanged value: CGFloat) {
        AudioPlayer.getSharedInstance().pauseBGM()
        AudioPlayer.getSharedInstance().pauseScene()
    }
}