JXSendRedPacketViewController.m 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. //
  2. // JXSendRedPacketViewController.m
  3. // shiku_im
  4. //
  5. // Created by 1 on 17/8/14.
  6. // Copyright © 2017年 Reese. All rights reserved.
  7. //
  8. #import "JXSendRedPacketViewController.h"
  9. #import "JXTopSiftJobView.h"
  10. #import "JXRedInputView.h"
  11. #import "JXRechargeViewController.h"
  12. #import "JXVerifyPayVC.h"
  13. #import "JXPayPasswordVC.h"
  14. #import "JXPayServer.h"
  15. #define TopHeight 40
  16. @interface JXSendRedPacketViewController ()<UITextFieldDelegate,UIScrollViewDelegate,RechargeDelegate>
  17. @property (nonatomic, strong) JXTopSiftJobView * topSiftView;
  18. @property (nonatomic, strong) JXRedInputView * luckyView;
  19. @property (nonatomic, strong) JXRedInputView * nomalView;
  20. @property (nonatomic, strong) JXRedInputView * orderView;
  21. @property (nonatomic, strong) JXVerifyPayVC * verVC;
  22. @property (nonatomic, copy) NSString * moneyText;
  23. @property (nonatomic, copy) NSString * countText;
  24. @property (nonatomic, copy) NSString * greetText;
  25. @property (nonatomic, assign) NSInteger indexInt;
  26. @end
  27. @implementation JXSendRedPacketViewController
  28. -(instancetype)init{
  29. if (self = [super init]) {
  30. }
  31. return self;
  32. }
  33. - (void)viewWillAppear:(BOOL)animated {
  34. [super viewWillAppear:animated];
  35. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent];
  36. }
  37. - (void)viewWillDisappear:(BOOL)animated {
  38. [super viewWillDisappear:animated];
  39. [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault];
  40. }
  41. - (void)viewDidLoad {
  42. [super viewDidLoad];
  43. self.heightFooter = 0;
  44. self.heightHeader = 0;
  45. [self createHeadAndFoot];
  46. [self createHeaderView];
  47. UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(endEdit:)];
  48. [self.tableBody addGestureRecognizer:tap];
  49. if (_isRoom) {
  50. self.tableBody.contentSize = CGSizeMake(JX_SCREEN_WIDTH *3, self.tableBody.frame.size.height);
  51. }else{
  52. self.tableBody.contentSize = CGSizeMake(JX_SCREEN_WIDTH *2, self.tableBody.frame.size.height);
  53. }
  54. self.tableBody.delegate = self;
  55. self.tableBody.pagingEnabled = YES;
  56. self.tableBody.showsHorizontalScrollIndicator = NO;
  57. self.tableBody.backgroundColor = HEXCOLOR(0xEFEFEF);
  58. [self.view addSubview:self.topSiftView];
  59. if(_isRoom){
  60. [self.tableBody addSubview:self.luckyView];
  61. [_luckyView.sendButton addTarget:self action:@selector(sendRedPacket:) forControlEvents:UIControlEventTouchUpInside];
  62. }
  63. [self.tableBody addSubview:self.nomalView];
  64. [self.tableBody addSubview:self.orderView];
  65. [_nomalView.sendButton addTarget:self action:@selector(sendRedPacket:) forControlEvents:UIControlEventTouchUpInside];
  66. [_orderView.sendButton addTarget:self action:@selector(sendRedPacket:) forControlEvents:UIControlEventTouchUpInside];
  67. }
  68. -(void)createHeaderView{
  69. int heightHeader = JX_SCREEN_TOP;
  70. UIView * tableHeader = [[UIView alloc]initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, heightHeader)];
  71. UIImageView* iv = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, JX_SCREEN_WIDTH, heightHeader)];
  72. iv.image = [[UIImage imageNamed:@"navBarBackground"] imageWithTintColor:HEXCOLOR(0xFA585E)];
  73. iv.userInteractionEnabled = YES;
  74. [tableHeader addSubview:iv];
  75. JXLabel* p = [[JXLabel alloc]initWithFrame:CGRectMake(60, JX_SCREEN_TOP -15- 17, JX_SCREEN_WIDTH-60*2, 20)];
  76. p.center = CGPointMake(tableHeader.center.x, p.center.y);
  77. p.backgroundColor = [UIColor clearColor];
  78. p.textAlignment = NSTextAlignmentCenter;
  79. p.textColor = [UIColor whiteColor];
  80. p.font = [UIFont systemFontOfSize:18.0];
  81. p.text = Localized(@"JX_SendGift");
  82. p.userInteractionEnabled = YES;
  83. p.didTouch = @selector(actionTitle:);
  84. p.delegate = self;
  85. p.changeAlpha = NO;
  86. [tableHeader addSubview:p];
  87. UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, JX_SCREEN_TOP - 46, 46, 46)];
  88. [btn setBackgroundImage:[[UIImage imageNamed:@"title_back_black_big"] imageWithTintColor:[UIColor whiteColor]] forState:UIControlStateNormal];
  89. [btn addTarget:self action:@selector(actionQuit) forControlEvents:UIControlEventTouchUpInside];
  90. [tableHeader addSubview:btn];
  91. [self.view addSubview:tableHeader];
  92. }
  93. -(JXRedInputView *)luckyView{
  94. if (!_luckyView) {
  95. _luckyView = [[JXRedInputView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_TOP+40, JX_SCREEN_WIDTH, self.tableBody.contentSize.height-JX_SCREEN_TOP-40) type:2 isRoom:_isRoom delegate:self];
  96. }
  97. return _luckyView;
  98. }
  99. -(JXRedInputView *)nomalView{
  100. if (!_nomalView) {
  101. _nomalView = [[JXRedInputView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_luckyView.frame), JX_SCREEN_TOP+40, JX_SCREEN_WIDTH, self.tableBody.contentSize.height-JX_SCREEN_TOP-40) type:1 isRoom:_isRoom delegate:self];
  102. }
  103. return _nomalView;
  104. }
  105. -(JXRedInputView *)orderView{
  106. if (!_orderView) {
  107. _orderView = [[JXRedInputView alloc] initWithFrame:CGRectMake(CGRectGetMaxX(_nomalView.frame), JX_SCREEN_TOP+40, JX_SCREEN_WIDTH, self.tableBody.contentSize.height-JX_SCREEN_TOP-40) type:3 isRoom:_isRoom delegate:self];
  108. }
  109. return _orderView;
  110. }
  111. -(JXTopSiftJobView *)topSiftView{
  112. if (!_topSiftView) {
  113. _topSiftView = [[JXTopSiftJobView alloc] initWithFrame:CGRectMake(0, JX_SCREEN_TOP, JX_SCREEN_WIDTH, 40)];
  114. _topSiftView.delegate = self;
  115. _topSiftView.isShowMoreParaBtn = NO;
  116. _topSiftView.preferred = 0;
  117. _topSiftView.backgroundColor = HEXCOLOR(0xFA585E);
  118. _topSiftView.titleNormalColor = HEXCOLOR(0xFCB3B4);
  119. _topSiftView.titleSelectedColor = [UIColor whiteColor];
  120. _topSiftView.isShowBottomLine = NO;
  121. NSArray * itemsArray;
  122. if (_isRoom) {
  123. itemsArray = [[NSArray alloc] initWithObjects:Localized(@"JX_LuckGift"),Localized(@"JX_UsualGift"),Localized(@"JX_MesGift"), nil];
  124. itemsArray = [[NSArray alloc] initWithObjects:Localized(@"JX_LuckGift"),Localized(@"JX_UsualGift"), nil];
  125. }else{
  126. itemsArray = [[NSArray alloc] initWithObjects:Localized(@"JX_UsualGift"),Localized(@"JX_MesGift"), nil];
  127. }
  128. _topSiftView.dataArray = itemsArray;
  129. UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 40 - LINE_WH, JX_SCREEN_WIDTH, LINE_WH)];
  130. line.backgroundColor = HEXCOLOR(0xFC1454);
  131. [_topSiftView addSubview:line];
  132. }
  133. return _topSiftView;
  134. }
  135. - (void)didReceiveMemoryWarning {
  136. [super didReceiveMemoryWarning];
  137. }
  138. - (void)topItemBtnClick:(UIButton *)btn{
  139. [self checkAfterScroll:(btn.tag-100)];
  140. }
  141. - (void)checkAfterScroll:(CGFloat)offsetX{
  142. [self.tableBody setContentOffset:CGPointMake(offsetX*JX_SCREEN_WIDTH, 0) animated:YES];
  143. [self endEdit:nil];
  144. }
  145. -(void)endEdit:(UIGestureRecognizer *)ges{
  146. [_luckyView stopEdit];
  147. [_nomalView stopEdit];
  148. [_orderView stopEdit];
  149. }
  150. #pragma mark -------------ScrollDelegate----------------
  151. - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
  152. [self endEdit:nil];
  153. int page = (int)(scrollView.contentOffset.x/JX_SCREEN_WIDTH);
  154. switch (page) {
  155. case 0:
  156. [_topSiftView resetItemBtnWith:0];
  157. [_topSiftView moveBottomSlideLine:0];
  158. break;
  159. case 1:
  160. [_topSiftView resetItemBtnWith:JX_SCREEN_WIDTH];
  161. [_topSiftView moveBottomSlideLine:JX_SCREEN_WIDTH];
  162. break;
  163. case 2:
  164. [_topSiftView resetItemBtnWith:JX_SCREEN_WIDTH*2];
  165. [_topSiftView moveBottomSlideLine:JX_SCREEN_WIDTH*2];
  166. break;
  167. default:
  168. break;
  169. }
  170. }
  171. -(void)sendRedPacket:(UIButton *)button{
  172. //1是普通红包,2是手气红包,3是口令红包
  173. if (button.tag == 1) {
  174. _moneyText = _nomalView.moneyTextField.text;
  175. _countText = _nomalView.countTextField.text;
  176. _greetText = _nomalView.greetTextField.text;
  177. }else if(button.tag == 2){
  178. _moneyText = _luckyView.moneyTextField.text;
  179. _countText = _luckyView.countTextField.text;
  180. _greetText = _luckyView.greetTextField.text;
  181. }else if(button.tag == 3){
  182. _moneyText = _orderView.moneyTextField.text;
  183. _countText = _orderView.countTextField.text;
  184. _greetText = _orderView.greetTextField.text;//口令
  185. }
  186. if (_moneyText == nil || [_moneyText isEqualToString:@""]) {
  187. [g_App showAlert:Localized(@"JX_InputGiftCount")];
  188. return;
  189. }
  190. if (!_isRoom) {
  191. _countText = @"1";
  192. }
  193. if (_isRoom && (_countText == nil|| [_countText isEqualToString:@""] || [_countText intValue] <= 0)) {
  194. [g_App showAlert:Localized(@"JXGiftForRoomVC_InputGiftCount")];
  195. return;
  196. }
  197. if (([_moneyText doubleValue]/[_countText intValue]) < 0.01) {
  198. [g_App showAlert:Localized(@"JXRedPaket_001")];
  199. return;
  200. }
  201. // if ([_moneyText doubleValue] > g_App.myMoney) {
  202. // [g_App showAlert:Localized(@"JX_NotEnough") delegate:self tag:2000 onlyConfirm:NO];
  203. // return;
  204. // }
  205. if (500 >= [_moneyText floatValue]&&[_moneyText floatValue] > 0) {
  206. if (button.tag == 3) {
  207. if ([_greetText isEqualToString:@""]) {
  208. [g_App showAlert:Localized(@"JXGiftForRoomVC_InputGiftWord")];
  209. return;
  210. }
  211. _greetText = [_greetText stringByReplacingOccurrencesOfString:@" " withString:@""];
  212. if ([_greetText isEqualToString:@""]) {
  213. [JXMyTools showTipView:@"请输入有效口令"];
  214. return;
  215. }
  216. }
  217. if (button.tag == 1) {
  218. if ((int)([_moneyText floatValue] * 100) % [_countText integerValue] != 0) {
  219. [JXMyTools showTipView:@"普通红包需要均分金额"];
  220. return;
  221. }
  222. }
  223. //祝福语
  224. if ([_greetText isEqualToString:@""]) {
  225. _greetText = Localized(@"JX_GiftText");
  226. }
  227. self.indexInt = button.tag;
  228. if ([g_server.myself.isPayPassword boolValue]) {
  229. self.verVC = [JXVerifyPayVC alloc];
  230. self.verVC.type = JXVerifyTypeSendReadPacket;
  231. self.verVC.RMB = _moneyText;
  232. self.verVC.delegate = self;
  233. self.verVC.didDismissVC = @selector(dismissVerifyPayVC);
  234. self.verVC.didVerifyPay = @selector(didVerifyPay:);
  235. self.verVC = [self.verVC init];
  236. [self.view addSubview:self.verVC.view];
  237. } else {
  238. JXPayPasswordVC *payPswVC = [JXPayPasswordVC alloc];
  239. payPswVC.type = JXPayTypeSetupPassword;
  240. payPswVC.enterType = JXEnterTypeSendRedPacket;
  241. payPswVC = [payPswVC init];
  242. [g_navigation pushViewController:payPswVC animated:YES];
  243. }
  244. }else{
  245. [g_App showAlert:Localized(@"JX_InputMoneyCount")];
  246. }
  247. }
  248. - (void)didVerifyPay:(NSString *)sender {
  249. long time = (long)[[NSDate date] timeIntervalSince1970];
  250. time = (time *1000 + g_server.timeDifference)/1000;
  251. NSString *secret = [self getSecretWithText:sender time:time];
  252. // [g_server sendRedPacketV1:[_moneyText doubleValue] type:(int)self.indexInt count:[_countText intValue] greetings:_greetText roomJid:self.roomJid toUserId:self.toUserId time:time secret:secret toView:self];
  253. // 参数顺序不能变,先放key再放value
  254. NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"type",[NSString stringWithFormat:@"%ld",self.indexInt],@"moneyStr",_moneyText,@"count",_countText,@"greetings",_greetText, nil];
  255. if (self.roomJid.length > 0) {
  256. // [dict setObject:self.roomJid forKey:@"roomJid"];
  257. [arr addObject:@"roomJid"];
  258. [arr addObject:self.roomJid];
  259. }else {
  260. // [dict setObject:self.toUserId forKey:@"toUserId"];
  261. [arr addObject:@"toUserId"];
  262. [arr addObject:self.toUserId];
  263. }
  264. [g_payServer payServerWithAction:act_sendRedPacketV2 param:arr payPassword:sender time:time toView:self];
  265. // [g_server sendRedPacketV2:_moneyText type:(int)self.indexInt count:[_countText intValue] greetings:_greetText roomJid:self.roomJid toUserId:self.toUserId time:time secret:secret payPassword:sender toView:self];
  266. }
  267. - (void)dismissVerifyPayVC {
  268. [self.verVC.view removeFromSuperview];
  269. }
  270. //服务端返回数据
  271. -(void) didServerResultSucces:(JXConnection*)aDownload dict:(NSDictionary*)dict array:(NSArray*)array1{
  272. [_wait stop];
  273. if ([aDownload.action isEqualToString:act_getUserMoeny]) {
  274. g_App.myMoney = [dict[@"balance"] doubleValue];
  275. if (g_App.myMoney <= 0) {
  276. [g_App showAlert:Localized(@"JX_NotEnough") delegate:self tag:2000 onlyConfirm:NO];
  277. }
  278. }
  279. if ([aDownload.action isEqualToString:act_sendRedPacket] || [aDownload.action isEqualToString:act_sendRedPacketV2]) {
  280. NSMutableDictionary * muDict = [NSMutableDictionary dictionaryWithDictionary:dict];
  281. [muDict setObject:_greetText forKey:@"greet"];
  282. [self dismissVerifyPayVC]; // 销毁支付密码界面
  283. //成功创建红包,发送一条含红包Id的消息
  284. if (_delegate && [_delegate respondsToSelector:@selector(sendRedPacketDelegate:)]) {
  285. [_delegate performSelector:@selector(sendRedPacketDelegate:) withObject:muDict];
  286. }
  287. [self actionQuit];
  288. }
  289. }
  290. -(int) didServerResultFailed:(JXConnection*)aDownload dict:(NSDictionary*)dict{
  291. [_wait stop];
  292. if ([aDownload.action isEqualToString:act_sendRedPacket] || [aDownload.action isEqualToString:act_sendRedPacketV2] || [aDownload.action isEqualToString:act_TransactionGetCode]) {
  293. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  294. [self.verVC clearUpPassword];
  295. });
  296. UIAlertView *alertShow=[[UIAlertView alloc]initWithTitle:nil message:dict[@"resultMsg"] delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles:nil, nil];
  297. [alertShow show];
  298. ;
  299. }
  300. return show_error;
  301. }
  302. -(int) didServerConnectError:(JXConnection*)aDownload error:(NSError *)error{//error为空时,代表超时
  303. [_wait stop];
  304. return show_error;
  305. }
  306. -(void) didServerConnectStart:(JXConnection*)aDownload{
  307. [_wait start];
  308. }
  309. #pragma mark - alertViewDelegate
  310. - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{
  311. if (alertView.tag == 2000){
  312. if (buttonIndex == 1) {
  313. [self rechargeButtonAction];
  314. }
  315. }
  316. }
  317. -(void)rechargeButtonAction{
  318. JXRechargeViewController * rechargeVC = [[JXRechargeViewController alloc]init];
  319. rechargeVC.rechargeDelegate = self;
  320. rechargeVC.isQuitAfterSuccess = YES;
  321. // [g_window addSubview:rechargeVC.view];
  322. [g_navigation pushViewController:rechargeVC animated:YES];
  323. }
  324. #pragma mark - RechargeDelegate
  325. -(void)rechargeSuccessed{
  326. }
  327. -(BOOL)textFieldShouldReturn:(UITextField *)textField{
  328. JXRedInputView * inputView = (JXRedInputView *)textField.superview.superview;
  329. if (textField.returnKeyType == UIReturnKeyDone) {
  330. [inputView stopEdit];
  331. }
  332. return YES;
  333. }
  334. -(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
  335. if ([string isEqualToString:@""]) {//删除
  336. return YES;
  337. }
  338. JXRedInputView * inputView = (JXRedInputView *)textField.superview.superview;
  339. if (textField == inputView.countTextField && [textField.text intValue] > 1000) {
  340. return NO;
  341. }
  342. // if (textField == inputView.moneyTextField) {
  343. // NSString * moneyStr = [textField.text stringByAppendingString:string];
  344. // if ([moneyStr floatValue] > 500.0f) {
  345. // return NO;
  346. // }
  347. // }
  348. if (textField == inputView.greetTextField && textField.text.length >= 20) {
  349. return NO;
  350. }
  351. if (textField == inputView.moneyTextField) {
  352. NSRange range = [textField.text rangeOfString:@"."];
  353. if (range.location != NSNotFound) {
  354. NSString *firstStr = [textField.text substringFromIndex:range.location + 1];
  355. if (firstStr.length > 1) {
  356. return NO;
  357. }
  358. }
  359. }
  360. if (textField == inputView.countTextField) {
  361. if ([textField.text rangeOfString:@"."].location != NSNotFound) {
  362. return NO;
  363. }
  364. NSMutableString *str = [NSMutableString string];
  365. [str appendString:textField.text];
  366. [str appendString:string];
  367. if ([str integerValue] > self.size) {
  368. textField.text = [NSString stringWithFormat:@"%ld", self.size];
  369. [JXMyTools showTipView:@"红包个数不能超过群人数"];
  370. return NO;
  371. }
  372. }
  373. return YES;
  374. }
  375. - (NSString *)getSecretWithText:(NSString *)text time:(long)time {
  376. NSMutableString *str1 = [NSMutableString string];
  377. [str1 appendString:APIKEY];
  378. [str1 appendString:[NSString stringWithFormat:@"%ld",time]];
  379. [str1 appendString:[NSString stringWithFormat:@"%@",[NSNumber numberWithDouble:[_moneyText doubleValue]]]];
  380. str1 = [[g_server getMD5String:str1] mutableCopy];
  381. [str1 appendString:g_myself.userId];
  382. [str1 appendString:g_server.access_token];
  383. NSMutableString *str2 = [NSMutableString string];
  384. str2 = [[g_server getMD5String:text] mutableCopy];
  385. [str1 appendString:str2];
  386. str1 = [[g_server getMD5String:str1] mutableCopy];
  387. return [str1 copy];
  388. }
  389. @end