//
|
// 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
|
}
|
}
|