杨锴
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
//
//  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
    }
}