SwitchAccountViewModel.swift 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //
  2. // SwitchAccountViewModel.swift
  3. // JiaPeiManage
  4. //
  5. // Created by Ning.ge on 2023/7/31.
  6. //
  7. import UIKit
  8. import RxSwift
  9. class SwitchAccountViewModel: NSObject {
  10. // MARK: 服务属性
  11. private let loginService: LoginServiceType = LoginService(networking: LoginNetworking())
  12. private let itemsSubject = BehaviorSubject<[AccountInfo]>(value: [])
  13. //删除的PublishSubject
  14. let deleteButtonTapped = PublishSubject<Int>()
  15. var items: Observable<[AccountInfo]> {
  16. return itemsSubject.asObservable()
  17. }
  18. @objc weak var vc:SwitchAccountViewController? = nil
  19. // MARK: 数据
  20. var rows:[AccountInfo] = []
  21. override init() {
  22. super.init()
  23. }
  24. //获取用户并刷新
  25. func getAccountsRead(){
  26. self.rows = NYAccountManager.shared.getAccounts()
  27. itemsSubject.onNext(self.rows)//消息rx
  28. }
  29. //切换事件
  30. func switchAccount(row:Int){
  31. let userinfo = self.rows[row];
  32. if userinfo.idcard == LocalManager.userInfo.idcard {
  33. NYTips.showMsg(txt: "您已经登录")
  34. return
  35. }
  36. let action1 = QMUIAlertAction(title: "取消", style: .cancel, handler: nil)
  37. let action2 = QMUIAlertAction(title: "切换", style: .destructive) { [unowned self] vc, action in
  38. self.switchLogindo(userinfo: userinfo)
  39. }
  40. let alertController = QMUIAlertController(title: "提示", message: "确认切换身份?", preferredStyle: .alert)
  41. alertController.addAction(action1)
  42. alertController.addAction(action2)
  43. let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
  44. visualEffectView.qmui_foregroundColor = UIColor(255, 255, 255,0.7) // UIColorMakeWithRGBA(255, 255, 255, 0.7) // Use the default value in most cases, only demonstrate how to set here
  45. alertController.mainVisualEffectView = visualEffectView
  46. alertController.alertHeaderBackgroundColor = nil // Remove these background colors when you need to use the blur effect
  47. alertController.alertButtonBackgroundColor = nil
  48. alertController.showWith(animated: true)
  49. }
  50. //切换登录逻辑
  51. private func switchLogindo(userinfo:AccountInfo){
  52. //登录逻辑
  53. //保存用户信息
  54. let account:String = userinfo.idcard
  55. let password:String = userinfo.password
  56. let city:String = userinfo.dqbh
  57. let cityPower:String = userinfo.cityPower
  58. LocalManager.userInfo.userAccount = account
  59. LocalManager.userInfo.password = password
  60. LocalManager.userInfo.city = city
  61. LocalManager.userInfo.cityPower = cityPower
  62. //登录api RX 订阅 观察 销毁 三部曲
  63. NYTips.show()
  64. self.loginService.loginRequest(user_name: account, user_password: password,city: city)
  65. .subscribe(onSuccess: { userinfo in
  66. NYTips.hide()
  67. LocalManager.userInfo = userinfo
  68. LocalManager.userInfo.isLogin = true //设置已经登录
  69. print("登录成功:%@", userinfo)
  70. NYTips.showMsg(txt: "切换成功")
  71. //self.navigationController?.popViewController(animated: true)
  72. }, onError: { error in
  73. NYTips.hide()
  74. NYTips.showErr(txt: (error as! RequestError).errorDescription)
  75. print("%@",error)
  76. })
  77. .disposed(by: vc!.disposeBag)
  78. }
  79. //删除用户
  80. func deleteAccount(row:Int){
  81. let action1 = QMUIAlertAction(title: "取消", style: .cancel, handler: nil)
  82. let alertController = QMUIAlertController(title: "提示", message: "确认删除这个用户信息?", preferredStyle: .alert)
  83. let action2 = QMUIAlertAction(title: "删除", style: .default) { [unowned self] vc, action in
  84. let account = self.rows[row]
  85. if account.idcard == LocalManager.userInfo.idcard {
  86. //清除登录信息
  87. LocalManager.userInfo.userAccount = ""
  88. LocalManager.userInfo.password = ""
  89. }
  90. NYAccountManager.shared.deleteAccount(at: row)
  91. self.getAccountsRead()
  92. }
  93. alertController.addAction(action1)
  94. alertController.addAction(action2)
  95. let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark))
  96. visualEffectView.qmui_foregroundColor = UIColor(255, 255, 255,0.7) // UIColorMakeWithRGBA(255, 255, 255, 0.7) // Use the default value in most cases, only demonstrate how to set here
  97. alertController.mainVisualEffectView = visualEffectView
  98. alertController.alertHeaderBackgroundColor = nil // Remove these background colors when you need to use the blur effect
  99. alertController.alertButtonBackgroundColor = nil
  100. alertController.showWith(animated: true)
  101. print("deleteAccount2")
  102. }
  103. }
  104. //事件
  105. extension SwitchAccountViewModel:UITableViewDelegate {
  106. }