LoginViewController.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. final class LoginViewController: BaseViewController {
  12. // MARK: 服务属性
  13. private let loginService: LoginServiceType = LoginService(networking: LoginNetworking())
  14. // MARK: UI属性
  15. @IBOutlet weak var wx_btn_layout_L: NSLayoutConstraint!
  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. LocalManager.loginService.dictRequest(dictType: "coach_version").subscribe(onSuccess: { cityItems in
  60. },onError: { Error in
  61. }).disposed(by: disposeBag)
  62. }
  63. // MARK: 私有方法
  64. func setupUI(){
  65. self.view.backgroundColor = .db_theme
  66. if ((LocalManager.userInfo.userAccount?.isEmpty) != nil) {
  67. self.username_textField.text = LocalManager.userInfo.userAccount
  68. }
  69. if ((LocalManager.userInfo.password?.isEmpty) != nil) {
  70. self.userpassword_textField.text = LocalManager.userInfo.password
  71. }
  72. self.username_textField.placeholderColor = .db_place
  73. self.userpassword_textField.placeholderColor = .db_place
  74. self.userlocation_textField.placeholderColor = .db_place
  75. if LocalManager.userInfo.ios_login_open == 0 {
  76. self.wx_btn_layout_L.constant = 21.f
  77. self.apple_button.isHidden = true
  78. }
  79. }
  80. func biandView(){
  81. //判断账号的输入是否可用
  82. let accountValid = username_textField.rx.text.orEmpty.map{ value in
  83. return value.count >= 18
  84. }
  85. //判断密码的输入是否可用
  86. let passwordValid = userpassword_textField.rx.text.orEmpty.map{ value in
  87. return value.count >= 6
  88. }
  89. //判断同意
  90. let agreeValid = agree_button.rx.tap.map { [unowned self] in
  91. NSLog("agreeValid")
  92. return self.agree_button.isSelected
  93. }
  94. //同意
  95. agree_button.rx.tap.subscribe ({ [unowned self] (_) in
  96. NSLog("tap.subscribe")
  97. self.agree_button.isSelected = !self.agree_button.isSelected
  98. }).disposed(by: disposeBag)
  99. //登录按钮的可用与否
  100. let loginObserver = Observable.combineLatest(accountValid,passwordValid,agreeValid){(account,password,agree) in
  101. account && password && agree
  102. }
  103. //绑定按钮
  104. loginObserver.bind(to: login_button.rx.isEnabled).disposed(by: disposeBag)
  105. loginObserver.subscribe(onNext: { [unowned self] valid in
  106. NSLog("loginObserver")
  107. self.login_button.alpha = valid ? 1 : 0.5
  108. }).disposed(by: disposeBag)
  109. //用户协议
  110. userprotocol_button.rx.tap.subscribe ({ [unowned self] (_) in
  111. //跳web
  112. navigator.push("https://ys.zzxcx.net/xy_jlgj.html")
  113. }).disposed(by: disposeBag)
  114. //隐私
  115. userprivacy_button.rx.tap.subscribe ({ [unowned self] (_) in
  116. //跳web
  117. navigator.push("https://ys.zzxcx.net/xy_jlgj.html")
  118. }).disposed(by: disposeBag)
  119. //显示密码
  120. pwdshow_button.rx.tap.subscribe ({ [unowned self] (_) in
  121. self.pwdshow_button.isSelected = !self.pwdshow_button.isSelected;
  122. self.userpassword_textField.isSecureTextEntry = !self.pwdshow_button.isSelected
  123. }).disposed(by: disposeBag)
  124. //选择区域
  125. location_button.rx.tap.subscribe ({ [unowned self] (_) in
  126. NSLog("点击了:选择区域- \(Thread.isMainThread)")
  127. self.showDialog(type: 0)
  128. }).disposed(by: disposeBag)
  129. location_button_btn.rx.tap.subscribe ({ [unowned self] (_) in
  130. self.showDialog(type: 0)
  131. }).disposed(by: disposeBag)
  132. //wxin_button
  133. wxin_button.rx.tap.subscribe ({ [unowned self] (_) in
  134. if WXApi.isWXAppInstalled() {
  135. self.showDialog(type: 1)
  136. }else {
  137. NYTips.showMsg(txt: "本地未安装微信")
  138. }
  139. }).disposed(by: disposeBag)
  140. //apple id
  141. apple_button.rx.tap.subscribe ({ [unowned self] (_) in
  142. if #available(iOS 13.0, *) {
  143. self.showDialog(type: 2)
  144. } else {
  145. // Fallback on earlier versions
  146. NYTips.showMsg(txt: "请升级到iOS13.0以上")
  147. }
  148. }).disposed(by: disposeBag)
  149. //登录
  150. login_button.rx.tap
  151. .asObservable()
  152. .withLatestFrom(loginObserver)
  153. .do(onNext: {
  154. [unowned self]_ in
  155. self.login_button.isEnabled = false
  156. self.view.endEditing(true)
  157. })
  158. .subscribe(onNext: {[unowned self]isLogin in
  159. NSLog("登录 - 登录: login %@","123")
  160. self.login_button.isEnabled = true
  161. //保存用户信息
  162. var account:String = self.username_textField.text!
  163. var password:String = self.userpassword_textField.text!
  164. if city=="" {
  165. NYTips.showMsg(txt: "请选择地区!")
  166. return
  167. }
  168. LocalManager.userInfo.userAccount = account
  169. LocalManager.userInfo.password = password
  170. LocalManager.userInfo.city = city
  171. LocalManager.userInfo.cityPower = cityPower
  172. //登录api RX 订阅 观察 销毁 三部曲
  173. NYTips.show()
  174. self.loginService.loginRequest(user_name: account, user_password: password,city: city)
  175. .subscribe(onSuccess: { userinfo in
  176. NYTips.hide()
  177. LocalManager.userInfo = userinfo
  178. LocalManager.userInfo.isLogin = true //设置已经登录
  179. NYAccountManager.shared.addUserinfo(user: userinfo)//添加用户信息
  180. print("登录成功:%@", userinfo)
  181. self.dismiss(animated: true)
  182. }, onError: { error in
  183. NYTips.hide()
  184. NYTips.showErr(txt: (error as! RequestError).errorDescription)
  185. print("%@",error)
  186. })
  187. .disposed(by: disposeBag)
  188. })
  189. .disposed(by: disposeBag)
  190. //获取wx绑定
  191. NYWXApiManager.shared.blockSubject.subscribe(onNext: {[unowned self] code in
  192. NYTips.show()
  193. self.loginService.authorizationRequest(code: code, city: NYWXApiManager.shared.city).subscribe(onSuccess: {[unowned self] model in
  194. NYTips.hide()
  195. //判断是否有 openid
  196. let data = model.data as AuthorizationDataInfoModel
  197. if data.coach != nil {
  198. //已经绑定直接登录
  199. LocalManager.userInfo.userAccount = data.coach?.idcard
  200. LocalManager.userInfo.password = data.coach?.password
  201. LocalManager.userInfo.city = self.city
  202. LocalManager.userInfo.cityPower = self.cityPower
  203. LocalManager.userInfo = data.coach!
  204. LocalManager.userInfo.isLogin = true //设置已经登录
  205. NYAccountManager.shared.addUserinfo(user: data.coach!)//添加用户信息
  206. self.dismiss(animated: true)
  207. }else{
  208. //未绑定-跳转绑定
  209. // NYSwRouter.push(NYSwPushType.login_biand_wx,context: data.openid)
  210. let bindingViewController = BindingViewController(nibName: "BindingViewController")
  211. bindingViewController.openid = data.openid
  212. self.addChild(bindingViewController)
  213. self.view.addSubview(bindingViewController.view)
  214. }
  215. }, onError: { error in
  216. NYTips.hide()
  217. //NYTips.showErr(txt: (error as! RequestError).errorDescription)
  218. print("%@",error)
  219. })
  220. .disposed(by: self.disposeBag)
  221. }).disposed(by: disposeBag)
  222. //绑定apple
  223. NYAppleApiManager.shared.blockSubject.subscribe(onNext: {[unowned self] array in
  224. NYTips.show()
  225. self.loginService.authorizationIosRequest(userId: array[0], identityToken: array[1], city: NYAppleApiManager.shared.city).subscribe(onSuccess: {[unowned self] model in
  226. NYTips.hide()
  227. //判断是否有 openid
  228. let data = model.data as AuthorizationDataInfoModel
  229. if data.coach != nil {
  230. //已经绑定直接登录
  231. LocalManager.userInfo.userAccount = data.coach?.idcard
  232. LocalManager.userInfo.password = data.coach?.password
  233. LocalManager.userInfo.city = self.city
  234. LocalManager.userInfo.cityPower = self.cityPower
  235. LocalManager.userInfo = data.coach!
  236. LocalManager.userInfo.isLogin = true //设置已经登录
  237. NYAccountManager.shared.addUserinfo(user: data.coach!)//添加用户信息
  238. self.dismiss(animated: true)
  239. }else{
  240. let bindingViewController = BindingViewController(nibName: "BindingViewController")
  241. bindingViewController.bindType = 1
  242. bindingViewController.openid = data.openid
  243. self.addChild(bindingViewController)
  244. self.view.addSubview(bindingViewController.view)
  245. }
  246. }, onError: { error in
  247. NYTips.hide()
  248. //NYTips.showErr(txt: (error as! RequestError).errorDescription)
  249. print("%@",error)
  250. })
  251. .disposed(by: self.disposeBag)
  252. }).disposed(by: disposeBag)
  253. //添加手势
  254. let tapBackground = UITapGestureRecognizer()
  255. tapBackground.rx.event
  256. .subscribe(onNext: { [weak self] _ in
  257. self?.view.endEditing(true)
  258. })
  259. .disposed(by: disposeBag)
  260. view.addGestureRecognizer(tapBackground)
  261. }
  262. func showDialog(type:Int){
  263. self.type = type
  264. let dialogViewController = QMUIDialogSelectionViewController()
  265. dialogViewController.title = "请选择地区"
  266. dialogViewController.items = Array(citys.keys)
  267. dialogViewController.selectedItemIndex = selectedItemIndex
  268. dialogViewController.addCancelButton(withText: "取消", block: nil)
  269. dialogViewController.addSubmitButton(withText: "确定") { [unowned self] dialogViewController in
  270. guard let dialogViewController:QMUIDialogSelectionViewController = dialogViewController as? QMUIDialogSelectionViewController else { return }
  271. if dialogViewController.selectedItemIndex == QMUIDialogSelectionViewControllerSelectedItemIndexNone {
  272. NYTips.showErr(txt: "请至少选一个")
  273. return
  274. }
  275. if dialogViewController.items?.count == 0 {return}
  276. let ItemIndex = dialogViewController.selectedItemIndex
  277. self.selectedItemIndex = ItemIndex
  278. let keyname = dialogViewController.items![self.selectedItemIndex]
  279. // self.citys[cityItem.dictLabel] = cityItem.dictValue
  280. var item:CityItem?
  281. for cityItem in self.cityItems {
  282. if cityItem.dictLabel == keyname {
  283. item = cityItem
  284. break
  285. }
  286. }
  287. self.city = item!.dictValue //编码
  288. self.cityPower = item!.remark //地区权重
  289. if (self.type==1){//微信登录
  290. let city = item!.dictValue //编码
  291. NYWXApiManager.shared.sendWxapi(city: city)
  292. dialogViewController.hide()
  293. return
  294. }
  295. if(self.type==2){//apple登录
  296. NYAppleApiManager.shared.performSignIn(city:city)
  297. dialogViewController.hide()
  298. return
  299. }
  300. self.userlocation_textField.text = item?.dictLabel
  301. dialogViewController.hide()
  302. }
  303. dialogViewController.show()
  304. }
  305. }