fix
杨锴
2025-04-16 f646fbebad928833d15546a36e37768d00fe6173
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
//
//  CommonWebVC.swift
//  WanPai
//
//  Created by 无故事王国 on 2023/9/11.
//
 
import UIKit
import WebKit
 
class CommonWebVC: BaseVC, WKNavigationDelegate {
 
                private var type:AgreentType!
                private var content:String!
                private var customTitle:String?
                @IBOutlet weak var webView: WKWebView!
                @IBOutlet weak var cons_webHeight: NSLayoutConstraint!
                @IBOutlet weak var view_btns: UIView!
                private var needBackBtn:Bool!
 
                init(type:AgreentType,customTitle:String? = nil,content:String? = nil,needBackBtn:Bool = false) {
                                super.init(nibName: nil, bundle: nil)
                                self.type = type
                                self.customTitle = customTitle
                                self.content = content
                                self.needBackBtn = needBackBtn
                }
 
                required init?(coder: NSCoder) {
                                fatalError("init(coder:) has not been implemented")
                }
 
                override func viewDidLoad() {
                                super.viewDidLoad()
                                view_btns.isHidden = true
                                webView.navigationDelegate = self
 
        yy_popBlock = {[unowned self] in
            NotificationCenter.default.post(name: Noti_showSheetView, object: nil)
            self.navigationController?.popViewController(animated: true)
        }
 
                                if type != .other{
                                                title = type.titleStr
 
                                                if customTitle != nil{
                                                                title = customTitle
                                                }else{
                                                                title = type.titleStr
                                                }
 
            if !content.isEmpty{
                webView.loadHTMLString(content!.jq_wrapHtml(), baseURL: nil)
                return
            }
 
                                                Services.queryProtocol(type,progress: false).subscribe(onNext: {[weak self] result in
                                                                if let text = result.data?.jq_wrapHtml(edge: .init(top: 5, left:0, bottom: 0, right: 0)){
                                                                                self?.webView.loadHTMLString(text, baseURL: nil)
                                                                }
                                                }).disposed(by: disposeBag)
                                }else{
                                                webView.loadHTMLString(content?.jq_wrapHtml(edge: .init(top: 5, left: 0, bottom: 0, right: 0)) ?? "", baseURL: nil)
                                }
                }
 
                override func setRx() {
                                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{
                                                                self.cons_webHeight.constant = height
                                                }
                                }).disposed(by: disposeBag)
 
                }
 
                @IBAction func readAction(_ sender: UIButton) {
                                switch type {
                                                case .user:
                                                                UserDefaults.standard.set(object: true, forKey: "agreent_2")
                                                                UserDefaults.standard.synchronize()
                                                case .safe:
                                                                UserDefaults.standard.set(object: true, forKey: "agreent_3")
                                                                UserDefaults.standard.synchronize()
                                                case .privacy:
                                                                UserDefaults.standard.set(object: true, forKey: "agreent_1")
                                                                UserDefaults.standard.synchronize()
                                                default:break
                                }
                                navigationController?.popViewController()
                }
 
                @IBAction func cancelAction(_ sender: UIButton) {
                                navigationController?.popViewController()
                }
 
 
                override var preferredStatusBarStyle: UIStatusBarStyle{
                                return .darkContent
                }
                
                override var shouldAutorotate: Bool{
                                return false
                }
 
                func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
 
                                let js = """
document.createElement('meta');script.name = 'viewport';script.content=\"width=device-width, initial-scale=1.0,maximum-scale=1.0, minimum-scale=1.0, user-scalable=no\";document.getElementsByTagName('head')[0].appendChild(script);var style = document.createElement('style');style.type='text/css';style.innerHTML='body{width:100%;height:auto;margin:auto;background-color:#ffffff}img{max-width:100%}p{word-wrap: break-word;color: #222;list-style-position: inside;list-style-type: square;margin-top: 17px;font-size: 18px;line-height: 1.76;border: none;outline: none;display: block;margin-block-start: 1em;margin-block-end: 1em;margin-inline-start: 0px;margin-inline-end: 0px;} p img {margin-bottom: -9px}';document.body.appendChild(style);
"""
                                webView.evaluateJavaScript(js)
 
                                switch type {
                                                case .user,.safe,.privacy:
                                                                view_btns.isHidden = false
                                                                if needBackBtn == false{
                                                                                navigationItem.leftBarButtonItem = UIBarButtonItem()
                                                                }else{
                                                                                view_btns.isHidden = true
                                                                }
                                                default:
                                                                view_btns.isHidden = true
                                }
                }
}