杨锴
2024-09-19 642175113bf6f2c90894e689dacda50278cad570
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
//
//  LoginVC.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/13.
//
 
import UIKit
import JQTools
import RxRelay
import QMUIKit
 
 
class LoginViewModel{
                var loginType = BehaviorRelay<LoginType>(value:.pwd)
                var loginPhone = BehaviorRelay<String>(value:"")
                var loginContent = BehaviorRelay<String>(value:"")
 
 
                func checkSafe()->Bool{
                                if loginPhone.value.isEmpty{
                                                alert(msg: "请输入您的手机号");return false
                                }
 
                                if loginContent.value.isEmpty{
                                                switch loginType.value {
                                                                case .code:alert(msg: "请输入您的验证码")
                                                                case .pwd:alert(msg: "请输入您的密码")
                                                }
                                                return false
                                }
 
                                if !loginPhone.value.jq_isPhone{
                                                alert(msg: "请输入正确的手机号");return false
                                }
 
                                if loginContent.value.count != 6 && loginType.value == .code{
                                                alertError(msg: "请输入6位验证码");return false
                                }
 
                                return true
                }
}
 
class LoginVC: BaseVC {
 
                @IBOutlet weak var btn_loginByPwd: UIButton!
                @IBOutlet weak var btn_loginByCode: UIButton!
                @IBOutlet weak var tf_phone: QMUITextField!
                @IBOutlet weak var tf_content: QMUITextField!
                @IBOutlet weak var btn_eye: UIButton!
                @IBOutlet weak var view_content: UIView!
                @IBOutlet weak var btn_register: UIButton!
                @IBOutlet weak var btn_login: UIButton!
                @IBOutlet weak var btn_sendCode: UIButton!
                @IBOutlet weak var btn_isRead: UIButton!
                
 
                private var viewModel = LoginViewModel()
 
                private let unlineImageView = UIImageView(image: UIImage(named: "icon_unline"))
 
 
                override func viewDidLoad() {
        super.viewDidLoad()
    }
 
                override func setRx() {
                                tf_phone.rx.text.orEmpty.bind(to: viewModel.loginPhone).disposed(by: disposeBag)
                                tf_content.rx.text.orEmpty.bind(to: viewModel.loginContent).disposed(by: disposeBag)
 
                                viewModel.loginType.subscribe(onNext: { type in
                                                UIView.animate(withDuration: 0.3) {[unowned self] in
                                                                self.tf_content.text = ""
                                                                self.tf_content.resignFirstResponder()
                                                                self.btn_eye.isHidden = type == .code
                                                                self.btn_sendCode.isHidden = type == .pwd
                                                                switch type {
                                                                                case .code:
                                                                                                self.tf_content.placeholder = "请输入验证码"
                                                                                                self.tf_content.keyboardType = .numberPad
                                                                                                self.tf_content.maximumTextLength = 6
                                                                                                self.tf_content.isSecureTextEntry = false
                                                                                                self.btn_loginByPwd.transform = .init(scaleX: 0.8, y: 0.8)
                                                                                                self.btn_loginByPwd.alpha = 0.5
                                                                                                self.btn_loginByCode.transform = .init(scaleX: 1, y: 1)
                                                                                                self.btn_loginByCode.alpha = 1
                                                                                                self.unlineImageView.snp.remakeConstraints { make in
                                                                                                                make.top.equalTo(self.btn_loginByPwd.snp.bottom).offset(6)
                                                                                                                make.centerX.equalTo(self.btn_loginByCode)
                                                                                                }
                                                                                case .pwd:
                                                                                                self.tf_content.placeholder = "请输入密码"
                                                                                                self.tf_content.maximumTextLength = 30
                                                                                                self.tf_content.isSecureTextEntry = true
                                                                                                self.tf_content.keyboardType = .asciiCapable
                                                                                                self.btn_loginByPwd.transform = .init(scaleX: 1, y: 1)
                                                                                                self.btn_loginByPwd.alpha = 1
                                                                                                self.btn_loginByCode.transform = .init(scaleX: 0.8, y: 0.8)
                                                                                                self.btn_loginByCode.alpha = 0.5
                                                                                                self.unlineImageView.snp.remakeConstraints { make in
                                                                                                                make.top.equalTo(self.btn_loginByPwd.snp.bottom).offset(6)
                                                                                                                make.centerX.equalTo(self.btn_loginByPwd)
                                                                                                }
                                                                }
 
                                                                self.view.layoutIfNeeded()
                                                }
                                }).disposed(by: disposeBag)
                }
 
