// // YMMemberCenterViewModel.m // MSYOUPAI // // Created by YoMi on 2024/2/27. // Copyright © 2024 MS. All rights reserved. // #import "YMMemberCenterViewModel.h" #import "YMMemberCenterModel.h" #import "YMOpenMemberPayInfoModel.h" #import #import //#import #import @interface YMMemberCenterViewModel () /// 用户头像 @property (nonatomic, strong, readwrite) NSString *userAvatar; /// 用户昵称 @property (nonatomic, strong, readwrite) NSString *userNickname; /// 有效期日期 @property (nonatomic, strong, readwrite) NSString *validityDate; /// 支付金额 @property (nonatomic, assign, readwrite) NSInteger payAmount; /// 续费项目数据 @property (nonatomic, strong, readwrite) NSArray *renewalItemDataArray; /// 特权项目数据 @property (nonatomic, strong, readwrite) NSArray *privilegeItemDataArray; /// 开通会员操作标题 @property (nonatomic, strong, readwrite) NSString *openMemberOperation; /// 选中充值项目 @property (nonatomic, strong, readwrite) RACSubject *selectedRechargeItemSubject; /// 选中支付方式 @property (nonatomic, strong, readwrite) RACSubject *selectedPayMethodTypeSubject; /// 续费项目Id @property (nonatomic, assign) NSInteger renewalItemId; /// 支付方式代码 @property (nonatomic, copy) NSString *payMethodCode; /// 会员操作类型 @property (nonatomic, copy) NSString *memberOperationType; @end @implementation YMMemberCenterViewModel - (void)ym_initialize{ [super ym_initialize]; self.customNavTitle = @"会员中心"; @weakify(self) [[self.selectedRechargeItemSubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNumber * _Nullable value) { @strongify(self) YMMemberRenewalItemCellViewModel *viewModel = (YMMemberRenewalItemCellViewModel*)self.renewalItemDataArray[[value intValue]]; self.renewalItemId = viewModel.renewalItemId; self.payAmount = viewModel.renewalItemAmount; }]; [[self.selectedPayMethodTypeSubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSString * _Nullable value) { @strongify(self) self.payMethodCode = value; [self submitMemberRechargeInfoData]; }]; } - (void)getMemberRechargeInfoData{ @weakify(self) [ZCHUDHelper showWithStatus:@"加载中..."]; [LCHttpHelper requestWithURLString:MemberRechargeInfo parameters:nil needToken:YES type:HttpRequestTypePost success:^(id responseObject) { @strongify(self) NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code == 0) { [ZCHUDHelper dismiss]; YMMemberCenterModel *model = [YMMemberCenterModel yy_modelWithJSON:[dict dictionaryValueForKey:@"data" defaultValue:@{}]]; self.memberOperationType = model.info.vip_id > 0 ? @"start" : @"none"; self.userAvatar = model.info.avatar; self.userNickname = model.info.nickname; self.validityDate = OCStringIsEmpty(model.info.expire_time) ? @"未开通VIP会员" : stringFormat(@"会员有效日期:%@",model.info.expire_time); self.renewalItemDataArray = [model.list.rac_sequence map:^(YMMemberRenewalItemModel * _Nullable model) { YMMemberRenewalItemCellViewModel *viewModel = [[YMMemberRenewalItemCellViewModel alloc]initWithParams:@{ ParamsModel:model }]; return viewModel; }].array; self.privilegeItemDataArray = [model.privilege.rac_sequence map:^(YMMemberPrivilegeItemModel * _Nullable model) { YMMemberPrivilegeItemCellViewModel *viewModel = [[YMMemberPrivilegeItemCellViewModel alloc]initWithParams:@{ ParamsModel:model }]; return viewModel; }].array; self.openMemberOperation = model.info.vip_id > 0 ? @"续费会员" : @"开通会员"; [self.refreshUISubject sendNext:@(YMRefreshUI)]; }else{ [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]]; } } failure:^(NSError *error) { [ZCHUDHelper showTitle:error.localizedDescription]; }]; } - (void)submitMemberRechargeInfoData{ @weakify(self) [ZCHUDHelper showWithStatus:@"请求中..."]; [LCHttpHelper requestWithURLString:SubmitMemberRechargeInfo parameters:@{ @"id":@(1), @"pay_type":self.payMethodCode, @"type":self.memberOperationType, @"vip_goods_id":@(self.renewalItemId) } needToken:YES type:HttpRequestTypePost success:^(id responseObject) { @strongify(self) NSDictionary* dict = (NSDictionary*)responseObject; NSInteger code = [[dict objectForKey:@"code"] integerValue]; if (code == 0) { [ZCHUDHelper dismiss]; YMOpenMemberPayInfoModel *model = [YMOpenMemberPayInfoModel yy_modelWithJSON:[dict dictionaryValueForKey:@"data" defaultValue:@{}]]; if ([self.payMethodCode containsString:@"wechat"]) { if(![WXApi isWXAppInstalled]){ [ZCHUDHelper showTitle:@"未安装微信或版本过低"]; return; } if (model.pay_obj.type == YMAccountBalabcePayType_H5) { NSDictionary*dic =model.pay_obj.data; NSURL *url = [NSURL URLWithString:dic[@"pay_info"]]; // 检查是否可以打开URL if ([[UIApplication sharedApplication] canOpenURL:url]) { // 打开URL [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; } }else if (model.pay_obj.type == YMAccountBalabcePayTypeAlipay_H5) { NSLog(@"当前支付类型3---:%@",model.pay_obj.data); NSURL *url = [NSURL URLWithString:model.pay_obj.data]; // 检查是否可以打开URL if ([[UIApplication sharedApplication] canOpenURL:url]) { // 打开URL [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; } }else if(model.pay_obj.type == YMAccountBalabcePayTypeWeChat_pay){ NSDictionary *result_dict = [dict objectForKey:@"data"]; NSDictionary *pay_obj_dict = [result_dict objectForKey:@"pay_obj"]; NSDictionary *pay_obj_data = [pay_obj_dict objectForKey:@"data"]; YMAccountBalanceWeChatInfoModel *wechatpayinfo = [YMAccountBalanceWeChatInfoModel yy_modelWithJSON:pay_obj_data]; PayReq *request = [[PayReq alloc] init]; request.partnerId = wechatpayinfo.mch_id; request.prepayId = wechatpayinfo.prepay_id; request.package = @"Sign=WXPay"; request.nonceStr = wechatpayinfo.nonce_str; request.timeStamp = (uint32_t)[wechatpayinfo.timestamp intValue]; request.sign = wechatpayinfo.paySign; NSString *partnerId = request.partnerId; NSString *prepayId = request.prepayId; NSString *package = request.package; NSString *nonceStr = request.nonceStr; NSInteger timeStamp = request.timeStamp; NSString *sign = request.sign; NSLog(@"输出🍀\n%@",@{ @"partnerId":partnerId, @"prepayId":prepayId, @"package":package, @"nonceStr":nonceStr, @"timeStamp":@(timeStamp), @"sign":sign }); [WXApi sendReq:request completion:^(BOOL success) { }]; }else{ NSString *token = model.pay_obj.data; NSLog(@"%@",token); if (!token || token.length == 0) { [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败,请联系技术人员"]]; return; } [SandPayGateService.shared queryTradeRecordInfoWith:token]; SandPayGateService.shared.sandpayGateResultBlock = ^(NSString* status, NSDictionary* dataDic) { NSLog(@"输出🍀\n%@ \n%@",status, dataDic); if ([status isEqualToString:@"success"]) { NSString *funcCode = dataDic[@"funcCode"]; NSString *ghOriId = dataDic[@"ghOriId"]; NSString *miniProgramType = dataDic[@"miniProgramType"]; NSString *pathUrl = dataDic[@"pathUrl"]; NSString *subAppId = dataDic[@"subAppId"]; NSString *tokenId = dataDic[@"tokenId"]; //跳转微信小程序 NSString *allPath = [NSString stringWithFormat:@"%@token_id=%@&funcCode=%@", pathUrl, tokenId, funcCode]; WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object]; launchMiniProgramReq.userName = ghOriId; launchMiniProgramReq.path = allPath; launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease; // 使用适当的类型,例如WXMiniProgramTypeRelease [WXApi sendReq:launchMiniProgramReq completion:^(BOOL success) { if (success) { NSLog(@"跳转微信成功"); } else { NSLog(@"跳转微信异常"); } }]; } else { //[ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败:%@",errorMsg]]; [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败"]]; } }; // XHPayParameterModel *payParameter = [XHPayParameterModel yy_modelWithJSON:model.pay_obj.data]; // [SDPTController shared].paySucTypeBlock = ^(ProductPyType type, id _Nonnull responseObj) { // NSLog(@"测试打印:%@",responseObj); // //跳转微信小程序 // NSDictionary *pay_extra = [payParameter.pay_extra mj_JSONObject]; // NSString * allPath = [NSString stringWithFormat:@"%@token_id=%@",pay_extra[@"path_url"],responseObj]; // WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object]; // // launchMiniProgramReq.userName = pay_extra[@"gh_ori_id"]; // launchMiniProgramReq.path = allPath; // /// 0 正式版 // launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease; // [WXApi sendReq:launchMiniProgramReq completion:^(BOOL success) { // if (success) { // NSLog(@"跳转微信成功"); // } else { // NSLog(@"跳转微信异常"); // } // }]; // // }; // [SDPTController shared].payFailureBlock = ^(ProductPyType type, NSString * _Nonnull errorMsg) { // [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败:%@",errorMsg]]; // }; // // if(payParameter){ // [[SDPTController shared] singletonPaymentMethodWith:payParameter]; // }else{ // [ZCHUDHelper showTitle:@"支付数据错误"]; // } } } else if ([self.payMethodCode containsString:@"alipay"]) { if(model.pay_obj.type == YMAccountBalabcePayTypeAliSdk_pay){ NSString *appScheme = @"msyoupaipay"; [[AlipaySDK defaultService] payOrder:model.payinfo fromScheme:appScheme callback:^(NSDictionary *resultDic) { NSLog(@"reslut ====== %@",resultDic); if ([resultDic[@"resultStatus"] isEqualToString:@"9000"] || [resultDic[@"resultStatus"] isEqualToString:@"8000"] || [resultDic[@"resultStatus"] isEqualToString:@"6004"]) { [ZCHUDHelper showTitle:@"支付成功"]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [OCNotificationCenter postNotificationName:ALIPAY_SUCCESS_NOTIFICATION object:nil]; }); } else if ([resultDic[@"resultStatus"] isEqualToString:@"6001"]) { }else { [ZCHUDHelper showTitle:@"支付失败"]; } }]; }else{ if (model.pay_obj.type == YMAccountBalabcePayType_H5) { NSDictionary*dic =model.pay_obj.data; NSURL *url = [NSURL URLWithString:dic[@"pay_info"]]; // 检查是否可以打开URL if ([[UIApplication sharedApplication] canOpenURL:url]) { // 打开URL [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; } }else if (model.pay_obj.type == YMAccountBalabcePayTypeAlipay_H5) { NSLog(@"当前支付类型3---:%@",model.pay_obj.data); NSURL *url = [NSURL URLWithString:model.pay_obj.data]; // 检查是否可以打开URL if ([[UIApplication sharedApplication] canOpenURL:url]) { // 打开URL [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil]; } }else { NSString *token = model.pay_obj.data; NSLog(@"%@",token); if (!token || token.length == 0) { [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败,请联系技术人员"]]; return; } [SandPayGateService.shared queryTradeRecordInfoWith:token]; SandPayGateService.shared.sandpayGateResultBlock = ^(NSString* status, NSDictionary* dataDic) { NSLog(@"输出🍀\n%@ \n%@",status, dataDic); if ([status isEqualToString:@"success"]) { } else { //[ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败:%@",errorMsg]]; [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败"]]; } }; } // XHPayParameterModel *payParameter = [XHPayParameterModel yy_modelWithJSON:model.pay_obj.data]; // [SDPTController shared].paySucTypeBlock = ^(ProductPyType type, id _Nonnull responseObj) { // NSLog(@"测试打印:%@",responseObj); // //跳转微信小程序 // NSDictionary *pay_extra = [payParameter.pay_extra mj_JSONObject]; // NSString * allPath = [NSString stringWithFormat:@"%@token_id=%@",pay_extra[@"path_url"],responseObj]; // WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object]; // // launchMiniProgramReq.userName = pay_extra[@"gh_ori_id"]; // launchMiniProgramReq.path = allPath; // /// 0 正式版 // launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease; // [WXApi sendReq:launchMiniProgramReq completion:^(BOOL success) { // if (success) { // NSLog(@"跳转微信成功"); // } else { // NSLog(@"跳转微信异常"); // } // }]; // // }; // [SDPTController shared].payFailureBlock = ^(ProductPyType type, NSString * _Nonnull errorMsg) { // [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败:%@",errorMsg]]; // }; // // if(payParameter){ // [[SDPTController shared] singletonPaymentMethodWith:payParameter]; // }else{ // [ZCHUDHelper showTitle:@"支付数据错误"]; // } } } }else{ [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]]; } } failure:^(NSError *error) { [ZCHUDHelper showTitle:error.localizedDescription]; }]; } - (RACSubject *)selectedRechargeItemSubject{ if (!_selectedRechargeItemSubject) { _selectedRechargeItemSubject = [RACSubject subject]; } return _selectedRechargeItemSubject; } - (RACSubject *)selectedPayMethodTypeSubject{ if (!_selectedPayMethodTypeSubject) { _selectedPayMethodTypeSubject = [RACSubject subject]; } return _selectedPayMethodTypeSubject; } @end