RAOrderEditViewController.m 27 KB

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