                override func setUI() {
                                btn_eye.isSelected = true
                                view.addSubview(unlineImageView)
                                unlineImageView.snp.makeConstraints { make in
                                                make.top.equalTo(self.btn_loginByPwd.snp.bottom).offset(6)
                                                make.centerX.equalTo(self.btn_loginByPwd)
                                                make.width.equalTo(21)
                                                make.height.equalTo(8)
                                }
 
                                let btn_attribute = AttributedStringbuilder.build().add(string: "注册账号", withFont: .systemFont(ofSize: 12, weight: .medium), withColor: UIColor(hexStr: "#003809")).underLine(color: UIColor(hexStr: "#003809")).mutableAttributedString
 
                                btn_register.setAttributedTitle(btn_attribute, for: .normal)
                }
 
 
                @IBAction func loginTypeAction(_ sender: UIButton) {
                                sender.tag == 10 ? viewModel.loginType.accept(.pwd):viewModel.loginType.accept(.code)
 
                                switch viewModel.loginType.value{
                                                case .code:
                                                                tf_content.isSecureTextEntry = false
                                                case .pwd:
                                                                tf_content.isSecureTextEntry = btn_eye.isSelected
                                }
                }
                
                @IBAction func eyeAction(_ sender: UIButton) {
                                sender.isSelected = !sender.isSelected
                                tf_content.isSecureTextEntry = sender.isSelected
                }
 
                @IBAction func isReadAction(_ sender: UIButton) {
                                sender.isSelected = !sender.isSelected
                }
                
 
                @IBAction func sendCodeAction(_ sender: UIButton) {
 
                                guard !viewModel.loginPhone.value.isEmpty else {alert(msg: "请输入手机号");return}
                                guard viewModel.loginPhone.value.jq_isPhone else {alert(msg: "请输入正确手机号");return}
 
                                Services.sendCode(type: .codeLogin).subscribe(onNext: {data in
                                                if let _ = data.data{
                                                                sender.jq_openCountDown(60, defultTitle: "发送验证码") {
                                                                                sender.titleLabel?.font = UIFont.systemFont(ofSize: 12)
                                                                                sender.setTitleColor(.black.withAlphaComponent(0.3), for: .normal)
                                                                } completeClouse: {
                                                                                sender.titleLabel?.font = UIFont.systemFont(ofSize: 14, weight: .medium)
                                                                                sender.setTitleColor(UIColor(hexStr: "#96AD81"), for: .normal)
                                                                }
                                                }
                                }).disposed(by: disposeBag)
                }
 
                @IBAction func loginAction(_ sender: UIButton) {
 
                                guard viewModel.checkSafe() else {return}
                                guard btn_isRead.isSelected else {
                                                alertError(msg: "请阅读并同意《用户注册协议》和《用户隐私协议》");return
                                }
 
                                Services.loginBy(phone: viewModel.loginPhone.value, content: viewModel.loginContent.value, type: viewModel.loginType.value).subscribe(onNext: { data in
                                                if let model = data.data{
                                                                sceneDelegate?.loginSuccess()
                                                                self.dismiss(animated: true)
 
                                                                UserViewModel.saveLoginInfo(model)
 
                                                                Services.getUserInfo().subscribe(onNext: {data in
                                                                                if let model = data.data{
                                                                                                UserViewModel.saveAvatarInfo(model)
                                                                                }
                                                                }).disposed(by: self.disposeBag)
                                                }
                                }).disposed(by: disposeBag)
                }
 
                @IBAction func userRegisterTreatyAction(_ sender: UIButton) {
                                let vc = LoginTreatyVC()
                                vc.topIndex = 0
                                vc.clickHandle {[unowned self] state in
                                                self.btn_isRead.isSelected = state
                                }
                                vc.modalPresentationStyle = .custom
                                present(vc, animated: true)
                }
 
                @IBAction func userPrivateTreatyAction(_ sender: UIButton) {
                                let vc = LoginTreatyVC()
                                vc.topIndex = 1
                                vc.clickHandle {[unowned self] state in
                                                self.btn_isRead.isSelected = state
                                }
                                vc.modalPresentationStyle = .custom
                                present(vc, animated: true)
                }
                
                @IBAction func registerAction(_ sender: UIButton) {
                                let vc = RegisterVC()
                                push(vc: vc)
                }
 
                @IBAction func loginByWechatAction(_ sender: UIButton) {
                                //todo
                }
 
                @IBAction func loginByAppleAction(_ sender: UIButton) {
                                //todo
                }
 
                @IBAction func forgotPwdAction(_ sender: UIButton) {
                                let vc = ForgotPasswordVC()
                                push(vc: vc)
                }
                
 
 
                override func viewDidLayoutSubviews() {
                                super.viewDidLayoutSubviews()
                                view_content.jq_gradientColor(colorArr: [UIColor(hexStr: "#F3FFF8").cgColor,UIColor.white.cgColor], cornerRadius: 30, startPoint: CGPoint(x: 0, y: 0), endPoint: CGPoint(x: 0, y: 1), bounds: nil, locations: [0.01,0.5])
 
 
                                btn_login.jq_gradientNibColor(colorArr: [UIColor(hexStr: "#8EA47A").cgColor,UIColor(hexStr: "#AFCA98").cgColor], cornerRadius: 20)
                }
}