YMMemberCenterViewModel.m 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. if ([self.payMethodCode containsString:@"wechat"]) {
  109. if(![WXApi isWXAppInstalled]){
  110. [ZCHUDHelper showTitle:@"未安装微信或版本过低"];
  111. return;
  112. }
  113. if (model.pay_obj.type == YMAccountBalabcePayType_H5) {
  114. NSDictionary*dic =model.pay_obj.data;
  115. NSURL *url = [NSURL URLWithString:dic[@"pay_info"]];
  116. // 检查是否可以打开URL
  117. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  118. // 打开URL
  119. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  120. }
  121. }else if (model.pay_obj.type == YMAccountBalabcePayTypeAlipay_H5) {
  122. NSLog(@"当前支付类型3---:%@",model.pay_obj.data);
  123. NSURL *url = [NSURL URLWithString:model.pay_obj.data];
  124. // 检查是否可以打开URL
  125. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  126. // 打开URL
  127. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  128. }
  129. }else if(model.pay_obj.type == YMAccountBalabcePayTypeWeChat_pay){
  130. NSDictionary *result_dict = [dict objectForKey:@"data"];
  131. NSDictionary *pay_obj_dict = [result_dict objectForKey:@"pay_obj"];
  132. NSDictionary *pay_obj_data = [pay_obj_dict objectForKey:@"data"];
  133. YMAccountBalanceWeChatInfoModel *wechatpayinfo = [YMAccountBalanceWeChatInfoModel yy_modelWithJSON:pay_obj_data];
  134. PayReq *request = [[PayReq alloc] init];
  135. request.partnerId = wechatpayinfo.mch_id;
  136. request.prepayId = wechatpayinfo.prepay_id;
  137. request.package = @"Sign=WXPay";
  138. request.nonceStr = wechatpayinfo.nonce_str;
  139. request.timeStamp = (uint32_t)[wechatpayinfo.timestamp intValue];
  140. request.sign = wechatpayinfo.paySign;
  141. NSString *partnerId = request.partnerId;
  142. NSString *prepayId = request.prepayId;
  143. NSString *package = request.package;
  144. NSString *nonceStr = request.nonceStr;
  145. NSInteger timeStamp = request.timeStamp;
  146. NSString *sign = request.sign;
  147. NSLog(@"输出🍀\n%@",@{
  148. @"partnerId":partnerId,
  149. @"prepayId":prepayId,
  150. @"package":package,
  151. @"nonceStr":nonceStr,
  152. @"timeStamp":@(timeStamp),
  153. @"sign":sign
  154. });
  155. [WXApi sendReq:request completion:^(BOOL success) {
  156. }];
  157. }else{
  158. NSString *token = model.pay_obj.data;
  159. NSLog(@"%@",token);
  160. if (!token || token.length == 0) {
  161. [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败,请联系技术人员"]];
  162. return;
  163. }
  164. [SandPayGateService.shared queryTradeRecordInfoWith:token];
  165. SandPayGateService.shared.sandpayGateResultBlock = ^(NSString* status, NSDictionary* dataDic) {
  166. NSLog(@"输出🍀\n%@ \n%@",status, dataDic);
  167. if ([status isEqualToString:@"success"]) {
  168. NSString *funcCode = dataDic[@"funcCode"];
  169. NSString *ghOriId = dataDic[@"ghOriId"];
  170. NSString *miniProgramType = dataDic[@"miniProgramType"];
  171. NSString *pathUrl = dataDic[@"pathUrl"];
  172. NSString *subAppId = dataDic[@"subAppId"];
  173. NSString *tokenId = dataDic[@"tokenId"];
  174. //跳转微信小程序
  175. NSString *allPath = [NSString stringWithFormat:@"%@token_id=%@&funcCode=%@", pathUrl, tokenId, funcCode];
  176. WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
  177. launchMiniProgramReq.userName = ghOriId;
  178. launchMiniProgramReq.path = allPath;
  179. launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease; // 使用适当的类型,例如WXMiniProgramTypeRelease
  180. [WXApi sendReq:launchMiniProgramReq completion:^(BOOL success) {
  181. if (success) {
  182. NSLog(@"跳转微信成功");
  183. } else {
  184. NSLog(@"跳转微信异常");
  185. }
  186. }];
  187. } else {
  188. //[ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败:%@",errorMsg]];
  189. [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败"]];
  190. }
  191. };
  192. // XHPayParameterModel *payParameter = [XHPayParameterModel yy_modelWithJSON:model.pay_obj.data];
  193. // [SDPTController shared].paySucTypeBlock = ^(ProductPyType type, id _Nonnull responseObj) {
  194. // NSLog(@"测试打印:%@",responseObj);
  195. // //跳转微信小程序
  196. // NSDictionary *pay_extra = [payParameter.pay_extra mj_JSONObject];
  197. // NSString * allPath = [NSString stringWithFormat:@"%@token_id=%@",pay_extra[@"path_url"],responseObj];
  198. // WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
  199. //
  200. // launchMiniProgramReq.userName = pay_extra[@"gh_ori_id"];
  201. // launchMiniProgramReq.path = allPath;
  202. // /// 0 正式版
  203. // launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease;
  204. // [WXApi sendReq:launchMiniProgramReq completion:^(BOOL success) {
  205. // if (success) {
  206. // NSLog(@"跳转微信成功");
  207. // } else {
  208. // NSLog(@"跳转微信异常");
  209. // }
  210. // }];
  211. //
  212. // };
  213. // [SDPTController shared].payFailureBlock = ^(ProductPyType type, NSString * _Nonnull errorMsg) {
  214. // [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败:%@",errorMsg]];
  215. // };
  216. //
  217. // if(payParameter){
  218. // [[SDPTController shared] singletonPaymentMethodWith:payParameter];
  219. // }else{
  220. // [ZCHUDHelper showTitle:@"支付数据错误"];
  221. // }
  222. }
  223. } else if ([self.payMethodCode containsString:@"alipay"]) {
  224. if(model.pay_obj.type == YMAccountBalabcePayTypeAliSdk_pay){
  225. NSString *appScheme = @"msyoupaipay";
  226. [[AlipaySDK defaultService] payOrder:model.payinfo fromScheme:appScheme callback:^(NSDictionary *resultDic) {
  227. NSLog(@"reslut ====== %@",resultDic);
  228. if ([resultDic[@"resultStatus"] isEqualToString:@"9000"] || [resultDic[@"resultStatus"] isEqualToString:@"8000"] || [resultDic[@"resultStatus"] isEqualToString:@"6004"]) {
  229. [ZCHUDHelper showTitle:@"支付成功"];
  230. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  231. [OCNotificationCenter postNotificationName:ALIPAY_SUCCESS_NOTIFICATION object:nil];
  232. });
  233. } else if ([resultDic[@"resultStatus"] isEqualToString:@"6001"]) {
  234. }else {
  235. [ZCHUDHelper showTitle:@"支付失败"];
  236. }
  237. }];
  238. }else{
  239. if (model.pay_obj.type == YMAccountBalabcePayType_H5) {
  240. NSDictionary*dic =model.pay_obj.data;
  241. NSURL *url = [NSURL URLWithString:dic[@"pay_info"]];
  242. // 检查是否可以打开URL
  243. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  244. // 打开URL
  245. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  246. }
  247. }else if (model.pay_obj.type == YMAccountBalabcePayTypeAlipay_H5) {
  248. NSLog(@"当前支付类型3---:%@",model.pay_obj.data);
  249. NSURL *url = [NSURL URLWithString:model.pay_obj.data];
  250. // 检查是否可以打开URL
  251. if ([[UIApplication sharedApplication] canOpenURL:url]) {
  252. // 打开URL
  253. [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
  254. }
  255. }else {
  256. NSString *token = model.pay_obj.data;
  257. NSLog(@"%@",token);
  258. if (!token || token.length == 0) {
  259. [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败,请联系技术人员"]];
  260. return;
  261. }
  262. [SandPayGateService.shared queryTradeRecordInfoWith:token];
  263. SandPayGateService.shared.sandpayGateResultBlock = ^(NSString* status, NSDictionary* dataDic) {
  264. NSLog(@"输出🍀\n%@ \n%@",status, dataDic);
  265. if ([status isEqualToString:@"success"]) {
  266. } else {
  267. //[ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败:%@",errorMsg]];
  268. [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败"]];
  269. }
  270. };
  271. }
  272. // XHPayParameterModel *payParameter = [XHPayParameterModel yy_modelWithJSON:model.pay_obj.data];
  273. // [SDPTController shared].paySucTypeBlock = ^(ProductPyType type, id _Nonnull responseObj) {
  274. // NSLog(@"测试打印:%@",responseObj);
  275. // //跳转微信小程序
  276. // NSDictionary *pay_extra = [payParameter.pay_extra mj_JSONObject];
  277. // NSString * allPath = [NSString stringWithFormat:@"%@token_id=%@",pay_extra[@"path_url"],responseObj];
  278. // WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
  279. //
  280. // launchMiniProgramReq.userName = pay_extra[@"gh_ori_id"];
  281. // launchMiniProgramReq.path = allPath;
  282. // /// 0 正式版
  283. // launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease;
  284. // [WXApi sendReq:launchMiniProgramReq completion:^(BOOL success) {
  285. // if (success) {
  286. // NSLog(@"跳转微信成功");
  287. // } else {
  288. // NSLog(@"跳转微信异常");
  289. // }
  290. // }];
  291. //
  292. // };
  293. // [SDPTController shared].payFailureBlock = ^(ProductPyType type, NSString * _Nonnull errorMsg) {
  294. // [ZCHUDHelper showTitle:[NSString stringWithFormat:@"支付失败:%@",errorMsg]];
  295. // };
  296. //
  297. // if(payParameter){
  298. // [[SDPTController shared] singletonPaymentMethodWith:payParameter];
  299. // }else{
  300. // [ZCHUDHelper showTitle:@"支付数据错误"];
  301. // }
  302. }
  303. }
  304. }else{
  305. [ZCHUDHelper showTitle:[dict stringValueForKey:@"message" defaultValue:@""]];
  306. }
  307. } failure:^(NSError *error) {
  308. [ZCHUDHelper showTitle:error.localizedDescription];
  309. }];
  310. }
  311. - (RACSubject *)selectedRechargeItemSubject{
  312. if (!_selectedRechargeItemSubject) {
  313. _selectedRechargeItemSubject = [RACSubject subject];
  314. }
  315. return _selectedRechargeItemSubject;
  316. }
  317. - (RACSubject *)selectedPayMethodTypeSubject{
  318. if (!_selectedPayMethodTypeSubject) {
  319. _selectedPayMethodTypeSubject = [RACSubject subject];
  320. }
  321. return _selectedPayMethodTypeSubject;
  322. }
  323. @end