RAOrderEditViewController.m 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881
  1. //
  2. // RAOrderEditViewController.m
  3. // Apex And Drivers
  4. //
  5. // Created by Jack on 2018/6/4.
  6. // Copyright © 2018年 USAI. All rights reserved.
  7. //
  8. #import "RAOrderEditViewController.h"
  9. #import "RAEditInputModel.h"
  10. #import "RAEditMultInputModel.h"
  11. #import "RAEditLabelModel.h"
  12. #import "RAEditImageBaseModel.h"
  13. #import "RAEditPhotoModel.h"
  14. #import "RAEditSignatureModel.h"
  15. #import "RAEditDateModel.h"
  16. #import "RAEditMultPhotoModel.h"
  17. #import "RAProgressHUD.h"
  18. #import "ZipArchive.h"
  19. #import "AppDelegate.h"
  20. #import "UIScrollView+Empty.h"
  21. #import "RAEmptyView.h"
  22. #import "RAEditRequiredAlert.h"
  23. #import <CoreLocation/CoreLocation.h>
  24. #import "RAPhotoCell.h"
  25. @interface RAEditSectionModel : NSObject
  26. @property (nonatomic,strong) NSArray <RAEditBaseModel *> *items;
  27. @property (nonatomic,copy) NSString *title;
  28. @end
  29. @implementation RAEditSectionModel
  30. - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
  31. }
  32. - (void)setItems:(NSArray<RAEditBaseModel *> *)items {
  33. NSMutableArray *tmpItems = [items mutableCopy];
  34. // [tmpItems addObject:@{
  35. // @"type" : @6,
  36. // @"title" : @"Container Photo",
  37. // @"required" : @(YES),
  38. // @"photos" : @[@{@"placeHolder":@"btn_add_photo"},@{@"placeHolder":@"btn_add_photo"},@{@"placeHolder":@"btn_add_photo"},@{@"placeHolder":@"btn_add_photo"},@{@"placeHolder":@"btn_add_photo"},@{@"placeHolder":@"btn_add_photo"}],
  39. // @"key" : @"test_photo"
  40. // }];
  41. NSMutableArray *itemArr = [NSMutableArray arrayWithCapacity:items.count];
  42. for (int i = 0; i < tmpItems.count; i++) {
  43. NSDictionary *item = [tmpItems objectAtIndex:i];
  44. RAEditBaseModel *model = [self createModelWithJsonItem:item];
  45. if (model) {
  46. [itemArr addObject:model];
  47. // expand 直接放出来
  48. if (model.expand && [model.expand isKindOfClass:[NSDictionary class]]) {
  49. RAEditBaseModel *expand = [self createModelWithJsonItem:model.expand];
  50. if (expand) {
  51. model.expand = expand;
  52. [itemArr addObject:expand];
  53. }
  54. }
  55. }
  56. }
  57. _items = itemArr;
  58. }
  59. - (NSInteger)itemCount {
  60. return self.items.count;
  61. }
  62. - (RAEditBaseModel *)itemModelForIndex:(NSInteger)index {
  63. return [self.items objectAtIndex:index];
  64. }
  65. - (RAEditBaseModel *)createModelWithJsonItem:(NSDictionary *)item {
  66. RAEditType type = [[item objectForKey:@"type"] intValue];
  67. RAEditBaseModel *model;
  68. switch (type) {
  69. case RAEditTypeLabel: {
  70. model = [RAEditLabelModel new];
  71. }
  72. break;
  73. case RAEditTypeInput: {
  74. model = [RAEditInputModel new];
  75. }
  76. break;
  77. case RAEditTypeMultInput: {
  78. model = [RAEditMultInputModel new];
  79. }
  80. break;
  81. case RAEditTypePhoto: {
  82. model = [RAEditPhotoModel new];
  83. }
  84. break;
  85. case RAEditTypeSignature: {
  86. model = [RAEditSignatureModel new];
  87. }
  88. break;
  89. case RAEditTypeDate: {
  90. model = [RAEditDateModel new];
  91. }
  92. break;
  93. case RAEditTypeMultPhoto: {
  94. RAEditMultPhotoModel *photoModel = [RAEditMultPhotoModel new];
  95. photoModel.width = [UIScreen mainScreen].bounds.size.width;
  96. model = photoModel;
  97. }
  98. break;
  99. default: {
  100. model = nil;
  101. }
  102. break;
  103. }
  104. [model setValuesForKeysWithDictionary:item];
  105. return model;
  106. }
  107. - (RAEditBaseModel *)addModelAtIndex:(NSUInteger)idx withJsonItem:(NSDictionary *)item {
  108. RAEditBaseModel *model = [self createModelWithJsonItem:item];
  109. if (model) {
  110. NSMutableArray *arr = [self.items mutableCopy];
  111. [arr insertObject:model atIndex:idx];
  112. _items = arr;
  113. }
  114. return model;
  115. }
  116. @end
  117. #pragma mark - View Controller
  118. @interface RAOrderEditViewController ()
  119. @property (nonatomic,strong) IBOutlet UITableView *orderEditTableView;
  120. @property (nonatomic,strong) NSMutableArray *sectionArray;
  121. @property (nonatomic,copy) NSString *photoDir;
  122. @property (nonatomic,strong) UIRefreshControl *refreshControl;
  123. @end
  124. @implementation RAOrderEditViewController
  125. + (instancetype)viewControllerFromStoryboard {
  126. RAOrderEditViewController *editVC = [[UIStoryboard storyboardWithName:@"Edit" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
  127. return editVC;
  128. }
  129. - (void)viewDidLoad {
  130. [super viewDidLoad];
  131. // Do any additional setup after loading the view.
  132. [self configureTable];
  133. [self configureNavigationBar];
  134. [self loadData];
  135. }
  136. - (void)viewWillAppear:(BOOL)animated {
  137. [super viewWillAppear:animated];
  138. [self registKeyboardListener];
  139. }
  140. - (void)viewWillDisappear:(BOOL)animated {
  141. [super viewWillDisappear:animated];
  142. [self unregistKeyboardListener];
  143. }
  144. - (void)didReceiveMemoryWarning {
  145. [super didReceiveMemoryWarning];
  146. // Dispose of any resources that can be recreated.
  147. }
  148. #pragma mark - Configure
  149. - (void)configureTable {
  150. if (@available(iOS 11.0, *)) {
  151. self.orderEditTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
  152. } else {
  153. self.automaticallyAdjustsScrollViewInsets = NO;
  154. }
  155. self.orderEditTableView.tableFooterView = [UIView new];
  156. self.orderEditTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
  157. UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
  158. [refresh addTarget:self action:@selector(refreshControlValueChanged:) forControlEvents:UIControlEventValueChanged];
  159. [self.orderEditTableView addSubview:refresh];
  160. self.refreshControl = refresh;
  161. // empty
  162. __weak typeof(self) weakSelf = self;
  163. self.orderEditTableView.emptyView = [RAEmptyView emptyViewWithTapBlk:^(id sender) {
  164. [weakSelf loadData];
  165. }];
  166. [RAPhotoCell regist2TableView:self.orderEditTableView];
  167. }
  168. - (void)configureNavigationBar {
  169. UIBarButtonItem *updateItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Update", nil) style:UIBarButtonItemStylePlain target:self action:@selector(updateBtnClick:)];
  170. self.navigationItem.rightBarButtonItem = updateItem;
  171. }
  172. - (void)registKeyboardListener {
  173. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
  174. }
  175. - (void)unregistKeyboardListener {
  176. [[NSNotificationCenter defaultCenter] removeObserver:self];
  177. }
  178. #pragma mark - Action
  179. - (void)refreshControlValueChanged:(UIRefreshControl *)refresh {
  180. [self loadData];
  181. }
  182. #pragma mark - Getter
  183. - (UITableView *)tableView {
  184. return self.orderEditTableView;
  185. }
  186. - (NSMutableArray *)sectionArray {
  187. if (!_sectionArray) {
  188. _sectionArray = [NSMutableArray array];
  189. }
  190. return _sectionArray;
  191. }
  192. - (NSUInteger)editSectionCount {
  193. return self.sectionArray.count;
  194. }
  195. - (NSInteger)itemCountForSection:(NSInteger)section {
  196. return [[self.sectionArray objectAtIndex:section] itemCount];
  197. }
  198. - (RAEditBaseModel *)modelForIndexPath:(NSIndexPath *)indexPath {
  199. return [[self.sectionArray objectAtIndex:indexPath.section] itemModelForIndex:indexPath.row];
  200. }
  201. - (NSString *)titleForSection:(NSInteger)section {
  202. return [[self.sectionArray objectAtIndex:section] title];
  203. }
  204. - (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell {
  205. return [self.orderEditTableView indexPathForCell:cell];
  206. }
  207. #pragma mark - Setter
  208. - (void)addModelAtIndex:(NSIndexPath *)indexPath withJsonItem:(NSDictionary *)item {
  209. RAEditSectionModel *section = [self.sectionArray objectAtIndex:indexPath.section];
  210. [section addModelAtIndex:indexPath.row withJsonItem:item];
  211. [self.orderEditTableView reloadData];
  212. }
  213. - (void)expandIndexPath:(NSIndexPath *)indexPath withJsonItem:(NSDictionary *)item {
  214. RAEditSectionModel *section = [self.sectionArray objectAtIndex:indexPath.section];
  215. RAEditBaseModel *curM = [self modelForIndexPath:indexPath];
  216. RAEditBaseModel *expand = [section addModelAtIndex:(indexPath.row + 1) withJsonItem:item];
  217. curM.expand = expand;
  218. [self.orderEditTableView reloadData];
  219. }
  220. #pragma mark - Data
  221. - (void)loadData {
  222. if (self.loading) {
  223. return;
  224. }
  225. self.loading = YES;
  226. [self.orderEditTableView hideEmpty];
  227. // show progress
  228. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  229. NSString *orderID = self.orderID;
  230. NSInteger actionID = self.actionID;
  231. NSInteger actionIdx = self.actionIdx;
  232. __weak typeof(self) weakSelf = self;
  233. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  234. NSDictionary *json = [RADataProvider requestUpdateOrder:orderID driverAction:actionID index:actionIdx];
  235. dispatch_async(dispatch_get_main_queue(), ^{
  236. // dismiss progress
  237. [hud dismiss];
  238. if (weakSelf.refreshControl.isRefreshing) {
  239. [weakSelf.refreshControl endRefreshing];
  240. }
  241. if (weakSelf) {
  242. __strong typeof(weakSelf) strongSelf = weakSelf;
  243. int result = [[json objectForKey:@"result"] intValue];
  244. if (result == RESULT_TRUE) {
  245. NSArray *sectionArray = [json objectForKey:@"sections"];
  246. [strongSelf.sectionArray removeAllObjects];
  247. for (int i = 0; i < sectionArray.count; i++) {
  248. NSDictionary *section = [sectionArray objectAtIndex:i];
  249. RAEditSectionModel *model = [RAEditSectionModel new];
  250. [model setValuesForKeysWithDictionary:section];
  251. [strongSelf.sectionArray addObject:model];
  252. }
  253. [strongSelf.orderEditTableView reloadData];
  254. } else {
  255. [strongSelf.sectionArray removeAllObjects];
  256. strongSelf.orderEditTableView.contentOffset = CGPointZero;
  257. [strongSelf.orderEditTableView reloadData];
  258. // process error
  259. NSString *msg = [json objectForKey:@"err_msg"];
  260. // [strongSelf showAlert:msg];
  261. [strongSelf showAlertTilte:NSLocalizedString(@"Warning", nil) message:msg];
  262. }
  263. }
  264. weakSelf.loading = NO;
  265. if (weakSelf.editSectionCount == 0) {
  266. [weakSelf.orderEditTableView showEmpty];
  267. } else {
  268. [weakSelf.orderEditTableView hideEmpty];
  269. }
  270. });
  271. });
  272. }
  273. #pragma mark - Private
  274. - (void)submitSuccessWithPhoto:(BOOL)photo {
  275. NSString *msg = NSLocalizedString(@"update_success", nil);
  276. if (photo) {
  277. msg = NSLocalizedString(@"update_photo_success", nil);
  278. }
  279. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleAlert];
  280. __weak typeof(self) weakSelf = self;
  281. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Ok", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  282. if (weakSelf) {
  283. __strong typeof(weakSelf) strongSelf = weakSelf;
  284. [strongSelf gobackHome];
  285. }
  286. }];
  287. [alertVC addAction:okAction];
  288. [self presentViewController:alertVC animated:YES completion:nil];
  289. }
  290. #pragma mark - Tap Action
  291. - (IBAction)tapToResignFirstResponder:(UITapGestureRecognizer *)sender {
  292. [self.view endEditing:YES];
  293. }
  294. - (void)updateBtnClick:(UIBarButtonItem *)sender {
  295. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  296. if (self.orderType2) {
  297. [params setObject:self.orderType2 forKey:@"orderType2"];
  298. }
  299. if (RASingleton.sharedInstance.requiredLocation) {
  300. CLLocation *location = RASingleton.sharedInstance.currentLocation;
  301. NSString *latLon = nil;
  302. if (location) {
  303. latLon = [NSString stringWithFormat:@"%f,%f",location.coordinate.latitude,location.coordinate.longitude];
  304. } else {
  305. latLon = @"-999,-999";
  306. }
  307. [params setObject:latLon forKey:@"location"];
  308. }
  309. NSMutableArray *emptyArr = [NSMutableArray array];
  310. NSMutableArray <RAEditImageBaseModel *> *photoArr = [self prepareParams:params checkRequired:emptyArr];
  311. if (emptyArr.count > 0) {
  312. RAEditRequiredAlert *alertVC = [RAEditRequiredAlert alertWithTile:NSLocalizedString(@"Warning", nil) message:[NSString localizedStringWithFormat:NSLocalizedString(@"please complete missing field:\n%@", nil),[emptyArr componentsJoinedByString:@"\n"]]];
  313. [self presentViewController:alertVC animated:YES completion:nil];
  314. return;
  315. }
  316. [params setValue:@(photoArr.count) forKey:@"photoCount"];
  317. // show progress
  318. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  319. __weak typeof(self) weakSelf = self;
  320. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  321. if (RASingleton.sharedInstance.offline) { // 离线
  322. NSDictionary *json = [weakSelf offlineUpdate:params photos:photoArr];
  323. [hud dismiss:^{
  324. NSString *msg = [json objectForKey:@"err_msg"];
  325. [weakSelf showAlertTilte:NSLocalizedString(@"Warning", nil) message:msg];
  326. }];
  327. }
  328. else { // 在线
  329. NSDictionary *json = [RADataProvider submitEditOrder:params];
  330. if (weakSelf) {
  331. __strong typeof(weakSelf) strongSelf = weakSelf;
  332. int result = [[json objectForKey:@"result"] intValue];
  333. if (result == RESULT_TRUE) {
  334. BOOL requiredLocation = [[json objectForKey:@"requiredLocation"] boolValue];
  335. [RASingleton sharedInstance].requiredLocation = requiredLocation;
  336. dispatch_async(dispatch_get_main_queue(), ^{
  337. // if (photoArr.count > 0) {
  338. // [strongSelf syncUploadPhotos:photoArr Json:json HUD:hud];
  339. // } else {
  340. // [strongSelf gobackHome];
  341. // }
  342. // 全部转为后台上传
  343. if (photoArr.count > 0) {
  344. [strongSelf backgroundUploadPhoto:photoArr Json:json];
  345. }
  346. [hud dismiss:^{
  347. [strongSelf submitSuccessWithPhoto:photoArr.count > 0];
  348. }];
  349. });
  350. } else {
  351. // process error
  352. dispatch_async(dispatch_get_main_queue(), ^{
  353. // dismiss progress
  354. [hud dismiss:^{
  355. NSString *msg = [json objectForKey:@"err_msg"];
  356. [strongSelf showAlertTilte:NSLocalizedString(@"Warning", nil) message:msg];
  357. }];
  358. });
  359. }
  360. }
  361. }
  362. });
  363. }
  364. - (void)backgroundUploadPhoto:(NSArray<RAEditImageBaseModel *> *)photos Json:(NSDictionary *)json {
  365. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  366. NSDate *date = [NSDate date];
  367. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  368. formatter.dateFormat = @"MM/dd/YYYY HH:mm";
  369. NSString *time = [formatter stringFromDate:date];
  370. NSString *serial = [json objectForKey:@"serial"];
  371. for (RAEditImageBaseModel *model in photos) {
  372. if (serial.length) {
  373. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  374. [params setObject:serial forKey:@"serial"];
  375. if (self.orderType2) {
  376. [params setObject:self.orderType2 forKey:@"orderType2"];
  377. }
  378. if (model.key) {
  379. [params setObject:model.key forKey:@"key"];
  380. }
  381. [params setObject:@(model.type) forKey:@"type"];
  382. [params setObject:[RASingleton.sharedInstance encryptUser] forKey:@"name"];
  383. [params setObject:[RASingleton.sharedInstance encryptPassword] forKey:@"password"];
  384. [params setObject:@"iOS" forKey:@"platform"];
  385. NSString *photoPath = [self.photoDir.lastPathComponent stringByAppendingPathComponent:model.imageName];
  386. NSMutableDictionary *task = [@{
  387. @"order" : self.orderID,
  388. @"action" : self.actionTitle,
  389. @"name" : model.title,
  390. @"time" : time,
  391. @"url" : URL_UPLOAD,
  392. @"file" : photoPath,
  393. @"params" : params
  394. } mutableCopy];
  395. [appDelegate.uploadManager addTask:task];
  396. }
  397. }
  398. }
  399. - (void)syncUploadPhotos:(NSMutableArray<RAEditImageBaseModel *> *)photoArr Json:(NSDictionary *)json HUD:(RAProgressHUD *)hud {
  400. dispatch_async(dispatch_get_main_queue(), ^{
  401. RAProgressHUD *innerHUD = hud;
  402. if (!innerHUD) {
  403. innerHUD = [RAProgressHUD showHUDOnView:self.view]; // main queue
  404. }
  405. dispatch_async(dispatch_get_global_queue(0, 0), ^{ // network
  406. int retryCount = 0;
  407. NSMutableArray *completArr = [NSMutableArray array];
  408. NSString *serial = [json objectForKey:@"serial"];
  409. // 同步上传照片
  410. for (int i = 0; i < photoArr.count; i++) {
  411. RAEditImageBaseModel *model = [photoArr objectAtIndex:i];
  412. if (serial.length) {
  413. NSMutableDictionary *fileParams = [NSMutableDictionary dictionary];
  414. [fileParams setObject:serial forKey:@"serial"];
  415. if (self.orderType2) {
  416. [fileParams setObject:self.orderType2 forKey:@"orderType2"];
  417. }
  418. if (model.key) {
  419. [fileParams setObject:model.key forKey:@"key"];
  420. }
  421. [fileParams setObject:@(model.type) forKey:@"type"];
  422. NSString *photoPath = [self.photoDir stringByAppendingPathComponent:model.imageName];
  423. NSDictionary *uploadJson = [RADataProvider uploadFile:photoPath parameters:fileParams];
  424. int uploadResult = [[uploadJson objectForKey:@"result"] intValue];
  425. if (uploadResult != RESULT_TRUE) { // 失败重试
  426. i--;
  427. retryCount++;
  428. if (retryCount >= 3) {
  429. break;
  430. }
  431. } else {
  432. [completArr addObject:model];
  433. retryCount = 0;
  434. // 删除文件
  435. if ([[NSFileManager defaultManager] fileExistsAtPath:photoPath]) {
  436. [[NSFileManager defaultManager] removeItemAtPath:photoPath error:nil];
  437. }
  438. }
  439. } // serial
  440. } // for
  441. // 将完成的model移除
  442. [photoArr removeObjectsInArray:completArr];
  443. __weak typeof(self) weakSelf = self;
  444. dispatch_async(dispatch_get_main_queue(), ^{
  445. [innerHUD dismiss:^{
  446. if (photoArr.count > 0) {
  447. // 上传失败,询问是否丢到后台线程上传,否则重启上传
  448. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Warning", nil) message:NSLocalizedString(@"update_upload_failed_tips", nil) preferredStyle:UIAlertControllerStyleAlert];
  449. UIAlertAction *backgroundAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Background", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  450. // 开启后台上传
  451. [weakSelf backgroundUploadPhoto:photoArr Json:json];
  452. // 返回首页
  453. [weakSelf gobackHome];
  454. }];
  455. UIAlertAction *retryAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Retry", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  456. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  457. [weakSelf syncUploadPhotos:photoArr Json:json HUD:nil];
  458. });
  459. }];
  460. [alertVC addAction:backgroundAction];
  461. [alertVC addAction:retryAction];
  462. [weakSelf presentViewController:alertVC animated:YES completion:nil];
  463. } else {
  464. // 上传完成,返回到首页
  465. [weakSelf gobackHome];
  466. }
  467. }];
  468. });
  469. });
  470. });
  471. }
  472. - (void)gobackHome {
  473. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationReloadHome object:nil];
  474. [self.navigationController popToRootViewControllerAnimated:YES];
  475. }
  476. #pragma mark - Keyboard Listener
  477. - (void)keyboardWillChangeFrame:(NSNotification *)notification {
  478. CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  479. CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
  480. CGFloat keyboardHeight = screenHeight - CGRectGetMinY(end);
  481. UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, keyboardHeight, 0);
  482. self.orderEditTableView.contentInset = insets;
  483. if (self.editingIndexPath) {
  484. [self.orderEditTableView scrollToRowAtIndexPath:self.editingIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
  485. }
  486. }
  487. #pragma mark - Package Update Data
  488. - (NSString *)photoDir {
  489. if (!_photoDir) {
  490. NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  491. NSString *photoDir = [cacheDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%ld_%@",self.orderID,(long)self.actionID,[NSUUID UUID].UUIDString]];
  492. NSError *error;
  493. // BOOL dirExist = YES;
  494. // for (int i = 0; i < INT_MAX; i++) {
  495. // if (i != 0) {
  496. // photoDir = [photoDir stringByAppendingString:[NSString stringWithFormat:@"(%d)",i]];
  497. // }
  498. // if (![[NSFileManager defaultManager] fileExistsAtPath:photoDir]) {
  499. // dirExist = NO;
  500. // break;
  501. // }
  502. // }
  503. [[NSFileManager defaultManager] createDirectoryAtPath:photoDir withIntermediateDirectories:YES attributes:nil error:&error];
  504. if (error) {
  505. NSLog(@"create dir %@ failed %@",photoDir,error.localizedDescription);
  506. return nil;
  507. }
  508. _photoDir = photoDir;
  509. }
  510. return _photoDir;
  511. }
  512. - (NSMutableArray <RAEditImageBaseModel *> *)prepareParams:(NSMutableDictionary *)params checkRequired:(NSMutableArray *)emptyArr {
  513. if (params == nil) {
  514. return nil;;
  515. }
  516. NSMutableArray <RAEditImageBaseModel *> *photoArr = [NSMutableArray array];
  517. NSString *photoDir = [self photoDir];
  518. [params setObject:self.orderID forKey:@"orderID"];
  519. [params setObject:@(self.actionID) forKey:@"actionID"];
  520. NSString* encryptu = [RASingleton sharedInstance].encryptUser;
  521. NSString* encryptp = [RASingleton sharedInstance].encryptPassword;
  522. [params setObject:encryptu forKey:@"name"];
  523. [params setObject:encryptp forKey:@"password"];
  524. [params setObject:@"iOS" forKey:@"platform"];
  525. for (RAEditSectionModel *section in self.sectionArray) {
  526. for (int i = 0; i < [section itemCount]; i++) {
  527. RAEditBaseModel *model = [section itemModelForIndex:i];
  528. switch (model.type) {
  529. case RAEditTypeLabel: {
  530. }
  531. break;
  532. case RAEditTypeInput: {
  533. RAEditInputModel *inputModel = (RAEditInputModel *)model;
  534. if (model.required && inputModel.value.length == 0) {
  535. [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
  536. continue;
  537. }
  538. if (inputModel.key && inputModel.value.length > 0) {
  539. [params setObject:inputModel.value forKey:inputModel.key];
  540. }
  541. }
  542. break;
  543. case RAEditTypeMultInput: {
  544. RAEditMultInputModel *multInputModel = (RAEditMultInputModel *)model;
  545. if (model.required && multInputModel.value.length == 0) {
  546. [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
  547. continue;
  548. }
  549. if (multInputModel.key && multInputModel.value.length > 0) {
  550. [params setObject:multInputModel.value forKey:multInputModel.key];
  551. }
  552. }
  553. break;
  554. case RAEditTypePhoto:
  555. case RAEditTypeSignature: {
  556. if (photoDir) {
  557. RAEditImageBaseModel *imageModel = (RAEditImageBaseModel *)model;
  558. if (model.required && imageModel.image == nil) {
  559. [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
  560. continue;
  561. }
  562. if (imageModel.image) {
  563. NSString *photoPath = [photoDir stringByAppendingPathComponent:imageModel.imageName];
  564. NSData *imgData = UIImageJPEGRepresentation(imageModel.image, 1.0f);
  565. if (imgData) {
  566. [imgData writeToFile:photoPath atomically:NO];
  567. [params setObject:imageModel.imageName forKey:imageModel.key];
  568. [photoArr addObject:imageModel];
  569. }
  570. }
  571. }
  572. }
  573. break;
  574. case RAEditTypeDate: {
  575. RAEditDateModel *dateModel = (RAEditDateModel *)model;
  576. if (model.required && dateModel.value.length == 0) {
  577. [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
  578. continue;
  579. }
  580. if (model.key && dateModel.value) {
  581. [params setObject:dateModel.value forKey:model.key];
  582. }
  583. }
  584. break;
  585. case RAEditTypeMultPhoto: {
  586. if (photoDir) {
  587. RAEditMultPhotoModel *photoModel = (RAEditMultPhotoModel *)model;
  588. NSUInteger count = photoModel.photoCount;
  589. if (photoModel.required && count == 0) {
  590. [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
  591. continue;
  592. }
  593. for (int i = 0; i < count; i++) {
  594. UIImage *img = [photoModel photoForIndex:i];
  595. if (img) {
  596. // 临时创建PhotoModel,用于上传
  597. RAEditPhotoModel *tmpPhotoModel = [RAEditPhotoModel new];
  598. tmpPhotoModel.type = RAEditTypePhoto;
  599. tmpPhotoModel.key = [NSString stringWithFormat:@"%@_%d",photoModel.key,i];
  600. tmpPhotoModel.photo = img;
  601. NSString *photoPath = [photoDir stringByAppendingPathComponent:tmpPhotoModel.imageName];
  602. NSData *imgData = UIImageJPEGRepresentation(tmpPhotoModel.photo, 1.0f);
  603. if (imgData) {
  604. [imgData writeToFile:photoPath atomically:NO];
  605. [params setObject:tmpPhotoModel.imageName forKey:tmpPhotoModel.key];
  606. [photoArr addObject:tmpPhotoModel];
  607. }
  608. }
  609. }
  610. }
  611. }
  612. break;
  613. default:
  614. break;
  615. }
  616. }
  617. }
  618. return photoArr;
  619. }
  620. //- (NSMutableDictionary *)preparePackage {
  621. //
  622. // NSMutableDictionary *params = [NSMutableDictionary dictionary];
  623. // NSInteger photoCount = [self prepareParams:params].count;
  624. // NSString *photoDir = [self photoDir];
  625. //
  626. // NSMutableDictionary *task = [@{
  627. // @"order" : self.orderID,
  628. // @"action" : self.actionTitle,
  629. // @"url" : URL_HOST,
  630. // @"noFile" : @(YES)
  631. // } mutableCopy];
  632. //
  633. // if (photoCount > 0) {
  634. //
  635. // // 压缩文件
  636. // NSString *zipPath = [photoDir stringByAppendingPathExtension:@".zip"];
  637. // ZipArchive *zip = [[ZipArchive alloc] init];
  638. // [zip CreateZipFile2:zipPath];
  639. // NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:photoDir];
  640. // for (NSString *subPath in subPaths) {
  641. // NSString *fullPath = [photoDir stringByAppendingPathComponent:subPath];
  642. // BOOL isDir;
  643. // if([[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDir]) {
  644. // if (!isDir) {
  645. // [zip addFileToZip:fullPath newname:subPath];
  646. // }
  647. // }
  648. // }
  649. // [zip CloseZipFile2];
  650. //
  651. // [[NSFileManager defaultManager] removeItemAtPath:photoDir error:nil];
  652. //
  653. // NSString *md5 = [RAUtils md5WithFile:zipPath];
  654. //
  655. // [params setObject:md5 forKey:@"md5"];
  656. // [task setObject:@"file" forKey:zipPath.lastPathComponent];
  657. // }
  658. //
  659. // [task setObject:params forKey:@"params"];
  660. //
  661. // return task;
  662. //}
  663. #pragma mark - Offline
  664. - (NSDictionary *)offlineUpdate:(NSDictionary *)params photos:(NSArray *)photos {
  665. return [RADataProvider offlineSubmitOrder:self.orderID action:self.actionID title:self.actionTitle index:self.actionIdx withParams:params photos:photos cacheDir:self.photoDir];
  666. }
  667. @end