杨锴
2025-04-16 09a372bc45fde16fd42257ab6f78b8deeecf720b
XQMuse/Root/Course/VC/CourseVCTeacherSpecialVC.swift
@@ -32,15 +32,13 @@
                        cell0!.backgroundColor = UIColor(hexString: "#f6f6f6")
                        cell1!.backgroundColor = UIColor(hexString: "#f6f6f6")
                        Services.tutorInfo().subscribe(onNext: { data in
                                    if let m = data.data{
                                                self.model = m
                                                self.cell0!.setContent(title: "导师简介", content: m.tutorIntroduction)
                                                self.cell1!.setItems(m.list)
                                                self.tableView?.reloadData()
                self.headerView.updateVideoUrl(m.videoUrl,autoPlay: false,placeHoderImageUrl: m.coverUrl.jq_urlEncoded(),delegate: self)
                                    }
                        }).disposed(by: disposeBag)
        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
@@ -53,6 +51,23 @@
                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)
@@ -93,6 +108,45 @@
            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 {