宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-04 38f768d39ba27d303147c59a222655b7c6cdfb25
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//
//  LoginVC.swift
//  OKProject
//
//  Created by alvin_y on 2020/5/20.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
import RxSwift
import RxCocoa
import TTTAttributedLabel
import SwiftDate
 
let LoginSuccess_Noti = Notification.Name.init("LoginSuccess_Noti")
 
/// 登录注册
class LoginVC: YYViewController {
    
    /// 其他方式登录
    @IBOutlet weak var label_otherLogin: UILabel!
    
    /// 验证码图片
    @IBOutlet weak var image_code: UIImageView!
    
    /// 验证码标题
    @IBOutlet weak var label_code: UILabel!
    
    /// 微信登录
    @IBOutlet weak var button_weChat: UIButton!
    
    /// 忘记密码
    @IBOutlet weak var button_forgotPassword: UIButton!
    
    /// 登录按钮
    @IBOutlet weak var button_login: UIButton!
    
    /// 获取验证码
    @IBOutlet weak var button_getCode: UIButton!
    
    /// 协议内容
    @IBOutlet weak var label_agreement: TTTAttributedLabel!
    
    /// 协议按钮
    @IBOutlet weak var button_agreement: UIButton!
    
    /// 验证码
    @IBOutlet weak var textField_code: UITextField!
    
    /// 手机号
    @IBOutlet weak var textField_phone: UITextField!
    
    /// 密码登录
    @IBOutlet weak var button_password: UIButton!
    
    /// 验证码登录
    @IBOutlet weak var button_code: UIButton!
    
    /// 登录类型
    let loginType = BehaviorRelay<LoginType>(value: .code)
    
    /// 滑块
    let view_slider: UIView = {
       let view = UIView()
        view.backgroundColor = ThemeColor
        return view
    }()
    
    let viewModel = LoginViewModel()
    
    let userInfoViewModel = MineInfoViewModel()
    
    let agreeViewModel = AgreementViewModel()
    
    var password = DisposeBag()
    
