CNLiveLoginViewModel.swift
4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//
// CNLiveLoginViewModel.swift
// metaCode
//
// Created by open on 2023/6/5.
//
import Foundation
import CNLiveRequestBastKit
import HandyJSON
//验证码发送成功回调
typealias resultCodeBlock = () -> Void
//登陆成功回调
typealias resultLoginBlock = () -> Void
class CNLiveLoginViewModel : NSObject {
/// 获取验证码
/// - Parameters:
/// - phone: 手机号
/// - code: 验证码
/// - resultBlock: 登陆结果
static func requestVerCodeAction(phone: String, code: String, resultBlock: @escaping resultCodeBlock) {
if !phone.isNotBlank() {
showMessage(message: "请输入手机号")
return
}
if phone.lengthOfBytes(using: .utf8) >= 20 {
showMessage(message: "手机号格式不正确")
return
}
if !phone.isPurnInt() {
showMessage(message: "手机号格式不正确")
return
}
showLoading(message: "")
let param = ["mobile":phone,"countryCode":code]
let custom_param = NSMutableDictionary()
custom_param.setValue(APPId, forKey: "appId")
custom_param.setValue(APPKey, forKey: "appKey")
CNLiveNetworking.setupCustomParam(custom_param as? [String : String])
CNLiveNetworking.setAllowRequestDefaultArgument(true)
CNLiveNetworking.setupShowDataResult(false)
CNLiveNetworking.requestNetwork(with: .POST, urlString: APPLoginHost+"/open/api2/user/sendVerifyCode", param: param, cacheType: .networkOnly) { task, result, error in
hiddenLoading()
if (error == nil) {
let resp = result as! NSDictionary
let errorCode = resp["errorCode"]
if (errorCode as! String == "0") {
showMessage(message: "验证码已发送")
resultBlock()
} else {
showMessage(message: resp["errorMessage"] as! String)
}
} else{
showMessage(message: "获取验证码失败")
}
}
}
/// 登陆
/// - Parameters:
/// - userName: 手机号
/// - countryCode: 国家区号
/// - verCode: 验证码
/// - resultBlock: 结果回调
static func requestLoginAction(userName: String, countryCode: String, verCode: String, resultBlock: @escaping resultLoginBlock) {
if !userName.isNotBlank() {
showMessage(message: "请输入手机号")
return
}
if userName.lengthOfBytes(using: .utf8) >= 20 {
showMessage(message: "手机号格式不正确")
return
}
if !userName.isPurnInt() {
showMessage(message: "手机号格式不正确")
return
}
if !verCode.isNotBlank() {
showMessage(message: "请输入验证码")
return
}
if verCode.lengthOfBytes(using: .utf8) >= 20 {
showMessage(message: "验证码格式不正确")
return
}
if !verCode.isPurnInt() {
showMessage(message: "验证码格式不正确")
return
}
showLoading(message: "")
let param = ["userName":userName,"countryCode":countryCode,"verificationCode":verCode,"inviterId":""]
let custom_param = NSMutableDictionary()
custom_param.setValue(APPId_wjj, forKey: "appId")
custom_param.setValue(APPKey_wjj, forKey: "appKey")
CNLiveNetworking.setupCustomParam(custom_param as? [String : String])
CNLiveNetworking.setAllowRequestDefaultArgument(true)
CNLiveNetworking.setupShowDataResult(false)
CNLiveNetworking.requestNetwork(with: .POST, urlString: APPLoginHost+"/open/api2/user/loginInMetaCode", param: param, cacheType: .networkOnly) { task, result, error in
hiddenLoading()
if (error == nil) {
let resp = result as! NSDictionary
let errorCode = resp["errorCode"]
if (errorCode as! String == "0") {
if resp.allKeys.contains(where: { item in
return item as! String == "data"
}) {
let userInfoModel = dictionaryToModel(resp as! [String : Any], UserInfoModel.self,"data")
UserInfoManager.cacheUserInfo(userInfo: userInfoModel as! UserInfoModel)
UserDefaults.standard.set(true,forKey: "is_login")
resultBlock()
}
} else {
showMessage(message: resp["errorMessage"] as! String)
}
} else {
showMessage(message: "登陆失败")
}
}
}
}