Files
yingmeng-ios-switf/yinmeng-ios/Modules/Auth/VM/AuthViewModel.swift

182 lines
5.7 KiB
Swift
Raw Normal View History

2024-02-24 13:49:51 +08:00
//
// AuthViewModel.swift
// yinmeng-ios
//
// Created by MaiMang on 2024/2/22.
//
import Foundation
import RxSwift
2024-02-29 23:49:12 +08:00
import NIMSDK
2024-03-04 14:14:33 +08:00
import MBProgressHUD
2024-02-24 13:49:51 +08:00
class AuthViewModel: NSObject {
2024-02-25 12:53:56 +08:00
static let authVM = AuthViewModel.init()
2024-02-24 13:49:51 +08:00
let data = PublishSubject<Bool>()
2024-02-25 12:53:56 +08:00
let token = PublishSubject<UserTokenObject?>()
2024-02-25 23:11:02 +08:00
let loginSuccess = BehaviorSubject(value: false)
let resetPwd = PublishSubject<Bool>()
2024-02-24 13:49:51 +08:00
2024-02-25 23:11:02 +08:00
func appClientConfig() {
RequestGet(path: "client/init", parma: [:]) { data in
} fail: { code, message in
}
}
2024-02-29 23:49:12 +08:00
func logout() {
if NIMSDK.shared().loginManager.isLogined() {
NIMSDK.shared().loginManager.logout()
}
let token = LoginTokenConfig.config.getAccountInfo()?.access_token
LoginTokenConfig.config.removeTicketFromLoaction()
LoginTokenConfig.config.removeTicketFromLoaction()
if let token = token {
let params = ["auaccess_token": token]
RequestPost(path: "acc/logout", parma: params) { data in
print("你好")
} fail: { code, msg in
print("de")
}
}
self.loginSuccess.onNext(false)
}
2024-02-25 23:11:02 +08:00
///
2024-02-24 13:49:51 +08:00
func getSmsCode(phone:String, type:Int) {
if let phoneDes = phone.encrypt() {
let params:[String: Any] = ["mobile":phoneDes, "type":type]
2024-02-25 12:53:56 +08:00
RequestPost(path: "sms/getCode", parma: params) { data in
self.data.onNext(true)
} fail: { code, message in
2024-02-24 13:49:51 +08:00
self.data.onNext(true)
2024-02-25 12:53:56 +08:00
}
2024-02-24 13:49:51 +08:00
}
}
2024-02-25 23:11:02 +08:00
///
2024-02-24 13:49:51 +08:00
func authPhoneCode(phone:String, code:String) {
2024-02-25 12:53:56 +08:00
if let phoneDes = phone.encrypt() {
2024-03-04 14:14:33 +08:00
MBProgressHUD.showAdded(to: UIApplication.shared.keyWindow!, animated: true)
2024-02-25 12:53:56 +08:00
let params = ["phone":phoneDes, "code":code, "client_secret":"uyzjdhds", "version":"1", "client_id":"erban-client", "grant_type":"password"]
RequestPost(path: "oauth/token", parma: params) { data in
2024-03-04 14:14:33 +08:00
if let window = UIApplication.shared.keyWindow{
MBProgressHUD.hide(for: window, animated: true)
}
2024-02-25 12:53:56 +08:00
if let account = Deserialized<UserTokenObject>.toModel(with: data) {
LoginTokenConfig.config.saveTokenToLocaltion(token: account)
self.token.onNext(account)
self.token.onCompleted()
///token
if let accessToken = account.access_token {
self.tokenTransformTicket(accesToken: accessToken)
}
}
} fail: { code, data in
2024-03-04 14:14:33 +08:00
if let window = UIApplication.shared.keyWindow{
MBProgressHUD.hide(for: window, animated: true)
}
HUDTool.show(with: data)
2024-02-25 12:53:56 +08:00
self.token.onNext(nil)
self.token.onCompleted()
}
}
}
2024-02-25 23:11:02 +08:00
///ID
func authIDPassword(account:String, password: String) {
if let phoneDes = account.encrypt(), let pwdDes = password.encrypt() {
2024-03-04 14:14:33 +08:00
MBProgressHUD.showAdded(to: UIApplication.shared.keyWindow!, animated: true)
2024-02-25 23:11:02 +08:00
let params = ["phone":phoneDes, "password":pwdDes, "client_secret":"uyzjdhds", "version":"1", "client_id":"erban-client", "grant_type":"password"]
RequestPost(path: "oauth/token", parma: params) { data in
2024-03-04 14:14:33 +08:00
if let window = UIApplication.shared.keyWindow{
MBProgressHUD.hide(for: window, animated: true)
}
2024-02-25 23:11:02 +08:00
if let account = Deserialized<UserTokenObject>.toModel(with: data) {
LoginTokenConfig.config.saveTokenToLocaltion(token: account)
self.token.onNext(account)
self.token.onCompleted()
///token
if let accessToken = account.access_token {
self.tokenTransformTicket(accesToken: accessToken)
}
}
} fail: { code, data in
2024-03-04 14:14:33 +08:00
if let window = UIApplication.shared.keyWindow{
MBProgressHUD.hide(for: window, animated: true)
}
HUDTool.show(with: data)
2024-02-25 23:11:02 +08:00
self.token.onNext(nil)
self.token.onCompleted()
}
}
}
2024-02-29 00:42:41 +08:00
func authApple(params:[String: Any]) {
var par = params
par.updateValue("5", forKey: "type")
RequestPost(path: "acc/third/login", parma: par) { data in
if let account = Deserialized<UserTokenObject>.toModel(with: data) {
LoginTokenConfig.config.saveTokenToLocaltion(token: account)
self.token.onNext(account)
self.token.onCompleted()
///token
if let accessToken = account.access_token {
self.tokenTransformTicket(accesToken: accessToken)
}
}
} fail: { code, msg in
self.token.onNext(nil)
self.token.onCompleted()
}
}
2024-02-25 12:53:56 +08:00
///tokenticket
func tokenTransformTicket(accesToken:String) {
let params = ["access_token": accesToken, "issue_type": "multi"]
RequestPost(path: "oauth/ticket", parma: params) { data in
///
if let dic = data as? [String: Any], let tickets = dic["tickets"] as? [[String: Any]], let ticket1 = tickets[safe: 0], let ticket = ticket1["ticket"] as? String{
LoginTokenConfig.config.saveTicketToLoaction(ticket: ticket)
self.loginSuccess.onNext(true)
} else {
self.loginSuccess.onNext(false)
}
} fail: { code, message in
self.loginSuccess.onNext(false)
2024-02-25 23:11:02 +08:00
}
}
///
func resetPassword(phone:String, newPwd:String, smsCode:String) {
if let phoneDes = phone.encrypt(), let pwdDes = newPwd.encrypt() {
let params = ["phone":phoneDes, "newPwd":pwdDes, "smsCode": smsCode]
RequestPost(path: "acc/pwd/reset", parma: params) { data in
self.resetPwd.onNext(true)
} fail: { code, message in
self.resetPwd.onNext(false)
}
2024-02-25 12:53:56 +08:00
}
}
func checkUserIsLogin() {
if let account = LoginTokenConfig.config.getAccountInfo(), let token = account.access_token {
let ticket = AuthManager.ticket
if ticket.count > 0 {
///
self.loginSuccess.onNext(true)
} else {
/// ticket ticket
tokenTransformTicket(accesToken: token)
}
} else {
self.loginSuccess.onNext(false)
}
2024-02-24 13:49:51 +08:00
}
}