RAOrderEditViewController.m 33 KB

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