Files
my_yuyin/QXLive/Mine(音域)/View/充值/QXRechargeView.m
2025-09-22 18:48:29 +08:00

512 lines
20 KiB
Objective-C

//
// QXRechargeView.m
// QXLive
//
// Created by 启星 on 2025/5/16.
//
#import "QXRechargeView.h"
#import "QXRechargePriceCell.h"
#import "QXRechargePayTypeCell.h"
#import "QXMineNetwork.h"
#import <AlipaySDK/AlipaySDK.h>
#import <WXApi.h>
@class QXRechargeCustomView,QXRechargeHeaderView;
@interface QXRechargeView()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,UIGestureRecognizerDelegate>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)QXRechargeCustomView *rechargeCustomView;
@property (nonatomic,strong)QXRechargeHeaderView *rechargeHeaderView;
@property (nonatomic,strong)UIButton *rechargeBtn;
@property (nonatomic,strong)UIView *bgView;
@property (nonatomic,assign)BOOL isCustom;
@property (nonatomic,strong)QXPayTypeModel *selectedPayTypeModel;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UITapGestureRecognizer *tap;
@property (nonatomic,strong)NSMutableArray *rechargeDataArray;
@property (nonatomic,strong)NSMutableArray *payTypeArray;
@end
@implementation QXRechargeView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubviews];
}
return self;
}
-(void)initSubviews{
QXRechargeListModel *model = [[QXRechargeListModel alloc] init];
model.money = @"0";
model.coins = @"0";
[self.rechargeDataArray addObject:model];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hide)];
tap.delegate = self;
tap.enabled = NO;
self.tap = tap;
[self addGestureRecognizer:tap];
self.bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, self.height)];
self.bgView.backgroundColor = [UIColor clearColor];
[self addSubview:self.bgView];
self.titleLabel = [[UILabel alloc] init];
self.titleLabel.textColor = QXConfig.textColor;
self.titleLabel.font = [UIFont boldSystemFontOfSize:16];
self.titleLabel.text = QXText(@"充值");
self.titleLabel.hidden = YES;
[self.bgView addSubview:self.titleLabel];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.top.mas_equalTo(12);
make.centerX.equalTo(self.bgView);
}];
[self.bgView addSubview:self.collectionView];
[self.bgView addSubview:self.rechargeBtn];
[self.rechargeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(-38);
make.left.mas_equalTo(38);
make.height.mas_equalTo(42);
make.bottom.mas_equalTo(-kSafeAreaBottom);
}];
[self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(0);
make.left.mas_equalTo(0);
make.top.equalTo(self.bgView);
make.bottom.equalTo(self.rechargeBtn.mas_top).offset(-10);
}];
[self getRechargeList];
}
-(void)getRechargeList{
MJWeakSelf
[QXMineNetwork getRechargeListSuccessBlock:^(NSArray<QXRechargeListModel *> * _Nonnull list) {
[weakSelf.rechargeDataArray removeAllObjects];
[weakSelf.rechargeDataArray addObjectsFromArray:list];
QXRechargeListModel *model = [[QXRechargeListModel alloc] init];
model.money = @"0";
model.coins = @"0";
[weakSelf.rechargeDataArray addObject:model];
[weakSelf.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
} failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
}];
[QXMineNetwork walletPayTypeWithUserId:QXGlobal.shareGlobal.loginModel.user_id successBlock:^(QXPayTypeStatusModel * _Nonnull model) {
[weakSelf.payTypeArray removeAllObjects];
if (model.wx.is_pay_open.intValue == 1) {
[weakSelf.payTypeArray addObject:model.wx];
}
if (model.ali.is_pay_open.intValue == 1) {
[weakSelf.payTypeArray addObject:model.ali];
}
if (model.wx_tl.is_pay_open.intValue == 1) {
[weakSelf.payTypeArray addObject:model.wx_tl];
}
if (model.ali_tl.is_pay_open.intValue == 1) {
[weakSelf.payTypeArray addObject:model.ali_tl];
}
if (!self.isOnlyDisplayPayType) {
[weakSelf.collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];
}
} failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
}];
}
-(void)setIsPop:(BOOL)isPop{
_isPop = isPop;
self.tap.enabled = YES;
self.bgView.frame = CGRectMake(0, 0, self.width, ScaleWidth(575));
self.bgView.backgroundColor = [UIColor whiteColor];
self.backgroundColor = [UIColor colorWithWhite:0.0 alpha:0.3];
self.titleLabel.hidden = !isPop;
[self.bgView addRoundedCornersWithRadius:16 byRoundingCorners:(UIRectCornerTopLeft|UIRectCornerTopRight)];
[self.collectionView mas_remakeConstraints:^(MASConstraintMaker *make) {
make.right.mas_equalTo(0);
make.left.mas_equalTo(0);
make.top.equalTo(self.bgView).offset(50);
make.bottom.equalTo(self.rechargeBtn.mas_top).offset(-10);
}];
}
-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
return touch.view == self;
}
-(void)showInView:(UIView *)view{
self.bgView.y = SCREEN_HEIGHT;
[view addSubview:self];
[UIView animateWithDuration:0.3 animations:^{
self.bgView.y = SCREEN_HEIGHT-self.bgView.height;
}];
}
-(void)hide{
if (!self.isPop) {
return;
}
[UIView animateWithDuration:0.3 animations:^{
self.bgView.y = SCREEN_HEIGHT;
} completion:^(BOOL finished) {
[self removeFromSuperview];
}];
}
//-(void)setRechargeDataArray:(NSArray *)rechargeDataArray{
// _rechargeDataArray = rechargeDataArray;
// [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
//}
//-(void)setPayTypeArray:(NSArray *)payTypeArray{
// _payTypeArray = payTypeArray;
// [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:1]];
//}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
if (self.isOnlyDisplayPayType) {
return 1;
}
return 2;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
if (section == 0 && !self.isOnlyDisplayPayType) {
return self.rechargeDataArray.count;
}
return self.payTypeArray.count;
}
-(__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0 && !self.isOnlyDisplayPayType) {
QXRechargePriceCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QXRechargePriceCell" forIndexPath:indexPath];
cell.model = self.rechargeDataArray[indexPath.row];
return cell;
}else{
QXRechargePayTypeCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"QXRechargePayTypeCell" forIndexPath:indexPath];
cell.model = self.payTypeArray[indexPath.row];
return cell;
}
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0 && !self.isOnlyDisplayPayType) {
return CGSizeMake((SCREEN_WIDTH-16*2-12*2-1)/3, 76);
}
return CGSizeMake(SCREEN_WIDTH, 40);
}
-(CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
if (section == 0) {
return 12;
}
return 0;
}
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0 && [kind isEqualToString:UICollectionElementKindSectionFooter] && self.isCustom) {
self.rechargeCustomView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"QXRechargeCustomView" forIndexPath:indexPath];
return self.rechargeCustomView;
}else if (indexPath.section == 1 && [kind isEqualToString:UICollectionElementKindSectionHeader] ) {
self.rechargeHeaderView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"QXRechargeHeaderView" forIndexPath:indexPath];
return self.rechargeHeaderView;
}
return nil;
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
if (section == 0) {
return CGSizeZero;
}
return CGSizeMake(SCREEN_WIDTH, 40);
}
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
if (section == 0 && self.isCustom) {
return CGSizeMake(SCREEN_WIDTH, 50);
}
return CGSizeZero;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.section == 0 && !self.isOnlyDisplayPayType) {
QXRechargeListModel *model = self.rechargeDataArray[indexPath.row];
if (self.selectedModel) {
self.selectedModel.isSelected = NO;
}
model.isSelected = YES;
self.selectedModel = model;
if (model.money.intValue == 0 && model.coins.intValue == 0) {
self.isCustom = YES;
}else{
self.isCustom = NO;
}
[collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
}else{
QXPayTypeModel *model = self.payTypeArray[indexPath.row];
if (model == self.selectedPayTypeModel) {
return;
}
if (self.selectedPayTypeModel) {
self.selectedPayTypeModel.isSelected = NO;
}
model.isSelected = YES;
self.selectedPayTypeModel = model;
[collectionView reloadData];
}
}
-(void)rechargeAction:(UIButton*)sender{
if (self.selectedPayTypeModel == nil) {
showToast(@"请选择支付方式");
return;
}
if (self.selectedModel == nil) {
showToast(@"请选择充值金额");
return;
}
if (self.isPop) {
[self hide];
}
MJWeakSelf
[QXMineNetwork rechargePayWithMoney:self.selectedModel.money coin:self.selectedModel.coins type:self.selectedPayTypeModel.type userId:QXGlobal.shareGlobal.loginModel.user_id successBlock:^(NSDictionary * _Nonnull dict) {
if (weakSelf.selectedPayTypeModel.type.intValue == 2) {
NSDictionary *resultDict = dict[@"data"];
NSString *order = [NSString stringWithFormat:@"%@",resultDict[@"ali"]];
[[AlipaySDK defaultService] payOrder:order fromScheme:@"midilive" callback:^(NSDictionary *resultDic) {
NSLog(@"支付宝H5支付回调 - %@", resultDic);
}];
}else if (weakSelf.selectedPayTypeModel.type.intValue == 1) {
NSDictionary *resultDict = dict[@"data"][@"wx"];
NSString *appid = resultDict[@"appid"];
NSString *partnerId = resultDict[@"partnerid"];
NSString *prepayId = [NSString stringWithFormat:@"%@",resultDict[@"prepayid"]];
NSString *nonceStr = resultDict[@"noncestr"];
UInt32 timeStamp = (UInt32)[resultDict[@"timestamp"] intValue];
NSString *package = resultDict[@"package"];
NSString *sign = resultDict[@"sign"];
PayReq *req = [[PayReq alloc] init];
req.openID = appid;
req.partnerId = partnerId;
req.prepayId = prepayId;
req.nonceStr = nonceStr;
req.timeStamp = timeStamp;
req.package = package;
req.sign = sign;
[WXApi sendReq:req completion:^(BOOL success) {
}];
}
else if (weakSelf.selectedPayTypeModel.type.intValue == 4) {
NSMutableDictionary*dic = [NSMutableDictionary dictionaryWithDictionary:dict[@"data"][@"tl"]];
[dic removeObjectForKey:@"json_data"];
NSString *json = [dic jsonStringEncoded];
NSString *thirdPartSchema = @"thirdPartSchema=qxlive://";
NSString *json1 = [json stringByReplacingOccurrencesOfString:@"\\" withString:@""];
NSCharacterSet *customSet1 = [[NSCharacterSet characterSetWithCharactersInString:@"!*'();:@&=+$,/?%#[]{}\""] invertedSet];
// NSCharacterSet *customSet2 = [[NSCharacterSet characterSetWithCharactersInString:@"!*'();:@&=+$,/?%#[]{}\""] invertedSet];
NSCharacterSet *customSet3 = [[NSCharacterSet characterSetWithCharactersInString:@"!*'();:@&=+$,/?%#[]{}\""] invertedSet];
NSString *json2 = [json1 stringByAddingPercentEncodingWithAllowedCharacters:customSet1];
NSString *encodedString = [thirdPartSchema stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
NSString *queryString = [NSString stringWithFormat:@"payinfo=%@", json2];
NSString *doubleEncodedQuery = [queryString stringByAddingPercentEncodingWithAllowedCharacters:customSet3];
NSString *jumpStr = [NSString stringWithFormat:@"alipays://platformapi/startapp?appId=2021001104615521&page=pages/orderDetail/orderDetail&%@&query=%@",encodedString,doubleEncodedQuery];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:jumpStr] options:@{} completionHandler:nil];
}else if (weakSelf.selectedPayTypeModel.type.intValue == 5) {
WXLaunchMiniProgramReq *launchMiniProgramReq = [WXLaunchMiniProgramReq object];
launchMiniProgramReq.userName = @"gh_e64a1a89a0ad";
NSDictionary *dic = dict[@"data"][@"tl"];
NSString *param = @"";
for (NSString*key in dic.allKeys) {
if (param.length == 0) {
param = [param stringByAppendingFormat:@"%@=%@",key,dic[key]];
}else{
param = [param stringByAppendingFormat:@"&%@=%@",key,dic[key]];
}
}
launchMiniProgramReq.path = [NSString stringWithFormat:@"pages/orderDetail/orderDetail?%@",param];
launchMiniProgramReq.miniProgramType = WXMiniProgramTypeRelease;
[WXApi sendReq:launchMiniProgramReq completion:nil];
}
} failBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
showToast(msg)
}];
}
-(UICollectionView *)collectionView{
if (!_collectionView) {
UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
layout.minimumLineSpacing = 12;
layout.minimumInteritemSpacing = 12;
layout.sectionInset = UIEdgeInsetsMake(0, 16, 0, 16);
_collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
_collectionView.delegate = self;
_collectionView.dataSource = self;
_collectionView.backgroundColor = [UIColor clearColor];
[_collectionView registerNib:[UINib nibWithNibName:@"QXRechargePriceCell" bundle:[NSBundle mainBundle]] forCellWithReuseIdentifier:@"QXRechargePriceCell"];
[_collectionView registerClass:[QXRechargePayTypeCell class] forCellWithReuseIdentifier:@"QXRechargePayTypeCell"];
[_collectionView registerClass:[QXRechargeCustomView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"QXRechargeCustomView"];
[_collectionView registerClass:[QXRechargeHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"QXRechargeHeaderView"];
}
return _collectionView;
}
-(UIButton *)rechargeBtn{
if (!_rechargeBtn) {
_rechargeBtn = [[UIButton alloc] init];
[_rechargeBtn setTitle:QXText(@"确认充值") forState:(UIControlStateNormal)];
[_rechargeBtn setTitleColor:QXConfig.btnTextColor forState:(UIControlStateNormal)];
[_rechargeBtn addTarget:self action:@selector(rechargeAction:) forControlEvents:(UIControlEventTouchUpInside)];
[_rechargeBtn addRoundedCornersWithRadius:21];
_rechargeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
_rechargeBtn.backgroundColor = QXConfig.themeColor;
}
return _rechargeBtn;
}
-(QXRechargeCustomView *)rechargeCustomView{
if (!_rechargeCustomView) {
_rechargeCustomView = [[QXRechargeCustomView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
}
return _rechargeCustomView;
}
-(QXRechargeHeaderView *)rechargeHeaderView{
if (!_rechargeHeaderView) {
_rechargeHeaderView = [[QXRechargeHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 40)];
}
return _rechargeHeaderView;
}
-(NSMutableArray *)rechargeDataArray{
if (!_rechargeDataArray) {
_rechargeDataArray = [NSMutableArray array];
}
return _rechargeDataArray;
}
-(NSMutableArray *)payTypeArray{
if (!_payTypeArray) {
_payTypeArray = [NSMutableArray array];
}
return _payTypeArray;
}
@end
@implementation QXRechargeCustomView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubviews];
}
return self;
}
- (instancetype)init
{
self = [super init];
if (self) {
[self initSubviews];
}
return self;
}
-(void)initSubviews{
self.bgView = [[UIView alloc] init];
[self addSubview:self.bgView];
[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(16);
make.right.mas_equalTo(-16);
make.top.bottom.equalTo(self);
}];
self.unitLabel = [[UILabel alloc] init];
self.unitLabel.text = @"";
self.unitLabel.font = [UIFont boldSystemFontOfSize:18];
self.unitLabel.textColor = RGB16(0x333333);
[self.bgView addSubview:self.unitLabel];
[self.unitLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.centerY.left.equalTo(self.bgView);
}];
self.cornLabel = [[UILabel alloc] init];
self.cornLabel.font = [UIFont systemFontOfSize:12];
self.cornLabel.textColor = RGB16(0x333333);
self.cornLabel.text = [NSString stringWithFormat:@"%@0%@",QXText(@"将获得"),QXText(@"金币")];
[self.bgView addSubview:self.cornLabel];
[self.cornLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.right.centerY.equalTo(self.bgView);
}];
self.textField = [[UITextField alloc] init];
self.textField.returnKeyType = UIReturnKeyDone;
self.textField.delegate = self;
self.textField.font = [UIFont systemFontOfSize:13];
self.textField.textColor = RGB16(0x333333);
self.textField.placeholder = QXText(@"请输入充值金额");
[self.bgView addSubview:self.textField];
[self.textField mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.unitLabel.mas_right).offset(3);
make.top.bottom.equalTo(self.bgView);
make.width.mas_equalTo(260);
}];
self.bottomLine = [[UIView alloc] init];
self.bottomLine.backgroundColor = RGB16(0x999999);
[self.bgView addSubview:self.bottomLine];
[self.bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.bottom.right.equalTo(self.bgView);
make.height.mas_equalTo(0.5);
}];
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[textField resignFirstResponder];
return YES;
}
@end
@implementation QXRechargeHeaderView
- (instancetype)init
{
self = [super init];
if (self) {
[self initSubviews];
}
return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self initSubviews];
}
return self;
}
-(void)initSubviews{
self.bgView = [[UIView alloc] init];
[self addSubview:self.bgView];
[self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.mas_equalTo(16);
make.right.mas_equalTo(-16);
make.top.bottom.equalTo(self);
}];
self.titleLabel = [[UILabel alloc] init];
self.titleLabel.text = QXText(@"支付方式");
self.titleLabel.font = [UIFont boldSystemFontOfSize:16];
self.titleLabel.textColor = RGB16(0x333333);
[self.bgView addSubview:self.titleLabel];
[self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.bgView);
make.right.mas_equalTo(-16);
make.top.bottom.equalTo(self);
}];
}
@end