123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- //
- // LoginViewController.swift
- // JiaPeiManage
- //
- // Created by Ning.ge on 2023/6/12.
- //
- import UIKit
- import RxSwift
- import RxCocoa
- import SwiftyUserDefaults
- final class LoginViewController: BaseViewController {
-
- // MARK: 服务属性
- private let loginService: LoginServiceType = LoginService(networking: LoginNetworking())
-
- // MARK: UI属性
-
- // 账号
- @IBOutlet weak var username_textField: QMUITextField!
- //密码
- @IBOutlet weak var userpassword_textField: QMUITextField!
- //显示密码
- @IBOutlet weak var pwdshow_button: QMUIButton!
- //定位arrow
- @IBOutlet weak var location_button: QMUIButton!
- @IBOutlet weak var location_button_btn: UIButton!
- //区域
- @IBOutlet weak var userlocation_textField: QMUITextField!
- //登录按钮
- @IBOutlet weak var login_button: UIButton!
- //已同意
- @IBOutlet weak var agree_button: QMUIButton!
- //用户协议
- @IBOutlet weak var userprotocol_button: UIButton!
- //隐私政策
- @IBOutlet weak var userprivacy_button: UIButton!
-
- var city:String = ""
- var cityPower:String = "" //地区权限
- var cityItems:[CityItem] = []
- var citys: [String:String] = [:]
- var selectedItemIndex = 0
- override func viewDidLoad() {
- super.viewDidLoad()
- setupUI()
- //biand绑定
- biandView()
- //获取字典数据
- loginService.dictRequest(dictType: "coach_city").subscribe(onSuccess: { [unowned self] cityItems in
- self.cityItems = cityItems
- for cityItem in cityItems {
- self.citys[cityItem.dictLabel] = cityItem.dictValue
- }
- },onError: { Error in
- }).disposed(by: disposeBag)
- }
- // MARK: 私有方法
- func setupUI(){
- self.view.backgroundColor = .db_theme
- if ((LocalManager.userInfo.userAccount?.isEmpty) != nil) {
- self.username_textField.text = LocalManager.userInfo.userAccount
- }
- if ((LocalManager.userInfo.password?.isEmpty) != nil) {
- self.userpassword_textField.text = LocalManager.userInfo.password
- }
- self.username_textField.placeholderColor = .db_place
- self.userpassword_textField.placeholderColor = .db_place
- self.userlocation_textField.placeholderColor = .db_place
- }
- func biandView(){
- //判断账号的输入是否可用
- let accountValid = username_textField.rx.text.orEmpty.map{ value in
- return value.count >= 18
- }
- //判断密码的输入是否可用
- let passwordValid = userpassword_textField.rx.text.orEmpty.map{ value in
- return value.count >= 6
- }
-
- //判断同意
- let agreeValid = agree_button.rx.tap.map { [unowned self] in
- NSLog("agreeValid")
- return self.agree_button.isSelected
- }
- //同意
- agree_button.rx.tap.subscribe ({ [unowned self] (_) in
- NSLog("tap.subscribe")
- self.agree_button.isSelected = !self.agree_button.isSelected
- }).disposed(by: disposeBag)
- //登录按钮的可用与否
- let loginObserver = Observable.combineLatest(accountValid,passwordValid,agreeValid){(account,password,agree) in
- account && password && agree
- }
- //绑定按钮
- loginObserver.bind(to: login_button.rx.isEnabled).disposed(by: disposeBag)
- loginObserver.subscribe(onNext: { [unowned self] valid in
- NSLog("loginObserver")
- self.login_button.alpha = valid ? 1 : 0.5
- }).disposed(by: disposeBag)
-
- //用户协议
- userprotocol_button.rx.tap.subscribe ({ [unowned self] (_) in
- //跳web
- navigator.push("https://ys.zzxcx.net/xy_jlgj.html")
- }).disposed(by: disposeBag)
- //隐私
- userprivacy_button.rx.tap.subscribe ({ [unowned self] (_) in
- //跳web
- navigator.push("https://ys.zzxcx.net/xy_jlgj.html")
- }).disposed(by: disposeBag)
- //显示密码
- pwdshow_button.rx.tap.subscribe ({ [unowned self] (_) in
- self.pwdshow_button.isSelected = !self.pwdshow_button.isSelected;
- self.userpassword_textField.isSecureTextEntry = !self.pwdshow_button.isSelected
- }).disposed(by: disposeBag)
- //选择区域
- location_button.rx.tap.subscribe ({ [unowned self] (_) in
- NSLog("点击了:选择区域- \(Thread.isMainThread)")
- self.showDialog()
- }).disposed(by: disposeBag)
- location_button_btn.rx.tap.subscribe ({ [unowned self] (_) in
- self.showDialog()
- }).disposed(by: disposeBag)
- //登录
- login_button.rx.tap
- .asObservable()
- .withLatestFrom(loginObserver)
- .do(onNext: {
- [unowned self]_ in
- self.login_button.isEnabled = false
- self.view.endEditing(true)
- })
- .subscribe(onNext: {[unowned self]isLogin in
- NSLog("登录 - 登录: login %@","123")
- self.login_button.isEnabled = true
- //保存用户信息
- var account:String = self.username_textField.text!
- var password:String = self.userpassword_textField.text!
- if city=="" {
- NYTips.showMsg(txt: "请选择地区!")
- return
- }
- LocalManager.userInfo.userAccount = account
- LocalManager.userInfo.password = password
- LocalManager.userInfo.city = city
- LocalManager.userInfo.cityPower = cityPower
- //登录api RX 订阅 观察 销毁 三部曲
- NYTips.show()
- self.loginService.loginRequest(user_name: account, user_password: password,city: city)
- .subscribe(onSuccess: { userinfo in
- NYTips.hide()
- LocalManager.userInfo = userinfo
- LocalManager.userInfo.isLogin = true //设置已经登录
- print("登录成功:%@", userinfo)
- self.dismiss(animated: true)
- }, onError: { error in
- NYTips.hide()
- NYTips.showErr(txt: (error as! RequestError).errorDescription)
- print("%@",error)
- })
- .disposed(by: disposeBag)
-
- })
- .disposed(by: disposeBag)
- //添加手势
- let tapBackground = UITapGestureRecognizer()
- tapBackground.rx.event
- .subscribe(onNext: { [weak self] _ in
- self?.view.endEditing(true)
- })
- .disposed(by: disposeBag)
- view.addGestureRecognizer(tapBackground)
- }
-
-
- func showDialog(){
-
- let dialogViewController = QMUIDialogSelectionViewController()
- dialogViewController.title = "请选择地区"
- dialogViewController.items = Array(citys.keys)
- dialogViewController.selectedItemIndex = selectedItemIndex
- dialogViewController.addCancelButton(withText: "取消", block: nil)
- dialogViewController.addSubmitButton(withText: "确定") { [weak self] dialogViewController in
- guard let dialogViewController:QMUIDialogSelectionViewController = dialogViewController as? QMUIDialogSelectionViewController else { return }
- if dialogViewController.selectedItemIndex == QMUIDialogSelectionViewControllerSelectedItemIndexNone {
- NYTips.showErr(txt: "请至少选一个")
- return
- }
- self?.selectedItemIndex = dialogViewController.selectedItemIndex
- let keyname = dialogViewController.items![self!.selectedItemIndex]
- // self.citys[cityItem.dictLabel] = cityItem.dictValue
- var item:CityItem?
- for cityItem in self!.cityItems {
- if cityItem.dictLabel == keyname {
- item = cityItem
- break
- }
- }
- self?.city = item!.dictValue //编码
- self?.cityPower = item!.remark //地区权重
- self?.userlocation_textField.text = item?.dictLabel
- dialogViewController.hide()
- }
- dialogViewController.show()
- }
-
-
- }
|