137 lines
4.6 KiB
Swift
137 lines
4.6 KiB
Swift
//
|
|
// LoginViewModel.swift
|
|
// QuickLocation
|
|
//
|
|
|
|
import Foundation
|
|
import RxSwift
|
|
import SwiftyUserDefaults
|
|
import AuthenticationServices
|
|
#if !targetEnvironment(simulator)
|
|
import GeYanSdk
|
|
#endif
|
|
|
|
enum LoginSessionHandler {
|
|
private static let disposeBag = DisposeBag()
|
|
|
|
static func login(type: String, data: [String: Any]) {
|
|
DLToast.showLoading()
|
|
UserService.login(type: type, data: data)
|
|
.delaySubscription(.seconds(1), scheduler: MainScheduler.instance)
|
|
.retry(1)
|
|
.subscribe(onNext: { response in
|
|
GroupIMService.shared.logout()
|
|
DLToast.dismiss()
|
|
guard let model = response.model else { return }
|
|
NotificationCenter.default.post(name: .invalidatePopupQueue, object: nil)
|
|
Defaults[\.loginToken] = model.token
|
|
DLToast.showSuccess(text: "登录成功") {
|
|
if let userId = model.uid {
|
|
MQTTService.shared.updateClientID("smartdrive_\(userId)")
|
|
}
|
|
NotificationCenter.default.post(name: .RefreshUserConfigNotification, object: nil)
|
|
if let nav = AppRouter.shared.navigationController, nav.viewControllers.count > 1 {
|
|
AppRouter.shared.popOrDismiss()
|
|
} else {
|
|
AppDelegate.shared.showMainViewController()
|
|
}
|
|
}
|
|
}, onError: { error in
|
|
DLToast.dismiss()
|
|
DLToast.showError(text: "登录失败,请重试")
|
|
}).disposed(by: disposeBag)
|
|
}
|
|
}
|
|
|
|
final class LoginViewModel: BaseViewModel {
|
|
|
|
/// 验证码接口返回值
|
|
var timestamp: String = ""
|
|
|
|
let oneClickLoginResult = PublishSubject<Bool>()
|
|
let loginSuccess = PublishSubject<Void>()
|
|
|
|
var onAppleLoginRequest: ((ASAuthorizationController) -> Void)?
|
|
|
|
#if !targetEnvironment(simulator)
|
|
lazy var gyAuthVM: GyAuthViewModel = {
|
|
let viewModel = GyAuthViewModel()
|
|
viewModel.statusBarStyle = .default
|
|
viewModel.userInterfaceStyle = 0
|
|
viewModel.pullAuthVCStyle = .modal
|
|
viewModel.clickAuthButtonBlock = {
|
|
return true
|
|
}
|
|
return viewModel
|
|
}()
|
|
#endif
|
|
|
|
/// 登录
|
|
func loginAction(type: String, data: [String : Any]) {
|
|
LoginSessionHandler.login(type: type, data: data)
|
|
}
|
|
|
|
func performGuestLogin() {
|
|
loginSuccess.onNext(())
|
|
}
|
|
|
|
#if !targetEnvironment(simulator)
|
|
func performOneTapLogin(from vc: UIViewController, contentView: OneTapLoginView) {
|
|
NotificationCenter.default.post(name: .pausePopupQueue, object: nil)
|
|
let subViews: [UIView] = [
|
|
contentView.phoneNumberLabel,
|
|
contentView.carrierLabel,
|
|
contentView.loginButton,
|
|
contentView.checkBox,
|
|
contentView.agreementLabel
|
|
]
|
|
GeYanSdk.oneTapLogin(vc, viewModel: gyAuthVM, contentView: contentView,
|
|
subViewList: subViews) { [weak self] result in
|
|
|
|
guard let dict = result as? [String: Any],
|
|
let code = dict["code"] as? Int else { return }
|
|
print(dict)
|
|
if code == 30000, // 成功
|
|
let gyuid = dict["gyuid"],
|
|
let token = dict["token"] {
|
|
|
|
// let opType: String = {
|
|
// switch dict["operatorType"] as? Int {
|
|
// case 1: return "CM"
|
|
// case 2: return "CU"
|
|
// case 3: return "CT"
|
|
// default: return ""
|
|
// }
|
|
// }()
|
|
GeYanSdk.closeAuthVC(true) {
|
|
self?.loginAction(type: "onekey", data: [
|
|
"gyuid": gyuid,
|
|
"token": token
|
|
])
|
|
}
|
|
}
|
|
else {
|
|
DLToast.show(text: dict["msg"] as? String ?? "运营商返回失败")
|
|
}
|
|
}
|
|
}
|
|
#endif
|
|
|
|
func performWechatLogin() {
|
|
NotificationCenter.default.post(name: .pausePopupQueue, object: nil)
|
|
let req = SendAuthReq()
|
|
req.scope = "snsapi_userinfo"
|
|
req.state = "login"
|
|
WXApi.send(req)
|
|
}
|
|
|
|
func performAppleLogin() {
|
|
NotificationCenter.default.post(name: .pausePopupQueue, object: nil)
|
|
let provider = ASAuthorizationAppleIDProvider()
|
|
let request = provider.createRequest()
|
|
request.requestedScopes = [.fullName, .email]
|
|
let controller = ASAuthorizationController(authorizationRequests: [request])
|
|
onAppleLoginRequest?(controller)
|
|
}
|
|
}
|