CNLiveMineSignOutViewController.swift 10.4 KB
//
//  CNLiveMineSignOutViewController.swift
//  metaCode
//
//  Created by open on 2023/11/10.
//

import Foundation
import WebKit
import APButton

class CNLiveMineSignOutViewController: CNLiveBaseViewController, WKNavigationDelegate, WKUIDelegate, UITextViewDelegate {
    
    lazy var webView: WKWebView = {
        let config = WKWebViewConfiguration.init()
        config.preferences = WKPreferences()
        config.preferences.javaScriptEnabled = true
        config.preferences.javaScriptCanOpenWindowsAutomatically = false
        config.userContentController = WKUserContentController.init()
        
        let userController = WKUserContentController.init()
        let js = "var script = 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('div')[0].appendChild(script);"
        let script = WKUserScript.init(source: js, injectionTime: .atDocumentEnd, forMainFrameOnly: false)
        
        let javascript = NSMutableString.init()
        javascript.append("document.documentElement.style.webkitTouchCallout='none';")
        javascript.append("document.documentElement.style.webkitUserSelect='none';")
        let noneSelectScript = WKUserScript.init(source: javascript as String, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
        userController.addUserScript(script)
        userController.addUserScript(noneSelectScript)
        config.userContentController = userController
        
        let webView = WKWebView.init(frame: CGRect.zero, configuration: config)
        webView.navigationDelegate = self
        webView.uiDelegate = self
        webView.scrollView.bounces = false
        webView.scrollView.showsVerticalScrollIndicator = false
        webView.scrollView.showsHorizontalScrollIndicator = false
        webView.load(URLRequest.init(url: URL(string: "https://wjjh5.cnlive.com/yuanmaLogoutApplication.html")!))
        return webView
    }()
    
    lazy var selectButton: HotButton = {
        let selectButton = HotButton(type: .custom)
        selectButton.backgroundColor = .clear
        selectButton.setImage(UIImage(named: "Login_body_choose_btn_nomal"), for: .normal)
        selectButton.setImage(UIImage(named: "Login_body_choose_btn_nomal"), for: .highlighted)
        selectButton.setImage(UIImage(named: "Login_body_choose_btn_select"), for: .selected)
        selectButton.contentMode = .scaleAspectFill
        selectButton.addTarget(self, action: #selector(changeSelectBtnMothod), for: .touchUpInside)
        return selectButton
    }()
    
    lazy var textView: UITextView = {
        let attributedString = NSMutableAttributedString(string: "我已阅读并同意《注销协议》",
                                                         attributes: [.foregroundColor: HexColor("#999999") as Any, .font: regular_adapt_font(pointSize: 12)])
        let p_Range = NSRange(attributedString.string.range(of: "《注销协议》")!, in: attributedString.string)
        attributedString.addAttribute(.link, value: "delegate://", range: p_Range)
        let textView: UITextView = UITextView.init()
        textView.backgroundColor = .clear
        textView.isEditable = false
        textView.isScrollEnabled = false
        textView.delegate = self
        textView.attributedText = attributedString
        return textView
    }()
    
    lazy var doneButton: APButton = {
        let doneButton = APButton()
        doneButton.setTitle("确认注销", for: .normal)
        doneButton.setTitleColor(.white, for: .normal)
        doneButton.titleLabel?.font = BoldFont_16_Fit
        doneButton.layer.cornerRadius = adapt_length(length: 20)
        doneButton.activityIndicator.color = .white
        doneButton.layer.masksToBounds = true
        doneButton.addTarget(self, action: #selector(sendDoneActionMothod), for: .touchUpInside)
        return doneButton
    }()
    
    var is_agree: Bool!
    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarHidden = false
        view.backgroundColor = .white
        titleName = "审核申请"
        
        view.addSubview(webView)
        webView.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(KNavigationHeight)
            make.right.left.equalToSuperview()
            make.bottom.equalToSuperview().offset(-adapt_length(length: 110) + get_system_tabbar_ui_offsetHeight())
        }
        
        view.addSubview(doneButton)
        doneButton.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.bottom.equalToSuperview().offset(-(get_system_tabbar_ui_offsetHeight()+adapt_length(length: 10)))
            make.left.equalToSuperview().offset(adapt_length(length: 20))
            make.right.equalToSuperview().offset(-adapt_length(length: 20))
            make.height.equalTo(adapt_length(length: 40))
        }
        
        view.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(adapt_length(length: 40))
            make.bottom.equalTo(doneButton.snp.top).offset(-adapt_length(length: 10))
            make.right.equalToSuperview().offset(-adapt_length(length: 20))
        }
        
        view.addSubview(selectButton)
        selectButton.snp.makeConstraints { make in
            make.centerY.equalTo(textView)
            make.left.equalToSuperview().offset(adapt_length(length: 20))
            make.size.equalTo(adapt_length(length: 25))
        }
        
