123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- //
- // 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 <WechatOpenSDK/WXApi.h>
- #import <AlipaySDK/AlipaySDK.h>
- //#import <SDPTLib/SDPTFramework.h>
- #import <SandPayGate/SandPayGate.h>
- @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 <YMMemberRenewalItemCellViewModel*>*renewalItemDataArray;
- /// 特权项目数据
- @property (nonatomic, strong, readwrite) NSArray <YMMemberPrivilegeItemCellViewModel*>*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
|