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