CNLiveLoginViewController.swift 30.7 KB
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 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
//
//  CNLiveLoginViewController.swift
//  metaCode
//
//  Created by open on 2023/6/5.
//

import Foundation
import UIKit
import SnapKit
import APButton
import CNLiveShareToolKit
import AuthenticationServices
import CNLivePayCostModule

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: 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
    }()
    
    lazy var wxButton: APButton = {
        let wxButton = APButton()
        wxButton.setTitle("微信登录", for: .normal)
        wxButton.setTitleColor(.white, for: .normal)
        wxButton.titleLabel?.font = BoldFont_16_Fit
        wxButton.layer.cornerRadius = 20
        wxButton.activityIndicator.color = .white
        wxButton.layer.masksToBounds = true
        wxButton.addTarget(self, action: #selector(sendWXLoginActionMothod), for: .touchUpInside)
        return wxButton
    }()

    lazy var tipLabel: UILabel = {
        let tipLabel = UILabel.init()
        tipLabel.text = "未注册的手机号验证后自动为您创建元码账户"
        tipLabel.textColor = HexColor("#464646")
        tipLabel.textAlignment = .left
        tipLabel.font = Font_11_Fit
        return tipLabel
    }()

    lazy var navBackButton: UIButton = {
        let wxButton = UIButton()
        wxButton.setImage(UIImage(named: "mine_header_icon_white_back"), for: .normal)
        wxButton.addTarget(self, action: #selector(ClickBackBtn), for: .touchUpInside)
        return wxButton
    }()
    
    var is_agree : Bool = false //是否同意协议
    var countryCode: String = "86"
//    var countryStr: String = "中国大陆"
    var timeOut = 30//多少秒计时器
    
    
    override func viewDidAppear(_ animated: Bool) {
    }
    
    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(KSreenWidth*250/375)
        }
        
        self.view.addSubview(self.navBackButton)
        self.navBackButton.snp.makeConstraints { make in
            make.left.equalTo(0)
            make.top.equalTo(get_system_nav_height()-10)
            make.width.equalTo(50)
            make.height.equalTo(30)
        }
        let count = self.navigationController?.viewControllers.count ?? 1
        if count > 1{
            self.navBackButton.isHidden = false
        }else{
            self.navBackButton.isHidden = true
        }
        
        self.setupPhoneView()
        self.setupVeriCodeView()
        self.setupLoginButtonView()
        
//        LoginAuthInfoView.showAuthView()
    }
    
    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)
        }
        
        self.view.addSubview(tipLabel)
        tipLabel.snp.makeConstraints { make in
            make.centerX.equalToSuperview()
            make.width.equalTo(KSreenWidth-80)
            make.top.equalTo(verView.snp.bottom).offset(10)
        }
    }
    
    @objc func sendWXLoginActionMothod() {
        if !is_agree {
            showMessage(message: "请先阅读并同意协议")
            return
        }
        self.threeLogin(resp: CNLiveSendAuthRespWX)
    }
    
    @objc func appleButtonAction(sender : UIButton) {
        if !is_agree {
            showMessage(message: "请先阅读并同意协议")
            return
        }
        self.threeLogin(resp: CNLiveSendAuthRespApple)
    }
    
    //三方登录
    func threeLogin(resp:CNLiveSendAuthResp){
        CNLiveShareImageManager.send(resp) { [weak self] resp, userInfo, error in
            if (error != nil){
                if let err = error as? NSError{
//                    if err.code == 1001{//苹果登录错误
//                        showMessage(message: "取消登录")
//                        return
//                    }
                    if err.code == 10000{//用户取消
                        showMessage(message: "取消登录")
                    }else{
                        showMessage(message: "登录失败")
                    }
                    return
                }else{
                    showMessage(message: "登录失败")
                }
                return
            }
            if let userModel = userInfo{
                let threeModel = CNThreeLoginModel()
                threeModel.thirdPartyId = userModel.thirdPartyId
                threeModel.nickName = userModel.nickName ?? ""
                threeModel.gender = userModel.gender ?? ""
                threeModel.location = userModel.location ?? ""
                threeModel.faceUrl = userModel.faceUrl ?? ""
                threeModel.access_token = userModel.access_token ?? ""
                threeModel.refresh_token = userModel.refresh_token ?? ""
                threeModel.type = CNLiveSendAuthRespWX
//                threeModel.countryName = self!.countryStr
                threeModel.countryCode = self?.countryCode ?? "86"
                threeModel.frmId = "Apple"//[CNTools getFrmId]
                switch resp {
                case CNLiveSendAuthRespApple:
                    threeModel.thirdPartyPlat = "5"
                    break
                case CNLiveSendAuthRespWX:
                    threeModel.thirdPartyPlat = "3"
                    break
                default:
                    threeModel.thirdPartyPlat = "3"
                    break
                }
                if resp == CNLiveSendAuthRespApple{
                    self?.appleLogin(model: threeModel)
                }else{
                    self?.thirdPartyLoginWithType(type: resp, model: threeModel)
                }
            }else{
                
            }
            
            
        }
    }
    
    func appleLogin(model:CNThreeLoginModel){
        CNLiveLoginViewModel.appleLogin(model: model) { result, status in
            if status == .success{
                //获取用户信息 /api/identityApi/getUserMsg
                CNLiveMineViewModel.requestGetUserMsg(sid: UserInfoManager.shared.userInfo.uid) { [weak self] result, respStatue in
                    guard let self = self else { return }
                    if respStatue == .success{
                        if let res = result as? MineGetUserMsgUsersModel{
                            let userInfo = UserInfoManager.shared.userInfo
                            userInfo.userMsgUsers = res
                            userInfo.currentUser = res.verifyUsers.first
                            UserInfoManager.cacheUserInfo(userInfo: userInfo)
                            
                            if var userDic = UserDefaults.standard.dictionary(forKey: UserInfoManager.shared.userInfo.uid){
                                
                            }else{
                                let tempDic = NSMutableDictionary()
                                UserDefaults.standard.setValue(tempDic, forKey: UserInfoManager.shared.userInfo.uid)
                                UserDefaults.standard.synchronize()
                            }
                            
                            NotificationCenter.default.post(name: Notification.Name.LoginManage.CNLiveLoginSuccessNotification, object: nil)
                            metaAppDelegate.isLogin = true
                            if self.navBackButton.isHidden{
                                //退出登录后->登录
                                let window = UIApplication.shared.delegate?.window
                                window??.rootViewController = CNLiveBaseTabBarViewController.init()
                            }else{
                                //未登录push过来的
                                self.navigationController?.popViewController(animated: true)
                            }
                        }
                    }else{
                        let window = UIApplication.shared.delegate?.window
                        window??.rootViewController = CNLiveBaseTabBarViewController.init()
                    }
                }
            }else{
                
            }
        }
    }
    
    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.top.equalTo(tipLabel.snp.bottom).offset(adapt_length(length: 102))
            make.left.equalTo(40)
            make.right.equalTo(-40)
            make.height.equalTo(40)
        }
        if WXApi.isWXAppInstalled(){
            view.addSubview(wxButton)
        }else{
        }
        
        if #available(iOS 13.0, *) {
            let appleButton = ASAuthorizationAppleIDButton(type: ASAuthorizationAppleIDButton.ButtonType.default, style: ASAuthorizationAppleIDButton.Style.white)
            appleButton.addTarget(self, action: #selector(appleButtonAction), for: .touchUpInside)
            appleButton.layer.masksToBounds = true
            appleButton.layer.cornerRadius = 20
            appleButton.layer.borderWidth = 1.0
            appleButton.layer.borderColor = UIColor.black.cgColor
            view.addSubview(appleButton)
            appleButton.snp.makeConstraints { make in
                make.left.equalTo(40)
                make.right.equalTo(loginButton.snp.centerX).offset(-5)
                make.top.equalTo(loginButton.snp.bottom).offset(15)
                make.height.equalTo(loginButton.snp.height)
            }
            if WXApi.isWXAppInstalled(){
                wxButton.snp.makeConstraints { make in
                    make.right.equalTo(loginButton.snp.right)
                    make.left.equalTo(loginButton.snp.centerX).offset(5)
                    make.top.equalTo(loginButton.snp.bottom).offset(15)
                    make.height.equalTo(loginButton.snp.height)
                }
            }
        } else {
            // Fallback on earlier versions
            if WXApi.isWXAppInstalled(){
                wxButton.snp.makeConstraints { make in
                    make.centerX.equalTo(view)
                    make.top.equalTo(loginButton.snp.bottom).offset(15)
                    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))
        if WXApi.isWXAppInstalled(){
            wxButton.setGradient(colors: [UIColor(red: 35/255.0, green: 203/255.0, blue: 105/255.0, alpha: 1),UIColor(red: 13/255.0, green: 179/255.0, blue: 85/255.0, alpha: 1)], startPoint: CGPoint(x: 0, y: 0.5), endPoint: CGPoint(x: 1, y: 0.5))
        }
//        wxButton.setGradient(colors: [UIColor(red: 35/255.0, green: 203/255.0, blue: 105/255.0, alpha: 0.3),UIColor(red: 13/255.0, green: 179/255.0, blue: 85/255.0, alpha: 0.3)], startPoint: CGPoint(x: 0, y: 0.5), endPoint: CGPoint(x: 1, y: 0.5))
    }
    
    // MARK: - 三方登录
    func thirdPartyLoginWithType(type:CNLiveSendAuthResp,model:CNThreeLoginModel){
        
        showLoading(message: "")
        CNLiveLoginViewModel.thirdPartyLoginWithType(type: type, model: model) {[weak self] result, status in
            hiddenLoading()
            if status == .success{
                //登录成功
                //获取用户信息 /api/identityApi/getUserMsg
                CNLiveMineViewModel.requestGetUserMsg(sid: UserInfoManager.shared.userInfo.uid) {[weak self] result, respStatue in
                    guard let self = self else { return }
                    if respStatue == .success{
                        
                        if let res = result as? MineGetUserMsgUsersModel{
                            let userInfo = UserInfoManager.shared.userInfo
                            userInfo.userMsgUsers = res
                            userInfo.currentUser = res.verifyUsers.first
                            UserInfoManager.cacheUserInfo(userInfo: userInfo)
                            
                            if var userDic = UserDefaults.standard.dictionary(forKey: UserInfoManager.shared.userInfo.uid){
                                
                            }else{
                                let tempDic = NSMutableDictionary()
                                UserDefaults.standard.setValue(tempDic, forKey: UserInfoManager.shared.userInfo.uid)
                                UserDefaults.standard.synchronize()
                            }
                            
                            NotificationCenter.default.post(name: Notification.Name.LoginManage.CNLiveLoginSuccessNotification, object: nil)
                            metaAppDelegate.isLogin = true
                            if self.navBackButton.isHidden{
                                let window = UIApplication.shared.delegate?.window
                                window??.rootViewController = CNLiveBaseTabBarViewController.init()
                            }else{
                                //发通知
                                self.navigationController?.popViewController(animated: true)
                            }
                        }
                    }else{
                        let window = UIApplication.shared.delegate?.window
                        window??.rootViewController = CNLiveBaseTabBarViewController.init()

                    }
                }
            }else{
                if let res = result as? String{
                    if res == "42013"{
                        if (type == CNLiveSendAuthRespApple) {
                            //获取用户信息 /api/identityApi/getUserMsg
                            CNLiveMineViewModel.requestGetUserMsg(sid: UserInfoManager.shared.userInfo.uid) { [weak self] result, respStatue in
                                guard let self = self else { return }
                                if respStatue == .success{
                                    if let res = result as? MineGetUserMsgUsersModel{
                                        let userInfo = UserInfoManager.shared.userInfo
                                        userInfo.userMsgUsers = res
                                        userInfo.currentUser = res.verifyUsers.first
                                        UserInfoManager.cacheUserInfo(userInfo: userInfo)
                                        
                                        if var userDic = UserDefaults.standard.dictionary(forKey: UserInfoManager.shared.userInfo.uid){
                                            
                                        }else{
                                            let tempDic = NSMutableDictionary()
                                            UserDefaults.standard.setValue(tempDic, forKey: UserInfoManager.shared.userInfo.uid)
                                            UserDefaults.standard.synchronize()
                                        }
                                        NotificationCenter.default.post(name: Notification.Name.LoginManage.CNLiveLoginSuccessNotification, object: nil)
                                        metaAppDelegate.isLogin = true
                                        if self.navBackButton.isHidden{
                                            let window = UIApplication.shared.delegate?.window
                                            window??.rootViewController = CNLiveBaseTabBarViewController.init()
                                        }else{
                                            //发通知
                                            self.navigationController?.popViewController(animated: true)
                                        }
                                    }
                                }else{
                                    let window = UIApplication.shared.delegate?.window
                                    window??.rootViewController = CNLiveBaseTabBarViewController.init()

                                }
                            }
                            return
                        }
                        
                        //未注册,需要绑定手机号
                        //跳转绑定手机号页面
                        let vc = CNThreeLoginViewController(model: model)
                        
                        self!.navigationController?.pushViewController(vc, animated: true)
                    }
                }else{
                    
                }
            }
        }
    }
    
}

