OOLoginViewModel.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. //
  2. // OOLoginViewModel.swift
  3. // O2Platform
  4. //
  5. // Created by 刘振兴 on 2018/4/5.
  6. // Copyright © 2018年 zoneland. All rights reserved.
  7. //
  8. import UIKit
  9. import Moya
  10. import Promises
  11. import ReactiveSwift
  12. import ReactiveCocoa
  13. import CocoaLumberjack
  14. import O2OA_Auth_SDK
  15. public enum OOLoginError:Error {
  16. case imLoginFail(OOAppError)
  17. case imRegisterFail(OOAppError)
  18. }
  19. class OOLoginViewModel: NSObject {
  20. // private let faceRecognizeAPI = OOMoyaProvider<FaceRecognizeAPI>()
  21. //密码输入及验证码按钮可用
  22. let passwordIsValid = MutableProperty(true)
  23. // 内网密码
  24. let pwdIsValid = MutableProperty(true)
  25. let submitButtionIsValid = MutableProperty(false)
  26. let submitButtonCurrentColor = MutableProperty(UIColor.lightGray)
  27. // let faceRecognizeLoginButtonisValid = MutableProperty(false)
  28. override init() {
  29. super.init()
  30. }
  31. }
  32. extension OOLoginViewModel {
  33. func _saveAppConfigToDb() {
  34. //判断本地hash和服务器的hash是否一致
  35. let local = AppConfigSettings.shared.customStyleHash
  36. if let remote = O2AuthSDK.shared.customStyleHash(), remote != local {
  37. if let current = O2AuthSDK.shared.customStyle() {
  38. var currentIndex = 0
  39. var existApps:[String] = [] // 删除配置中不存在的 或者 不需要显示的应用
  40. if let nativeList = current.nativeAppList {
  41. nativeList.forEachEnumerated { (index, app) in
  42. if app.enable == true {
  43. let nApp = O2App(title: app.name, appId: app.key, storyBoard: app.iOS?.storyboard, vcName: app.iOS?.vcname, segueIdentifier: nil, normalIcon: "icon_\(app.key!)", selectedIcon: "icon_\(app.key!)", customParameter: nil, order: currentIndex, mainOrder: currentIndex)
  44. DBManager.shared.insertData(nApp)
  45. currentIndex += 1
  46. existApps.append(app.key!)
  47. }
  48. }
  49. }
  50. if let portalList = current.portalList {
  51. portalList.forEachEnumerated { (index, o2app2) in
  52. let status = "{'portalId':'"+o2app2.id!+"'}"
  53. let weburl = AppDelegate.o2Collect.genrateURLWithWebContextKey(DesktopContext.DesktopContextKey, query: DesktopContext.appDetailQuery, parameter: ["##status##":status as AnyObject],covertd:false)
  54. let app = O2App(title: o2app2.name!, appId:o2app2.id!, storyBoard: "webview", vcName: weburl!, segueIdentifier: "showMailSegue", normalIcon: "icon_youjian", selectedIcon: "icon_youjian", order: currentIndex, mainOrder: currentIndex)
  55. DBManager.shared.insertData(app)
  56. currentIndex += 1
  57. existApps.append(o2app2.id!)
  58. }
  59. }
  60. // 删除配置中不存在的 或者 不需要显示的应用
  61. DBManager.shared.deleteNotExistApp(existApps)
  62. }
  63. }else {
  64. DDLogError("不需要更新数据???????")
  65. }
  66. }
  67. /// 人脸识别
  68. ///
  69. /// - Parameter image: 需要识别的图片
  70. /// - Returns: 如果成功识别 返回userId
  71. // func faceRecognize(image: UIImage) -> Promise<String> {
  72. // return Promise { fulfill, reject in
  73. // //TODO 这里的faceset 到时候要用collect上的unit id代替
  74. // self.faceRecognizeAPI.request(.search(image, "dev_o2oa_io"), completion: { (result) in
  75. // switch result {
  76. // case .success(let data):
  77. // let response = data.mapObject(FaceSearchResponse.self)
  78. // var userId = ""
  79. // if let ar = response?.data?.results {
  80. // if ar.count > 0 {
  81. // userId = ar[0].user_id ?? ""
  82. // DDLogDebug("userId:\(userId)")
  83. // }
  84. // }
  85. // if userId != "" {
  86. // fulfill(userId)
  87. // }else {
  88. // reject(OOAppError.common(type:"faceError",message:"没有识别到用户",statusCode:1024))
  89. // }
  90. // break
  91. // case .failure(let error):
  92. // reject(error)
  93. // break
  94. // }
  95. // })
  96. // }
  97. // }
  98. // MARK:- helper
  99. private static func cleanCookieCache(_ remoteURL:String) {
  100. let cstorage = HTTPCookieStorage.shared
  101. if let comps = URLComponents(string:remoteURL) {
  102. let url = "\(comps.scheme!)://\(comps.host!):\(comps.port!)"
  103. if let cookies = cstorage.cookies(for: URL(string: url)!){
  104. for cookie in cookies {
  105. cstorage.deleteCookie(cookie)
  106. }
  107. }
  108. }
  109. }
  110. }
  111. extension OOLoginViewModel {
  112. func loginControlIsValid(_ userNameField:UITextField,_ passwordTextField:UITextField, _ isInner: Bool = false) -> Void {
  113. let phoneValidSingal = userNameField.reactive.continuousTextValues.map { (text) -> Bool in
  114. guard text.count > 0 else {
  115. return false
  116. }
  117. return true
  118. }
  119. // phoneValidSingal.observeValues { (res) in
  120. // if isInner {
  121. // self.pwdIsValid.value = res
  122. // }else {
  123. // self.passwordIsValid.value = res
  124. // }
  125. // }
  126. let passwordValidSingal = passwordTextField.reactive.continuousTextValues.map { (text) -> Bool in
  127. if isInner {
  128. guard text.count > 0 else {
  129. return false
  130. }
  131. }else {
  132. guard text.count >= 4 else {
  133. return false
  134. }
  135. }
  136. return true
  137. }
  138. let signUpActiveSignal = Signal.combineLatest(phoneValidSingal, passwordValidSingal).scan(into: false) { (myResult, arg) in
  139. myResult = arg.0 && arg.1
  140. }
  141. signUpActiveSignal.observeValues { (res) in
  142. self.submitButtionIsValid.value = res
  143. }
  144. let signUpColorSignal = signUpActiveSignal.map { (param) -> UIColor in
  145. if param {
  146. return base_color
  147. }else{
  148. return UIColor.lightGray
  149. }
  150. }
  151. signUpColorSignal.observeValues { (currentColor) in
  152. self.submitButtonCurrentColor.value = currentColor
  153. }
  154. }
  155. // /// 人脸识别功能是否可用
  156. // func faceRecognizeValidate() {
  157. // // face++ 授权验证
  158. // MGFaceLicenseHandle.license { (result, date) in
  159. // DDLogInfo("face++ 验证权限完成, result:\(result)")
  160. // self.faceRecognizeLoginButtonisValid.value = result
  161. // }
  162. //
  163. // }
  164. }