ExchangeViewController.m
12.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
//
// ExchangeViewController.m
// IVTMeLiveTwo
//
// Created by xrg on 16/9/29.
// Copyright © 2016年 QLQ. All rights reserved.
//
#import "ExchangeViewController.h"
#import "IVTMyAccountTableViewCell.h"
static int mitang;
@interface ExchangeViewController ()<UITableViewDataSource,UITableViewDelegate,UIActionSheetDelegate,UIActionSheetDelegate>
@property (nonatomic, strong) UITableView *accountTableview;
@property (nonatomic, strong) UILabel *accountText;
@property (nonatomic, strong) UIImageView *goldImg;
@property (nonatomic, strong) UILabel *accountNumber;
@property (nonatomic, strong) NSMutableArray *listArr;
@end
@implementation ExchangeViewController
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[self request];
[self creatTableView];
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"返回键"] style:UIBarButtonItemStylePlain target:self action:@selector(backToTopPage:)];
leftBarButton.tintColor = [UIColor whiteColor];
self.navigationItem.leftBarButtonItem = leftBarButton;
[self exchangeBySelf];
_listArr = [[NSMutableArray alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - 创建tableview
- (void)creatTableView
{
_accountTableview = [[UITableView alloc] initWithFrame:CGRectMake(0,0, kScreenWidth, kScreenHeight) style:UITableViewStyleGrouped];
_accountTableview.delegate = self;
_accountTableview.dataSource = self;
_accountTableview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
_accountTableview.scrollEnabled = NO;
[self.view addSubview:_accountTableview];
UIView *headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 50)];
headView.backgroundColor = [UIColor whiteColor];
_accountTableview.tableHeaderView = headView;
_accountText = [[UILabel alloc] initWithFrame:CGRectZero];
_accountText.text = @"中国币余额";
_accountText.textColor = [UIColor IVTMainTextColor];
_accountText.font = [UIFont IVTH3Font];
[_accountText sizeToFit];
_accountText.center = CGPointMake(16+_accountText.frame.size.width/2, headView.frame.size.height/2);
[headView addSubview:_accountText];
_accountNumber = [[UILabel alloc] initWithFrame:CGRectMake(_accountText.frame.size.width + 10, 10, 100, 30)];
_accountNumber.text = [NSString stringWithFormat:@"%.0f", [[IVTAccountTool getMeCoinBalance] floatValue]];
_accountNumber.textColor = [UIColor IVTMainTextColor];
_accountNumber.font = [UIFont IVTH3Font];
_accountNumber.textAlignment = 1;
[headView addSubview:_accountNumber];
}
- (void)exchangeBySelf
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.backgroundColor = [UIColor colorWithRed:0.7765 green:0.0 blue:0.3137 alpha:1.0];
button.titleLabel.font = [UIFont systemFontOfSize:13.0];
button.titleLabel.tintColor = [UIColor whiteColor];
button.frame = CGRectMake(kScreenWidth / 2 - 50, 300,100, 40);
button.layer.cornerRadius = 20;
[button setTitle:@"自定义金额" forState:UIControlStateNormal];
[button addTarget:self action:@selector(exchangeBySelfFunc:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (IBAction)exchangeBySelfFunc:(id)sender {
UILabel * mitangLabel = [[UILabel alloc] init];
mitangLabel.text = [NSString stringWithFormat:@"蜜糖 = 0中国币"];
UIAlertController * alertController = [UIAlertController alertControllerWithTitle: @"兑换中国币"
message: [NSString stringWithFormat:@"账户余额%.0f蜜糖", [[IVTAccountTool getMeCoinBalance] floatValue]]
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField *textField) {
textField.placeholder = @"0";
textField.keyboardType = UIKeyboardTypePhonePad;
textField.textColor = [UIColor blueColor];
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.borderStyle = UITextBorderStyleRoundedRect;
[textField addTarget:self action:@selector(uernameChange:) forControlEvents:UIControlEventEditingChanged];
}];
[alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
if ([[IVTAccountTool getMeCoinBalance] floatValue] - mitang < 0) {
[SVProgressHUD showErrorWithStatus:@"蜜糖币余额不足,无法完成支付"];
}else{
[SVProgressHUD showWithStatus:@"充值中…"];
NSMutableDictionary * paramDict =[[NSMutableDictionary alloc] init];
[paramDict setObject:[IVTAccountTool getUserId] forKey:@"username"];
[paramDict setObject:[IVTAccountTool getPassword] forKey:@"captcha"];
[paramDict setObject:[NSString stringWithFormat:@"%zd",mitang] forKey:@"money"];
[paramDict setObject:@"meWithdrawSuccess" forKey:@"withdrawMark"];
[[IVTNetworkAPIClient sharedClient] withdrawMarkSuccess:paramDict success:^(NSDictionary *successData) {
if ([successData[@"data"] isEqualToString:@"0"]) {
[IVTAccountTool setMeCoinBalance:_accountNumber.text];
}
dispatch_async(dispatch_get_main_queue(), ^{
_accountNumber.text = [NSString stringWithFormat:@"%.0f", [[IVTAccountTool getMeCoinBalance] floatValue] - mitang];
[IVTAccountTool setMeCoinBalance:_accountNumber.text];
});
[SVProgressHUD showSuccessWithStatus:@"充值成功"];
} error:^(NSDictionary *successData) {
NSString *msg = successData[@"description"];
[SVProgressHUD setMinimumDismissTimeInterval:2];
[SVProgressHUD showErrorWithStatus:msg];
} failure:^(NSError *failureError) {
}];
}
}]];
[alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
}]];
[self presentViewController:alertController animated:YES completion:nil];
}
- (void)uernameChange:(UITextField *)textField
{
mitang = [textField.text intValue];
LRLog(@"-------------------------%d",mitang);
}
#pragma mark - 请求数据
- (void)request
{
__weak __typeof(self)vc = self;
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setObject:[IVTAccountTool getUserId] forKey:@"username"];
[[IVTNetworkAPIClient sharedClient] getConversionPrice:dict success:^(NSDictionary *successData) {
NSArray *listArray = [successData objectForKey:@"list"];
for (NSMutableDictionary *dict in listArray) {
IVTMyAccountTableViewModel *accountModel = [[IVTMyAccountTableViewModel alloc] initWithDictionary:dict];
[vc.listArr addObject:accountModel];
}
dispatch_async(dispatch_get_main_queue(), ^{
[vc.accountTableview reloadData];
});
} error:^(NSDictionary *successData) {
} failure:^(NSError *failureError) {
}];
}
#pragma mark - delegate&datasorce
// 设置行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
return 48;
}
- ( CGFloat )tableView:( UITableView *)tableView heightForHeaderInSection:( NSInteger )section
{
return 36.0 ;
}
// 设置标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
self.title = @"兑换";
return self.title;
}
//1、确定tableView中的分区数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
//2、确定每个section中的cell数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return _listArr.count;
}
//3、确定Cell中的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";
IVTMyAccountTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[IVTMyAccountTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
cell.charge.frame = CGRectMake(kScreenWidth - 110, 10, 100, 25);
IVTMyAccountTableViewModel *accountTableViewModel = [_listArr objectAtIndex:indexPath.row];
cell.accountModel = accountTableViewModel;
if (indexPath.row == 0) {
[cell.charge setTitle:@"1200蜜糖" forState:UIControlStateNormal];
}else if(indexPath.row == 1) {
[cell.charge setTitle:@"6000蜜糖" forState:UIControlStateNormal];
}else if(indexPath.row == 2) {
[cell.charge setTitle:@"18000蜜糖" forState:UIControlStateNormal];
}
return cell;
}
//4、点击cell时
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
IVTMyAccountTableViewModel * newModel = _listArr[indexPath.row];
if (indexPath.row == 0) {
mitang = 1200;
}else if (indexPath.row == 1){
mitang = 6000;
}else if (indexPath.row == 2){
mitang = 18000;
}
UIAlertView * alertV = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"你确定要用%zd蜜糖兑换%.0f中国币?", mitang,[newModel.chineseCoin floatValue]] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];;
[alertV show];
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 1) {
if ([[IVTAccountTool getMeCoinBalance] floatValue] - mitang < 0) {
[SVProgressHUD showErrorWithStatus:@"蜜糖币余额不足,无法完成支付"];
}else{
[SVProgressHUD showWithStatus:@"充值中…"];
NSMutableDictionary * paramDict =[[NSMutableDictionary alloc] init];
[paramDict setObject:[IVTAccountTool getUserId] forKey:@"username"];
[paramDict setObject:[IVTAccountTool getPassword] forKey:@"captcha"];
[paramDict setObject:[NSString stringWithFormat:@"%d",mitang] forKey:@"money"];
[paramDict setObject:@"meWithdrawSuccess" forKey:@"withdrawMark"];
[[IVTNetworkAPIClient sharedClient] withdrawMarkSuccess:paramDict success:^(NSDictionary *successData) {
if ([successData[@"data"] isEqualToString:@"0"]) {
[IVTAccountTool setMeCoinBalance:_accountNumber.text];
}
dispatch_async(dispatch_get_main_queue(), ^{
[IVTAccountTool setMeCoinBalance:[NSString stringWithFormat:@"%.f",[[IVTAccountTool getMeCoinBalance] floatValue] + (mitang * [[IVTAccountTool getAppInfoByType:7] floatValue]) * 100]];
_accountNumber.text = [NSString stringWithFormat:@"%.0f", [[IVTAccountTool getMeCoinBalance] floatValue] - mitang];
[IVTAccountTool setMeCoinBalance:_accountNumber.text];
});
[SVProgressHUD showSuccessWithStatus:@"充值成功"];
} error:^(NSDictionary *successData) {
NSString *msg = successData[@"description"];
[SVProgressHUD setMinimumDismissTimeInterval:2];
[SVProgressHUD showErrorWithStatus:msg];
} failure:^(NSError *failureError) {
[HUDUtil showError:@"网络异常"];
}];
}
}else {
}
}
#pragma mark - 按钮点击事件
//返回按钮点击
-(void)backToTopPage:(id)sender
{
if (self.accountNumberEdit) {
self.accountNumberEdit(_accountNumber.text);
}
[self.navigationController popViewControllerAnimated:YES];
[SVProgressHUD dismiss];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end