extension CNLiveLoginViewController {
    
    @objc func changeCityNumberMothod(sender : UIButton) {
        CNLiveLoginPickerView.showSelectView { [weak self] text,countryStr  in
            self!.countryCode = text
            self!.areaButton.setTitle("+\(text)", for: .normal)
            self!.areaButton.setTitle("+\(text)", for: .highlighted)
        }
    }
    
    @objc func sendVerifiCodeMothod(sender : UIButton) {
        view.endEditing(true)
        CNLiveLoginViewModel.requestVerCodeAction(phone: phoneText.text ?? "", code: countryCode) { [weak self] in
            self!.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))
            if WXApi.isWXAppInstalled(){
                wxButton.setGradient(colors: [UIColor(red: 35/255.0, green: 203/255.0, blue: 105/255.0, alpha: 1),UIColor(red: 13/255.0, green: 179/255.0, blue: 85/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))
//            wxButton.setGradient(colors: [UIColor(red: 35/255.0, green: 203/255.0, blue: 105/255.0, alpha: 0.3),UIColor(red: 13/255.0, green: 179/255.0, blue: 85/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)
        loginButton.startAnimating()
        
        CNLiveLoginViewModel.requestLoginAction(userName: phoneText.text ?? "", countryCode: countryCode, verCode: veriText.text ?? "") {[self] in
            loginButton.stopAnimating()
            //获取用户信息 /api/identityApi/getUserMsg
            CNLiveMineViewModel.requestGetUserMsg(sid: UserInfoManager.shared.userInfo.uid) { [weak self] result, respStatue in
                guard let self = self else { return }
                if respStatue == .success{
                    if let res = result as? MineGetUserMsgUsersModel{
                        let userInfo = UserInfoManager.shared.userInfo
                        userInfo.userMsgUsers = res
                        userInfo.currentUser = res.verifyUsers.first
                        UserInfoManager.cacheUserInfo(userInfo: userInfo)
                        
                        if var userDic = UserDefaults.standard.dictionary(forKey: UserInfoManager.shared.userInfo.uid){
                            
                        }else{
                            let tempDic = NSMutableDictionary()
                            UserDefaults.standard.setValue(tempDic, forKey: UserInfoManager.shared.userInfo.uid)
                            UserDefaults.standard.synchronize()
                        }
                        
                        //发通知
                        NotificationCenter.default.post(name: Notification.Name.LoginManage.CNLiveLoginSuccessNotification, object: nil)
                        metaAppDelegate.isLogin = true
                        if self.navBackButton.isHidden{
                            let window = UIApplication.shared.delegate?.window
                            window??.rootViewController = CNLiveBaseTabBarViewController.init()
                        }else{
                            self.navigationController?.popViewController(animated: true)
                        }
                    }
                }else{
                    let window = UIApplication.shared.delegate?.window
                    window??.rootViewController = CNLiveBaseTabBarViewController.init()

                }
            }
        } endrefreshBlock: {[self] in
            loginButton.stopAnimating()
        }

    }
    
    @objc func pushAgreeActionMothod() {
        self.present(CNLiveInfoShowViewController.init(show_type: .service, is_login: true), animated: true)
//        navi.pushViewController(CNLiveInfoShowViewController.init(show_type: .service), animated: true)
    }
    
    @objc func pushInfoActionMothod() {
        self.present(CNLiveInfoShowViewController.init(show_type: .privacy, is_login: true), animated: true)
//        navi.pushViewController(CNLiveInfoShowViewController.init(show_type: .privacy, is_login: true), 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
        }
    }
}