CNLiveLoginViewController.swift 13.6 KB
//
//  CNLiveLoginViewController.swift
//  metaCode
//
//  Created by open on 2023/6/5.
//

import Foundation
import UIKit
import SnapKit

class CNLiveLoginViewController : CNLiveBaseViewController, UITextViewDelegate {
    
    
    lazy var headerView : UIImageView = {
        let headerView = UIImageView()
        headerView.backgroundColor = .white
        headerView.contentMode = .scaleToFill
        headerView.image = UIImage(named: "Login_page_header_icon")
        return headerView
    }()
    
    lazy var areaButton : UIButton = {
        let areaButton = UIButton(type: .custom)
        areaButton.backgroundColor = .white
        areaButton.setTitle("+86", for: .normal)
        areaButton.setTitle("+86", for: .highlighted)
        areaButton.titleLabel?.font = BoldFont_16
        areaButton.setTitleColor(HexColor("#333333"), for: .normal)
        areaButton.setTitleColor(HexColor("#333333"), for: .highlighted)
        areaButton.addTarget(self, action: #selector(changeCityNumberMothod), for: .touchUpInside)
        return areaButton
    }()
    
    lazy var phoneText : UITextField = {
        let phoneText = UITextField.init()
        phoneText.backgroundColor = .white
        phoneText.placeholder = "输入手机号"
        phoneText.font = BoldFont_16_Fit
        phoneText.keyboardType = .numberPad
        phoneText.clearButtonMode = .whileEditing
        return phoneText
    }()
    
    lazy var veriText: UITextField = {
        let veriText = UITextField.init()
        veriText.backgroundColor = .white
        veriText.placeholder = "输入验证码"
        veriText.font = BoldFont_16_Fit
        veriText.keyboardType = .numberPad
        veriText.clearButtonMode = .whileEditing
        return veriText
    }()
    
    lazy var loginButton: UIButton = {
        let loginButton = UIButton(type: .custom)
        loginButton.setTitle("登  录", for: .normal)
        loginButton.setTitleColor(.white, for: .normal)
        loginButton.titleLabel?.font = BoldFont_16_Fit
        loginButton.layer.cornerRadius = 20
        loginButton.layer.masksToBounds = true
        loginButton.addTarget(self, action: #selector(sendLoginActionMothod), for: .touchUpInside)
        return loginButton
    }()
    
    var is_agree : Bool = false //是否同意协议
    var country : String = "86"
    var timeOut = 30//多少秒计时器
    
    
    override func viewDidAppear(_ animated: Bool) {
        LoginAuthInfoView.showAuthView()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNeedsStatusBarAppearanceUpdate()
        view.backgroundColor = .white
        //添加主视图
        self.view.addSubview(self.headerView)
        self.headerView.snp.makeConstraints { make in
            make.centerX.equalTo(self.view)
            make.top.left.right.equalTo(self.view)
            make.height.equalTo(250.0)
        }
        
        self.setupPhoneView()
        self.setupVeriCodeView()
        self.setupLoginButtonView()
    }
    
    deinit {
        print("已经销毁了")
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
      return .lightContent
    }
    
    func setupPhoneView() {
        
        let phoneV = UIView.init()
        phoneV.backgroundColor = .white
        view.addSubview(phoneV)
        phoneV.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.top.equalTo(self.headerView.snp.bottom).offset(10)
            make.height.equalTo(50)
        }
        
        phoneV.addSubview(areaButton)
        areaButton.snp.makeConstraints { make in
            make.centerY.equalTo(phoneV)
            make.left.equalTo(phoneV.left).offset(40)
            make.width.lessThanOrEqualTo(50)
        }
        
        let v_line = UILabel.init()
        v_line.backgroundColor = HexColor("#EBEBEB")
        phoneV.addSubview(v_line)
        v_line.snp.makeConstraints { make in
            make.centerY.equalTo(phoneV)
            make.left.equalTo(areaButton.snp.right).offset(10)
            make.width.equalTo(1)
            make.height.equalTo(areaButton.snp.height).multipliedBy(0.35)
        }
        
        let h_line = UILabel.init()
        h_line.backgroundColor = HexColor("#EBEBEB")
        phoneV.addSubview(h_line)
        h_line.snp.makeConstraints { make in
            make.left.equalTo(phoneV.left).offset(40)
            make.right.equalTo(phoneV.right).offset(-40)
            make.height.equalTo(v_line.snp.width)
            make.bottom.equalTo(phoneV.snp.bottom).offset(-1)
        }
        
        phoneV.addSubview(phoneText)
        phoneText.snp.makeConstraints { make in
            make.centerY.equalTo(phoneV)
            make.left.equalTo(areaButton.snp.right).offset(21)
            make.height.equalTo(areaButton.snp.height)
            make.right.equalTo(phoneV.right).offset(-40)
        }
    }
    
    func setupVeriCodeView() {
        
        let verView = UIView.init()
        verView.backgroundColor = .white
        self.view.addSubview(verView)
        verView.snp.makeConstraints { make in
            make.left.right.equalTo(self.view)
            make.top.equalTo(self.headerView.snp.bottom).offset(70)
            make.height.equalTo(50)
        }
        
        let h_line = UILabel.init()
        h_line.backgroundColor = HexColor("#EBEBEB")
        verView.addSubview(h_line)
        h_line.snp.makeConstraints { make in
            make.left.equalTo(verView.left).offset(40)
            make.right.equalTo(verView.right).offset(-40)
            make.height.equalTo(1)
            make.bottom.equalTo(verView.snp.bottom).offset(-1)
        }
        
        let codeButton = UIButton(type: .custom)
        codeButton.backgroundColor = .white
        codeButton.setTitle("获取验证码", for: .normal)
        codeButton.setTitle("获取验证码", for: .highlighted)
        codeButton.titleLabel?.font = Font_16
        codeButton.setTitleColor(HexColor("#0066FE"), for: .normal)
        codeButton.setTitleColor(HexColor("#0066FE"), for: .highlighted)
        codeButton.addTarget(self, action: #selector(sendVerifiCodeMothod), for: .touchUpInside)
        verView.addSubview(codeButton)
        codeButton.snp.makeConstraints { make in
            make.centerY.equalTo(verView)
            make.right.equalTo(verView.snp.right).offset(-40)
            make.width.lessThanOrEqualTo(100)
        }
        
        verView.addSubview(veriText)
        veriText.snp.makeConstraints { make in
            make.centerY.equalTo(verView)
            make.right.equalTo(codeButton.snp.left).offset(-20)
            make.height.equalTo(codeButton.snp.height)
            make.left.equalTo(verView.snp.left).offset(40)
        }
        
        let tipLabel = UILabel.init()
        tipLabel.text = "未注册的手机号验证后自动为您创建元码账户"
        tipLabel.textColor = HexColor("#464646")
        tipLabel.textAlignment = .left
        tipLabel.font = Font_11_Fit
        self.view.addSubview(tipLabel)
        tipLabel.snp.makeConstraints { make in
            make.centerX.equalTo(self.view)
            make.left.right.equalTo(self.view).offset(40)
            make.top.equalTo(verView.snp.bottom).offset(10)
        }
    }
    
    func setupLoginButtonView() {
        
        let selectButton = HotButton(type: .custom)
        selectButton.backgroundColor = .white
        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)
        view.addSubview(selectButton)
        selectButton.snp.makeConstraints { make in
            make.bottom.equalTo(view.snp.bottom).offset(-50)
            make.left.equalTo(view.snp.left).offset(25)
            make.size.equalTo(15)
        }
    
        let attributedString = NSMutableAttributedString(string: "我已阅读并同意《APP用户服务协议》《个人信息保护政策》",
                                                         attributes: [.foregroundColor: HexColor("#999999") as Any, .font: Font_10])
        let p_Range = NSRange(attributedString.string.range(of: "《APP用户服务协议》")!, in: attributedString.string)
        attributedString.addAttribute(.link, value: "delegate://", range: p_Range)
        let i_Range = NSRange(attributedString.string.range(of: "《个人信息保护政策》")!, in: attributedString.string)
        attributedString.addAttribute(.link, value: "info://", range: i_Range)
        let textView: UITextView = UITextView.init()
        textView.backgroundColor = .clear
        textView.isEditable = false
        textView.isScrollEnabled = false
        textView.delegate = self
        textView.attributedText = attributedString
        view.addSubview(textView)
        textView.snp.makeConstraints { make in
            make.centerY.equalTo(selectButton)
            make.left.equalTo(selectButton.snp.right)
            make.right.equalTo(view.snp.right).offset(-25)
        }
        

        view.addSubview(loginButton)
        loginButton.snp.makeConstraints { make in
            make.centerX.equalTo(view)
            make.bottom.equalTo(textView.snp.top).offset(-10)
            make.left.equalTo(40)
            make.right.equalTo(-40)
            make.height.equalTo(40)
        }
        loginButton.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))
    }
}

