YMMemberCenterViewModel.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. //
  2. // YMMemberCenterViewModel.m
  3. // MSYOUPAI
  4. //
  5. // Created by YoMi on 2024/2/27.
  6. // Copyright © 2024 MS. All rights reserved.
  7. //
  8. #import "YMMemberCenterViewModel.h"
  9. #import "YMMemberCenterModel.h"
  10. #import "YMOpenMemberPayInfoModel.h"
  11. #import <WechatOpenSDK/WXApi.h>
  12. #import <AlipaySDK/AlipaySDK.h>
  13. //#import <SDPTLib/SDPTFramework.h>
  14. #import <SandPayGate/SandPayGate.h>
  15. @interface YMMemberCenterViewModel ()
  16. /// 用户头像
  17. @property (nonatomic, strong, readwrite) NSString *userAvatar;
  18. /// 用户昵称
  19. @property (nonatomic, strong, readwrite) NSString *userNickname;
  20. /// 有效期日期
  21. @property (nonatomic, strong, readwrite) NSString *validityDate;
  22. /// 支付金额
  23. @property (nonatomic, assign, readwrite) NSInteger payAmount;
  24. /// 续费项目数据
  25. @property (nonatomic, strong, readwrite) NSArray <YMMemberRenewalItemCellViewModel*>*renewalItemDataArray;
  26. /// 特权项目数据
  27. @property (nonatomic, strong, readwrite) NSArray <YMMemberPrivilegeItemCellViewModel*>*privilegeItemDataArray;
  28. /// 开通会员操作标题
  29. @property (nonatomic, strong, readwrite) NSString *openMemberOperation;
  30. /// 选中充值项目
  31. @property (nonatomic, strong, readwrite) RACSubject *selectedRechargeItemSubject;
  32. /// 选中支付方式
  33. @property (nonatomic, strong, readwrite) RACSubject *selectedPayMethodTypeSubject;
  34. /// 续费项目Id
  35. @property (nonatomic, assign) NSInteger renewalItemId;
  36. /// 支付方式代码
  37. @property (nonatomic, copy) NSString *payMethodCode;
  38. /// 会员操作类型
  39. @property (nonatomic, copy) NSString *memberOperationType;
  40. @end
  41. @implementation YMMemberCenterViewModel
  42. - (void)ym_initialize{
  43. [super ym_initialize];
  44. self.customNavTitle = @"会员中心";
  45. @weakify(self)
  46. [[self.selectedRechargeItemSubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSNumber * _Nullable value) {
  47. @strongify(self)
  48. YMMemberRenewalItemCellViewModel *viewModel = (YMMemberRenewalItemCellViewModel*)self.renewalItemDataArray[[value intValue]];
  49. self.renewalItemId = viewModel.renewalItemId;
  50. self.payAmount = viewModel.renewalItemAmount;
  51. }];
  52. [[self.selectedPayMethodTypeSubject takeUntil:self.rac_willDeallocSignal] subscribeNext:^(NSString * _Nullable value) {
  53. @strongify(self)
  54. self.payMethodCode = value;
  55. [self submitMemberRechargeInfoData];
  56. }];
  57. }
  58. - (void)getMemberRechargeInfoData{
  59. @weakify(self)
  60. [ZCHUDHelper showWithStatus:@"加载中..."];
  61. [LCHttpHelper requestWithURLString:MemberRechargeInfo parameters:nil needToken:YES type:HttpRequestTypePost success:^(id responseObject) {
  62. @strongify(self)
  63. NSDictionary* dict = (NSDictionary*)responseObject;
  64. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  65. if (code == 0) {
  66. [ZCHUDHelper dismiss];
  67. YMMemberCenterModel *model = [YMMemberCenterModel yy_modelWithJSON:[dict dictionaryValueForKey:@"data" defaultValue:@{}]];
  68. self.memberOperationType = model.info.vip_id > 0 ? @"start" : @"none";
  69. self.userAvatar = model.info.avatar;
  70. self.userNickname = model.info.nickname;
  71. self.validityDate = OCStringIsEmpty(model.info.expire_time) ? @"未开通VIP会员" : stringFormat(@"会员有效日期:%@",model.info.expire_time);
  72. self.renewalItemDataArray = [model.list.rac_sequence map:^(YMMemberRenewalItemModel * _Nullable model) {
  73. YMMemberRenewalItemCellViewModel *viewModel = [[YMMemberRenewalItemCellViewModel alloc]initWithParams:@{
  74. ParamsModel:model
  75. }];
  76. return viewModel;
  77. }].array;
  78. self.privilegeItemDataArray = [model.privilege.rac_sequence map:^(YMMemberPrivilegeItemModel * _Nullable model) {
  79. YMMemberPrivilegeItemCellViewModel *viewModel = [[YMMemberPrivilegeItemCellViewModel alloc]initWithParams:@{
  80. ParamsModel:model
  81. }];
  82. return viewModel;
  83. }].array;
  84. self.openMemberOperation = model.info.vip_id > 0 ? @"续费会员" : @"开通会员";
  85. [self.refreshUISubject sendNext:@(YMRefreshUI)];
  86. }else{
  87. [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]];
  88. }
  89. } failure:^(NSError *error) {
  90. [ZCHUDHelper showTitle:error.localizedDescription];
  91. }];
  92. }
  93. - (void)submitMemberRechargeInfoData{
  94. @weakify(self)
  95. [ZCHUDHelper showWithStatus:@"请求中..."];
  96. [LCHttpHelper requestWithURLString:SubmitMemberRechargeInfo parameters:@{
  97. @"id":@(1),
  98. @"pay_type":self.payMethodCode,
  99. @"type":self.memberOperationType,
  100. @"vip_goods_id":@(self.renewalItemId)
  101. } needToken:YES type:HttpRequestTypePost success:^(id responseObject) {
  102. @strongify(self)
  103. NSDictionary* dict = (NSDictionary*)responseObject;
  104. NSInteger code = [[dict objectForKey:@"code"] integerValue];
  105. if (code == 0) {
  106. [ZCHUDHelper dismiss];
  107. YMOpenMemberPayInfoModel *model = [YMOpenMemberPayInfoModel yy_modelWithJSON:[dict dictionaryValueForKey:@"data" defaultValue:@{}]];
  108. NSLog(@"这里----12");
  109. if ([self.payMethodCode containsString:@"wechat"]) {
  110. if(![WXApi isWXAppInstalled]){
  111. [ZCHUDHelper showTitle:@"未安装微信或版本过低"];
  112. return;
  113. }
  114. if (model.pay_obj.type == YMAccountBalabcePayType_H5) {
  115. NSDictionary*dic =model.pay_obj.data;
  116. NSURL *url = [NSURL URLWithString:dic[@"pay_info"]];
  117. // 检查是否可以打开URL
  118. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  119. // 打开URL
  120. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  121. }
  122. }else if (model.pay_obj.type == YMAccountBalabcePayTypeAlipay_H5) {
  123. NSLog(@"当前支付类型3---:%@",model.pay_obj.data);
  124. NSURL *url = [NSURL URLWithString:model.pay_obj.data];
  125. // 检查是否可以打开URL
  126. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  127. // 打开URL
  128. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  129. }
  130. } else if(model.pay_obj.type == YMAccountBalabcePayTypeWeChat_pay){
  131. NSDictionary *result_dict = [dict objectForKey:@"data"];
  132. NSDictionary *pay_obj_dict = [result_dict objectForKey:@"pay_obj"];
  133. NSDictionary *pay_obj_data = [pay_obj_dict objectForKey:@"data"];
  134. YMAccountBalanceWeChatInfoModel *wechatpayinfo = [YMAccountBalanceWeChatInfoModel yy_modelWithJSON:pay_obj_data];
  135. PayReq *request = [[PayReq alloc] init];
  136. request.partnerId = wechatpayinfo.mch_id;
  137. request.prepayId = wechatpayinfo.prepay_id;
  138. request.package = @"Sign=WXPay";
  139. request.nonceStr = wechatpayinfo.nonce_str;
  140. request.timeStamp = (uint32_t)[wechatpayinfo.timestamp intValue];
  141. request.sign = wechatpayinfo.paySign;
  142. NSString *partnerId = request.partnerId;
  143. NSString *prepayId = request.prepayId;
  144. NSString *package = request.package;
  145. NSString *nonceStr = request.nonceStr;
  146. NSInteger timeStamp = request.timeStamp;
  147. NSString *sign = request.sign;
  148. NSLog(@"输出🍀\n%@",@{
  149. @"partnerId":partnerId,
  150. @"prepayId":prepayId,
  151. @"package":package,
  152. @"nonceStr":nonceStr,
  153. @"timeStamp":@(timeStamp),
  154. @"sign":sign
  155. });
  156. [WXApi sendReq:request completion:^(BOOL success) {
  157. }];
  158. }else{
  159. NSString *token = model.pay_obj.data;
  160. NSLog(@"%@",token);
  161. if (!token || token.length == 0) {
  162. [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败,请联系技术人员"]];
  163. return;
  164. }
  165. [SandPayGateService.shared queryTradeRecordInfoWith:token];
  166. SandPayGateService.shared.sandpayGateResultBlock = ^(NSString* status, NSDictionary* dataDic) {
  167. NSLog(@"输出🍀\n%@ \n%@",status, dataDic);
  168. if ([status isEqualToString:@"success"]) {
  169. NSString *funcCode = dataDic[@"funcCode"];
  170. NSString *ghOriId = dataDic[@"ghOriId"];
  171. NSString *miniProgramType = dataDic[@"miniProgramType"];
  172. NSString *pathUrl = dataDic[@"pathUrl"];
  173. NSString *subAppId = dataDic[@"subAppId"];
  174. NSString *tokenId = dataDic[@"tokenId"];
  175. //跳转微信小程序
  176. NSString *allPath = [NSString stringWithFormat:@"%@token_id=%@&funcCode=%@", pathUrl, tokenId, funcCode];
  177. WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
  178. launchMiniProgramReq.userName = ghOriId;
  179. launchMiniProgramReq.path = allPath;
  180. launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease; // 使用适当的类型,例如WXMiniProgramTypeRelease
  181. [WXApi sendReq:launchMiniProgramReq completion:^(BOOL success) {
  182. if (success) {
  183. NSLog(@"跳转微信成功");
  184. } else {
  185. NSLog(@"跳转微信异常");
  186. }
  187. }];
  188. } else {
  189. //[ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败:%@",errorMsg]];
  190. [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败"]];
  191. }
  192. };
  193. }
  194. // XHPayParameterModel *payParameter = [XHPayParameterModel yy_modelWithJSON:model.pay_obj.data];
  195. // [SDPTController shared].paySucTypeBlock = ^(ProductPyType type, id _Nonnull responseObj) {
  196. // NSLog(@"测试打印:%@",responseObj);
  197. // //跳转微信小程序
  198. // NSDictionary *pay_extra = [payParameter.pay_extra mj_JSONObject];
  199. // NSString * allPath = [NSString stringWithFormat:@"%@token_id=%@",pay_extra[@"path_url"],responseObj];
  200. // WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
  201. //
  202. // launchMiniProgramReq.userName = pay_extra[@"gh_ori_id"];
  203. // launchMiniProgramReq.path = allPath;
  204. // /// 0 正式版
  205. // launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease;
  206. // [WXApi sendReq:launchMiniProgramReq completion:^(BOOL success) {
  207. // if (success) {
  208. // NSLog(@"跳转微信成功");
  209. // } else {
  210. // NSLog(@"跳转微信异常");
  211. // }
  212. // }];
  213. //
  214. // };
  215. // [SDPTController shared].payFailureBlock = ^(ProductPyType type, NSString * _Nonnull errorMsg) {
  216. // [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败:%@",errorMsg]];
  217. // };
  218. //
  219. // if(payParameter){
  220. // [[SDPTController shared] singletonPaymentMethodWith:payParameter];
  221. // }else{
  222. // [ZCHUDHelper showTitle:@"支付数据错误"];
  223. // }
  224. } else if ([self.payMethodCode containsString:@"alipay"]) {
  225. if(model.pay_obj.type == YMAccountBalabcePayTypeAliSdk_pay){
  226. NSString *appScheme = @"msyoupaipay";
  227. [[AlipaySDK defaultService] payOrder:model.payinfo fromScheme:appScheme callback:^(NSDictionary *resultDic) {
  228. NSLog(@"reslut ====== %@",resultDic);
  229. if ([resultDic[@"resultStatus"] isEqualToString:@"9000"] || [resultDic[@"resultStatus"] isEqualToString:@"8000"] || [resultDic[@"resultStatus"] isEqualToString:@"6004"]) {
  230. [ZCHUDHelper showTitle:@"支付成功"];
  231. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  232. [OCNotificationCenter postNotificationName:ALIPAY_SUCCESS_NOTIFICATION object:nil];
  233. });
  234. } else if ([resultDic[@"resultStatus"] isEqualToString:@"6001"]) {
  235. }else {
  236. [ZCHUDHelper showTitle:@"支付失败"];
  237. }
  238. }];
  239. }else{
  240. NSLog(@"这里----2");
  241. if (model.pay_obj.type == YMAccountBalabcePayType_H5) {
  242. NSDictionary*dic =model.pay_obj.data;
  243. NSURL *url = [NSURL URLWithString:dic[@"pay_info"]];
  244. // 检查是否可以打开URL
  245. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  246. // 打开URL
  247. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  248. }
  249. }else if (model.pay_obj.type == YMAccountBalabcePayTypeAlipay_H5) {
  250. NSLog(@"当前支付类型3---:%@",model.pay_obj.data);
  251. NSURL *url = [NSURL URLWithString:model.pay_obj.data];
  252. // 检查是否可以打开URL
  253. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  254. // 打开URL
  255. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  256. }
  257. }else {
  258. NSString *token = model.pay_obj.data;
  259. NSLog(@"%@",token);
  260. if (!token || token.length == 0) {
  261. [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败,请联系技术人员"]];
  262. return;
  263. }
  264. [SandPayGateService.shared queryTradeRecordInfoWith:token];
  265. SandPayGateService.shared.sandpayGateResultBlock = ^(NSString* status, NSDictionary* dataDic) {
  266. NSLog(@"输出🍀\n%@ \n%@",status, dataDic);
  267. if ([status isEqualToString:@"success"]) {
  268. } else {
  269. //[ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败:%@",errorMsg]];
  270. [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败"]];
  271. }
  272. };
  273. }
  274. // XHPayParameterModel *payParameter = [XHPayParameterModel yy_modelWithJSON:model.pay_obj.data];
  275. // [SDPTController shared].paySucTypeBlock = ^(ProductPyType type, id _Nonnull responseObj) {
  276. // NSLog(@"测试打印:%@",responseObj);
  277. // //跳转微信小程序
  278. // NSDictionary *pay_extra = [payParameter.pay_extra mj_JSONObject];
  279. // NSString * allPath = [NSString stringWithFormat:@"%@token_id=%@",pay_extra[@"path_url"],responseObj];
  280. // WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
  281. //
  282. // launchMiniProgramReq.userName = pay_extra[@"gh_ori_id"];
  283. // launchMiniProgramReq.path = allPath;
  284. // /// 0 正式版
  285. // launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease;
  286. // [WXApi sendReq:launchMiniProgramReq completion:^(BOOL success) {
  287. // if (success) {
  288. // NSLog(@"跳转微信成功");
  289. // } else {
  290. // NSLog(@"跳转微信异常");
  291. // }
  292. // }];
  293. //
  294. // };
  295. // [SDPTController shared].payFailureBlock = ^(ProductPyType type, NSString * _Nonnull errorMsg) {
  296. // [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败:%@",errorMsg]];
  297. // };
  298. //
  299. // if(payParameter){
  300. // [[SDPTController shared] singletonPaymentMethodWith:payParameter];
  301. // }else{
  302. // [ZCHUDHelper showTitle:@"支付数据错误"];
  303. // }
  304. }
  305. }
  306. }else{
  307. [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]];
  308. }
  309. } failure:^(NSError *error) {
  310. [ZCHUDHelper showTitle:error.localizedDescription];
  311. }];
  312. }
  313. - (RACSubject *)selectedRechargeItemSubject{
  314. if (!_selectedRechargeItemSubject) {
  315. _selectedRechargeItemSubject = [RACSubject subject];
  316. }
  317. return _selectedRechargeItemSubject;
  318. }
  319. - (RACSubject *)selectedPayMethodTypeSubject{
  320. if (!_selectedPayMethodTypeSubject) {
  321. _selectedPayMethodTypeSubject = [RACSubject subject];
  322. }
  323. return _selectedPayMethodTypeSubject;
  324. }
  325. @end