LoginViewController.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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. import AuthenticationServices
  12. final class LoginViewController: BaseViewController {
  13. // MARK: 服务属性
  14. private let loginService: LoginServiceType = LoginService(networking: LoginNetworking())
  15. // MARK: UI属性
  16. // 账号
  17. @IBOutlet weak var username_textField: QMUITextField!
  18. //密码
  19. @IBOutlet weak var userpassword_textField: QMUITextField!
  20. //显示密码
  21. @IBOutlet weak var pwdshow_button: QMUIButton!
  22. //定位arrow
  23. @IBOutlet weak var location_button: QMUIButton!
  24. @IBOutlet weak var location_button_btn: UIButton!
  25. //区域
  26. @IBOutlet weak var userlocation_textField: QMUITextField!
  27. //登录按钮
  28. @IBOutlet weak var login_button: UIButton!
  29. //已同意
  30. @IBOutlet weak var agree_button: QMUIButton!
  31. //用户协议
  32. @IBOutlet weak var userprotocol_button: UIButton!
  33. //隐私政策
  34. @IBOutlet weak var userprivacy_button: UIButton!
  35. //wx
  36. @IBOutlet weak var wxin_button: UIButton!
  37. //apple
  38. @IBOutlet weak var apple_button: UIButton!
  39. var city:String = ""
  40. var cityPower:String = "" //地区权限
  41. var cityItems:[CityItem] = []
  42. var citys: [String:String] = [:]
  43. var selectedItemIndex = 0
  44. var type = 0
  45. override func viewDidLoad() {
  46. super.viewDidLoad()
  47. setupUI()
  48. //biand绑定
  49. biandView()
  50. //获取字典数据
  51. loginService.dictRequest(dictType: "coach_city").subscribe(onSuccess: { [unowned self] cityItems in
  52. self.cityItems = cityItems
  53. for cityItem in cityItems {
  54. self.citys[cityItem.dictLabel] = cityItem.dictValue
  55. }
  56. },onError: { Error in
  57. }).disposed(by: disposeBag)
  58. }
  59. // MARK: 私有方法
  60. func setupUI(){
  61. self.view.backgroundColor = .db_theme
  62. if ((LocalManager.userInfo.userAccount?.isEmpty) != nil) {
  63. self.username_textField.text = LocalManager.userInfo.userAccount
  64. }
  65. if ((LocalManager.userInfo.password?.isEmpty) != nil) {
  66. self.userpassword_textField.text = LocalManager.userInfo.password
  67. }
  68. self.username_textField.placeholderColor = .db_place
  69. self.userpassword_textField.placeholderColor = .db_place
  70. self.userlocation_textField.placeholderColor = .db_place
  71. }
  72. func biandView(){
  73. //判断账号的输入是否可用
  74. let accountValid = username_textField.rx.text.orEmpty.map{ value in
  75. return value.count >= 18
  76. }
  77. //判断密码的输入是否可用
  78. let passwordValid = userpassword_textField.rx.text.orEmpty.map{ value in
  79. return value.count >= 6
  80. }
  81. //判断同意
  82. let agreeValid = agree_button.rx.tap.map { [unowned self] in
  83. NSLog("agreeValid")
  84. return self.agree_button.isSelected
  85. }
  86. //同意
  87. agree_button.rx.tap.subscribe ({ [unowned self] (_) in
  88. NSLog("tap.subscribe")
  89. self.agree_button.isSelected = !self.agree_button.isSelected
  90. }).disposed(by: disposeBag)
  91. //登录按钮的可用与否
  92. let loginObserver = Observable.combineLatest(accountValid,passwordValid,agreeValid){(account,password,agree) in
  93. account && password && agree
  94. }
  95. //绑定按钮
  96. loginObserver.bind(to: login_button.rx.isEnabled).disposed(by: disposeBag)
  97. loginObserver.subscribe(onNext: { [unowned self] valid in
  98. NSLog("loginObserver")
  99. self.login_button.alpha = valid ? 1 : 0.5
  100. }).disposed(by: disposeBag)
  101. //用户协议
  102. userprotocol_button.rx.tap.subscribe ({ [unowned self] (_) in
  103. //跳web
  104. navigator.push("https://ys.zzxcx.net/xy_jlgj.html")
  105. }).disposed(by: disposeBag)
  106. //隐私
  107. userprivacy_button.rx.tap.subscribe ({ [unowned self] (_) in
  108. //跳web
  109. navigator.push("https://ys.zzxcx.net/xy_jlgj.html")
  110. }).disposed(by: disposeBag)
  111. //显示密码
  112. pwdshow_button.rx.tap.subscribe ({ [unowned self] (_) in
  113. self.pwdshow_button.isSelected = !self.pwdshow_button.isSelected;
  114. self.userpassword_textField.isSecureTextEntry = !self.pwdshow_button.isSelected
  115. }).disposed(by: disposeBag)
  116. //选择区域
  117. location_button.rx.tap.subscribe ({ [unowned self] (_) in
  118. NSLog("点击了:选择区域- \(Thread.isMainThread)")
  119. self.showDialog(type: 0)
  120. }).disposed(by: disposeBag)
  121. location_button_btn.rx.tap.subscribe ({ [unowned self] (_) in
  122. self.showDialog(type: 0)
  123. }).disposed(by: disposeBag)
  124. //wxin_button
  125. wxin_button.rx.tap.subscribe ({ [unowned self] (_) in
  126. self.showDialog(type: 1)
  127. }).disposed(by: disposeBag)
  128. //apple id
  129. apple_button.rx.tap.subscribe ({ [unowned self] (_) in
  130. self.performSignIn()
  131. }).disposed(by: disposeBag)
  132. //登录
  133. login_button.rx.tap
  134. .asObservable()
  135. .withLatestFrom(loginObserver)
  136. .do(onNext: {
  137. [unowned self]_ in
  138. self.login_button.isEnabled = false
  139. self.view.endEditing(true)
  140. })
  141. .subscribe(onNext: {[unowned self]isLogin in
  142. NSLog("登录 - 登录: login %@","123")
  143. self.login_button.isEnabled = true
  144. //保存用户信息
  145. var account:String = self.username_textField.text!
  146. var password:String = self.userpassword_textField.text!
  147. if city=="" {
  148. NYTips.showMsg(txt: "请选择地区!")
  149. return
  150. }
  151. LocalManager.userInfo.userAccount = account
  152. LocalManager.userInfo.password = password
  153. LocalManager.userInfo.city = city
  154. LocalManager.userInfo.cityPower = cityPower
  155. //登录api RX 订阅 观察 销毁 三部曲
  156. NYTips.show()
  157. self.loginService.loginRequest(user_name: account, user_password: password,city: city)
  158. .subscribe(onSuccess: { userinfo in
  159. NYTips.hide()
  160. LocalManager.userInfo = userinfo
  161. LocalManager.userInfo.isLogin = true //设置已经登录
  162. NYAccountManager.shared.addUserinfo(user: userinfo)//添加用户信息
  163. print("登录成功:%@", userinfo)
  164. self.dismiss(animated: true)
  165. }, onError: { error in
  166. NYTips.hide()
  167. NYTips.showErr(txt: (error as! RequestError).errorDescription)
  168. print("%@",error)
  169. })
  170. .disposed(by: disposeBag)
  171. })
  172. .disposed(by: disposeBag)
  173. //获取wx绑定
  174. NYWXApiManager.shared.blockSubject.subscribe(onNext: {[unowned self] code in
  175. NYTips.show()
  176. self.loginService.authorizationRequest(code: code, city: NYWXApiManager.shared.city).subscribe(onSuccess: {[unowned self] model in
  177. NYTips.hide()
  178. //判断是否有 openid
  179. let data = model.data as AuthorizationDataInfoModel
  180. if data.coach != nil {
  181. //已经绑定直接登录
  182. LocalManager.userInfo.userAccount = data.coach?.idcard
  183. LocalManager.userInfo.password = data.coach?.password
  184. LocalManager.userInfo.city = self.city
  185. LocalManager.userInfo.cityPower = self.cityPower
  186. LocalManager.userInfo = data.coach!
  187. LocalManager.userInfo.isLogin = true //设置已经登录
  188. NYAccountManager.shared.addUserinfo(user: data.coach!)//添加用户信息
  189. self.dismiss(animated: true)
  190. }else{
  191. //未绑定-跳转绑定
  192. // NYSwRouter.push(NYSwPushType.login_biand_wx,context: data.openid)
  193. let bindingViewController = BindingViewController(nibName: "BindingViewController")
  194. bindingViewController.openid = data.openid
  195. self.addChild(bindingViewController)
  196. self.view.addSubview(bindingViewController.view)
  197. }
  198. }, onError: { error in
  199. NYTips.hide()
  200. //NYTips.showErr(txt: (error as! RequestError).errorDescription)
  201. print("%@",error)
  202. })
  203. .disposed(by: self.disposeBag)
  204. }).disposed(by: disposeBag)
  205. //添加手势
  206. let tapBackground = UITapGestureRecognizer()
  207. tapBackground.rx.event
  208. .subscribe(onNext: { [weak self] _ in
  209. self?.view.endEditing(true)
  210. })
  211. .disposed(by: disposeBag)
  212. view.addGestureRecognizer(tapBackground)
  213. }
  214. func showDialog(type:Int){
  215. self.type = type
  216. let dialogViewController = QMUIDialogSelectionViewController()
  217. dialogViewController.title = "请选择地区"
  218. dialogViewController.items = Array(citys.keys)
  219. dialogViewController.selectedItemIndex = selectedItemIndex
  220. dialogViewController.addCancelButton(withText: "取消", block: nil)
  221. dialogViewController.addSubmitButton(withText: "确定") { [unowned self] dialogViewController in
  222. guard let dialogViewController:QMUIDialogSelectionViewController = dialogViewController as? QMUIDialogSelectionViewController else { return }
  223. if dialogViewController.selectedItemIndex == QMUIDialogSelectionViewControllerSelectedItemIndexNone {
  224. NYTips.showErr(txt: "请至少选一个")
  225. return
  226. }
  227. if dialogViewController.items?.count == 0 {return}
  228. let ItemIndex = dialogViewController.selectedItemIndex
  229. self.selectedItemIndex = ItemIndex
  230. let keyname = dialogViewController.items![self.selectedItemIndex]
  231. // self.citys[cityItem.dictLabel] = cityItem.dictValue
  232. var item:CityItem?
  233. for cityItem in self.cityItems {
  234. if cityItem.dictLabel == keyname {
  235. item = cityItem
  236. break
  237. }
  238. }
  239. self.city = item!.dictValue //编码
  240. self.cityPower = item!.remark //地区权重
  241. if (self.type==1){//微信登录
  242. let city = item!.dictValue //编码
  243. NYWXApiManager.shared.sendWxapi(city: city)
  244. dialogViewController.hide()
  245. return
  246. }
  247. self.userlocation_textField.text = item?.dictLabel
  248. dialogViewController.hide()
  249. }
  250. dialogViewController.show()
  251. }
  252. }
  253. extension LoginViewController: ASAuthorizationControllerDelegate, ASAuthorizationControllerPresentationContextProviding {
  254. func performSignIn() {
  255. if #available(iOS 13.0, *) {
  256. let appleIDProvider = ASAuthorizationAppleIDProvider()
  257. let request = appleIDProvider.createRequest()
  258. request.requestedScopes = [.fullName, .email]
  259. let authorizationController = ASAuthorizationController(authorizationRequests: [request])
  260. authorizationController.delegate = self
  261. authorizationController.presentationContextProvider = self
  262. authorizationController.performRequests()
  263. } else {
  264. // Fallback on earlier versions
  265. }
  266. }
  267. // ASAuthorizationControllerDelegate methods
  268. @available(iOS 13.0, *)
  269. func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
  270. if let appleIDCredential = authorization.credential as? ASAuthorizationAppleIDCredential {
  271. // Get the user's unique Apple ID and user information
  272. let userIdentifier = appleIDCredential.user
  273. let fullName = appleIDCredential.fullName
  274. let email = appleIDCredential.email
  275. // Handle the user data as needed
  276. print("User ID: \(userIdentifier)")
  277. if let fullName = fullName {
  278. print("Full Name: \(fullName.givenName ?? "") \(fullName.familyName ?? "")")
  279. }
  280. if let email = email {
  281. print("Email: \(email)")
  282. }
  283. }
  284. }
  285. @available(iOS 13.0, *)
  286. func authorizationController(controller: ASAuthorizationController, didCompleteWithError error: Error) {
  287. // Handle sign-in errors
  288. print("Sign In with Apple failed: \(error.localizedDescription)")
  289. }
  290. // ASAuthorizationControllerPresentationContextProviding method
  291. @available(iOS 13.0, *)
  292. func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
  293. return view.window!
  294. }
  295. }