fix
杨锴
2024-10-09 2e91ee1387ef545ecef49230f2024b89b2f82a58
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
//
//  UpdatePhoneVC.swift
//  XQMuse
//
//  Created by 无故事王国 on 2024/8/13.
//
 
import UIKit
import AuthenticationServices
import QMUIKit
import JQTools
 
class UpdatePhoneVC: BaseVC {
    private var credential:ASAuthorizationAppleIDCredential?
    @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) {
        super.init(nibName: nil, bundle: nil)
        self.credential = credential
    }
    
    @MainActor required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
                                title = "验证手机号"
                                
    }
 
    @IBAction func getCodeAction(_ sender: UIButton) {
        view.endEditing(true)
        guard tf_phone.text!.jq_isPhone else {
            alertError(msg: "请输入正确的手机号");return
        }
 
        Services.sendCode(type: .threePlantform).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) {
        view.endEditing(true)
        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.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)
    }
    
}