LoginViewController.swift 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. //
  2. // LoginViewController.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 16/6/28.
  6. // Copyright © 2016年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import AlamofireObjectMapper
  10. import Alamofire
  11. import SwiftyTimer
  12. import SwiftyJSON
  13. import ObjectMapper
  14. import CocoaLumberjack
  15. import Promises
  16. import O2OA_Auth_SDK
  17. class LoginViewController: UIViewController {
  18. @IBOutlet weak var iconImageView: UIImageView!
  19. @IBOutlet weak var startImage: UIImageView!
  20. var showView = 0
  21. var viewModel:OOLoginViewModel = {
  22. return OOLoginViewModel()
  23. }()
  24. override var preferredStatusBarStyle: UIStatusBarStyle {
  25. return UIStatusBarStyle.default
  26. }
  27. override func viewDidLoad() {
  28. super.viewDidLoad()
  29. //load image
  30. if AppConfigSettings.shared.isFirstTime != true {
  31. let launchImage = OOCustomImageManager.default.loadImage(.launch_logo)
  32. iconImageView.image = launchImage
  33. iconImageView.isHidden = false
  34. }
  35. self.startImage.image = UIImage(named: "startImage")
  36. }
  37. override func viewWillAppear(_ animated: Bool) {
  38. super.viewWillAppear(animated)
  39. NotificationCenter.default.addObserver(self, selector: #selector(receiveBindCompleted(customNotification:)), name: OONotification.bindCompleted.notificationName, object: nil)
  40. }
  41. override func viewDidAppear(_ animated: Bool) {
  42. super.viewDidAppear(animated)
  43. self.showView += 1
  44. if AppConfigSettings.shared.isFirstTime == true {
  45. DDLogDebug("启动开始 isFirstTime is true")
  46. AppConfigSettings.shared.isFirstTime = false
  47. let pVC = OOGuidePageController(nibName: "OOGuidePageController", bundle: nil)
  48. //let navVC = ZLNavigationController(rootViewController: pVC)
  49. self.presentVC(pVC)
  50. }else{
  51. if self.showView == 1 {
  52. DDLogDebug("启动开始 isFirstTime is false")
  53. self.startFlowForPromise()
  54. }
  55. }
  56. }
  57. func startFlowForPromise() {
  58. if !O2IsConnect2Collect {
  59. let unit = O2BindUnitModel()
  60. if let infoPath = Bundle.main.path(forResource: "Info", ofType: "plist"), let dic = NSDictionary(contentsOfFile: infoPath) {
  61. let o2Server = dic["o2 server"] as? NSDictionary
  62. let id = o2Server?["id"] as? String
  63. let name = o2Server?["name"] as? String
  64. let centerHost = o2Server?["centerHost"] as? String
  65. let centerContext = o2Server?["centerContext"] as? String
  66. let centerPort = o2Server?["centerPort"] as? Int
  67. let httpProtocol = o2Server?["httpProtocol"] as? String
  68. DDLogDebug("连接服务器:\(String(describing: name)) , host:\(String(describing: centerHost)) , context:\(String(describing: centerContext)), port:\(centerPort ?? 0), portocal:\(String(describing: httpProtocol)) ")
  69. if name == nil || centerHost == nil || centerContext == nil {
  70. self.showError(title: "服务器配置信息异常!")
  71. return
  72. }
  73. unit.id = id
  74. unit.centerContext = centerContext
  75. unit.centerHost = centerHost
  76. unit.centerPort = centerPort
  77. unit.httpProtocol = httpProtocol
  78. unit.name = name
  79. }else {
  80. self.showError(title: "没有配置服务器信息!")
  81. return
  82. }
  83. O2AuthSDK.shared.launchInner(unit: unit) { (state, msg) in
  84. switch state {
  85. case .bindError:
  86. //校验绑定结点信息错误
  87. self.showError(title: "未知错误!")
  88. break
  89. case .loginError:
  90. self.forwardToSegue("loginSystemSegue")
  91. //自动登录出错
  92. break
  93. case .unknownError:
  94. self.showError(title: msg ?? "未知错误!")
  95. break
  96. case .success:
  97. //处理移动端应用
  98. self.viewModel._saveAppConfigToDb()
  99. //跳转到主页
  100. let destVC = O2MainController.genernateVC()
  101. destVC.selectedIndex = 2 // 首页选中 TODO 图标不亮。。。。。
  102. UIApplication.shared.keyWindow?.rootViewController = destVC
  103. UIApplication.shared.keyWindow?.makeKeyAndVisible()
  104. }
  105. }
  106. }else {
  107. //本地 -> 校验 -> 下载NodeAPI -> 下载configInfo -> 自动登录
  108. O2AuthSDK.shared.launch { (state, msg) in
  109. switch state {
  110. case .bindError:
  111. //校验绑定结点信息错误
  112. self.forwardToSegue("bindPhoneSegue")
  113. break
  114. case .loginError:
  115. self.forwardToSegue("loginSystemSegue")
  116. //自动登录出错
  117. break
  118. case .unknownError:
  119. // self.showError(title: msg ?? "未知错误!")
  120. self.needReBind(msg ?? "未知错误!")
  121. break
  122. case .success:
  123. //处理移动端应用
  124. self.viewModel._saveAppConfigToDb()
  125. //跳转到主页
  126. let destVC = O2MainController.genernateVC()
  127. destVC.selectedIndex = 2 // 首页选中 TODO 图标不亮。。。。。
  128. UIApplication.shared.keyWindow?.rootViewController = destVC
  129. UIApplication.shared.keyWindow?.makeKeyAndVisible()
  130. }
  131. }
  132. }
  133. }
  134. // MARK:- 到不同的segue
  135. func forwardToSegue(_ segueIdentitifer:String){
  136. DispatchQueue.main.async {
  137. self.performSegue(withIdentifier: segueIdentitifer, sender: nil)
  138. }
  139. }
  140. @objc func receiveBindCompleted(customNotification:Notification){
  141. self.startFlowForPromise()
  142. }
  143. private func needReBind(_ error: String) {
  144. DispatchQueue.main.async {
  145. let alertController = UIAlertController(title: "提示", message: "加载出错,是否重新绑定?错误:\(error)", preferredStyle: .alert)
  146. let okAction = UIAlertAction(title: "重新绑定", style: .default, handler: {(action) in
  147. self.rebind()
  148. })
  149. let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: {(action) in
  150. })
  151. alertController.addAction(okAction)
  152. alertController.addAction(cancelAction)
  153. self.present(alertController, animated: true, completion: nil)
  154. }
  155. }
  156. private func rebind() {
  157. O2AuthSDK.shared.clearAllInformationBeforeReBind(callback: { (result, msg) in
  158. DDLogInfo("清空登录和绑定信息,result:\(result), msg:\(msg ?? "")")
  159. DBManager.shared.removeAll()
  160. DispatchQueue.main.async {
  161. self.forwardToSegue("bindPhoneSegue")
  162. }
  163. })
  164. }
  165. @IBAction func unBindComplete(_ sender: UIStoryboardSegue){
  166. //绑定完成,执行
  167. self.startFlowForPromise()
  168. }
  169. @IBAction func show(_ sender: UITapGestureRecognizer) {
  170. //ProgressHUD.show("系统加截中,请稍候...", interaction: true)
  171. }
  172. //登录后返回执行此方法
  173. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  174. segue.destination.modalPresentationStyle = .fullScreen
  175. }
  176. deinit {
  177. NotificationCenter.default.removeObserver(self)
  178. }
  179. }