123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317 |
- //
- // LoginViewController.swift
- // JiaPeiManage
- //
- // Created by Ning.ge on 2023/6/12.
- //
- import UIKit
- import RxSwift
- import RxCocoa
- import SwiftyUserDefaults
- import AuthenticationServices
- 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!
- //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
- }
- 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(type: 0)
- }).disposed(by: disposeBag)
- location_button_btn.rx.tap.subscribe ({ [unowned self] (_) in
- self.showDialog(type: 0)
- }).disposed(by: disposeBag)
- //wxin_button
- wxin_button.rx.tap.subscribe ({ [unowned self] (_) in
- self.showDialog(type: 1)
- }).disposed(by: disposeBag)
- //apple id
- apple_button.rx.tap.subscribe ({ [unowned self] (_) in
- self.performSignIn()
- }).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)
- //添加手势
- let tapBackground = UITapGestureRecognizer()
- tapBackground.rx.event
- .subscribe(onNext: { [weak self] _ in
- self?.view.endEditing(true)
- })
- .disposed(by: disposeBag)
- view.addGestureRecognizer(tapBackground)
- }
-
- 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
- }
- self.userlocation_textField.text = item?.dictLabel
- dialogViewController.hide()
- }
- dialogViewController.show()
- }
-
- }
- extension LoginViewController: ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {
- func performSignIn() {
- if #available(iOS 13.0, *) {
- let appleIDProvider = ASAuthorizationAppleIDProvider()
- let request = appleIDProvider.createRequest()
- request.requestedScopes = [.fullName, .email]
-
- let authorizationController = ASAuthorizationController(authorizationRequests: [request])
- authorizationController.delegate = self
- authorizationController.presentationContextProvider = self
- authorizationController.performRequests()
- } else {
- // Fallback on earlier versions
- }
-
- }
-
- // ASAuthorizationControllerDelegate methods
- @available(iOS 13.0, *)
- func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
- if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
- // Get the user's unique Apple ID and user information
- let userIdentifier = appleIDCredential.user
- let fullName = appleIDCredential.fullName
- let email = appleIDCredential.email
-
- // Handle the user data as needed
- print("User ID: \(userIdentifier)")
- if let fullName = fullName {
- print("Full Name: \(fullName.givenName ?? "") \(fullName.familyName ?? "")")
- }
- if let email = email {
- print("Email: \(email)")
- }
- }
- }
-
- @available(iOS 13.0, *)
- func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
- // Handle sign-in errors
- print("Sign In with Apple failed: \(error.localizedDescription)")
- }
-
- // ASAuthorizationControllerPresentationContextProviding method
- @available(iOS 13.0, *)
- func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
- return view.window!
- }
- }
|