// // 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 wx_btn_layout_L: NSLayoutConstraint! // 账号 @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! //wx @IBOutlet weak var wxin_button: UIButton! //apple @IBOutlet weak var apple_button: UIButton! var city:String = "" var cityPower:String = "" //地区权限 var cityItems:[CityItem] = [] var citys: [String:String] = [:] var selectedItemIndex = 0 var type = 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 if LocalManager.userInfo.ios_login_open == 1 { self.wx_btn_layout_L.constant = 21.f self.apple_button.isHidden = true } } 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.getCityArray(type: 0) }).disposed(by: disposeBag) location_button_btn.rx.tap.subscribe ({ [unowned self] (_) in self.getCityArray(type: 0) }).disposed(by: disposeBag) //wxin_button wxin_button.rx.tap.subscribe ({ [unowned self] (_) in if LocalManager.userInfo.ios_login_open == 1 { //关闭审核状态 if WXApi.isWXAppInstalled() { self.getCityArray(type: 1) }else { NYTips.showMsg(txt: "本地未安装微信") } }else{ //打开审核 self.getCityArray(type: 1) } }).disposed(by: disposeBag) //apple id apple_button.rx.tap.subscribe ({ [unowned self] (_) in if #available(iOS 13.0, *) { self.getCityArray(type: 2) } else { // Fallback on earlier versions NYTips.showMsg(txt: "请升级到iOS13.0以上") } }).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 //设置已经登录 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: disposeBag) }) .disposed(by: disposeBag) //获取wx绑定 NYWXApiManager.shared.blockSubject.subscribe(onNext: {[unowned self] code in NYTips.show() self.loginService.authorizationRequest(code: code, city: NYWXApiManager.shared.city).subscribe(onSuccess: {[unowned self] model in NYTips.hide() //判断是否有 openid let data = model.data as AuthorizationDataInfoModel if data.coach != nil { //已经绑定直接登录 LocalManager.userInfo.userAccount = data.coach?.idcard LocalManager.userInfo.password = data.coach?.password LocalManager.userInfo.city = self.city LocalManager.userInfo.cityPower = self.cityPower LocalManager.userInfo = data.coach! LocalManager.userInfo.isLogin = true //设置已经登录 NYAccountManager.shared.addUserinfo(user: data.coach!)//添加用户信息 self.dismiss(animated: true) }else{ //未绑定-跳转绑定 // NYSwRouter.push(NYSwPushType.login_biand_wx,context: data.openid) let bindingViewController = BindingViewController(nibName: "BindingViewController") bindingViewController.openid = data.openid self.addChild(bindingViewController) self.view.addSubview(bindingViewController.view) } }, onError: { error in NYTips.hide() //NYTips.showErr(txt: (error as! RequestError).errorDescription) print("%@",error) }) .disposed(by: self.disposeBag) }).disposed(by: disposeBag) //绑定apple NYAppleApiManager.shared.blockSubject.subscribe(onNext: {[unowned self] array in NYTips.show() self.loginService.authorizationIosRequest(userId: array[0], identityToken: array[1], city: NYAppleApiManager.shared.city).subscribe(onSuccess: {[unowned self] model in NYTips.hide() //判断是否有 openid let data = model.data as AuthorizationDataInfoModel if data.coach != nil { //已经绑定直接登录 LocalManager.userInfo.userAccount = data.coach?.idcard LocalManager.userInfo.password = data.coach?.password LocalManager.userInfo.city = self.city LocalManager.userInfo.cityPower = self.cityPower LocalManager.userInfo = data.coach! LocalManager.userInfo.isLogin = true //设置已经登录 NYAccountManager.shared.addUserinfo(user: data.coach!)//添加用户信息 self.dismiss(animated: true) }else{ let bindingViewController = BindingViewController(nibName: "BindingViewController") bindingViewController.bindType = 1 bindingViewController.openid = data.openid self.addChild(bindingViewController) self.view.addSubview(bindingViewController.view) } }, 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 getCityArray(type:Int){ if self.cityItems.count == 0 { NYTips.show() //获取字典数据 loginService.dictRequest(dictType: "coach_city").subscribe(onSuccess: { [unowned self] cityItems in self.cityItems = cityItems for cityItem in cityItems { self.citys[cityItem.dictLabel] = cityItem.dictValue } NYTips.hide() self.showDialog(type: type) },onError: { Error in NYTips.hide() }).disposed(by: disposeBag) }else{ self.showDialog(type: type) } } func showDialog(type:Int){ self.type = type 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 } if dialogViewController.items?.count == 0 {return} let ItemIndex = dialogViewController.selectedItemIndex self.selectedItemIndex = ItemIndex 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 //地区权重 if (self.type==1){//微信登录 let city = item!.dictValue //编码 NYWXApiManager.shared.sendWxapi(city: city) dialogViewController.hide() return } if(self.type==2){//apple登录 NYAppleApiManager.shared.performSignIn(city:city) dialogViewController.hide() return } self.userlocation_textField.text = item?.dictLabel dialogViewController.hide() } dialogViewController.show() } }