ExchangeCoinView.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. //
  2. // ExchangeCoinView.m
  3. // BuguLive
  4. //
  5. // Created by yy on 17/2/16.
  6. // Copyright © 2017年 xfg. All rights reserved.
  7. //
  8. #import "ExchangeCoinView.h"
  9. @implementation ExchangeCoinView
  10. + (instancetype)EditNibFromXib
  11. {
  12. return [[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil].lastObject;
  13. }
  14. - (void)createSomething
  15. {
  16. _httpManager = [NetHttpsManager manager];
  17. _BuguLive = [GlobalVariables sharedInstance];
  18. [self createStyle];
  19. }
  20. - (void)createStyle
  21. {
  22. [self.confirmButton addTarget:self
  23. action:@selector(confirmButtonActon)
  24. forControlEvents:UIControlEventTouchUpInside];
  25. [self.confirmButton setTitleColor:[UIColor colorWithHexString:@"#9152F8"]
  26. forState:UIControlStateNormal];
  27. [self.cancelButton setTitleColor:kAppGrayColor1
  28. forState:UIControlStateNormal];
  29. self.diamondLeftTextfield.layer.borderColor = [[UIColor whiteColor]CGColor];
  30. self.diamondLeftTextfield.keyboardType = UIKeyboardTypeNumberPad;
  31. self.textfieldBgView.layer.masksToBounds = YES;
  32. self.textfieldBgView.layer.cornerRadius = 5;
  33. self.textfieldBgView.layer.borderColor = kAppGrayColor4.CGColor;
  34. self.textfieldBgView.layer.borderWidth = 0.5;
  35. self.diamondLeftTextfield.delegate = self;
  36. self.confirmButton.backgroundColor = [UIColor whiteColor];
  37. self.cancelButton.backgroundColor = [UIColor whiteColor];
  38. self.verticalLine.backgroundColor = kAppGrayColor4;
  39. self.horizonLine.backgroundColor = kAppGrayColor4;
  40. self.titleBgView.backgroundColor = kClearColor;
  41. // = kAppMainColor;
  42. self.exchangeTitle.textColor = kWhiteColor;
  43. self.diamondLabel.textColor = kAppGrayColor1;
  44. self.scaleLabel.textColor = kAppGrayColor1;
  45. // self.coinLabel.textColor = kAppGrayColor1;
  46. self.layer.masksToBounds = YES;
  47. self.layer.cornerRadius = 10;
  48. self.bgImgView.layer.masksToBounds = YES;
  49. self.bgImgView.clipsToBounds = YES;
  50. self.bgImgView.frame = self.bounds;
  51. [self.cancelButton setLocalizedString];
  52. if (_exchangeType == 1)
  53. {
  54. self.exchangeTitle.text = ASLocalizedString(@"兑换游戏币");
  55. self.coinLabel.text = ASLocalizedString(@"0游戏币");
  56. [self.confirmButton setTitle:ASLocalizedString(@"兑换")forState:UIControlStateNormal];
  57. }
  58. else if (_exchangeType == 2)
  59. {
  60. self.exchangeTitle.text = [NSString stringWithFormat:ASLocalizedString(@"兑换%@"),self.BuguLive.appModel.diamond_name];
  61. // [NSString stringWithFormat:@"0%@",self.BuguLive.appModel.diamond_name];
  62. self.coinLabel.text = [NSString stringWithFormat:@"0%@",self.BuguLive.appModel.diamond_name];
  63. // [NSString stringWithFormat:@"0%@",self.BuguLive.appModel.diamond_name];
  64. [self.confirmButton setTitle:ASLocalizedString(@"兑换")forState:UIControlStateNormal];
  65. }
  66. else if (_exchangeType == 3)
  67. {
  68. self.exchangeTitle.text = ASLocalizedString(@"赠送游戏币");
  69. self.scaleLabel.hidden = YES;
  70. self.coinLabel.hidden = YES;
  71. [self.confirmButton setTitle:ASLocalizedString(@"赠送")forState:UIControlStateNormal];
  72. }
  73. else if (_exchangeType == 4)
  74. {
  75. self.exchangeTitle.text = [NSString stringWithFormat:ASLocalizedString(@"赠送%@"),self.BuguLive.appModel.diamond_name];
  76. self.scaleLabel.hidden = YES;
  77. self.coinLabel.hidden = YES;
  78. [self.confirmButton setTitle:ASLocalizedString(@"赠送")forState:UIControlStateNormal];
  79. }
  80. }
  81. #pragma marlk textfieldDelegate代理
  82. - (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
  83. return YES;
  84. }
  85. - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
  86. NSString * toBeString = [textField.text stringByReplacingCharactersInRange:range withString:string];
  87. if (toBeString.length > 6 && range.length!= 1){
  88. textField.text = [toBeString substringToIndex:6];
  89. return NO;
  90. }
  91. // if ([toBeString integerValue] > [_ticket integerValue])
  92. // {
  93. // [[BGHUDHelper sharedInstance]tipMessage:ASLocalizedString(@"输入金额大于账户余额")];
  94. //
  95. // return NO;
  96. // }
  97. _rat = [toBeString integerValue] * [ratio floatValue];
  98. if (_exchangeType == 3 || _exchangeType == 4)
  99. {
  100. NSLog(@"%f",[_diamondLeftTextfield.text floatValue]);
  101. if ([toBeString integerValue] > 0)
  102. {
  103. [_confirmButton setTitleColor:[UIColor colorWithHexString:@"#9152F8"] forState:UIControlStateNormal];
  104. }
  105. else
  106. {
  107. [_confirmButton setTitleColor:[UIColor colorWithHexString:@"#9152F8"] forState:UIControlStateNormal];
  108. }
  109. }
  110. else
  111. {
  112. if (_rat >0)
  113. {
  114. [_confirmButton setTitleColor:[UIColor colorWithHexString:@"#9152F8"] forState:UIControlStateNormal];
  115. }
  116. else
  117. {
  118. [_confirmButton setTitleColor:[UIColor colorWithHexString:@"#9152F8"] forState:UIControlStateNormal];
  119. }
  120. if (_exchangeType == 1) {
  121. _coinLabel.text = [NSString stringWithFormat:ASLocalizedString(@"%ld游戏币"),(long)_rat];
  122. }
  123. else
  124. {
  125. _coinLabel.text = [NSString stringWithFormat:@"%ld%@",(long)_rat,self.BuguLive.appModel.diamond_name];
  126. }
  127. }
  128. NSLog(@"++++++++++++++++++++++++%ld", (long)_rat);
  129. return [self validateNumber:string];
  130. }
  131. //限制只输入数字
  132. - (BOOL)validateNumber:(NSString*)number {
  133. BOOL res = YES;
  134. NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
  135. int i = 0;
  136. while (i < number.length) {
  137. NSString * string = [number substringWithRange:NSMakeRange(i, 1)];
  138. NSRange range = [string rangeOfCharacterFromSet:tmpSet];
  139. if (range.length == 0) {
  140. res = NO;
  141. break;
  142. }
  143. i++;
  144. }
  145. return res;
  146. }
  147. #pragma mark 获取游戏币兑换比例
  148. - (void)obtainCoinProportion
  149. {
  150. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  151. [parmDict setObject:@"games" forKey:@"ctl"];
  152. [parmDict setObject:@"exchangeRate" forKey:@"act"];
  153. [_httpManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  154. {
  155. if ([responseJson toInt:@"status"] ==1)
  156. {
  157. ratio = [responseJson objectForKey:@"exchange_rate"];
  158. //兑换比例
  159. if (ratio != nil&& ![ratio isEqual:[NSNull null]])
  160. {
  161. _scaleLabel.text = [NSString stringWithFormat:ASLocalizedString(@"兑换比例: %@"),ratio];
  162. }
  163. }
  164. } FailureBlock:^(NSError *error)
  165. {
  166. }];
  167. }
  168. #pragma marlk 获得钻石兑换比例
  169. - (void)requetRatio
  170. {
  171. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  172. [parmDict setObject:@"user_center" forKey:@"ctl"];
  173. [parmDict setObject:@"exchange" forKey:@"act"];
  174. [_httpManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  175. {
  176. if ((NSNull *)responseJson != [NSNull null])
  177. {
  178. if ([responseJson toInt:@"status"] ==1)
  179. {
  180. FreshAcountModel *model3 =[[FreshAcountModel alloc]init];
  181. model3 =[FreshAcountModel mj_objectWithKeyValues:responseJson];
  182. ratio =model3.ratio;
  183. //兑换比例
  184. if (ratio != nil&& ![ratio isEqual:[NSNull null]]) {
  185. _scaleLabel.text = [NSString stringWithFormat:ASLocalizedString(@"兑换比例: %@"),ratio];
  186. }
  187. }
  188. }
  189. } FailureBlock:^(NSError *error)
  190. {
  191. }];
  192. }
  193. #pragma mark 钻石兑换游戏币
  194. - (void)exchangeCoin
  195. {
  196. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  197. [parmDict setObject:@"games" forKey:@"ctl"];
  198. [parmDict setObject:@"exchangeCoin" forKey:@"act"];
  199. [parmDict setObject:[NSString stringWithFormat:@"%@",_diamondLeftTextfield.text] forKey:@"diamonds"];
  200. [_httpManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  201. {
  202. if ([responseJson toInt:@"status"]==1)
  203. {
  204. [[NSNotificationCenter defaultCenter]postNotificationName:@"updateCoin" object:nil];
  205. [_diamondLeftTextfield resignFirstResponder];
  206. [[BGHUDHelper sharedInstance]tipMessage:ASLocalizedString(@"兑换成功")];
  207. }
  208. } FailureBlock:^(NSError *error)
  209. {
  210. [FanweMessage alert:ASLocalizedString(@"请求失败")];
  211. }];
  212. }
  213. #pragma marlk 兑换钻石
  214. - (void)converDiamondsRequest
  215. {
  216. NSMutableDictionary *parmDict = [NSMutableDictionary dictionary];
  217. [parmDict setObject:@"user_center" forKey:@"ctl"];
  218. [parmDict setObject:@"do_exchange" forKey:@"act"];
  219. [parmDict setObject:[NSString stringWithFormat:@"%@",_diamondLeftTextfield.text] forKey:@"ticket"];
  220. [_httpManager POSTWithParameters:parmDict SuccessBlock:^(NSDictionary *responseJson)
  221. {
  222. if ([responseJson toInt:@"status"]==1)
  223. {
  224. [[NSNotificationCenter defaultCenter]postNotificationName:@"refreshAcount" object:nil];
  225. [[FDHUDManager defaultManager]show:[responseJson valueForKey:@"error"] ToView:self.superview];
  226. }
  227. } FailureBlock:^(NSError *error)
  228. {
  229. [FanweMessage alert:ASLocalizedString(@"请求失败")];
  230. }];
  231. }
  232. - (void)confirmButtonActon
  233. {
  234. if (_exchangeType == 3 || _exchangeType == 4)
  235. {
  236. if ([_diamondLeftTextfield.text integerValue] < 1) {
  237. [[BGHUDHelper sharedInstance]tipMessage:ASLocalizedString(@"请输入足够的金额")];
  238. return;
  239. }
  240. }
  241. else
  242. {
  243. //当输入金额 不足兑换 1钻石
  244. if(ratio.floatValue != 0)
  245. {
  246. if (_rat < 1 || [_diamondLeftTextfield.text integerValue] < 1)
  247. {
  248. // [SVProgressHUD showInfoWithStatus:ASLocalizedString(@"请输入足够的金额")];
  249. [[BGHUDHelper sharedInstance]tipMessage:ASLocalizedString(@"请输入足够的金额")];
  250. return;
  251. }
  252. }
  253. }
  254. //当输入金额 大于账户
  255. if(_diamondLeftTextfield.text.integerValue > _ticket.floatValue)
  256. {
  257. _diamondLeftTextfield.text = nil;
  258. if (_exchangeType == 1) {
  259. _coinLabel.text = ASLocalizedString(@"0游戏币");
  260. }
  261. else
  262. {
  263. _coinLabel.text = [NSString stringWithFormat:@"0%@",self.BuguLive.appModel.diamond_name];
  264. }
  265. [_confirmButton setTitleColor:[UIColor colorWithHexString:@"#9152F8"] forState:UIControlStateNormal];
  266. [[BGHUDHelper sharedInstance]tipMessage:ASLocalizedString(@"输入金额不得大于账户余额")];
  267. return;
  268. }
  269. if (_exchangeType == 3 || _exchangeType == 4)
  270. {
  271. if (_delegate && [_delegate respondsToSelector:@selector(comfirmClickWithExchangeCoinView:)])
  272. {
  273. [_delegate comfirmClickWithExchangeCoinView:self];
  274. }
  275. [self backToBefore];
  276. if (_delegate && [_delegate respondsToSelector:@selector(exchangeViewDownWithExchangeCoinView:)])
  277. {
  278. [_delegate exchangeViewDownWithExchangeCoinView:self];
  279. }
  280. }
  281. else
  282. {
  283. if (_rat > 0)
  284. {
  285. if (_exchangeType == 1)
  286. {
  287. [self exchangeCoin];
  288. }
  289. else if (_exchangeType == 2)
  290. {
  291. [self converDiamondsRequest];
  292. }
  293. [self backToBefore];
  294. if (_delegate && [_delegate respondsToSelector:@selector(exchangeViewDownWithExchangeCoinView:)])
  295. {
  296. [_delegate exchangeViewDownWithExchangeCoinView:self];
  297. }
  298. }else{
  299. [SVProgressHUD showInfoWithStatus:ASLocalizedString(@"兑换比例为0,不能兑换")];
  300. }
  301. }
  302. }
  303. - (void)backToBefore
  304. {
  305. if (_exchangeType == 1)
  306. {
  307. _coinLabel.text = ASLocalizedString(@"0游戏币");
  308. }
  309. else if (_exchangeType == 2)
  310. {
  311. _coinLabel.text = [NSString stringWithFormat:@"0%@",self.BuguLive.appModel.diamond_name];
  312. }
  313. [self.confirmButton setTitleColor:[UIColor colorWithHexString:@"#9152F8"] forState:UIControlStateNormal];
  314. }
  315. - (IBAction)cancelButtonAction:(id)sender
  316. {
  317. if (_delegate && [_delegate respondsToSelector:@selector(exchangeViewDownWithExchangeCoinView:)])
  318. {
  319. [_delegate exchangeViewDownWithExchangeCoinView:self];
  320. }
  321. [self backToBefore];
  322. }
  323. @end