RAOrderEditViewController.m 29 KB

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