        doneButton.setGradient(colors: [UIColor(red: 103/255.0, green: 197/255.0, blue: 255/255.0, alpha: 0.3),UIColor(red: 96/255.0, green: 154/255.0, blue: 255/255.0, alpha: 0.3)], startPoint: CGPoint(x: 0, y: 0.5), endPoint: CGPoint(x: 1, y: 0.5))
    }
    
    @objc func changeSelectBtnMothod(sender : UIButton) {
        sender.isSelected = !sender.isSelected
        is_agree = sender.isSelected
        if is_agree {
            doneButton.setGradient(colors: [UIColor(red: 103/255.0, green: 197/255.0, blue: 255/255.0, alpha: 1),UIColor(red: 96/255.0, green: 154/255.0, blue: 255/255.0, alpha: 1)], startPoint: CGPoint(x: 0, y: 0.5), endPoint: CGPoint(x: 1, y: 0.5))
        } else {
            doneButton.setGradient(colors: [UIColor(red: 103/255.0, green: 197/255.0, blue: 255/255.0, alpha: 0.3),UIColor(red: 96/255.0, green: 154/255.0, blue: 255/255.0, alpha: 0.3)], startPoint: CGPoint(x: 0, y: 0.5), endPoint: CGPoint(x: 1, y: 0.5))
        }
    }
    
    private func setupWithSendBtnStatue(statue: Bool) {
        var alpha = 0.3
        if statue {
            alpha = 1
        }
        doneButton.setGradient(colors: [UIColor(red: 103/255.0, green: 197/255.0, blue: 255/255.0, alpha: alpha),UIColor(red: 96/255.0, green: 154/255.0, blue: 255/255.0, alpha: alpha)], startPoint: CGPoint(x: 0, y: 0.5), endPoint: CGPoint(x: 1, y: 0.5))
    }
    
    @objc func sendDoneActionMothod(sender: APButton) {
        if !is_agree {
            showMessage(message: "请先阅读并同意协议")
            return
        }
        AlertTool.showSystemAlert(title: "注销将会清除您在元码上的个人信息和购买的所有权益,且不可恢复,确定注销吗?", message: "", actionTitles: ["确定","取消"], style: .alert) { index in
            if index == 1 {
                sender.startAnimating()
                CNLiveMineViewModel.requestSignOut { [weak self] result, respStatue in
                    sender.stopAnimating()
                    if respStatue == .success {
                        let message = result as! String
                        if message == "455" {
                            showMessage(message: "账号已经在注销中,请等待")
                            self!.navigationController?.popViewController(animated: true)
                        } else {
                            UserDefaults.standard.set(false,forKey: "is_login")
                            UserInfoManager.clearUserInfo()
                            let window = UIApplication.shared.delegate?.window
                            window??.rootViewController = CNLiveLoginViewController.init()
                        }
                    }
                }
            }
        }
    }
    
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        hiddenLoading()
    }
    
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        loadHTMLString()
        hiddenLoading()
    }
    
    func loadHTMLString() {
        let htmlContent = "<div style=\"padding:0 5px;\"><p style=\"font-size:1.0625em;\"><strong>我们对你注销元码账号的决定深表遗憾。如果你仍执意注销账号,你的账号需同时满足以下条件:</strong></p><hr style=\"background-color:#EEEEEE;height:1px;border:none;\"/><p style=\"font-size:1em;\">1.账号处于安全状态: 最近1个月内无绑定手机/修改手机号、无找回密码/修改密码等操作,账号为正常使用中且在1年内无任何账号被限制的记录。</p><hr style=\"background-color:#EEEEEE;height:1px;border:none;\"/><p style=\"font-size:1em;\">2.账号财产已结清,没有未完成和/或存在争议的服务: 账号中没有资产、欠款、未结清的资金和虚拟权益;本账号及通过本账号接入的第三方中没有未完成和/或存在争议的服务。</p><hr style=\"background-color:#EEEEEE;height:1px;border:none;\"/><p style=\"font-size:1em;\">3.账号权限解除: 账号已解除与其他产品、网站授权登录或绑定关系。</p><hr style=\"background-color:#EEEEEE;height:1px;border:none;\"/><p style=\"font-size:1em;\">4.账号无任何纠纷,包括投诉举报或被投诉举报。</p></div>"
        webView.loadHTMLString(htmlContent, baseURL: URL(string: ""))
    }
}
extension CNLiveMineSignOutViewController {
    func textView(_ textView: UITextView, shouldInteractWith textAttachment: NSTextAttachment, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        return false
    }
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        if URL.absoluteString == "delegate://" {
            self.navigationController?.pushViewController(CNLiveInfoShowViewController.init(show_type: .cancel, is_login: true), animated: true)
            return false
        }
        return true
    }
}