RAOrderEditViewController.m 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  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)submitSuccessWithMsg:(NSString *)msg {
  275. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleAlert];
  276. __weak typeof(self) weakSelf = self;
  277. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Ok", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  278. if (weakSelf) {
  279. __strong typeof(weakSelf) strongSelf = weakSelf;
  280. [strongSelf gobackHome];
  281. }
  282. }];
  283. [alertVC addAction:okAction];
  284. [self presentViewController:alertVC animated:YES completion:nil];
  285. }
  286. - (void)submitSuccessWithPhoto:(BOOL)photo {
  287. NSString *msg = NSLocalizedString(@"update_success", nil);
  288. if (photo) {
  289. msg = NSLocalizedString(@"update_photo_success", nil);
  290. }
  291. [self submitSuccessWithMsg:msg];
  292. }
  293. #pragma mark - Tap Action
  294. - (IBAction)tapToResignFirstResponder:(UITapGestureRecognizer *)sender {
  295. [self.view endEditing:YES];
  296. }
  297. - (void)updateBtnClick:(UIBarButtonItem *)sender {
  298. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  299. if (self.orderType2) {
  300. [params setObject:self.orderType2 forKey:@"orderType2"];
  301. }
  302. if (RASingleton.sharedInstance.requiredLocation) {
  303. CLLocation *location = RASingleton.sharedInstance.currentLocation;
  304. NSString *latLon = nil;
  305. if (location) {
  306. latLon = [NSString stringWithFormat:@"%f,%f",location.coordinate.latitude,location.coordinate.longitude];
  307. } else {
  308. latLon = @"-999,-999";
  309. }
  310. [params setObject:latLon forKey:@"location"];
  311. }
  312. NSMutableArray *emptyArr = [NSMutableArray array];
  313. NSMutableArray <RAEditImageBaseModel *> *photoArr = [self prepareParams:params checkRequired:emptyArr];
  314. if (emptyArr.count > 0) {
  315. RAEditRequiredAlert *alertVC = [RAEditRequiredAlert alertWithTile:NSLocalizedString(@"Warning", nil) message:[NSString localizedStringWithFormat:NSLocalizedString(@"please complete missing field:\n%@", nil),[emptyArr componentsJoinedByString:@"\n"]]];
  316. [self presentViewController:alertVC animated:YES completion:nil];
  317. return;
  318. }
  319. [params setValue:@(photoArr.count) forKey:@"photoCount"];
  320. // show progress
  321. RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
  322. __weak typeof(self) weakSelf = self;
  323. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  324. if (RASingleton.sharedInstance.offline) { // 离线
  325. NSDictionary *json = [weakSelf offlineUpdate:params photos:photoArr];
  326. [hud dismiss:^{
  327. int result = [[json objectForKey:@"result"] intValue];
  328. NSString *msg = NSLocalizedString(@"sorry", nil);
  329. if (result == RESULT_TRUE) {
  330. msg = NSLocalizedString(@"offline_update_success", nil);
  331. [weakSelf submitSuccessWithMsg:msg];
  332. } else {
  333. msg = [json objectForKey:@"err_msg"];
  334. if (msg == nil) {
  335. msg = NSLocalizedString(@"sorry", nil);
  336. }
  337. [weakSelf showAlert:msg];
  338. }
  339. }];
  340. }
  341. else { // 在线
  342. NSDictionary *json = [RADataProvider submitEditOrder:params];
  343. if (weakSelf) {
  344. __strong typeof(weakSelf) strongSelf = weakSelf;
  345. int result = [[json objectForKey:@"result"] intValue];
  346. if (result == RESULT_TRUE) {
  347. BOOL requiredLocation = [[json objectForKey:@"requiredLocation"] boolValue];
  348. [RASingleton sharedInstance].requiredLocation = requiredLocation;
  349. dispatch_async(dispatch_get_main_queue(), ^{
  350. // if (photoArr.count > 0) {
  351. // [strongSelf syncUploadPhotos:photoArr Json:json HUD:hud];
  352. // } else {
  353. // [strongSelf gobackHome];
  354. // }
  355. // 全部转为后台上传
  356. if (photoArr.count > 0) {
  357. [strongSelf backgroundUploadPhoto:photoArr Json:json];
  358. }
  359. [hud dismiss:^{
  360. [strongSelf submitSuccessWithPhoto:photoArr.count > 0];
  361. }];
  362. });
  363. } else {
  364. // process error
  365. dispatch_async(dispatch_get_main_queue(), ^{
  366. // dismiss progress
  367. [hud dismiss:^{
  368. NSString *msg = [json objectForKey:@"err_msg"];
  369. [strongSelf showAlertTilte:NSLocalizedString(@"Warning", nil) message:msg];
  370. }];
  371. });
  372. }
  373. }
  374. }
  375. });
  376. }
  377. - (void)backgroundUploadPhoto:(NSArray<RAEditImageBaseModel *> *)photos Json:(NSDictionary *)json {
  378. AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
  379. NSDate *date = [NSDate date];
  380. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  381. formatter.dateFormat = @"MM/dd/YYYY HH:mm";
  382. NSString *time = [formatter stringFromDate:date];
  383. NSString *serial = [json objectForKey:@"serial"];
  384. for (RAEditImageBaseModel *model in photos) {
  385. if (serial.length) {
  386. NSMutableDictionary *params = [NSMutableDictionary dictionary];
  387. [params setObject:serial forKey:@"serial"];
  388. if (self.orderType2) {
  389. [params setObject:self.orderType2 forKey:@"orderType2"];
  390. }
  391. if (model.key) {
  392. [params setObject:model.key forKey:@"key"];
  393. }
  394. [params setObject:@(model.type) forKey:@"type"];
  395. [params setObject:[RASingleton.sharedInstance encryptUser] forKey:@"name"];
  396. [params setObject:[RASingleton.sharedInstance encryptPassword] forKey:@"password"];
  397. [params setObject:@"iOS" forKey:@"platform"];
  398. NSString *photoPath = [self.photoDir.lastPathComponent stringByAppendingPathComponent:model.imageName];
  399. NSMutableDictionary *task = [@{
  400. @"order" : self.orderID,
  401. @"action" : self.actionTitle,
  402. @"name" : model.title,
  403. @"time" : time,
  404. @"url" : URL_UPLOAD,
  405. @"file" : photoPath,
  406. @"params" : params
  407. } mutableCopy];
  408. [appDelegate.uploadManager addTask:task];
  409. }
  410. }
  411. }
  412. - (void)syncUploadPhotos:(NSMutableArray<RAEditImageBaseModel *> *)photoArr Json:(NSDictionary *)json HUD:(RAProgressHUD *)hud {
  413. dispatch_async(dispatch_get_main_queue(), ^{
  414. RAProgressHUD *innerHUD = hud;
  415. if (!innerHUD) {
  416. innerHUD = [RAProgressHUD showHUDOnView:self.view]; // main queue
  417. }
  418. dispatch_async(dispatch_get_global_queue(0, 0), ^{ // network
  419. int retryCount = 0;
  420. NSMutableArray *completArr = [NSMutableArray array];
  421. NSString *serial = [json objectForKey:@"serial"];
  422. // 同步上传照片
  423. for (int i = 0; i < photoArr.count; i++) {
  424. RAEditImageBaseModel *model = [photoArr objectAtIndex:i];
  425. if (serial.length) {
  426. NSMutableDictionary *fileParams = [NSMutableDictionary dictionary];
  427. [fileParams setObject:serial forKey:@"serial"];
  428. if (self.orderType2) {
  429. [fileParams setObject:self.orderType2 forKey:@"orderType2"];
  430. }
  431. if (model.key) {
  432. [fileParams setObject:model.key forKey:@"key"];
  433. }
  434. [fileParams setObject:@(model.type) forKey:@"type"];
  435. NSString *photoPath = [self.photoDir stringByAppendingPathComponent:model.imageName];
  436. NSDictionary *uploadJson = [RADataProvider uploadFile:photoPath parameters:fileParams];
  437. int uploadResult = [[uploadJson objectForKey:@"result"] intValue];
  438. if (uploadResult != RESULT_TRUE) { // 失败重试
  439. i--;
  440. retryCount++;
  441. if (retryCount >= 3) {
  442. break;
  443. }
  444. } else {
  445. [completArr addObject:model];
  446. retryCount = 0;
  447. // 删除文件
  448. if ([[NSFileManager defaultManager] fileExistsAtPath:photoPath]) {
  449. [[NSFileManager defaultManager] removeItemAtPath:photoPath error:nil];
  450. }
  451. }
  452. } // serial
  453. } // for
  454. // 将完成的model移除
  455. [photoArr removeObjectsInArray:completArr];
  456. __weak typeof(self) weakSelf = self;
  457. dispatch_async(dispatch_get_main_queue(), ^{
  458. [innerHUD dismiss:^{
  459. if (photoArr.count > 0) {
  460. // 上传失败,询问是否丢到后台线程上传,否则重启上传
  461. UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Warning", nil) message:NSLocalizedString(@"update_upload_failed_tips", nil) preferredStyle:UIAlertControllerStyleAlert];
  462. UIAlertAction *backgroundAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Background", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  463. // 开启后台上传
  464. [weakSelf backgroundUploadPhoto:photoArr Json:json];
  465. // 返回首页
  466. [weakSelf gobackHome];
  467. }];
  468. UIAlertAction *retryAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Retry", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
  469. dispatch_async(dispatch_get_global_queue(0, 0), ^{
  470. [weakSelf syncUploadPhotos:photoArr Json:json HUD:nil];
  471. });
  472. }];
  473. [alertVC addAction:backgroundAction];
  474. [alertVC addAction:retryAction];
  475. [weakSelf presentViewController:alertVC animated:YES completion:nil];
  476. } else {
  477. // 上传完成,返回到首页
  478. [weakSelf gobackHome];
  479. }
  480. }];
  481. });
  482. });
  483. });
  484. }
  485. - (void)gobackHome {
  486. [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationReloadHome object:nil];
  487. [self.navigationController popToRootViewControllerAnimated:YES];
  488. }
  489. #pragma mark - Keyboard Listener
  490. - (void)keyboardWillChangeFrame:(NSNotification *)notification {
  491. CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
  492. CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
  493. CGFloat keyboardHeight = screenHeight - CGRectGetMinY(end);
  494. UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, keyboardHeight, 0);
  495. self.orderEditTableView.contentInset = insets;
  496. if (self.editingIndexPath) {
  497. [self.orderEditTableView scrollToRowAtIndexPath:self.editingIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
  498. }
  499. }
  500. #pragma mark - Package Update Data
  501. - (NSString *)photoDir {
  502. if (!_photoDir) {
  503. NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
  504. NSString *photoDir = [cacheDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%ld_%@",self.orderID,(long)self.actionID,[NSUUID UUID].UUIDString]];
  505. NSError *error;
  506. // BOOL dirExist = YES;
  507. // for (int i = 0; i < INT_MAX; i++) {
  508. // if (i != 0) {
  509. // photoDir = [photoDir stringByAppendingString:[NSString stringWithFormat:@"(%d)",i]];
  510. // }
  511. // if (![[NSFileManager defaultManager] fileExistsAtPath:photoDir]) {
  512. // dirExist = NO;
  513. // break;
  514. // }
  515. // }
  516. [[NSFileManager defaultManager] createDirectoryAtPath:photoDir withIntermediateDirectories:YES attributes:nil error:&error];
  517. if (error) {
  518. NSLog(@"create dir %@ failed %@",photoDir,error.localizedDescription);
  519. return nil;
  520. }
  521. _photoDir = photoDir;
  522. }
  523. return _photoDir;
  524. }
  525. - (NSMutableArray <RAEditImageBaseModel *> *)prepareParams:(NSMutableDictionary *)params checkRequired:(NSMutableArray *)emptyArr {
  526. if (params == nil) {
  527. return nil;;
  528. }
  529. NSMutableArray <RAEditImageBaseModel *> *photoArr = [NSMutableArray array];
  530. NSString *photoDir = [self photoDir];
  531. [params setObject:self.orderID forKey:@"orderID"];
  532. [params setObject:@(self.actionID) forKey:@"actionID"];
  533. NSString* encryptu = [RASingleton sharedInstance].encryptUser;
  534. NSString* encryptp = [RASingleton sharedInstance].encryptPassword;
  535. [params setObject:encryptu forKey:@"name"];
  536. [params setObject:encryptp forKey:@"password"];
  537. [params setObject:@"iOS" forKey:@"platform"];
  538. for (RAEditSectionModel *section in self.sectionArray) {
  539. for (int i = 0; i < [section itemCount]; i++) {
  540. RAEditBaseModel *model = [section itemModelForIndex:i];
  541. switch (model.type) {
  542. case RAEditTypeLabel: {
  543. }
  544. break;
  545. case RAEditTypeInput: {
  546. RAEditInputModel *inputModel = (RAEditInputModel *)model;
  547. if (model.required && inputModel.value.length == 0) {
  548. [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
  549. continue;
  550. }
  551. if (inputModel.key && inputModel.value.length > 0) {
  552. [params setObject:inputModel.value forKey:inputModel.key];
  553. }
  554. }
  555. break;
  556. case RAEditTypeMultInput: {
  557. RAEditMultInputModel *multInputModel = (RAEditMultInputModel *)model;
  558. if (model.required && multInputModel.value.length == 0) {
  559. [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
  560. continue;
  561. }
  562. if (multInputModel.key && multInputModel.value.length > 0) {
  563. [params setObject:multInputModel.value forKey:multInputModel.key];
  564. }
  565. }
  566. break;
  567. case RAEditTypePhoto:
  568. case RAEditTypeSignature: {
  569. if (photoDir) {
  570. RAEditImageBaseModel *imageModel = (RAEditImageBaseModel *)model;
  571. if (model.required && imageModel.image == nil) {
  572. [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
  573. continue;
  574. }
  575. if (imageModel.image) {
  576. NSString *photoPath = [photoDir stringByAppendingPathComponent:imageModel.imageName];
  577. NSData *imgData = UIImageJPEGRepresentation(imageModel.image, 1.0f);
  578. if (imgData) {
  579. [imgData writeToFile:photoPath atomically:NO];
  580. [params setObject:imageModel.imageName forKey:imageModel.key];
  581. [photoArr addObject:imageModel];
  582. }
  583. }
  584. }
  585. }
  586. break;
  587. case RAEditTypeDate: {
  588. RAEditDateModel *dateModel = (RAEditDateModel *)model;
  589. if (model.required && dateModel.value.length == 0) {
  590. [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
  591. continue;
  592. }
  593. if (model.key && dateModel.value) {
  594. [params setObject:dateModel.value forKey:model.key];
  595. }
  596. }
  597. break;
  598. case RAEditTypeMultPhoto: {
  599. if (photoDir) {
  600. RAEditMultPhotoModel *photoModel = (RAEditMultPhotoModel *)model;
  601. NSUInteger count = photoModel.photoCount;
  602. if (photoModel.required && count == 0) {
  603. [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
  604. continue;
  605. }
  606. for (int i = 0; i < count; i++) {
  607. UIImage *img = [photoModel photoForIndex:i];
  608. if (img) {
  609. // 临时创建PhotoModel,用于上传
  610. RAEditPhotoModel *tmpPhotoModel = [RAEditPhotoModel new];
  611. tmpPhotoModel.type = RAEditTypePhoto;
  612. tmpPhotoModel.title = [NSString stringWithFormat:@"%@_%d",photoModel.title,i];
  613. tmpPhotoModel.key = [NSString stringWithFormat:@"%@_%d",photoModel.key,i];
  614. tmpPhotoModel.photo = img;
  615. NSString *photoPath = [photoDir stringByAppendingPathComponent:tmpPhotoModel.imageName];
  616. NSData *imgData = UIImageJPEGRepresentation(tmpPhotoModel.photo, 1.0f);
  617. if (imgData) {
  618. [imgData writeToFile:photoPath atomically:NO];
  619. [params setObject:tmpPhotoModel.imageName forKey:tmpPhotoModel.key];
  620. [photoArr addObject:tmpPhotoModel];
  621. }
  622. }
  623. }
  624. }
  625. }
  626. break;
  627. default:
  628. break;
  629. }
  630. }
  631. }
  632. return photoArr;
  633. }
  634. //- (NSMutableDictionary *)preparePackage {
  635. //
  636. // NSMutableDictionary *params = [NSMutableDictionary dictionary];
  637. // NSInteger photoCount = [self prepareParams:params].count;
  638. // NSString *photoDir = [self photoDir];
  639. //
  640. // NSMutableDictionary *task = [@{
  641. // @"order" : self.orderID,
  642. // @"action" : self.actionTitle,
  643. // @"url" : URL_HOST,
  644. // @"noFile" : @(YES)
  645. // } mutableCopy];
  646. //
  647. // if (photoCount > 0) {
  648. //
  649. // // 压缩文件
  650. // NSString *zipPath = [photoDir stringByAppendingPathExtension:@".zip"];
  651. // ZipArchive *zip = [[ZipArchive alloc] init];
  652. // [zip CreateZipFile2:zipPath];
  653. // NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:photoDir];
  654. // for (NSString *subPath in subPaths) {
  655. // NSString *fullPath = [photoDir stringByAppendingPathComponent:subPath];
  656. // BOOL isDir;
  657. // if([[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDir]) {
  658. // if (!isDir) {
  659. // [zip addFileToZip:fullPath newname:subPath];
  660. // }
  661. // }
  662. // }
  663. // [zip CloseZipFile2];
  664. //
  665. // [[NSFileManager defaultManager] removeItemAtPath:photoDir error:nil];
  666. //
  667. // NSString *md5 = [RAUtils md5WithFile:zipPath];
  668. //
  669. // [params setObject:md5 forKey:@"md5"];
  670. // [task setObject:@"file" forKey:zipPath.lastPathComponent];
  671. // }
  672. //
  673. // [task setObject:params forKey:@"params"];
  674. //
  675. // return task;
  676. //}
  677. #pragma mark - Offline
  678. - (NSDictionary *)offlineUpdate:(NSDictionary *)params photos:(NSArray *)photos {
  679. return [RADataProvider offlineSubmitOrder:self.orderID action:self.actionID title:self.actionTitle index:self.actionIdx withParams:params photos:photos cacheDir:self.photoDir];
  680. }
  681. @end