RAOrderEditViewController.m 28 KB

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