BindingViewController.swift 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. //
  2. // BindingViewController.swift
  3. // JiaPeiManage
  4. //
  5. // Created by Ning.ge on 2023/8/3.
  6. //
  7. import UIKit
  8. import RxSwift
  9. import RxCocoa
  10. import SwiftyUserDefaults
  11. final class BindingViewController: 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. var openid:String = ""
  29. var city:String = ""
  30. var cityPower:String = "" //地区权限
  31. var cityItems:[CityItem] = []
  32. var citys: [String:String] = [:]
  33. var selectedItemIndex = 0
  34. override func viewDidLoad() {
  35. super.viewDidLoad()
  36. setupUI()
  37. //biand绑定
  38. biandView()
  39. //获取字典数据
  40. loginService.dictRequest(dictType: "coach_city").subscribe(onSuccess: { [unowned self] cityItems in
  41. self.cityItems = cityItems
  42. for cityItem in cityItems {
  43. self.citys[cityItem.dictLabel] = cityItem.dictValue
  44. }
  45. },onError: { Error in
  46. }).disposed(by: disposeBag)
  47. }
  48. // MARK: 私有方法
  49. func setupUI(){
  50. self.view.backgroundColor = .db_theme
  51. self.username_textField.placeholderColor = .db_place
  52. self.userpassword_textField.placeholderColor = .db_place
  53. self.userlocation_textField.placeholderColor = .db_place
  54. }
  55. func biandView(){
  56. //判断账号的输入是否可用
  57. let accountValid = username_textField.rx.text.orEmpty.map{ value in
  58. return value.count >= 18
  59. }
  60. //判断密码的输入是否可用
  61. let passwordValid = userpassword_textField.rx.text.orEmpty.map{ value in
  62. return value.count >= 6
  63. }
  64. //登录按钮的可用与否
  65. let loginObserver = Observable.combineLatest(accountValid,passwordValid){(account,password) in
  66. account && password
  67. }
  68. //绑定按钮
  69. loginObserver.bind(to: login_button.rx.isEnabled).disposed(by: disposeBag)
  70. loginObserver.subscribe(onNext: { [unowned self] valid in
  71. NSLog("loginObserver")
  72. self.login_button.alpha = valid ? 1 : 0.5
  73. }).disposed(by: disposeBag)
  74. //显示密码
  75. pwdshow_button.rx.tap.subscribe ({ [unowned self] (_) in
  76. self.pwdshow_button.isSelected = !self.pwdshow_button.isSelected;
  77. self.userpassword_textField.isSecureTextEntry = !self.pwdshow_button.isSelected
  78. }).disposed(by: disposeBag)
  79. //选择区域
  80. location_button.rx.tap.subscribe ({ [unowned self] (_) in
  81. NSLog("点击了:选择区域- \(Thread.isMainThread)")
  82. self.showDialog()
  83. }).disposed(by: disposeBag)
  84. location_button_btn.rx.tap.subscribe ({ [unowned self] (_) in
  85. self.showDialog()
  86. }).disposed(by: disposeBag)
  87. //登录
  88. login_button.rx.tap
  89. .asObservable()
  90. .withLatestFrom(loginObserver)
  91. .do(onNext: {
  92. [unowned self]_ in
  93. self.login_button.isEnabled = false
  94. self.view.endEditing(true)
  95. })
  96. .subscribe(onNext: {[unowned self]isLogin in
  97. self.login_button.isEnabled = true
  98. //保存用户信息
  99. var account:String = self.username_textField.text!
  100. var password:String = self.userpassword_textField.text!
  101. if city=="" {
  102. NYTips.showMsg(txt: "请选择地区!")
  103. return
  104. }
  105. LocalManager.userInfo.userAccount = account
  106. LocalManager.userInfo.password = password
  107. LocalManager.userInfo.city = city
  108. LocalManager.userInfo.cityPower = cityPower
  109. //登录api RX 订阅 观察 销毁 三部曲
  110. NYTips.show()
  111. self.loginService.biandUserRequest(idcard: account, user_password: password,openid: self.openid,city: city)
  112. .subscribe(onSuccess: {[unowned self] userinfo in
  113. NYTips.hide()
  114. LocalManager.userInfo = userinfo
  115. LocalManager.userInfo.isLogin = true //设置已经登录
  116. NYAccountManager.shared.addUserinfo(user: userinfo)//添加用户信息
  117. print("登录成功:%@", userinfo)
  118. self.dismiss(animated: true)
  119. }, onError: { error in
  120. NYTips.hide()
  121. NYTips.showErr(txt: (error as! RequestError).errorDescription)
  122. print("%@",error)
  123. })
  124. .disposed(by: self.disposeBag)
  125. })
  126. .disposed(by: disposeBag)
  127. //添加手势
  128. let tapBackground = UITapGestureRecognizer()
  129. tapBackground.rx.event
  130. .subscribe(onNext: { [weak self] _ in
  131. self?.view.endEditing(true)
  132. })
  133. .disposed(by: disposeBag)
  134. view.addGestureRecognizer(tapBackground)
  135. }
  136. func showDialog(){
  137. let dialogViewController = QMUIDialogSelectionViewController()
  138. dialogViewController.title = "请选择地区"
  139. dialogViewController.items = Array(citys.keys)
  140. dialogViewController.selectedItemIndex = selectedItemIndex
  141. dialogViewController.addCancelButton(withText: "取消", block: nil)
  142. dialogViewController.addSubmitButton(withText: "确定") { [unowned self] dialogViewController in
  143. guard let dialogViewController:QMUIDialogSelectionViewController = dialogViewController as? QMUIDialogSelectionViewController else { return }
  144. if dialogViewController.selectedItemIndex == QMUIDialogSelectionViewControllerSelectedItemIndexNone {
  145. NYTips.showErr(txt: "请至少选一个")
  146. return
  147. }
  148. self.selectedItemIndex = dialogViewController.selectedItemIndex
  149. let keyname = dialogViewController.items![self.selectedItemIndex]
  150. // self.citys[cityItem.dictLabel] = cityItem.dictValue
  151. var item:CityItem?
  152. for cityItem in self.cityItems {
  153. if cityItem.dictLabel == keyname {
  154. item = cityItem
  155. break
  156. }
  157. }
  158. self.city = item!.dictValue //编码
  159. self.cityPower = item!.remark //地区权重
  160. self.userlocation_textField.text = item?.dictLabel
  161. dialogViewController.hide()
  162. }
  163. dialogViewController.show()
  164. }
  165. }