YMMemberCenterViewModel.m 21 KB

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