财神随手记账

JZOutPutViewController.m 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. //
  2. // JZOutPutViewController.m
  3. // JIZHANG
  4. //
  5. // Created by kuxuan on 2018/2/2.
  6. // Copyright © 2018年 kuxuan. All rights reserved.
  7. //
  8. #import "JZOutPutViewController.h"
  9. @interface JZOutPutViewController ()<PGDatePickerDelegate>
  10. @property (nonatomic, strong) UIButton *startButton;
  11. @property (nonatomic, strong) UIButton *endButton;
  12. @end
  13. @implementation JZOutPutViewController
  14. - (void)viewDidLoad {
  15. [super viewDidLoad];
  16. // Do any additional setup after loading the view.
  17. [self setupNavBar];
  18. [self setupUI];
  19. }
  20. - (void)didReceiveMemoryWarning {
  21. [super didReceiveMemoryWarning];
  22. // Dispose of any resources that can be recreated.
  23. }
  24. - (void)setupNavBar {
  25. self.navTitle = @"账单导出";
  26. [self addLeftBarButtonItemWithImageName:@"mine_back" title:@"返回" target:self selector:@selector(backItemAction)];
  27. }
  28. - (void)backItemAction {
  29. [self.navigationController popViewControllerAnimated:YES];
  30. }
  31. - (void)setupUI {
  32. UIView *topView = [[UIView alloc] initWithFrame:CGRectMake(0, 10, SCREEN_WIDTH, FitSize(92))];
  33. topView.backgroundColor = [UIColor whiteColor];
  34. [self.view addSubview:topView];
  35. UILabel *line = [[UILabel alloc] init];
  36. line.backgroundColor = [UIColor JZColorWithHex:0xd8d8d8];
  37. if (iPhoneX) {
  38. line.backgroundColor = [[UIColor JZColorWithHex:0xd8d8d8] colorWithAlphaComponent:0.5];
  39. }
  40. [topView addSubview:line];
  41. UILabel *startLabel = [[UILabel alloc] init];
  42. startLabel.backgroundColor = [UIColor clearColor];
  43. startLabel.text = @"开始时间";
  44. startLabel.font = FONT_SYS(15);
  45. startLabel.textColor = [UIColor JZColorWithHex:0x666666];
  46. [topView addSubview:startLabel];
  47. UIButton *startButton = [UIButton buttonWithType:UIButtonTypeCustom];
  48. startButton.backgroundColor = [UIColor clearColor];
  49. startButton.tag = 111;
  50. [startButton addTarget:self action:@selector(timeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  51. startButton.titleLabel.font = FONT_SYS(12);
  52. [startButton setTitleColor:[UIColor JZColorWithHex:0x666666] forState:0];
  53. [startButton setImage:[UIImage imageNamed:@"mine_person_arrows"] forState:0];
  54. [topView addSubview:startButton];
  55. self.startButton = startButton;
  56. [startButton setTitle:[self getTodayString] forState:0];
  57. [startButton setButtonStyle:WSLButtonStyleImageRight spacing:10];
  58. UILabel *endLabel = [[UILabel alloc] init];
  59. endLabel.backgroundColor = [UIColor clearColor];
  60. endLabel.text = @"结束时间";
  61. endLabel.font = FONT_SYS(15);
  62. endLabel.textColor = [UIColor JZColorWithHex:0x666666];
  63. [topView addSubview:endLabel];
  64. UIButton *endButton = [UIButton buttonWithType:UIButtonTypeCustom];
  65. endButton.backgroundColor = [UIColor clearColor];
  66. endButton.tag = 222;
  67. [endButton addTarget:self action:@selector(timeButtonAction:) forControlEvents:UIControlEventTouchUpInside];
  68. endButton.titleLabel.font = FONT_SYS(12);
  69. [endButton setTitleColor:[UIColor JZColorWithHex:0x666666] forState:0];
  70. [endButton setImage:[UIImage imageNamed:@"mine_person_arrows"] forState:0];
  71. [topView addSubview:endButton];
  72. self.endButton = endButton;
  73. [endButton setTitle:[self getTodayString] forState:0];
  74. [endButton setButtonStyle:WSLButtonStyleImageRight spacing:10];
  75. UILabel *budgetLabel = [[UILabel alloc] init];
  76. budgetLabel.backgroundColor = [UIColor clearColor];
  77. budgetLabel.text = @"导出文件到:";
  78. budgetLabel.font = FONT_SYS(15);
  79. budgetLabel.textColor = [UIColor JZColorWithHex:0x666666];
  80. [self.view addSubview:budgetLabel];
  81. UITextField *budgetTF = [[UITextField alloc] init];
  82. budgetTF.backgroundColor = [UIColor whiteColor];
  83. [budgetTF addTarget:self action:@selector(textChanged:) forControlEvents:UIControlEventEditingChanged];
  84. budgetTF.placeholder = @"请输入邮箱地址";
  85. budgetTF.textColor = [UIColor JZColorWithHex:0x222222];
  86. budgetTF.font = FONT_SYS(15);
  87. budgetTF.tag = 888;
  88. budgetTF.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 16, FitSize(42))];
  89. budgetTF.leftViewMode = UITextFieldViewModeAlways;
  90. budgetTF.clearButtonMode = UITextFieldViewModeWhileEditing;
  91. budgetTF.keyboardType = UIKeyboardTypeEmailAddress;
  92. [self.view addSubview:budgetTF];
  93. NSString *mail = [[NSUserDefaults standardUserDefaults] valueForKey:JZOUTPUTEMAIL];
  94. if (mail.length) {
  95. budgetTF.text = mail;
  96. }
  97. UIButton *saveButton = [UIButton buttonWithType:UIButtonTypeCustom];
  98. saveButton.frame = CGRectMake(FitSize(28), FitSize(290), FitSize(315), FitSize(50));
  99. [saveButton addTarget:self action:@selector(outputAction) forControlEvents:UIControlEventTouchUpInside];
  100. [saveButton setTitle:@"导出" forState:0];
  101. [saveButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
  102. saveButton.titleLabel.font = FONT_SYS(15);
  103. saveButton.cornerRadius = 12;
  104. saveButton.tag = 999;
  105. saveButton.enabled = NO;
  106. saveButton.backgroundColor = [UIColor JZColorWithHex:0xdddddd];
  107. [self.view addSubview:saveButton];
  108. if (budgetTF.text.length) {
  109. saveButton.backgroundColor = [UIColor baseColor];
  110. saveButton.enabled = YES;
  111. }
  112. else {
  113. saveButton.backgroundColor = [UIColor JZColorWithHex:0xdddddd];
  114. saveButton.enabled = NO;
  115. }
  116. [line mas_makeConstraints:^(MASConstraintMaker *make) {
  117. make.left.and.right.and.centerY.equalTo(topView);
  118. make.height.mas_equalTo(0.5);
  119. }];
  120. [startLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  121. make.left.mas_equalTo(topView).offset(FitSize(16));
  122. make.top.mas_equalTo(topView).offset(FitSize(16));
  123. }];
  124. [startButton mas_makeConstraints:^(MASConstraintMaker *make) {
  125. make.right.equalTo(topView).offset(-15);
  126. make.centerY.equalTo(startLabel);
  127. }];
  128. [endLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  129. make.left.equalTo(startLabel);
  130. make.top.equalTo(line.mas_bottom).offset(FitSize(16));
  131. }];
  132. [endButton mas_makeConstraints:^(MASConstraintMaker *make) {
  133. make.right.and.left.equalTo(startButton);
  134. make.centerY.equalTo(endLabel);
  135. }];
  136. [budgetLabel mas_makeConstraints:^(MASConstraintMaker *make) {
  137. make.left.equalTo(self.view).offset(16);
  138. make.top.equalTo(topView.mas_bottom).offset(FitSize(20));
  139. }];
  140. [budgetTF mas_makeConstraints:^(MASConstraintMaker *make) {
  141. make.left.and.right.equalTo(self.view);
  142. make.top.equalTo(topView.mas_bottom).offset(FitSize(45));
  143. make.height.mas_equalTo(FitSize(42));
  144. }];
  145. }
  146. - (void)timeButtonAction:(UIButton *)sender {
  147. PGDatePicker *pgDatePicker = [[PGDatePicker alloc] init];
  148. pgDatePicker.tag = sender.tag;
  149. pgDatePicker.delegate = self;
  150. pgDatePicker.datePickerMode = PGDatePickerModeDate;
  151. pgDatePicker.minimumDate = [NSDate setYear:2010 month:1 day:1];
  152. pgDatePicker.maximumDate = [NSDate date];
  153. pgDatePicker.lineBackgroundColor = [UIColor lineColor];
  154. pgDatePicker.textColorOfSelectedRow = [UIColor grayColor];
  155. pgDatePicker.textColorOfOtherRow = [UIColor grayColor];
  156. pgDatePicker.cancelButtonTextColor = [UIColor JZColorWithHex:0x007aff];
  157. pgDatePicker.confirmButtonTextColor = [UIColor JZColorWithHex:0x007aff];
  158. [pgDatePicker showWithShadeBackgroud];
  159. }
  160. - (void)outputAction {
  161. UITextField *tf = [self.view viewWithTag:888];
  162. if (![self validateEmail:tf.text]) {
  163. [MBProgressHUD showTip:@"您输入的邮箱不合法"];
  164. }
  165. else {
  166. [[NSUserDefaults standardUserDefaults] setValue:tf.text forKey:JZOUTPUTEMAIL];
  167. [[NSUserDefaults standardUserDefaults] synchronize];
  168. NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  169. fmt.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
  170. fmt.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
  171. fmt.dateFormat = @"yyyy年MM月dd日";
  172. NSDate *startDate = [fmt dateFromString:self.startButton.titleLabel.text];
  173. NSDate *endDate = [fmt dateFromString:self.endButton.titleLabel.text];
  174. NSDate *nowDate = [NSDate date];
  175. NSTimeZone *zone = [NSTimeZone systemTimeZone];
  176. NSTimeInterval interval = [zone secondsFromGMTForDate:nowDate];
  177. nowDate = [nowDate dateByAddingTimeInterval:interval];
  178. NSString *startTime = [NSString stringWithFormat:@"%ld", (long)[startDate timeIntervalSince1970]];
  179. NSString *endTime = [NSString stringWithFormat:@"%ld", (long)[endDate timeIntervalSince1970]];
  180. NSString *nowTime = [NSString stringWithFormat:@"%ld", (long)[nowDate timeIntervalSince1970]];
  181. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  182. [params setValue:tf.text forKey:@"email"];
  183. [params setValue:startTime forKey:@"start_time"];
  184. [params setValue:endTime forKey:@"end_time"];
  185. [params setValue:nowTime forKey:@"time"];
  186. [params setValue:[params md5String] forKey:@"sign"];
  187. NSString *urlString = [NSString stringWithFormat:@"%@/applyExport",URL];
  188. [JZHttp post:urlString params:params success:^(id json) {
  189. [MBProgressHUD showTip:@"正在导出数据中..." time:5.0f];
  190. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0f * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
  191. [MBProgressHUD showTip:@"数据导出成功,稍后请查看邮件"];
  192. });
  193. } failure:^(NSError *error) {
  194. }];
  195. }
  196. }
  197. - (void)textChanged:(UITextField *)tf {
  198. UIButton *saveBtn = [self.view viewWithTag:999];
  199. if (tf.text.length) {
  200. saveBtn.backgroundColor = [UIColor baseColor];
  201. saveBtn.enabled = YES;
  202. }
  203. else {
  204. saveBtn.backgroundColor = [UIColor JZColorWithHex:0xdddddd];
  205. saveBtn.enabled = NO;
  206. }
  207. }
  208. - (void)datePicker:(PGDatePicker *)datePicker didSelectDate:(NSDateComponents *)dateComponents {
  209. NSString *time = nil;
  210. if (dateComponents.month > 9 && dateComponents.day > 9) {
  211. time = [NSString stringWithFormat:@"%ld年%ld月%ld日", (long)dateComponents.year, (long)dateComponents.month, (long)dateComponents.day];
  212. }
  213. else if (dateComponents.month <= 9 && dateComponents.day > 9) {
  214. time = [NSString stringWithFormat:@"%ld年0%ld月%ld日", (long)dateComponents.year, (long)dateComponents.month, (long)dateComponents.day];
  215. }
  216. else if (dateComponents.month > 9 && dateComponents.day <= 9) {
  217. time = [NSString stringWithFormat:@"%ld年%ld月0%ld日", (long)dateComponents.year, (long)dateComponents.month, (long)dateComponents.day];
  218. }
  219. else if (dateComponents.month <= 9 && dateComponents.day <= 9) {
  220. time = [NSString stringWithFormat:@"%ld年0%ld月0%ld日", (long)dateComponents.year, (long)dateComponents.month, (long)dateComponents.day];
  221. }
  222. if (datePicker.tag == 111) {
  223. [self.startButton setTitle:time forState:0];
  224. }
  225. else {
  226. [self.endButton setTitle:time forState:0];
  227. }
  228. }
  229. //邮箱
  230. - (BOOL)validateEmail:(NSString *)email
  231. {
  232. NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
  233. NSPredicate *emailTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", emailRegex];
  234. return [emailTest evaluateWithObject:email];
  235. }
  236. - (NSString *)getTodayString {
  237. NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
  238. fmt.locale = [NSLocale localeWithLocaleIdentifier:@"zh_CN"];
  239. fmt.timeZone = [NSTimeZone timeZoneForSecondsFromGMT:0];
  240. fmt.dateFormat = @"yyyy年MM月dd日";
  241. NSDate *date = [NSDate date];
  242. NSTimeZone *zone = [NSTimeZone systemTimeZone];
  243. NSTimeInterval interval = [zone secondsFromGMTForDate:date];
  244. date = [date dateByAddingTimeInterval:interval];
  245. NSString *todayTime = [fmt stringFromDate:date];
  246. return todayTime;
  247. }
  248. /*
  249. #pragma mark - Navigation
  250. // In a storyboard-based application, you will often want to do a little preparation before navigation
  251. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
  252. // Get the new view controller using [segue destinationViewController].
  253. // Pass the selected object to the new view controller.
  254. }
  255. */
  256. @end