CNLiveMineChangePhoneController.swift 12.5 KB
//
//  CNLiveMineChangePhoneController.swift
//  metaCode
//
//  Created by zx on 2024/4/28.
//  更换手机号

import Foundation
import APButton
import SnapKit

class CNLiveMineChangePhoneController:CNLiveBaseViewController{

    lazy var countryView :UIView = {
        let countryView = UIView()
        countryView.backgroundColor = .white
        countryView.layer.cornerRadius = 9
        countryView.clipsToBounds = true
        return countryView
    }()

    lazy var countryLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.textColor = HexColor("#333333")
        titleLabel.text = "中国"
        titleLabel.font = Font_15
        return titleLabel
    }()

    lazy var moreImgView :UIImageView = {
        let moreImgView = UIImageView()
        moreImgView.image = UIImage(named: "mine_set_band_detail")
        return moreImgView
    }()
    
    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 phoneCodeView :UIView = {
        let countryView = UIView()
        countryView.backgroundColor = .white
        countryView.layer.cornerRadius = 9
        countryView.clipsToBounds = true
        return countryView
    }()
    
    lazy var phoneLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.textColor = HexColor("#333333")
        titleLabel.text = "手机号"
        titleLabel.font = Font_15
        return titleLabel
    }()
    
    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 phoneLineView :UIView = {
        let countryView = UIView()
        countryView.backgroundColor = HexColor("#F3F3F3")
        return countryView
    }()
    
    lazy var codeLabel: UILabel = {
        let titleLabel = UILabel()
        titleLabel.textColor = HexColor("#333333")
        titleLabel.text = "验证码"
        titleLabel.font = Font_15
        return titleLabel
    }()
    
    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: APButton = {
        let loginButton = APButton()
        loginButton.setTitle("更换手机号", for: .normal)
        loginButton.setTitleColor(.white, for: .normal)
        loginButton.titleLabel?.font = BoldFont_16_Fit
        loginButton.layer.cornerRadius = 20
        loginButton.activityIndicator.color = .white
        loginButton.layer.masksToBounds = true
        loginButton.addTarget(self, action: #selector(sendLoginActionMothod), for: .touchUpInside)
        return loginButton
    }()
    
    var countryCode: String = "86"
    var countryStr: String = "中国"
    var timeOut = 30//多少秒计时器
        
    override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
        super.init(nibName: nil, bundle: nil)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setNeedsStatusBarAppearanceUpdate()
        view.backgroundColor = HexColor(kCNDownSlideBackgroundColorHex)
        navigationBarHidden = false
        titleName = "更换手机号"
        
        self.setupCountryView()
        self.setupPhoneView()
        self.setupVeriCodeView()
        self.setupLoginButtonView()
    }
    
    deinit {
        print("已经销毁了")
    }
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
      return .lightContent
    }
    
    func setupCountryView() {
        self.view.addSubview(self.countryView)
        self.countryView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(KNavigationHeight+15)
            make.height.equalTo(61)
        }
        
        self.countryView.addSubview(self.countryLabel)
        self.countryLabel.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(15)
        }
        
        self.countryView.addSubview(self.moreImgView)
        self.moreImgView.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-15)
            make.width.height.equalTo(13)
        }
        
        self.countryView.addSubview(self.areaButton)
        self.areaButton.snp.makeConstraints { make in
            make.centerY.equalToSuperview()
            make.right.equalTo(self.moreImgView.snp.left)
            make.width.lessThanOrEqualTo(50)
        }

    }
    func setupPhoneView() {
        
        self.view.addSubview(self.phoneCodeView)
        self.phoneCodeView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.top.equalTo(self.countryView.snp.bottom).offset(15)
            make.height.equalTo(118)
        }
        
        self.phoneCodeView.addSubview(self.phoneLabel)
        self.phoneLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.top.equalToSuperview().offset(20)
            make.width.equalTo(50)
            make.height.equalTo(21)
        }
        
        self.phoneCodeView.addSubview(self.phoneText)
        self.phoneText.snp.makeConstraints { make in
            make.centerY.equalTo(phoneLabel.snp.centerY)
            make.left.equalTo(phoneLabel.snp.right).offset(19)
            make.height.equalTo(21)
            make.right.equalToSuperview().offset(-15)
        }
        
        self.phoneCodeView.addSubview(self.phoneLineView)
        self.phoneLineView.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.right.equalToSuperview().offset(-15)
            make.centerY.equalToSuperview()
            make.height.equalTo(0.5)
        }
    }
    
    func setupVeriCodeView() {
        self.phoneCodeView.addSubview(self.codeLabel)
        self.codeLabel.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(15)
            make.top.equalTo(self.phoneLineView.snp.bottom).offset(15)
            make.width.equalTo(50)
            make.height.equalTo(21)
        }
        
        let codeButton = UIButton(type: .custom)
        codeButton.backgroundColor = .white
        codeButton.setTitle("获取验证码", for: .normal)
        codeButton.setTitle("获取验证码", for: .highlighted)
        codeButton.titleLabel?.font = Font_13
        codeButton.setTitleColor(HexColor("#FEFFFF"), for: .normal)
        codeButton.setTitleColor(HexColor("#FEFFFF"), for: .highlighted)
        codeButton.layer.cornerRadius = 15.5
        codeButton.clipsToBounds = true
        // fillCode
        let bgLayer1 = CAGradientLayer()
        bgLayer1.colors = [UIColor(red: 1, green: 0.84, blue: 0.38, alpha: 1).cgColor, UIColor(red: 1, green: 0.55, blue: 0.21, alpha: 1).cgColor]
        bgLayer1.locations = [0, 1]
        bgLayer1.frame = CGRect(x: 0, y: 0, width: 100, height: 31)
        bgLayer1.startPoint = CGPoint(x: 0, y: 0.5)
        bgLayer1.endPoint = CGPoint(x: 0.5, y: 0.5)
        codeButton.layer.insertSublayer(bgLayer1, at: 0)
        codeButton.addTarget(self, action: #selector(sendVerifiCodeMothod), for: .touchUpInside)
        self.phoneCodeView.addSubview(codeButton)
        codeButton.snp.makeConstraints { make in
            make.centerY.equalTo(self.codeLabel.snp.centerY)
            make.right.equalToSuperview().offset(-15)
            make.width.lessThanOrEqualTo(100)
            make.height.equalTo(31)
        }
        
        self.phoneCodeView.addSubview(veriText)
        veriText.snp.makeConstraints { make in
            make.centerY.equalTo(self.codeLabel.snp.centerY)
            make.right.equalTo(codeButton.snp.left).offset(-20)
            make.height.equalTo(codeButton.snp.height)
            make.left.equalTo(self.codeLabel.snp.right).offset(19)
        }
    }
    func setupLoginButtonView() {
        
        view.addSubview(loginButton)
        loginButton.snp.makeConstraints { make in
            make.centerX.equalTo(view)
            make.top.equalTo(veriText.snp.bottom).offset(adapt_length(length: 105))
            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: 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))

    }
    
    @objc func changeCityNumberMothod(sender : UIButton) {
        CNLiveLoginPickerView.showSelectView { [weak self] codeStr,countryStr in
            self!.countryCode = codeStr
            self!.countryStr = countryStr
            self!.areaButton.setTitle("+\(codeStr)", for: .normal)
            self!.areaButton.setTitle("+\(codeStr)", for: .highlighted)
            self?.countryLabel.text = countryStr
        }
    }
    
    @objc func sendVerifiCodeMothod(sender : UIButton) {
        view.endEditing(true)
        CNLiveLoginViewModel.requestChangePhoneVerCodeAction(phone: phoneText.text ?? "", code: countryCode) { [weak self] in
            self!.setupCountdownAction(sender: sender)
        }
    }
    
    @objc func sendLoginActionMothod(sender : UIButton) {
        view.endEditing(true)
        if phoneText.text ?? "" == ""{
            showMessage(message: "请输入手机号")
            return
        }
        if veriText.text ?? "" == ""{
            showMessage(message: "请输入验证码")
            return
        }
        if veriText.text?.count != 6{
            showMessage(message: "请输入正确的验证码")
            return
        }
        showLoading(message: "")
        // 绑定手机号请求登录
        CNLiveMineViewModel.sendChangePhone(mobile: phoneText.text ?? "", countryCode: self.countryCode, verificationCode: veriText.text ?? "") { result, status in
            if status == .success{
                
                let userInfo = UserInfoManager.shared.userInfo
                userInfo.mobile = self.phoneText.text ?? ""
                userInfo.countryName = self.countryStr
                userInfo.countryCode = self.countryCode
                UserInfoManager.cacheUserInfo(userInfo: userInfo)
                showMessage(message: "更改成功")
                self.navigationController?.popViewController(animated: true)
            }else{
                
            }
        }
        

    }
    
    /// 设置发送验证码按钮
    /// - 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()
    }

}