// // SPWalletJinbiVC.m // SweetParty // // Created by bj_szd on 2022/6/10. // #import "SPWalletJinbiVC.h" #import "SPWalletRechargeCell.h" #import "SPWalletModel.h" #import #import #import #import "SPWalletRecordListVC.h" @interface SPWalletJinbiVC () @property (weak, nonatomic) IBOutlet UIView *balanceBgV; @property (weak, nonatomic) IBOutlet UILabel *balanceLab; @property (weak, nonatomic) IBOutlet UICollectionView *collectionView; @property (weak, nonatomic) IBOutlet UIButton *confirmBtn; @property (weak, nonatomic) IBOutlet UILabel *buyTitleLab; @property (weak, nonatomic) IBOutlet UILabel *explainLab; @property (weak, nonatomic) IBOutlet UIButton *zfbBtn; @property (weak, nonatomic) IBOutlet UIButton *wxBtn; @property (weak, nonatomic) IBOutlet UIButton *huifuAliBtn; @property (nonatomic, strong) NSArray *dataArray; @property (nonatomic, assign) NSInteger selectIndex; @property (nonatomic, strong) SPWalletModel *model; @property (nonatomic, strong) WKWebView *webView; @property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLayout; @property (nonatomic, assign) NSInteger payType; @end @implementation SPWalletJinbiVC - (void)dealloc { [[NSNotificationCenter defaultCenter] removeObserver:self]; // [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillEnterForegroundNotification object:nil]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:animated]; [self fetchData]; } - (void)viewDidLoad { [super viewDidLoad]; [self showNaviBarWithTitle:@"充值"]; [self createUI]; [self fetchRechargeListData]; // [ControlCreator createButton:self.view rect:CGRectMake(ScreenWidth-65-10, yb_StatusBar_H+7, 60, 30) text:@"明细" font:YBMediumFont(13) color:HEXCOLOR(0x111111) backguoundColor:nil imageName:nil target:self action:@selector(onRightClick)]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(payResultMethod:) name:@"wechatPayResult" object:nil]; //1-支付宝 2-微信 3-汇付支付宝 self.payType = 1; // [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endBackgroundFormOutAlipay) name:UIApplicationWillEnterForegroundNotification object:nil]; // // WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init]; // configuration.userContentController = [[WKUserContentController alloc] init]; // configuration.preferences = [[WKPreferences alloc] init]; // configuration.preferences.minimumFontSize = 10; // configuration.preferences.javaScriptEnabled = YES; // configuration.preferences.javaScriptCanOpenWindowsAutomatically = YES; // // _webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration]; // _webView.navigationDelegate = self; // _webView.UIDelegate = self; // [self.view addSubview:_webView]; } //- (void)endBackgroundFormOutAlipay { // [self fetchData]; //} - (void)onRightClick { SPWalletRecordListVC *vc = [[SPWalletRecordListVC alloc] init]; vc.type = 4; [vc pushSelf]; } - (IBAction)coinSendRecordButtonClick:(UIButton *)sender { SPWalletRecordListVC *vc = [[SPWalletRecordListVC alloc] init]; vc.type = 1; [vc pushSelf]; } - (void)payResultMethod:(NSNotification *)note { int code = [note.object intValue]; switch (code) { case 1:{ //支付成功 [HelpPageDefine showMessage:@"充值成功"]; [self fetchData]; }break; case 2:{ //支付取消 [HelpPageDefine showMessage:@"支付取消"]; }break; case 3:{ //支付失败 [HelpPageDefine showMessage:@"支付失败"]; }break; default: break; } } - (void)fetchData { NSDictionary *params = @{}; [[AFNetworkRequset shared] postRequestWithParams:params Path:@"api/user/get_user_money" Loading:NO Hud:NO Success:^(id _Nonnull responseDic) { SPWalletModel *model = [SPWalletModel mj_objectWithKeyValues:responseDic[@"data"]]; self.model = model; self.balanceLab.text = model.integral; } Failure:^(id _Nonnull errorData) { }]; } - (void)fetchRechargeListData { NSDictionary *params = @{}; [[AFNetworkRequset shared] postRequestWithParams:params Path:@"api/pay/get_can_recharge_list" Loading:NO Hud:NO Success:^(id _Nonnull responseDic) { self.dataArray = [SPWalletRechargeModel mj_objectArrayWithKeyValuesArray:responseDic[@"data"]]; [self.collectionView reloadData]; } Failure:^(id _Nonnull errorData) { }]; } - (void)createUI { // self.balanceBgV.backgroundColor = [UIColor bm_colorGradientChangeWithSize:CGSizeMake(ScreenWidth-14*2, 130) direction:FXGradientChangeDirectionHorizontal startColor:HEXCOLOR(0xB651F4) endColor:HEXCOLOR(0x722DD4)]; UIImageView *bgImgV = [[UIImageView alloc] initWithImage:ImageNamed(@"home_bg")]; [self.view addSubview:bgImgV]; [self.view sendSubviewToBack:bgImgV]; [bgImgV mas_makeConstraints:^(MASConstraintMaker *make) { make.top.left.right.equalTo(self.view); make.height.mas_equalTo(ScreenWidth/375*812); }]; self.topLayout.constant = yb_NavigationBar_H+10; [self.confirmBtn setJianBianWithCGSize:CGSizeMake(ScreenWidth-27*2, 49)]; UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; CGFloat itemW = (ScreenWidth-15*4)/3; layout.itemSize = CGSizeMake(itemW, 82); layout.minimumInteritemSpacing = 0; layout.minimumLineSpacing = 10; layout.sectionInset = UIEdgeInsetsMake(0, 15, 0, 15); _collectionView.collectionViewLayout = layout; _collectionView.delegate = self; _collectionView.dataSource = self; [_collectionView registerNib:[UINib nibWithNibName:@"SPWalletRechargeCell" bundle:nil] forCellWithReuseIdentifier:@"SPWalletRechargeCell"]; } - (IBAction)onMingxi:(id)sender { SPWalletRecordListVC *vc = [[SPWalletRecordListVC alloc] init]; vc.type = 4; [vc pushSelf]; } - (IBAction)zhifubaoClick:(id)sender { self.payType = 1; [self onConfirm:nil]; } - (IBAction)weixinClick:(id)sender { self.payType = 2; [self onConfirm:nil]; } - (IBAction)onSelZfb:(id)sender { self.zfbBtn.selected = YES; self.wxBtn.selected = NO; self.huifuAliBtn.selected = NO; self.payType = 1; } - (IBAction)onSelWx:(id)sender { self.zfbBtn.selected = NO; self.wxBtn.selected = YES; self.huifuAliBtn.selected = NO; self.payType = 2; } - (IBAction)huifuButtonClick:(UIButton *)sender { self.zfbBtn.selected = NO; self.wxBtn.selected = NO; self.huifuAliBtn.selected = YES; self.payType = 3; } - (IBAction)onConfirm:(id)sender { if (self.payType == 1){ [self aliPayMethod]; }else if (self.payType == 2){ [self wechatPayMethod]; }else if (self.payType == 3){ [self huifuAliPayMethod]; } return; UIAlertController *sheetAlert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet]; UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"支付宝支付" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self aliPayMethod]; }]; [action1 setValue:mainDeepColor forKey:@"_titleTextColor"]; [sheetAlert addAction:action1]; UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"微信支付" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) { [self wechatPayMethod]; }]; [action2 setValue:mainDeepColor forKey:@"_titleTextColor"]; [sheetAlert addAction:action2]; UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]; [action3 setValue:HEXCOLOR(0x333333) forKey:@"_titleTextColor"]; [sheetAlert addAction:action3]; [self presentViewController:sheetAlert animated:YES completion:nil]; } - (void)wechatPayMethod { SPWalletRechargeModel *model = self.dataArray[self.selectIndex]; NSDictionary *params = @{@"crid":model.crid, @"pay_type":@"2", @"uid":BJUserManager.userInfo.uid}; [[AFNetworkRequset shared] postRequestWithParams:params Path:@"api/pay/create_pay_order" Loading:YES Hud:NO Success:^(id _Nonnull responseDic) { NSDictionary *dict = responseDic[@"data"]; PayReq *req = [[PayReq alloc] init]; req.nonceStr = [dict objectForKey:@"noncestr"]; req.timeStamp = [[dict objectForKey:@"timestamp"] intValue]; req.package = [dict objectForKey:@"package"]; req.partnerId = [dict objectForKey:@"partnerid"]; req.prepayId = [dict objectForKey:@"prepayid"]; req.sign = [dict objectForKey:@"sign"]; [WXApi sendReq:req completion:^(BOOL success) { }]; } Failure:^(id _Nonnull errorData) { }]; } - (void)aliPayMethod { SPWalletRechargeModel *model = self.dataArray[self.selectIndex]; NSDictionary *params = @{@"crid":model.crid, @"pay_type":@"1", @"uid":BJUserManager.userInfo.uid}; [[AFNetworkRequset shared] postRequestWithParams:params Path:@"api/pay/create_pay_order" Loading:YES Hud:NO Success:^(id _Nonnull responseDic) { [self onAliPay:[responseDic safeStringForKey:@"data"]]; } Failure:^(id _Nonnull errorData) { }]; } - (void)huifuAliPayMethod { SPWalletRechargeModel *model = self.dataArray[self.selectIndex]; NSDictionary *params = @{@"crid":model.crid, @"pay_type":@"21", @"uid":BJUserManager.userInfo.uid}; [[AFNetworkRequset shared] postRequestWithParams:params Path:@"api/pay/create_pay_order" Loading:YES Hud:NO Success:^(id _Nonnull responseDic) { NSDictionary *data = [responseDic safeDictionaryForKey:@"data"]; NSString *rqinfo = [data safeStringForKey:@"pay_info"]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:rqinfo] options:@{} completionHandler:^(BOOL success) { }]; } Failure:^(id _Nonnull errorData) { }]; } -(void)onAliPay:(NSString *)payStr { [[AlipaySDK defaultService] payOrder:payStr fromScheme:@"redAlipay" callback:^(NSDictionary *resultDic) { NSLog(@"支付宝回调%@", resultDic); if ([resultDic[@"resultStatus"] integerValue] == 9000) { [HelpPageDefine showMessage:@"充值成功"]; [self fetchData]; }else if ([resultDic[@"resultStatus"] integerValue] == 6001) { [HelpPageDefine showMessage:@"支付取消"]; }else { [HelpPageDefine showMessage:@"支付失败"]; } }]; } #pragma mark - UICollectionViewDelegate && UICollectionViewDataSource - (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView { return 1; } - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section { return self.dataArray.count; } - (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{ SPWalletRechargeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SPWalletRechargeCell" forIndexPath:indexPath]; // NSDictionary *dict = self.dataArray[indexPath.row]; // cell.priceLab.text = [NSString stringWithFormat:@"¥%@", dict[@"price"]]; // cell.coinLab.text = [NSString stringWithFormat:@"%@", dict[@"coin"]]; SPWalletRechargeModel *model = self.dataArray[indexPath.row]; cell.priceLab.text = [NSString stringWithFormat:@"¥%@", model.money]; cell.coinLab.text = model.integral; if (self.selectIndex == indexPath.row) { cell.bgImgV.image = ImageNamed(@"wallet_recharge_sel"); cell.coinLab.textColor = HEXCOLOR(0x333333); cell.priceLab.textColor = HEXCOLORA(0x333333, 1); }else { cell.bgImgV.image = ImageNamed(@"wallet_recharge_nor"); cell.coinLab.textColor = HEXCOLORA(0x333333, 1); cell.priceLab.textColor = HEXCOLORA(0x999999, 1); } return cell; } - (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath { self.selectIndex = indexPath.row; [self.collectionView reloadData]; } #pragma mark - JXCategoryListContentViewDelegate - (UIView *)listView { return self.view; } #pragma mark - 杉德支付 - (void)onShandePayWithType:(NSInteger)type { /// product_code有02020004 判断支付宝安装 同样也可以判断微信是否安装 微信scheme是 weixin:// if (type == 10) { if (![[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"alipays://"]]) { [HelpPageDefine showMessage:@"未安装支付宝APP"]; return; } } SPWalletRechargeModel *model = self.dataArray[self.selectIndex]; NSDictionary *params = @{@"crid":model.crid, @"pay_type":@(10)}; [[AFNetworkRequset shared] postRequestWithParams:params Path:@"api/pay/create_pay_order" Loading:YES Hud:NO Success:^(id _Nonnull responseDic) { [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:C_string(responseDic[@"data"])]]]; } Failure:^(id _Nonnull errorData) { }]; } - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { NSString * reqUrl = navigationAction.request.URL.absoluteString; if ([reqUrl hasPrefix:@"alipays://"] || [reqUrl hasPrefix:@"alipay://"]) { // NOTE: 跳转支付宝App BOOL bSucc = [[UIApplication sharedApplication] openURL:navigationAction.request.URL]; decisionHandler(WKNavigationActionPolicyCancel); return; } decisionHandler(WKNavigationActionPolicyAllow); } @end