//
|
// YYSpecialCarViewModel.swift
|
// OKProject
|
//
|
// Created by alvin_y on 2020/9/17.
|
// Copyright © 2020 yangwang. All rights reserved.
|
//
|
|
import UIKit
|
import RxSwift
|
import RxCocoa
|
import HandyJSON
|
class YYSpecialCarViewModel: YYViewModel {
|
|
/// 订单Id
|
let orderId = BehaviorRelay<Int>(value: 0)
|
|
/// 司机信息
|
let driverInfo = BehaviorRelay<SocketInfoModel?>(value: nil)
|
|
/// 订单状态
|
let state = BehaviorRelay<SpecialCarState>(value: .witingForResponse)
|
|
/// 订单类型
|
let orderType = BehaviorRelay<OrderType>(value: .special)
|
|
/// 数据源
|
let dataSource = BehaviorRelay<YYSpecialCarModel?>(value: nil)
|
|
/// 下单时间
|
let orderTime = BehaviorRelay<String?>(value: nil)
|
|
/// 出行时间
|
let travelTime = BehaviorRelay<String?>(value: nil)
|
|
/// 取消订单Id
|
let cancelId = BehaviorRelay<Int>(value: 0)
|
|
/// 支付方式(0=其他方式,1=微信,2=支付宝)
|
let payType = BehaviorRelay<Int?>(value: nil)
|
|
/// 取消订单需要支付金额
|
let amount = BehaviorRelay<Double>(value: 0)
|
|
/// 取消原因
|
let reason = BehaviorRelay<String?>(value: nil)
|
|
/// 取消备注
|
let remark = BehaviorRelay<String?>(value: nil)
|
|
/// 评分
|
let fraction = BehaviorRelay<Int>(value: 0)
|
|
/// 评价内容
|
let content = BehaviorRelay<String?>(value: nil)
|
|
/// 评价数组
|
var evaluationArray: [String] = []
|
|
/// 报警电话
|
let policeNumber = BehaviorRelay<String>(value: "")
|
|
/// 投诉电话
|
let complaintsNumber = BehaviorRelay<String>(value: "")
|
|
/// 分享红包金额
|
let shareRedEnvelopeAmount = BehaviorRelay<Double>(value: 0)
|
|
/// 获取订单详情
|
func getOrderDetails() -> Observable<Result<YYSpecialCarModel?, Error>> {
|
|
guard orderId.value != 0 else {
|
return Observable.just(.failure(YYError.errorDescription("订单ID获取失败")))
|
}
|
|
return
|
APIManager.shared.provider.rx
|
.request(.queryOrderInfo(orderId: orderId.value, orderType: orderType.value.rawValue))
|
.mapThenValidate(YYSpecialCarModel.self)
|
.catchError { Single.just(.failure($0)) }
|
.asObservable()
|
}
|
|
|
/// 取消订单获取金额
|
func queryCancleAmount() -> Observable<Result<CancleAmountModel?, Error>> {
|
return APIManager.shared.provider.rx
|
.request(.queryCancleAmount(id: orderId.value,orderType: orderType.value.rawValue))
|
.mapThenValidate(CancleAmountModel.self)
|
.catchError { Single.just(.failure($0)) }
|
.asObservable()
|
}
|
|
/// 添加取消记录
|
func addCancledRecord(orderType:OrderType = .special) -> Observable<Result<SystemMessageModel?, Error>> {
|
return APIManager.shared.provider.rx
|
.request(.addCancle(id: orderId.value, orderType: orderType.rawValue, reason: reason.value, remark: remark.value))
|
.mapThenValidate(SystemMessageModel.self)
|
.catchError { Single.just(.failure($0)) }
|
.asObservable()
|
}
|
/// 取消订单
|
func cancleOrder() -> Observable<Result<String?, Error>> {
|
return
|
APIManager.shared.provider.rx
|
.request(.cancleOrderTaxi(cancleId: cancelId.value, id: orderId.value, orderType: orderType.value.rawValue, payType: payType.value))
|
.mapThenValidate(String.self)
|
.catchError { Single.just(.failure($0)) }
|
.asObservable()
|
}
|
|
/// 获取司机位置
|
func queryOrderServer() -> Observable<Result<SocketInfoModel?, Error>> {
|
return
|
APIManager.shared.provider.rx
|
.request(.queryOrderServer(orderId: self.orderId.value, orderType: orderType.value.rawValue))
|
.mapThenValidate(SocketInfoModel.self)
|
.catchError { Single.just(.failure($0)) }
|
.asObservable()
|
}
|
|
/// 获取没有司机接单的提醒数据
|
func queryEndPush() -> Observable<Result<SocketInfoModel?, Error>> {
|
return
|
APIManager.shared.provider.rx
|
.request(.queryEndPush(orderId: orderId.value, orderType: orderType.value.rawValue))
|
.mapThenValidate(SocketInfoModel.self)
|
.catchError { Single.just(.failure($0)) }
|
.asObservable()
|
}
|
|
|
/// 继续等待推单操作
|
func pushOrder() -> Observable<Result<TravelServiceModel?, Error>> {
|
return
|
APIManager.shared.provider.rx
|
.request(.pushOrderTaxi(id: orderId.value, orderType: orderType.value.rawValue))
|
.mapThenValidate(TravelServiceModel.self)
|
.catchError { Single.just(.failure($0)) }
|
.asObservable()
|
}
|
|
/// 评价
|
func orderEvaluate() -> Observable<Result<Nothing?, Error>> {
|
return
|
APIManager.shared.provider.rx
|
.request(.orderEvaluate(orderId: self.orderId.value, orderType: orderType.value.rawValue, fraction: fraction.value, content: content.value))
|
.mapThenValidate(Nothing.self)
|
.catchError { Single.just(.failure($0)) }
|
.asObservable()
|
}
|
|
|
/// 查询报警电话
|
/// - Returns: PhonesModel
|
func queryPhones() {
|
APIManager.shared.provider.rx
|
.request(.queryPhones(code: YYLocationManager.shared.currentCityCode))
|
.map(YYModel<[PhonesModel]>.self)
|
.validate()
|
.subscribe(onSuccess: {[unowned self] (model) in
|
guard let data = model.data else{return}
|
for item in data{
|
if item.type == 1{
|
self.policeNumber.accept(item.phone)
|
}else if item.type == 2{
|
self.complaintsNumber.accept(item.phone)
|
}
|
}
|
}) { (error) in
|
}
|
.disposed(by: disposeBag)
|
}
|
|
/// 查询是否有红包
|
func queryRedMoney() -> Observable<Result<CancleAmountModel?, Error>> {
|
return
|
APIManager.shared.provider.rx
|
.request(.queryRedMoney(orderId: orderId.value,orderType: orderType.value.rawValue))
|
.mapThenValidate(CancleAmountModel.self)
|
.catchError { Single.just(.failure($0)) }
|
.asObservable()
|
}
|
|
/// 评论成功后获取红包金额
|
func shareRedEnvelope() -> Observable<Result<Nothing?, Error>> {
|
APIManager.shared.provider.rx
|
.request(.shareRedEnvelope(orderId: orderId.value, orderType: orderType.value.rawValue))
|
.mapThenValidate(Nothing.self)
|
.catchError { Single.just(.failure($0)) }
|
.asObservable()
|
}
|
|
/// 获取底部显示文字
|
func attributedString(state: Int,data: SocketInfoModel) -> NSMutableAttributedString? {
|
if state == 2 || state == 3{
|
let attributedString = AttributedStringbuilder.build()
|
.add(string: "司机接单,距预约地点", withFont: UIFont.init(name: Medium, size: 14)!, withColor: ThemeColor)
|
.add(string: "\(data.reservationMileage)", withFont: UIFont.init(name: Medium, size: 14)!, withColor: UIColor(hexString: "#FB692A")!)
|
.add(string: "公里,预计", withFont: UIFont.init(name: Medium, size: 14)!, withColor: ThemeColor)
|
.add(string: "\(data.reservationTime)", withFont: UIFont.init(name: Medium, size: 14)!, withColor: UIColor(hexString: "#FB692A")!)
|
.add(string: "分钟到达", withFont: UIFont.init(name: Medium, size: 14)!, withColor: ThemeColor)
|
.mutableAttributedString
|
return attributedString
|
}else if state == 5 || state == 6{
|
let attributedString = AttributedStringbuilder.build()
|
.add(string: "已为您服务", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor.color(hexString: "#00BF30"))
|
.add(string: "\(data.servedTime)", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor(hexString: "#FB692A")!)
|
.add(string: "分钟,累计行驶", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor.color(hexString: "#00BF30"))
|
.add(string: "\(data.servedMileage)", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor(hexString: "#FB692A")!)
|
.add(string: "公里\n距离目的地还有大约", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor.color(hexString: "#00BF30"))
|
.add(string: "\(data.laveMileage)", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor(hexString: "#FB692A")!)
|
.add(string: "公里,预计行驶大约", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor.color(hexString: "#00BF30"))
|
.add(string: "\(data.laveTime)", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor(hexString: "#FB692A")!)
|
.add(string: "分钟", withFont: UIFont.init(name: Medium, size: 12)!, withColor: UIColor.color(hexString: "#00BF30"))
|
.mutableAttributedString
|
return attributedString
|
}
|
return nil
|
}
|
}
|