RAOrderEditViewController.m 32 KB

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