LoginViewController.swift 15 KB

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