我如何在另一个函数中使用retailId(来自flatMapLatest)

时间:2019-05-24 11:00:27

标签: swift xcode api rx-swift moya

ViewModel:

        searchButtonInDidTapSubject.withLatestFrom(retailIdSubject.map { text in return text}).flatMapLatest { [unowned self] retailId in
        return service.searchRetailPayCashBack(retailId: retailId, createdAt: self.createdDates, sig: self.sig).materialize();
        }.subscribe(onNext: { [weak self] event in
            switch (event) {
            case .next(_):
                self?.checkResultSubject.onNext(true)
                break;
            case .error(let error):
                self?.errorSubject.onNext(error as! ErrorResponse);
                break;
            default:
                break;
            }
        }).disposed(by: disposeBag);

//在这里,我需要转换retailId,currentTime和accessToken 之后,我发送请求api

 private var sig: String{
get {
    let accessToken = self.keychain.get(Constants.accessToken)
    // ???
    let newAccessToken = String((accessToken?.substring(with: 11..<21))!)
    let retailid = ???
    let newSig = "\(newAccessToken)\(self.createdDates)\(retailid)"
    let md5Base64 = newSig.base64Encoded()
    let md5Data = self.MD5(md5Base64!)
    return String(md5Data!)

}
}

retailId必须等于retailid

1 个答案:

答案 0 :(得分:0)

不确定您要达到什么目的,但听起来sig应该在每次单击按钮时生成,因为retailId可以不同。

searchButtonInDidTapSubject
    .withLatestFrom(retailIdSubject.map { text in return text })
    .flatMapLatest { [unowned self] retailId in
        return service
            .searchRetailPayCashBack(
                retailId: retailId,
                createdAt: self.createdDates,
                sig: self.sig(withRetailId: retailId, createdDate: self.createdDates)
            )
            .materialize()
    }
    .subscribe(onNext: { [weak self] event in
        switch (event) {
        case .next(_):
            self?.checkResultSubject.onNext(true)
            break;
        case .error(let error):
            self?.errorSubject.onNext(error as! ErrorResponse);
            break;
        default:
            break;
        }
    })
    .disposed(by: disposeBag)

func sig(withRetailId retailId: String, createdDate: String) -> String {
    let accessToken = self.keychain.get(Constants.accessToken)
    // ???
    let newAccessToken = String((accessToken?.substring(with: 11..<21))!)
    let newSig = "\(newAccessToken)\(createdDate)\(retailId)"
    let md5Base64 = newSig.base64Encoded()
    let md5Data = self.MD5(md5Base64!)
    return String(md5Data!)
}