宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-04-11 4356615a9252a987a62469331b1fcf91c102e24c
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
//
//  BindRelationVC.swift
//  OKProject
//
//  Created by 无故事王国 on 2022/2/10.
//  Copyright © 2022 yangwang. All rights reserved.
//
 
import UIKit
import RxSwift
import RxRelay
 
class BindRelationVC: YYViewController {
 
    @IBOutlet weak var tableView: UITableView!
    public var isCheck = false
    var relationM = [UserRelationModel]()
    private let viewModel = AgreementViewModel()
 
    override func viewDidLoad() {
        super.viewDidLoad()
        title = "绑定关系"
        tableView.delegate = self
        tableView.dataSource = self
        tableView.separatorStyle = .none
        tableView.register(cellName: "BindRelationAddTCell", identifier: "_BindRelationAddTCell")
 
        if relationM.count == 0{
            getData()
        }
 
        yy_popBlock = {
            self.yy_popToRoot(true)
        }
    }
 
    override func bindRx() {
        /// 协议
        viewModel.requestSubject
            .subscribe(onNext: {[unowned self] (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: disposeBag)
    }
 
    private func getData(){
        APIManager.shared.provider.rx.request(.getUserUserList).map(YYModel<[UserRelationModel]>.self).validate().subscribe(onSuccess: {data in
            self.relationM.removeAll()
            if data.data?.count == 0{
                let temp = UserRelationModel()
                self.relationM.append(temp)
            }else{
                self.relationM = data.data ?? []
            }
            self.tableView.reloadData()
        }) { error in
 
        }.disposed(by: disposeBag)
    }
 
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
    }
 
    @IBAction func agreementAction(_ sender: Any) {
        viewModel.type.accept(.relation)
        viewModel.agreement()
    }
 
    @IBAction func saveAction(_ sender: Any) {
 
        var saveModels = [Dictionary<String,String>]()
 
        for m in relationM {
            if m.content.isEmpty && m.name.isEmpty{continue}
 
            if m.content.isEmpty{
                alert(text: "请输入手机号");return
            }
 
            if m.name.isEmpty{
                alert(text: "请输入称呼");return
            }
 
            if m.id == 0{
                saveModels.append(["phone":m.content,"name":m.name])
            }else{
                saveModels.append(["id":"\(m.id)","phone":m.content,"name":m.name])
            }
        }
 
        if saveModels.count == 0{
            alert(text: "请填写内容");return
        }
 
        let content = XJsonUtil.arrayToJson(saveModels as NSArray)
        APIManager.shared.provider.rx.request(.addUserUser(content: content)).map(YYModel<Nothing>.self).subscribe(onSuccess: { data in
            if data.code == 200{
                alert(text: "保存成功")
                self.getData()
            }else{
                alert(text: data.msg)
            }
        }) { error in
            alert(text: error.localizedDescription)
        }.disposed(by: disposeBag)
    }
 
    @IBAction func addAction(_ sender: Any) {
        if relationM.count < 15{
            let addTemp = UserRelationModel()
            self.relationM.append(addTemp)
            tableView.reloadData()
            tableView.scrollToBottom(animated: true)
        }else{
            alert(text: "最多绑定15位联系人")
        }
    }
}
 
extension BindRelationVC:UITableViewDelegate{
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let m = relationM[indexPath.row]
        if indexPath.row == 0 && m.name.isEmpty && m.content.isEmpty{
            return 101
        }else{
            return 152
        }
    }
}
 
extension BindRelationVC:UITableViewDataSource{
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "_BindRelationAddTCell", for: indexPath) as! BindRelationAddTCell
        cell.indexPath = indexPath
        cell.delDelegate.delegate(on: self) { [weak self] (self, _) in
            self.getData()
        }
 
        cell.delCellDelegate.delegate(on: self) { (self,row) in
            self.relationM.remove(at: row)
            tableView.reloadData()
        }
 
        let m = relationM[indexPath.row]
        cell.userRelationModel = m
 
        if m.id == 0{
            cell.unbindBtn.setTitle("解除绑定", for: .normal)
        }else{
            cell.unbindBtn.setTitle("解除绑定", for: .normal)
        }
 
        return cell
    }
 
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return relationM.count
    }
}