LoginViewController.swift 8.9 KB

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