// // SwitchAccountViewModel.swift // JiaPeiManage // // Created by Ning.ge on 2023/7/31. // import UIKit import RxSwift class SwitchAccountViewModel: NSObject { // MARK: 服务属性 private let loginService: LoginServiceType = LoginService(networking: LoginNetworking()) private let itemsSubject = BehaviorSubject<[AccountInfo]>(value: []) //删除的PublishSubject let deleteButtonTapped = PublishSubject() var items: Observable<[AccountInfo]> { return itemsSubject.asObservable() } @objc weak var vc:SwitchAccountViewController? = nil // MARK: 数据 var rows:[AccountInfo] = [] override init() { super.init() } //获取用户并刷新 func getAccountsRead(){ self.rows = NYAccountManager.shared.getAccounts() itemsSubject.onNext(self.rows)//消息rx } //切换事件 func switchAccount(row:Int){ let userinfo = self.rows[row]; if userinfo.idcard == LocalManager.userInfo.idcard { NYTips.showMsg(txt: "您已经登录") return } let action1 = QMUIAlertAction(title: "取消", style: .cancel, handler: nil) let action2 = QMUIAlertAction(title: "切换", style: .destructive) { [unowned self] vc, action in self.switchLogindo(userinfo: userinfo) } let alertController = QMUIAlertController(title: "提示", message: "确认切换身份?", preferredStyle: .alert) alertController.addAction(action1) alertController.addAction(action2) let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark)) 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 alertController.mainVisualEffectView = visualEffectView alertController.alertHeaderBackgroundColor = nil // Remove these background colors when you need to use the blur effect alertController.alertButtonBackgroundColor = nil alertController.showWith(animated: true) } //切换登录逻辑 private func switchLogindo(userinfo:AccountInfo){ //登录逻辑 //保存用户信息 let account:String = userinfo.idcard let password:String = userinfo.password let city:String = userinfo.dqbh let cityPower:String = userinfo.cityPower LocalManager.userInfo.userAccount = account LocalManager.userInfo.password = password LocalManager.userInfo.city = city LocalManager.userInfo.cityPower = cityPower //登录api RX 订阅 观察 销毁 三部曲 NYTips.show() self.loginService.loginRequest(user_name: account, user_password: password,city: city) .subscribe(onSuccess: { userinfo in NYTips.hide() LocalManager.userInfo = userinfo LocalManager.userInfo.isLogin = true //设置已经登录 print("登录成功:%@", userinfo) NYTips.showMsg(txt: "切换成功") //self.navigationController?.popViewController(animated: true) }, onError: { error in NYTips.hide() NYTips.showErr(txt: (error as! RequestError).errorDescription) print("%@",error) }) .disposed(by: vc!.disposeBag) } //删除用户 func deleteAccount(row:Int){ let action1 = QMUIAlertAction(title: "取消", style: .cancel, handler: nil) let alertController = QMUIAlertController(title: "提示", message: "确认删除这个用户信息?", preferredStyle: .alert) let action2 = QMUIAlertAction(title: "删除", style: .default) { [unowned self] vc, action in let account = self.rows[row] if account.idcard == LocalManager.userInfo.idcard { //清除登录信息 LocalManager.userInfo.userAccount = "" LocalManager.userInfo.password = "" } NYAccountManager.shared.deleteAccount(at: row) self.getAccountsRead() } alertController.addAction(action1) alertController.addAction(action2) let visualEffectView = UIVisualEffectView(effect: UIBlurEffect(style: .dark)) 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 alertController.mainVisualEffectView = visualEffectView alertController.alertHeaderBackgroundColor = nil // Remove these background colors when you need to use the blur effect alertController.alertButtonBackgroundColor = nil alertController.showWith(animated: true) print("deleteAccount2") } } //事件 extension SwitchAccountViewModel:UITableViewDelegate { }