extension CNLiveLoginViewController {
    
    @objc func changeCityNumberMothod(sender : UIButton) {
        CNLiveLoginPickerView.showSelectView { [self] text in
            country = text
            areaButton.setTitle("+\(text)", for: .normal)
            areaButton.setTitle("+\(text)", for: .highlighted)
        }
    }
    
    @objc func sendVerifiCodeMothod(sender : UIButton) {
        view.endEditing(true)
        CNLiveLoginViewModel.requestVerCodeAction(phone: phoneText.text ?? "", code: country) { [self] in
            setupCountdownAction(sender: sender)
        }
    }
    
    @objc func changeSelectBtnMothod(sender : UIButton) {
        sender.isSelected = !sender.isSelected
        is_agree = sender.isSelected
        if is_agree {
            loginButton.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 {
            loginButton.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 sendLoginActionMothod(sender : UIButton) {
        if !is_agree {
            showMessage(message: "请先阅读并同意协议")
            return
        }
        view.endEditing(true)
        CNLiveLoginViewModel.requestLoginAction(userName: phoneText.text ?? "", countryCode: country, verCode: veriText.text ?? "") {
            let window = UIApplication.shared.delegate?.window
            window??.rootViewController = CNLiveBaseTabBarViewController.init()
        }
    }
    
    @objc func pushAgreeActionMothod() {
        self.navigationController?.pushViewController(CNLiveInfoShowViewController.init(show_type: .service), animated: true)
    }
    
    @objc func pushInfoActionMothod() {
        self.navigationController?.pushViewController(CNLiveInfoShowViewController.init(show_type: .privacy), animated: true)
    }
}

extension CNLiveLoginViewController {
    
    /// 设置发送验证码按钮
    /// - Parameter sender: 按钮
    private func setupCountdownAction(sender : UIButton) {
        var timeOut = self.timeOut
        let queue: DispatchQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.default)
        let _timer: DispatchSource = DispatchSource.makeTimerSource(flags: [], queue: queue) as! DispatchSource
        _timer.schedule(wallDeadline: DispatchWallTime.now(), repeating: .seconds(1))
        //一秒执行
        _timer.setEventHandler(handler: { () -> Void in
            if timeOut <= 0 {
                _timer.cancel()
                DispatchQueue.main.sync(execute: { () -> Void in
                    sender.setTitle("获取验证码", for: .normal)
                    sender.isEnabled = true
                })
            } else {
                let seconds = timeOut
                DispatchQueue.main.sync(execute: {() -> Void in
                    let str = String(describing: seconds)
                    sender.setTitle("\(str)s重新获取", for: .normal)
                    sender.isEnabled = false
                })
                timeOut -= 1
            }
        })
        _timer.resume()
    }
}

extension CNLiveLoginViewController {
    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://" {
            pushAgreeActionMothod()
            return false
        }else {
            pushInfoActionMothod()
            return false
        }
    }
}