    /// 验证按钮倒计时
    var codeTimeTick: CodeTimeTick = CodeTimeTick()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
        yy_popBlock = { [unowned self] in
            self.dismiss(animated: true, completion: nil)
        }
        agreeViewModel.type.accept(.userAgreement)
        let date = Date()
        let format = date.toFormat("yyyy-MM-dd")
        if format == "2020-08-27" || format == "2020-08-28" || format == "2020-08-29"{
            self.button_weChat.isHidden = true
            self.label_otherLogin.isHidden = true
        }
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        yy_isHiddenNavBarLine = true
    }
    
    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        codeTimeTick.close()
    }
    
    //MARK: - UI
    override func setupViews() {
        super.setupViews()
        self.view.backgroundColor = UIColor.color(light: UIColor.white, dark: UIColor.color(hexString: "#191919"))
        button_weChat.isHidden = !(UMSocialManager.default()?.isInstall(.wechatSession) ?? false)
        label_otherLogin.isHidden = !(UMSocialManager.default()?.isInstall(.wechatSession) ?? false)
        codeTimeTick.config(button: button_getCode, grayColor: UIColor.color(light: UIColor.color(hexString: "#333333",0.39), dark: UIColor.color(hexString: "#333333",0.39)))
        button_forgotPassword.isHidden = true
        textField_phone.keyboardType = .numberPad
        textField_code.keyboardType = .numberPad
        constrainsTextInputLength(length: 11, textField: textField_phone).disposed(by: disposeBag)
        constrainsTextInputLength(length: 4, textField: textField_code).disposed(by: password)
        button_code.setTitleColor(ButtonGray, for: .normal)
        button_code.setTitleColor(ThemeColor, for: .selected)
        button_password.setTitleColor(ButtonGray, for: .normal)
        button_password.setTitleColor(ThemeColor, for: .selected)
        button_forgotPassword.setTitleColor(UIColor.color(light: UIColor.color(hexString: "#666666"), dark: UIColor.color(hexString: "#FFFFFF")), for: .normal)
        view.addSubview(view_slider)
        let paragraph1 = "我已阅读并同意"
        let paragraph2 = "《宽窄出行用户服务协议》"
        label_agreement.text = paragraph1 + paragraph2
        label_agreement.textColor = UIColor.color(light: UIColor.color(hexString: "#333333"), dark: UIColor.color(hexString: "#FAFAFA"))
        label_otherLogin.textColor = UIColor.color(light: UIColor.color(hexString: "#666666"), dark: UIColor.color(hexString: "#FAFAFA"))
 
        label_agreement.linkAttributes = [NSAttributedString.Key.foregroundColor: ThemeColor,NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12)]
        label_agreement.activeLinkAttributes = [NSAttributedString.Key.foregroundColor: ThemeColor,NSAttributedString.Key.font: UIFont.systemFont(ofSize: 12)]
        label_agreement.addLink(toTransitInformation: nil, with: NSRange(location: paragraph1.count, length: paragraph2.count))
        label_agreement.delegate = self
        
    }
    
    //MARK: - Layouts
    override func defineLayouts() {
        super.defineLayouts()
        view_slider.snp.makeConstraints { (make) in
            make.bottom.equalTo(button_code.snp.bottom)
            make.left.equalTo(button_code.snp.left)
            make.size.equalTo(CGSize(width: 115, height: 2))
        }
    }
    
    //MARK: - Rx
    override func bindRx() {
        super.bindRx()
        
        textField_phone.rx.text.orEmpty
            .bind(to: viewModel.phoneNumber)
            .disposed(by: disposeBag)
        
        textField_code.rx.text.orEmpty
            .bind(to: viewModel.password)
            .disposed(by: disposeBag)
        
        loginType.bind(to: viewModel.type).disposed(by: disposeBag)
        
        button_agreement.rx.observeWeakly(Bool.self, #keyPath(UIButton.isSelected))
        .bind(to: viewModel.agreement)
        .disposed(by: disposeBag)
        
        
        loginType
            .skip(1)
            .subscribe(onNext: {[unowned self] (t) in
                self.view.endEditing(true)
                self.textField_code.text = nil
            switch t{
            case .code:
                self.textField_code.isSecureTextEntry = false
                self.password = DisposeBag()
                constrainsTextInputLength(length: 4, textField: self.textField_code).disposed(by: self.password)
                self.textField_code.keyboardType = .numberPad
                self.button_code.isSelected = true
                self.button_password.isSelected = false
                self.label_code.text = "验证码"
                self.image_code.image = UIImage.init(named: "icon_login_code")
                self.textField_code.placeholder = "请输入验证码"
                self.button_forgotPassword.isHidden = true
                self.button_getCode.isHidden = false
                UIView.animate(withDuration: 0.3) {
                    self.view_slider.snp.remakeConstraints { (make) in
                        make.bottom.equalTo(self.button_code.snp.bottom)
                        make.left.equalTo(self.button_code.snp.left)
                        make.size.equalTo(CGSize(width: 115, height: 2))
                    }
                    self.view.layoutIfNeeded()
                }
                break
            case .password:
                self.password = DisposeBag()
                self.textField_code.isSecureTextEntry = true
                constrainsTextInputLength(length: 18, textField: self.textField_code).disposed(by: self.password)
                self.textField_code.keyboardType = .default
                self.button_code.isSelected = false
                self.button_password.isSelected = true
                self.label_code.text = "密码"
                self.image_code.image = UIImage.init(named: "icon_login_password")
                self.textField_code.placeholder = "请输入密码"
                self.button_forgotPassword.isHidden = false
                self.button_getCode.isHidden = true
                UIView.animate(withDuration: 0.3) {
                    self.view_slider.snp.remakeConstraints { (make) in
                        make.bottom.equalTo(self.button_password.snp.bottom)
                        make.left.equalTo(self.button_password.snp.left)
                        make.size.equalTo(CGSize(width: 115, height: 2))
                    }
                    self.view.layoutIfNeeded()
                }
                break
            }
        }).disposed(by: disposeBag)
        
        /// 获取验证码
        button_getCode.rx.tap.subscribe(onNext: {[unowned self] (_) in
            self.viewModel.sendSMSCode(type: 2)
        }).disposed(by: disposeBag)
        
        button_code.rx.tap
            .subscribe(onNext: {[unowned self] (_) in
                self.loginType.accept(.code)
            }).disposed(by: disposeBag)
        
        button_password.rx.tap
            .subscribe(onNext: {[unowned self] (_) in
                self.loginType.accept(.password)
            }).disposed(by: disposeBag)
        
        /// 登录
        button_login.rx.tap
            .subscribe(onNext: {[unowned self] (_) in
                self.viewModel.login()
        }).disposed(by: disposeBag)
        
        /// 登录处理
        viewModel.loginSubject
            .subscribeOn(MainScheduler.instance)
            .subscribe(onNext: { [unowned self](status) in
                switch status{
                case .loading:
                    self.show()
                    break
                case .success(_ ):
                    self.hide()
                    self.dismiss(animated: true, completion: nil)
                    self.userInfoViewModel.queryUserInfo()
                    NotificationCenter.default.post(name: LoginSuccess_Noti, object: nil)
                    break
                case .error(let error):
                    self.hide()
                    alert(text: error.localizedDescription)
                    break
                }
        }).disposed(by: disposeBag)
 
        /// 登录处理
        viewModel.wxLoginSubject
        .subscribeOn(MainScheduler.instance)
            .subscribe(onNext: { [unowned self](status) in
                switch status{
                case .loading:
                    self.show()
                    break
                case .success(let model):
                    self.hide()
                    if let data: LoginModel = model as? LoginModel{
                        // 是否有手机号码(1=否,2=是)
                        if data.phone == 1{
                            app.loginInfo.save(model: data)
                            let vc = BindPhoneVC()
                            self.yy_push(vc: vc)
                        }else{
                            app.loginInfo.save(model: data)
                            self.userInfoViewModel.queryUserInfo()
                            self.dismiss(animated: true, completion: nil)
                            NotificationCenter.default.post(name: LoginSuccess_Noti, object: nil)
                        }
                    }
                   
                    break
                case .error(let error):
                    self.hide()
                    alert(text: error.localizedDescription)
                    break
                }
        }).disposed(by: disposeBag)
        
        /// 发送验证码处理
        viewModel.sendSMSCodeSubject
            .subscribe(onNext: {[unowned self] (status) in
                switch status{
                case .loading:
                    self.show()
                    break
                case .success(_ ):
                    self.hide()
                    // 倒计时
                    self.codeTimeTick.start()
                    break
                case .error(let error):
                    self.hide()
                    alert(text: error.localizedDescription)
                    break
                }
        }).disposed(by: disposeBag)
        
        /// 忘记密码
        button_forgotPassword.rx.tap
            .subscribe(onNext: {[unowned self] (_) in
                let vc = ForgotPasswordVC()
                self.yy_push(vc: vc)
        }).disposed(by: disposeBag)
        
        /// 协议绑定
        button_agreement.rx.tap.subscribe(onNext: { [unowned self](_) in
            self.button_agreement.isSelected = !self.button_agreement.isSelected
        }).disposed(by: disposeBag)
        
        /// 微信登录
        button_weChat.rx.tap.subscribe(onNext: {[unowned self] (_) in
            do{
                alert(text: "开放平台未创建")
//                try self.viewModel.agreement.value?.checkAgreement(agreementName: "宽窄出行用户服务协议")
//                self.wxLogin()
            }catch{
                alert(text: error.localizedDescription)
            }
        }).disposed(by: disposeBag)
        
        agreeViewModel.requestSubject
            .subscribe(onNext: { (status) in
                switch status{
                case .loading:
                    self.show()
                    break
                case .success(let model):
                    self.hide()
                    guard let data: SwitchCityModel = model as? SwitchCityModel else {return}
                    let vc = YYWebView()
                    vc.name = self.agreeViewModel.type.value.title()
                    vc.url = data.content
                    self.yy_push(vc: vc)
                    break
                case .error(let error):
                    self.hide()
                    alert(text: error.localizedDescription)
                    break
                }
        }).disposed(by: rx.disposeBag)
    }
 
    
    /// 微信登录
    func wxLogin()  {
        UMSocialManager.default()?.getUserInfo(with: .wechatSession, currentViewController: self, completion: { (result, error) in
            if (error == nil) {
                let resp = result as? UMSocialUserInfoResponse
                guard  let res = resp else {
                    return
                }
                self.viewModel.wxLogin(avatar: res.iconurl, nickName: res.name, sex: res.gender == "男" ? 1 : 2, openid: res.openid, unionid: res.unionId, registAreaCode: YYLocationManager.shared.currentCityCode)
            }else{
                alert(text: "登录失败")
            }
        })
    }
 
}
 
 
extension LoginVC: TTTAttributedLabelDelegate
{
    func attributedLabel(_ label: TTTAttributedLabel!, didSelectLinkWithTransitInformation components: [AnyHashable : Any]!) {
        agreeViewModel.agreement()
    }
}