杨锴
2024-11-06 63f7ed967433acee3ae8764c7a077e15c29c41f2
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
//
//  UpdatePhoneVC.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/13.
//
 
import UIKit
import AuthenticationServices
import QMUIKit
import JQTools
 
class UpdatePhoneVC: BaseVC {
    private var credential:ASAuthorizationAppleIDCredential?
    private var firstAccessToken:String? //后端问题,要把上次的token带过来
    @IBOutlet weak var tf_phone: QMUITextField!
    @IBOutlet weak var tf_code: QMUITextField!
    @IBOutlet weak var btn_code: UIButton!
    @IBOutlet weak var btn_isRead: UIButton!
 
 
    init(credential:ASAuthorizationAppleIDCredential? = nil,firstAccessToken:String?) {
        super.init(nibName: nil, bundle: nil)
        self.credential = credential
        self.firstAccessToken = firstAccessToken
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
                                title = "验证手机号"
    }
 
    @IBAction func isReadAction(_ sender: UIButton) {
        btn_isRead.isSelected = !btn_isRead.isSelected
    }
 
    @IBAction func getCodeAction(_ sender: UIButton) {
        view.endEditing(true)
        guard tf_phone.text!.jq_isPhone else {
            alertError(msg: "请输入正确的手机号");return
        }
 
        Services.sendCode(phone:tf_phone.text!,type: .threePlantform).subscribe(onNext: {data in
            if data.code == 200{
                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 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 loginAction(_ sender: UIButton) {
        view.endEditing(true)
 
        guard btn_isRead.isSelected else {
            alertError(msg: "请先阅读并同意《用户注册协议》《用户隐私协议》");return
        }
 
        guard tf_phone.text!.jq_isPhone else {
            alertError(msg: "请输入正确的手机号");return
        }
 
        guard tf_code.text!.count == 6 else {
            alertError(msg: "请输入6位验证码");return
        }
        guard let credentialUser = credential?.user else {
            alertError(msg: "Apple登录授权异常");return
        }
 
        let formatName = String(format: "%@%@%@", credential?.fullName?.familyName ?? "",credential?.fullName?.middleName ?? "", credential?.fullName?.middleName ?? "")
 
        Services.confirmByApple(cellPhone: tf_phone.text!, captcha: tf_code.text!, loginType: .apple, wxOrAppleId: credentialUser,name: formatName, mail: credential!.email).subscribe(onNext: {data in
            if var model = data.data{
                model.loginByAppleToken = String(data: self.credential!.identityToken!, encoding: .utf8)
                model.accessToken = self.firstAccessToken ?? ""
                model.appleId = self.credential!.user
                sceneDelegate?.loginSuccess()
                NotificationCenter.default.post(name: LoginDismiss_Noti, object: nil, userInfo: nil)
                UserViewModel.saveLoginInfo(model)
 
                Services.getUserInfo().subscribe(onNext: {data in
                    if let model = data.data{
                        UserViewModel.saveAvatarInfo(model)
                    }
                }).disposed(by: self.disposeBag)
            }
        }).disposed(by: disposeBag)
    }
 
 
 
 
}