// // BindingViewController.swift // JiaPeiManage // // Created by Ning.ge on 2023/8/3. // import UIKit import RxSwift import RxCocoa import SwiftyUserDefaults final class BindingViewController: 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! var openid:String = "" 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 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 loginObserver = Observable.combineLatest(accountValid,passwordValid){(account,password) in account && password } //绑定按钮 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) //显示密码 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 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.biandUserRequest(idcard: account, user_password: password,openid: self.openid,city: city) .subscribe(onSuccess: {[unowned self] userinfo in NYTips.hide() LocalManager.userInfo = userinfo LocalManager.userInfo.isLogin = true //设置已经登录 NYAccountManager.shared.addUserinfo(user: userinfo)//添加用户信息 print("登录成功:%@", userinfo) self.dismiss(animated: true) }, onError: { error in NYTips.hide() NYTips.showErr(txt: (error as! RequestError).errorDescription) print("%@",error) }) .disposed(by: self.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: "确定") { [unowned 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() } }