LoginViewController.swift 8.5 KB

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