无故事王国
2024-03-14 a478b668ca1a5d4f6d2d9b1075d292cbbef90de5
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
//
//  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.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
 
                                if type != .other{
                                                title = type.titleStr
 
                                                if customTitle != nil{
                                                                title = customTitle
                                                }else{
                                                                title = type.titleStr
                                                }
 
                                                Services.queryProtocol(type,progress: false).subscribe(onNext: {[weak self] result in
                                                                if let text = result.data?.jq_wrapHtml(){
                                                                                self?.webView.loadHTMLString(text, baseURL: nil)
                                                                }
                                                }).disposed(by: disposeBag)
                                }else{
                                                webView.loadHTMLString(content?.jq_wrapHtml() ?? "", 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!) {
                                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
                                }
                }
}