ConverDiamondsViewController.m 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. //
  2. // ConverDiamondsViewController.m
  3. // BuguLive
  4. //
  5. // Created by yy on 16/7/19.
  6. // Copyright © 2016年 xfg. All rights reserved.
  7. //
  8. #import "ConverDiamondsViewController.h"
  9. #import "MBProgressHUD.h"
  10. #import "FreshAcountModel.h"
  11. @interface ConverDiamondsViewController ()<UITextFieldDelegate,MBProgressHUDDelegate>
  12. @end
  13. @implementation ConverDiamondsViewController
  14. {
  15. NSString *ratio;//兑换比例
  16. int rat;
  17. }
  18. - (void)viewWillDisappear:(BOOL)animated
  19. {
  20. [super viewWillDisappear:animated];
  21. // [IQKeyboardManager sharedManager].enable = NO;
  22. // [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
  23. }
  24. - (void)viewWillAppear:(BOOL)animated
  25. {
  26. [_leftTextfield becomeFirstResponder];
  27. if (_whetherGame)
  28. {
  29. FWWeakify(self)
  30. [[IMAPlatform sharedInstance].host getMyInfo:^(AppBlockModel *blockModel) {
  31. FWStrongify(self)
  32. long currentDiamonds = [[IMAPlatform sharedInstance].host getDiamonds];
  33. self.tickte = [NSString stringWithFormat:@"%ld",currentDiamonds];
  34. self.acountLabel.text =[NSString stringWithFormat:ASLocalizedString(@"账户:%@%@"),self.tickte,self.BuguLive.appModel.diamond_name];
  35. }];
  36. [self obtainCoinProportion];
  37. }
  38. }
  39. - (void)viewDidLoad
  40. {
  41. [super viewDidLoad];
  42. // [IQKeyboardManager sharedManager].enable = YES;
  43. // [IQKeyboardManager sharedManager].enableAutoToolbar = NO;
  44. self.view.backgroundColor = kGrayTransparentColor4;
  45. _backView.layer.cornerRadius =5;
  46. _diamondsLabel.textColor = kAppGrayColor1;
  47. if (_whetherGame)
  48. {
  49. ratio = @"1"; //兑换比例
  50. _diamondsLabel.text = ASLocalizedString(@"兑换游戏币");
  51. _rightTextField.text =ASLocalizedString(@"0游戏币");
  52. _leftTextfield.placeholder =[NSString stringWithFormat:ASLocalizedString(@"请输入%@金额"),self.BuguLive.appModel.diamond_name];
  53. }
  54. else
  55. {
  56. _diamondsLabel.text = [NSString stringWithFormat:ASLocalizedString(@"兑换%@"),self.BuguLive.appModel.diamond_name];
  57. _rightTextField.text = [NSString stringWithFormat:@"0%@",self.BuguLive.appModel.diamond_name];
  58. _acountLabel.text =[NSString stringWithFormat:ASLocalizedString(@"账户:%@%@"),_tickte,self.BuguLive.appModel.ticket_name];
  59. _leftTextfield.placeholder =[NSString stringWithFormat:ASLocalizedString(@"请输入兑换的%@金额"),self.BuguLive.appModel.ticket_name];
  60. [self requetRatio];
  61. }
  62. _acountLabel.textColor =kAppGrayColor3;
  63. _textFieldView.layer.cornerRadius =15;
  64. _textFieldView.backgroundColor = kGrayTransparentColor1;
  65. [_cancelButton addTarget:self action:@selector(cancelButtonActon) forControlEvents:UIControlEventTouchUpInside];
  66. [_cancelButton setTitleColor:kAppGrayColor1 forState:UIControlStateNormal];
  67. _rightTextField.delegate =self;
  68. _leftTextfield.delegate =self;
  69. //设置键盘
  70. _leftTextfield.keyboardType = UIKeyboardTypeNumberPad;
  71. _rightTextField.textColor = kAppGrayColor1;
  72. _rightTextField.tag =300;
  73. if ([_rightTextField.text isEqualToString:@""]) {
  74. if (_whetherGame)
  75. {
  76. _rightTextField.text =ASLocalizedString(@"0游戏币");
  77. }
  78. else
  79. {
  80. _rightTextField.text = [NSString stringWithFormat:@"0%@",self.BuguLive.appModel.diamond_name];
  81. }
  82. }
  83. //兑换比例
  84. _exchangeRate_Lab.textColor = kAppGrayColor3;
  85. }
  86. #pragma marlk textfieldDelegate代理
  87. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  88. if (textField.tag ==300) {
  89. return NO;
  90. }
  91. else{
  92. return YES;
  93. }
  94. }
  95. - (void)textFieldDidEndEditing:(UITextField *)textField;
  96. {
  97. [self.view layoutIfNeeded];
  98. [UIView beginAnimations:nil context:nil];
  99. [UIView setAnimationDuration:0.1];
  100. _backView.centerY =_backView.frame.origin.y+200;
  101. [UIView commitAnimations];
  102. }
  103. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  104. NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
  105. // if ([_rightTextField.text intValue]>0) {
  106. //
  107. //
  108. // }
  109. if (toBeString.length > 6 && range.length!=1){
  110. textField.text = [toBeString substringToIndex:6];
  111. return NO;
  112. }
  113. rat =[toBeString intValue] *[ratio floatValue];
  114. if (toBeString.floatValue >0) {
  115. [_converButton setTitleColor:kAppGrayColor1 forState:UIControlStateNormal];
  116. [_converButton addTarget:self action:@selector(converButtonActon) forControlEvents:UIControlEventTouchUpInside];
  117. }
  118. if (toBeString.floatValue ==0) {
  119. [_converButton setTitleColor:kAppGrayColor1 forState:UIControlStateNormal];
  120. }
  121. if (_whetherGame) {
  122. _rightTextField.text= [NSString stringWithFormat:ASLocalizedString(@"%d游戏币"),rat];
  123. }
  124. else
  125. {
  126. _rightTextField.text= [NSString stringWithFormat:@"%d%@",rat,self.BuguLive.appModel.diamond_name];
  127. }
  128. NSLog(@"++++++++++++++++++++++++%d", rat);
  129. return [self validateNumber:string];
  130. }
  131. //- (BOOL)textFieldShouldClear:(UITextField *)textField;
  132. //{
  133. // [_converButton setTitleColor:kAppGrayColor1 forState:UIControlStateNormal];
  134. //
  135. // return YES;
  136. //}
  137. //限制只输入数字
  138. - (BOOL)validateNumber:(NSString*)number {
  139. BOOL res = YES;
  140. NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
  141. int i = 0;
  142. while (i < number.length) {
  143. NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
  144. NSRange range = [string rangeOfCharacterFromSet:tmpSet];
  145. if (range.length == 0) {
  146. res = NO;
  147. break;
  148. }
  149. i++;
  150. }
  151. return res;
  152. }
  153. //取消
  154. - (void)cancelButtonActon
  155. { //
  156. [self.view endEditing:YES];
  157. [self dismissViewControllerAnimated:YES completion:nil];
  158. }
  159. //兑换
  160. - (void)converButtonActon
  161. {
  162. //当输入金额 不足兑换 1钻石
  163. if(ratio.floatValue !=0){
  164. if (_rightTextField.text.floatValue == 0&&_leftTextfield.text.floatValue<1/ratio.floatValue) {
  165. [FanweMessage alert:ASLocalizedString(@"请输入足够的兑换金额")];
  166. return;
  167. }
  168. }
  169. //当输入金额 大于账户
  170. if(_leftTextfield.text.floatValue > _tickte.floatValue)
  171. {
  172. [FanweMessage alert:ASLocalizedString(@"兑换金额不得大于账户余额")];
  173. return;
  174. }
  175. if (rat >0)
  176. {
  177. [self showMyHud];
  178. if (_whetherGame)
  179. {
  180. [self exchangeCoin];
  181. }
  182. else
  183. {
  184. [self converDiamondsRequest];
  185. }
  186. }
  187. }
  188. #pragma marlk 砖石兑换
  189. - (void)converDiamondsRequest
  190. {
  191. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  192. [parmDict setObject:@"user_center" forKey:@"ctl"];
  193. [parmDict setObject:@"do_exchange" forKey:@"act"];
  194. [parmDict setObject:[NSString stringWithFormat:@"%@",_leftTextfield.text] forKey:@"ticket"];
  195. FWWeakify(self)
  196. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  197. {
  198. FWStrongify(self)
  199. [self hideMyHud];
  200. if ([responseJson toInt:@"status"]==1)
  201. {
  202. if ([_rightTextField.text intValue])
  203. {
  204. [self dismissViewControllerAnimated:YES completion:^{
  205. [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshAcount" object:nil];
  206. }];
  207. }
  208. else
  209. {
  210. [FanweMessage alert:ASLocalizedString(@"兑换失败")];
  211. }
  212. }
  213. } FailureBlock:^(NSError *error){
  214. FWStrongify(self)
  215. [self hideMyHud];
  216. }];
  217. }
  218. #pragma mark 钻石兑换游戏币
  219. - (void)exchangeCoin
  220. {
  221. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  222. [parmDict setObject:@"games" forKey:@"ctl"];
  223. [parmDict setObject:@"exchangeCoin" forKey:@"act"];
  224. [parmDict setObject:[NSString stringWithFormat:@"%@",_leftTextfield.text] forKey:@"diamonds"];
  225. FWWeakify(self)
  226. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  227. {
  228. FWStrongify(self)
  229. [self hideMyHud];
  230. if ([responseJson toInt:@"status"]==1)
  231. {
  232. if ([_rightTextField.text intValue]>0)
  233. {
  234. [[NSNotificationCenter defaultCenter]postNotificationName:@"updateCoin" object:nil];
  235. [_leftTextfield resignFirstResponder];
  236. [self dismissViewControllerAnimated:YES completion:^{
  237. }];
  238. [SVProgressHUD showSuccessWithStatus:ASLocalizedString(@"兑换成功")];
  239. }
  240. else
  241. {
  242. [SVProgressHUD showErrorWithStatus:ASLocalizedString(@"兑换失败")];
  243. }
  244. }
  245. } FailureBlock:^(NSError *error)
  246. {
  247. FWStrongify(self)
  248. [self hideMyHud];
  249. [FanweMessage alert:ASLocalizedString(@"兑换失败")];
  250. }];
  251. }
  252. #pragma mark 获取游戏币兑换比例
  253. - (void)obtainCoinProportion
  254. {
  255. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  256. [parmDict setObject:@"games" forKey:@"ctl"];
  257. [parmDict setObject:@"exchangeRate" forKey:@"act"];
  258. FWWeakify(self)
  259. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  260. {
  261. FWStrongify(self)
  262. if ([responseJson toInt:@"status"] ==1)
  263. {
  264. ratio = [responseJson objectForKey:@"exchange_rate"];
  265. //兑换比例
  266. if (ratio != nil&& ![ratio isEqual:[NSNull null]]) {
  267. self.exchangeRate_Lab.text = [NSString stringWithFormat:ASLocalizedString(@"兑换比例: %@"),ratio];
  268. }
  269. }
  270. } FailureBlock:^(NSError *error)
  271. {
  272. }];
  273. }
  274. #pragma marlk 获得兑换比例
  275. - (void)requetRatio
  276. {
  277. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  278. [parmDict setObject:@"user_center" forKey:@"ctl"];
  279. [parmDict setObject:@"exchange" forKey:@"act"];
  280. FWWeakify(self)
  281. [self.httpsManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  282. {
  283. FWStrongify(self)
  284. if (responseJson)
  285. {
  286. if ([responseJson toInt:@"status"] ==1)
  287. {
  288. FreshAcountModel *model3 =[[FreshAcountModel alloc]init];
  289. model3 =[FreshAcountModel mj_objectWithKeyValues:responseJson];
  290. ratio =model3.ratio;
  291. //兑换比例
  292. if (ratio != nil&& ![ratio isEqual:[NSNull null]]) {
  293. self.exchangeRate_Lab.text = [NSString stringWithFormat:ASLocalizedString(@"兑换比例: %@"),ratio];
  294. }
  295. }
  296. }
  297. } FailureBlock:^(NSError *error)
  298. {
  299. }];
  300. }
  301. - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
  302. {
  303. [_leftTextfield resignFirstResponder];
  304. [self dismissViewControllerAnimated:YES completion:^{
  305. }];
  306. }
  307. @end