LoginViewController.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // LoginViewController.swift
  3. // JiaPeiManage
  4. //
  5. // Created by Ning.ge on 2023/6/12.
  6. //
  7. import UIKit
  8. import RxSwift
  9. import RxCocoa
  10. import SwiftyUserDefaults
  11. final class LoginViewController: BaseViewController {
  12. // MARK: 服务属性
  13. private let loginService: LoginServiceType = LoginService(networking: LoginNetworking())
  14. // MARK: UI属性
  15. // 账号
  16. @IBOutlet weak var username_textField: QMUITextField!
  17. //密码
  18. @IBOutlet weak var userpassword_textField: QMUITextField!
  19. //显示密码
  20. @IBOutlet weak var pwdshow_button: QMUIButton!
  21. //定位arrow
  22. @IBOutlet weak var location_button: QMUIButton!
  23. @IBOutlet weak var location_button_btn: UIButton!
  24. //区域
  25. @IBOutlet weak var userlocation_textField: QMUITextField!
  26. //登录按钮
  27. @IBOutlet weak var login_button: UIButton!
  28. //已同意
  29. @IBOutlet weak var agree_button: QMUIButton!
  30. //用户协议
  31. @IBOutlet weak var userprotocol_button: UIButton!
  32. //隐私政策
  33. @IBOutlet weak var userprivacy_button: UIButton!
  34. var city:String = ""
  35. var cityItems:[CityItem] = []
  36. var citys: [String:String] = [:]
  37. var selectedItemIndex = 0
  38. override func viewDidLoad() {
  39. super.viewDidLoad()
  40. setupUI()
  41. //biand绑定
  42. biandView()
  43. //获取字典数据
  44. loginService.dictRequest(dictType: "coach_city").subscribe(onSuccess: { [unowned self] cityItems in
  45. self.cityItems = cityItems
  46. for cityItem in cityItems {
  47. self.citys[cityItem.dictLabel] = cityItem.dictValue
  48. }
  49. },onError: { Error in
  50. }).disposed(by: disposeBag)
  51. }
  52. // MARK: 私有方法
  53. func setupUI(){
  54. self.view.backgroundColor = .db_theme
  55. if ((LocalManager.userInfo.userAccount?.isEmpty) != nil) {
  56. self.username_textField.text = LocalManager.userInfo.userAccount
  57. }
  58. if ((LocalManager.userInfo.password?.isEmpty) != nil) {
  59. self.userpassword_textField.text = LocalManager.userInfo.password
  60. }
  61. self.username_textField.placeholderColor = .db_place
  62. self.userpassword_textField.placeholderColor = .db_place
  63. self.userlocation_textField.placeholderColor = .db_place
  64. }
  65. func biandView(){
  66. //判断账号的输入是否可用
  67. let accountValid = username_textField.rx.text.orEmpty.map{ value in
  68. return value.count >= 18
  69. }
  70. //判断密码的输入是否可用
  71. let passwordValid = userpassword_textField.rx.text.orEmpty.map{ value in
  72. return value.count >= 6
  73. }
  74. //判断同意
  75. let agreeValid = agree_button.rx.tap.map { [unowned self] in
  76. NSLog("agreeValid")
  77. return self.agree_button.isSelected
  78. }
  79. //同意
  80. agree_button.rx.tap.subscribe ({ [unowned self] (_) in
  81. NSLog("tap.subscribe")
  82. self.agree_button.isSelected = !self.agree_button.isSelected
  83. }).disposed(by: disposeBag)
  84. //登录按钮的可用与否
  85. let loginObserver = Observable.combineLatest(accountValid,passwordValid,agreeValid){(account,password,agree) in
  86. account && password && agree
  87. }
  88. //绑定按钮
  89. loginObserver.bind(to: login_button.rx.isEnabled).disposed(by: disposeBag)
  90. loginObserver.subscribe(onNext: { [unowned self] valid in
  91. NSLog("loginObserver")
  92. self.login_button.alpha = valid ? 1 : 0.5
  93. }).disposed(by: disposeBag)
  94. //用户协议
  95. userprotocol_button.rx.tap.subscribe ({ [unowned self] (_) in
  96. //跳web
  97. navigator.push("https://ys.zzxcx.net/xy_jsjp.html")
  98. }).disposed(by: disposeBag)
  99. //隐私
  100. userprivacy_button.rx.tap.subscribe ({ [unowned self] (_) in
  101. //跳web
  102. navigator.push("https://ys.zzxcx.net/xy_jsjp.html")
  103. }).disposed(by: disposeBag)
  104. //显示密码
  105. pwdshow_button.rx.tap.subscribe ({ [unowned self] (_) in
  106. self.pwdshow_button.isSelected = !self.pwdshow_button.isSelected;
  107. self.userpassword_textField.isSecureTextEntry = !self.pwdshow_button.isSelected
  108. }).disposed(by: disposeBag)
  109. //选择区域
  110. location_button.rx.tap.subscribe ({ [unowned self] (_) in
  111. NSLog("点击了:选择区域- \(Thread.isMainThread)")
  112. self.showDialog()
  113. }).disposed(by: disposeBag)
  114. location_button_btn.rx.tap.subscribe ({ [unowned self] (_) in
  115. self.showDialog()
  116. }).disposed(by: disposeBag)
  117. //登录
  118. login_button.rx.tap
  119. .asObservable()
  120. .withLatestFrom(loginObserver)
  121. .do(onNext: {
  122. [unowned self]_ in
  123. self.login_button.isEnabled = false
  124. self.view.endEditing(true)
  125. })
  126. .subscribe(onNext: {[unowned self]isLogin in
  127. NSLog("登录 - 登录: login %@","123")
  128. self.login_button.isEnabled = true
  129. //保存用户信息
  130. var account:String = self.username_textField.text!
  131. var password:String = self.userpassword_textField.text!
  132. if city=="" {
  133. NYTips.showMsg(txt: "请选择地区!")
  134. return
  135. }
  136. LocalManager.userInfo.userAccount = account
  137. LocalManager.userInfo.password = password
  138. LocalManager.userInfo.city = city
  139. //登录api RX 订阅 观察 销毁 三部曲
  140. NYTips.show()
  141. self.loginService.loginRequest(user_name: account, user_password: password,city: city)
  142. .subscribe(onSuccess: { userinfo in
  143. NYTips.hide()
  144. LocalManager.userInfo = userinfo
  145. LocalManager.userInfo.isLogin = true //设置已经登录
  146. print("登录成功:%@", userinfo)
  147. self.dismiss(animated: true)
  148. }, onError: { error in
  149. NYTips.hide()
  150. NYTips.showErr(txt: error.localizedDescription)
  151. print("%@",error)
  152. })
  153. .disposed(by: disposeBag)
  154. })
  155. .disposed(by: disposeBag)
  156. //添加手势
  157. let tapBackground = UITapGestureRecognizer()
  158. tapBackground.rx.event
  159. .subscribe(onNext: { [weak self] _ in
  160. self?.view.endEditing(true)
  161. })
  162. .disposed(by: disposeBag)
  163. view.addGestureRecognizer(tapBackground)
  164. }
  165. func showDialog(){
  166. let dialogViewController = QMUIDialogSelectionViewController()
  167. dialogViewController.title = "请选择地区"
  168. dialogViewController.items = Array(citys.keys)
  169. dialogViewController.selectedItemIndex = selectedItemIndex
  170. dialogViewController.addCancelButton(withText: "取消", block: nil)
  171. dialogViewController.addSubmitButton(withText: "确定") { [weak self] dialogViewController in
  172. guard let dialogViewController:QMUIDialogSelectionViewController = dialogViewController as? QMUIDialogSelectionViewController else { return }
  173. if dialogViewController.selectedItemIndex == QMUIDialogSelectionViewControllerSelectedItemIndexNone {
  174. NYTips.showErr(txt: "请至少选一个")
  175. return
  176. }
  177. self?.selectedItemIndex = dialogViewController.selectedItemIndex
  178. let item = self!.cityItems[dialogViewController.selectedItemIndex]
  179. self?.city = item.dictValue //编码
  180. self?.userlocation_textField.text = item.dictLabel
  181. dialogViewController.hide()
  182. }
  183. dialogViewController.show()
  184. }
  185. }