RAOrderEditViewController.m 25 KB

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