LoginViewController.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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 LocalManager.userInfo.ios_login_open == 1 { //关闭审核状态
  131. if WXApi.isWXAppInstalled() {
  132. self.getCityArray(type: 1)
  133. }else {
  134. NYTips.showMsg(txt: "本地未安装微信")
  135. }
  136. }else{
  137. //打开审核
  138. self.getCityArray(type: 1)
  139. }
  140. }).disposed(by: disposeBag)
  141. //apple id
  142. apple_button.rx.tap.subscribe ({ [unowned self] (_) in
  143. if #available(iOS 13.0, *) {
  144. self.getCityArray(type: 2)
  145. } else {
  146. // Fallback on earlier versions
  147. NYTips.showMsg(txt: "请升级到iOS13.0以上")
  148. }
  149. }).disposed(by: disposeBag)
  150. //登录
  151. login_button.rx.tap
  152. .asObservable()
  153. .withLatestFrom(loginObserver)
  154. .do(onNext: {
  155. [unowned self]_ in
  156. self.login_button.isEnabled = false
  157. self.view.endEditing(true)
  158. })
  159. .subscribe(onNext: {[unowned self]isLogin in
  160. NSLog("登录 - 登录: login %@","123")
  161. self.login_button.isEnabled = true
  162. //保存用户信息
  163. var account:String = self.username_textField.text!
  164. var password:String = self.userpassword_textField.text!
  165. if city=="" {
  166. NYTips.showMsg(txt: "请选择地区!")
  167. return
  168. }
  169. LocalManager.userInfo.userAccount = account
  170. LocalManager.userInfo.password = password
  171. LocalManager.userInfo.city = city
  172. LocalManager.userInfo.cityPower = cityPower
  173. //登录api RX 订阅 观察 销毁 三部曲
  174. NYTips.show()
  175. self.loginService.loginRequest(user_name: account, user_password: password,city: city)
  176. .subscribe(onSuccess: { userinfo in
  177. NYTips.hide()
  178. LocalManager.userInfo = userinfo
  179. LocalManager.userInfo.isLogin = true //设置已经登录
  180. NYAccountManager.shared.addUserinfo(user: userinfo)//添加用户信息
  181. print("登录成功:%@", userinfo)
  182. self.dismiss(animated: true)
  183. }, onError: { error in
  184. NYTips.hide()
  185. NYTips.showErr(txt: (error as! RequestError).errorDescription)
  186. print("%@",error)
  187. })
  188. .disposed(by: disposeBag)
  189. })
  190. .disposed(by: disposeBag)
  191. //获取wx绑定
  192. NYWXApiManager.shared.blockSubject.subscribe(onNext: {[unowned self] code in
  193. NYTips.show()
  194. self.loginService.authorizationRequest(code: code, city: NYWXApiManager.shared.city).subscribe(onSuccess: {[unowned self] model in
  195. NYTips.hide()
  196. //判断是否有 openid
  197. let data = model.data as AuthorizationDataInfoModel
  198. if data.coach != nil {
  199. //已经绑定直接登录
  200. LocalManager.userInfo.userAccount = data.coach?.idcard
  201. LocalManager.userInfo.password = data.coach?.password
  202. LocalManager.userInfo.city = self.city
  203. LocalManager.userInfo.cityPower = self.cityPower
  204. LocalManager.userInfo = data.coach!
  205. LocalManager.userInfo.isLogin = true //设置已经登录
  206. NYAccountManager.shared.addUserinfo(user: data.coach!)//添加用户信息
  207. self.dismiss(animated: true)
  208. }else{
  209. //未绑定-跳转绑定
  210. // NYSwRouter.push(NYSwPushType.login_biand_wx,context: data.openid)
  211. let bindingViewController = BindingViewController(nibName: "BindingViewController")
  212. bindingViewController.openid = data.openid
  213. self.addChild(bindingViewController)
  214. self.view.addSubview(bindingViewController.view)
  215. }
  216. }, onError: { error in
  217. NYTips.hide()
  218. //NYTips.showErr(txt: (error as! RequestError).errorDescription)
  219. print("%@",error)
  220. })
  221. .disposed(by: self.disposeBag)
  222. }).disposed(by: disposeBag)
  223. //绑定apple
  224. NYAppleApiManager.shared.blockSubject.subscribe(onNext: {[unowned self] array in
  225. NYTips.show()
  226. self.loginService.authorizationIosRequest(userId: array[0], identityToken: array[1], city: NYAppleApiManager.shared.city).subscribe(onSuccess: {[unowned self] model in
  227. NYTips.hide()
  228. //判断是否有 openid
  229. let data = model.data as AuthorizationDataInfoModel
  230. if data.coach != nil {
  231. //已经绑定直接登录
  232. LocalManager.userInfo.userAccount = data.coach?.idcard
  233. LocalManager.userInfo.password = data.coach?.password
  234. LocalManager.userInfo.city = self.city
  235. LocalManager.userInfo.cityPower = self.cityPower
  236. LocalManager.userInfo = data.coach!
  237. LocalManager.userInfo.isLogin = true //设置已经登录
  238. NYAccountManager.shared.addUserinfo(user: data.coach!)//添加用户信息
  239. self.dismiss(animated: true)
  240. }else{
  241. let bindingViewController = BindingViewController(nibName: "BindingViewController")
  242. bindingViewController.bindType = 1
  243. bindingViewController.openid = data.openid
  244. self.addChild(bindingViewController)
  245. self.view.addSubview(bindingViewController.view)
  246. }
  247. }, onError: { error in
  248. NYTips.hide()
  249. //NYTips.showErr(txt: (error as! RequestError).errorDescription)
  250. print("%@",error)
  251. })
  252. .disposed(by: self.disposeBag)
  253. }).disposed(by: disposeBag)
  254. //添加手势
  255. let tapBackground = UITapGestureRecognizer()
  256. tapBackground.rx.event
  257. .subscribe(onNext: { [weak self] _ in
  258. self?.view.endEditing(true)
  259. })
  260. .disposed(by: disposeBag)
  261. view.addGestureRecognizer(tapBackground)
  262. }
  263. func getCityArray(type:Int){
  264. if self.cityItems.count == 0 {
  265. NYTips.show()
  266. //获取字典数据
  267. loginService.dictRequest(dictType: "coach_city").subscribe(onSuccess: { [unowned self] cityItems in
  268. self.cityItems = cityItems
  269. for cityItem in cityItems {
  270. self.citys[cityItem.dictLabel] = cityItem.dictValue
  271. }
  272. NYTips.hide()
  273. self.showDialog(type: type)
  274. },onError: { Error in
  275. NYTips.hide()
  276. }).disposed(by: disposeBag)
  277. }else{
  278. self.showDialog(type: type)
  279. }
  280. }
  281. func showDialog(type:Int){
  282. self.type = type
  283. let dialogViewController = QMUIDialogSelectionViewController()
  284. dialogViewController.title = "请选择地区"
  285. dialogViewController.items = Array(citys.keys)
  286. dialogViewController.selectedItemIndex = selectedItemIndex
  287. dialogViewController.addCancelButton(withText: "取消", block: nil)
  288. dialogViewController.addSubmitButton(withText: "确定") { [unowned self] dialogViewController in
  289. guard let dialogViewController:QMUIDialogSelectionViewController = dialogViewController as? QMUIDialogSelectionViewController else { return }
  290. if dialogViewController.selectedItemIndex == QMUIDialogSelectionViewControllerSelectedItemIndexNone {
  291. NYTips.showErr(txt: "请至少选一个")
  292. return
  293. }
  294. if dialogViewController.items?.count == 0 {return}
  295. let ItemIndex = dialogViewController.selectedItemIndex
  296. self.selectedItemIndex = ItemIndex
  297. let keyname = dialogViewController.items![self.selectedItemIndex]
  298. // self.citys[cityItem.dictLabel] = cityItem.dictValue
  299. var item:CityItem?
  300. for cityItem in self.cityItems {
  301. if cityItem.dictLabel == keyname {
  302. item = cityItem
  303. break
  304. }
  305. }
  306. self.city = item!.dictValue //编码
  307. self.cityPower = item!.remark //地区权重
  308. if (self.type==1){//微信登录
  309. let city = item!.dictValue //编码
  310. NYWXApiManager.shared.sendWxapi(city: city)
  311. dialogViewController.hide()
  312. return
  313. }
  314. if(self.type==2){//apple登录
  315. NYAppleApiManager.shared.performSignIn(city:city)
  316. dialogViewController.hide()
  317. return
  318. }
  319. self.userlocation_textField.text = item?.dictLabel
  320. dialogViewController.hide()
  321. }
  322. dialogViewController.show()
  323. }
  324. }