//
|
// WebVC.swift
|
// XQMuse
|
//
|
// Created by 无故事王国 on 2024/8/13.
|
//
|
|
import UIKit
|
import WebKit
|
import JQTools
|
|
class WebVC: BaseVC {
|
|
private var webView:WKWebView?
|
private var type:AgreementType?
|
private(set) var url:String?
|
private(set) var htmlText:String?
|
private(set) var baseUrl:URL?
|
private var showHelp = false
|
private var progressView = UIProgressView()
|
private let jsCode = """
|
var meta = document.createElement('meta');"
|
"meta.name = 'viewport';"
|
"meta.content = 'width=device-width, initial-scale=1.0,minimum-scale=1.0, maximum-scale=1.0, user-scalable=yes';"
|
"document.getElementsByTagName('head')[0].appendChild(meta);
|
"""
|
|
public var tintColor = UIColor.blue
|
private var subVC:Bool = false
|
|
public convenience init(url:String,showHelp:Bool = false) {
|
self.init()
|
self.url = url
|
self.showHelp = showHelp
|
}
|
|
public convenience init(type:AgreementType,subVC:Bool = false){
|
self.init()
|
self.type = type
|
}
|
|
public convenience init(htmlText:String,baseURL:URL? = nil) {
|
self.init()
|
self.htmlText = htmlText.jq_wrapHtml()
|
self.baseUrl = baseURL
|
}
|
|
public override func viewDidLoad() {
|
super.viewDidLoad()
|
|
|
if showHelp{
|
let btn = UIButton(type: .custom)
|
btn.setTitle("我的助力", for: .normal)
|
btn.setTitleColor(UIColor(hexString: "#353535"), for: .normal)
|
btn.titleLabel?.font = .systemFont(ofSize: 15)
|
btn.addTarget(self, action: #selector(myHelpAction), for: .touchUpInside)
|
navigationItem.rightBarButtonItem = UIBarButtonItem(customView: btn)
|
}
|
|
|
let config = WKWebViewConfiguration()
|
let userScript = WKUserScript(source: jsCode, injectionTime: .atDocumentStart, forMainFrameOnly: true)
|
let content = WKUserContentController()
|
content.addUserScript(userScript)
|
config.userContentController = content
|
webView = WKWebView(frame: CGRect.zero, configuration: config)
|
webView?.addObserver(self, forKeyPath: "estimatedProgress", options: .new, context: nil)
|
view.addSubview(webView!)
|
webView?.snp.makeConstraints({ (make) in
|
make.top.equalToSuperview().offset(subVC ? UIDevice.jq_safeEdges.top:0)
|
make.left.right.bottom.equalToSuperview()
|
})
|
|
if #available(iOS 11.0, *) {
|
webView!.scrollView.contentInsetAdjustmentBehavior = .automatic
|
} else {
|
self.automaticallyAdjustsScrollViewInsets = false
|
}
|
|
webView!.scrollView.delegate = self
|
|
progressView.tintColor = tintColor
|
view.addSubview(progressView)
|
progressView.snp.makeConstraints { (make) in
|
make.top.left.right.equalToSuperview()
|
make.height.equalTo(2)
|
}
|
|
if type != nil{
|
Services.agreementBy(type!).subscribe(onNext: {data in
|
if let model = data.data{
|
self.webView?.loadHTMLString(model.content.jq_wrapHtml(edge: UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)), baseURL: nil)
|
}
|
}).disposed(by: disposeBag)
|
}
|
|
if url != nil {
|
let urlRequest = URLRequest(url: URL(string: url!)!)
|
webView?.load(urlRequest)
|
return
|
}
|
|
if htmlText != nil{
|
webView?.loadHTMLString(htmlText!, baseURL: baseUrl)
|
}
|
}
|
|
public override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
|
let value = change![NSKeyValueChangeKey(rawValue: "new")] as! Double
|
print(value)
|
|
|
progressView.setProgress(Float(value), animated: true)
|
if value == 1.0 {
|
progressView.isHidden = true
|
}else{
|
progressView.isHidden = false
|
}
|
}
|
|
@objc func myHelpAction(){
|
let vc = WebVC(url: ShareUrl + "/ranking/recommend?userId=\(UserViewModel.getAvatarInfo().id)")
|
vc.title = "我的助力"
|
push(vc: vc)
|
}
|
|
deinit {
|
webView?.removeObserver(self, forKeyPath: "estimatedProgress")
|
}
|
|
}
|
|
extension WebVC:UIScrollViewDelegate{
|
func scrollViewDidScroll(_ scrollView: UIScrollView) {
|
// let v = min(scrollView.contentOffset.y / JQ_NavBarHeight, 1)
|
// navigationController?.navigationBar.standardAppearance.backgroundColor = .white.withAlphaComponent(v)
|
navigationController?.navigationBar.standardAppearance.backgroundColor = .white
|
navigationController?.navigationBar.scrollEdgeAppearance?.backgroundColor = .white
|
}
|
}
|