宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
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
//
//  PassengerVC.swift
//  OKProject
//
//  Created by alvin_y on 2020/6/5.
//  Copyright © 2020 yangwang. All rights reserved.
//
 
import UIKit
import ContactsUI
import RxCocoa
 
class PassengerVC: YYViewController {
    
    /// 出行方式
    let orderType = BehaviorRelay<OrderType>(value: .taxi)
    
    /// 提交
    @IBOutlet weak var button_submit: YYButton!
    
    /// 协议
    @IBOutlet weak var button_agreement: UIButton!
    
    /// 通讯录
    @IBOutlet weak var button_addressbook: UIButton!
    
    /// 名字
    @IBOutlet weak var textField_name: YYTextField!
    
    /// 号码
    @IBOutlet weak var textField_phone: YYTextField!
    
    let complete = Delegate<(String,String),Void>()
    
    /// 协议
    let viewModel = AgreementViewModel()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Do any additional setup after loading the view.
    }
    
    //MARK: - UI
    override func setupViews() {
        super.setupViews()
        view.backgroundColor = UIColor.color(hexString: "#F3F4F5")
        navigationItem.title = "乘车人信息"
        button_agreement.setTitle(orderType.value == .taxi ? "《宽窄优行服务协议》" : "《宽窄优行服务协议》", for: .normal)
    }
    
    //MARK: - Rx
    override func bindRx() {
        super.bindRx()
        
        viewModel.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.viewModel.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)
        
        
        button_agreement.rx.tap.subscribe(onNext: {[unowned self] (_) in
            self.viewModel.type.accept(.userAgreement)
            self.viewModel.agreement()
        }).disposed(by: disposeBag)
        
        button_addressbook.rx.tap.subscribe(onNext: {[unowned self] (_) in
            openContact(from: self, delegate: self)
        }).disposed(by: disposeBag)
        
        button_submit.rx.tap.subscribe(onNext: {[unowned self] (_) in
            if self.textField_phone.isEmpty(empty: "请输入手机号"){
                return
            }
            if self.textField_name.isEmpty(empty: "请输入姓名"){
                return
            }
            self.yy_pop()
            self.complete.call((self.textField_phone.text!,self.textField_name.text!))
        }).disposed(by: disposeBag)
    }
 
}
 
//MAEK: - CNContactPickerDelegate
extension PassengerVC: CNContactPickerDelegate{
    //单选联系人
    func contactPicker(_ picker: CNContactPickerViewController,
                       didSelect contact: CNContact) {
        //获取联系人的姓名
        let lastName = contact.familyName
        let firstName = contact.givenName
        print("选中人的姓:\(lastName)")
        print("选中人的名:\(firstName)")
        
        //获取联系人电话号码
        //        print("选中人电话:")
        let phones = contact.phoneNumbers
        for phone in phones {
            //获得标签名(转为能看得懂的本地标签名,比如work、home)
            //            let phoneLabel = CNLabeledValue<NSString>.localizedString(forLabel: phone.label!)
            //获取号码
            let phoneValue:String = phone.value.stringValue
            self.textField_phone.text = phoneValue.byReplacingContact()
            self.textField_name.text = lastName + firstName
            //            print("\(phoneLabel):\(phoneValue)")
        }
    }
}