TRTCLoginViewController.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //
  2. // TRTCLoginViewController.swift
  3. // TXLiteAVDemo
  4. //
  5. // Created by gg on 2021/4/7.
  6. // Copyright © 2021 Tencent. All rights reserved.
  7. //
  8. import Foundation
  9. import WebKit
  10. import TUICore
  11. import TUIRoomKit
  12. class TRTCLoginViewController: UIViewController {
  13. let loading = UIActivityIndicatorView()
  14. override var shouldAutorotate: Bool {
  15. return false
  16. }
  17. override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
  18. return .portrait
  19. }
  20. override func viewDidLayoutSubviews() {
  21. super.viewDidLayoutSubviews()
  22. view.bringSubviewToFront(loading)
  23. }
  24. override func viewDidLoad() {
  25. super.viewDidLoad()
  26. navigationController?.navigationBar.barTintColor = .white
  27. TUICSToastManager.setDefaultPosition(TUICSToastPositionBottom)
  28. view.addSubview(loading)
  29. loading.snp.makeConstraints { (make) in
  30. make.width.height.equalTo(40)
  31. make.centerX.centerY.equalTo(view)
  32. }
  33. /// auto login
  34. if ProfileManager.shared.autoLogin(success: {
  35. self.loginIM { [weak self] (success) in
  36. guard let `self` = self else { return }
  37. self.loading.stopAnimating()
  38. if success {
  39. self.loginSucc()
  40. }
  41. }
  42. }, failed: { [weak self] (err) in
  43. guard let self = self else {return}
  44. self.loading.stopAnimating()
  45. self.view.makeToast(err)
  46. }) {
  47. loading.startAnimating()
  48. if let rootView = view as? TRTCLoginRootView {
  49. rootView.phoneNumTextField.text = ProfileManager.shared.curUserModel?.phone ?? ""
  50. }
  51. }
  52. }
  53. func login(phone: String, nickName: String) {
  54. loading.startAnimating()
  55. ProfileManager.shared.login(phone: phone, name: nickName) { [weak self] in
  56. guard let `self` = self else { return }
  57. self.loading.stopAnimating()
  58. self.loginIM { [weak self] (success) in
  59. guard let `self` = self else { return }
  60. if success {
  61. self.loginSucc()
  62. }
  63. }
  64. }
  65. }
  66. #if DEBUG
  67. let SdkBusiId: Int32 = 18_069
  68. #else
  69. let SdkBusiId: Int32 = 18_070
  70. #endif
  71. func loginIM(complete: @escaping (_ success: Bool)->Void) {
  72. guard let userID = ProfileManager.shared.curUserID() else { return }
  73. let userSig = ProfileManager.shared.curUserSig()
  74. if TUILogin.getUserID() != userID {
  75. ProfileManager.shared.curUserModel?.name = ""
  76. }
  77. ProfileManager.shared.IMLogin(userSig: userSig) {
  78. debugPrint("IM login success.")
  79. complete(true)
  80. } failed: { [weak self] (error) in
  81. guard let `self` = self else { return }
  82. self.view.makeToast(LoginLocalize(key: "Failed to log in to IM"))
  83. complete(false)
  84. }
  85. }
  86. func loginSucc() {
  87. if ProfileManager.shared.curUserModel?.name.count == 0 {
  88. showRegisterVC()
  89. } else {
  90. self.view.makeToast(LoginLocalize(key:"Logged in"))
  91. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) { [weak self] in
  92. guard let self = self else { return }
  93. AppUtils.shared.showConferenceOptionsViewController(nav: self.navigationController)
  94. }
  95. }
  96. }
  97. func showRegisterVC() {
  98. let vc = TRTCRegisterViewController()
  99. navigationController?.pushViewController(vc, animated: true)
  100. }
  101. override func loadView() {
  102. super.loadView()
  103. let rootView = TRTCLoginRootView()
  104. rootView.rootVC = self
  105. view = rootView
  106. }
  107. }
  108. extension String {
  109. static let verifySuccessStr = "verifySuccess"
  110. static let verifyCancelStr = "verifyCancel"
  111. static let verifyErrorStr = "verifyError"
  112. }