| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881 |
- //
- // RAOrderEditViewController.m
- // Apex And Drivers
- //
- // Created by Jack on 2018/6/4.
- // Copyright © 2018年 USAI. All rights reserved.
- //
- #import "RAOrderEditViewController.h"
- #import "RAEditInputModel.h"
- #import "RAEditMultInputModel.h"
- #import "RAEditLabelModel.h"
- #import "RAEditImageBaseModel.h"
- #import "RAEditPhotoModel.h"
- #import "RAEditSignatureModel.h"
- #import "RAEditDateModel.h"
- #import "RAEditMultPhotoModel.h"
- #import "RAProgressHUD.h"
- #import "ZipArchive.h"
- #import "AppDelegate.h"
- #import "UIScrollView+Empty.h"
- #import "RAEmptyView.h"
- #import "RAEditRequiredAlert.h"
- #import <CoreLocation/CoreLocation.h>
- #import "RAPhotoCell.h"
- @interface RAEditSectionModel : NSObject
- @property (nonatomic,strong) NSArray <RAEditBaseModel *> *items;
- @property (nonatomic,copy) NSString *title;
- @end
- @implementation RAEditSectionModel
- - (void)setValue:(id)value forUndefinedKey:(NSString *)key {
-
- }
- - (void)setItems:(NSArray<RAEditBaseModel *> *)items {
-
- NSMutableArray *tmpItems = [items mutableCopy];
- // [tmpItems addObject:@{
- // @"type" : @6,
- // @"title" : @"Container Photo",
- // @"required" : @(YES),
- // @"photos" : @[@{@"placeHolder":@"btn_add_photo"},@{@"placeHolder":@"btn_add_photo"},@{@"placeHolder":@"btn_add_photo"},@{@"placeHolder":@"btn_add_photo"},@{@"placeHolder":@"btn_add_photo"},@{@"placeHolder":@"btn_add_photo"}],
- // @"key" : @"test_photo"
- // }];
-
- NSMutableArray *itemArr = [NSMutableArray arrayWithCapacity:items.count];
- for (int i = 0; i < tmpItems.count; i++) {
- NSDictionary *item = [tmpItems objectAtIndex:i];
- RAEditBaseModel *model = [self createModelWithJsonItem:item];
- if (model) {
- [itemArr addObject:model];
- // expand 直接放出来
- if (model.expand && [model.expand isKindOfClass:[NSDictionary class]]) {
- RAEditBaseModel *expand = [self createModelWithJsonItem:model.expand];
- if (expand) {
- model.expand = expand;
- [itemArr addObject:expand];
- }
- }
- }
- }
- _items = itemArr;
- }
- - (NSInteger)itemCount {
- return self.items.count;
- }
- - (RAEditBaseModel *)itemModelForIndex:(NSInteger)index {
- return [self.items objectAtIndex:index];
- }
- - (RAEditBaseModel *)createModelWithJsonItem:(NSDictionary *)item {
-
- RAEditType type = [[item objectForKey:@"type"] intValue];
-
- RAEditBaseModel *model;
- switch (type) {
- case RAEditTypeLabel: {
-
- model = [RAEditLabelModel new];
- }
- break;
- case RAEditTypeInput: {
-
- model = [RAEditInputModel new];
- }
- break;
- case RAEditTypeMultInput: {
-
- model = [RAEditMultInputModel new];
- }
- break;
- case RAEditTypePhoto: {
-
- model = [RAEditPhotoModel new];
- }
- break;
- case RAEditTypeSignature: {
-
- model = [RAEditSignatureModel new];
- }
- break;
- case RAEditTypeDate: {
- model = [RAEditDateModel new];
- }
- break;
- case RAEditTypeMultPhoto: {
- RAEditMultPhotoModel *photoModel = [RAEditMultPhotoModel new];
- photoModel.width = [UIScreen mainScreen].bounds.size.width;
- model = photoModel;
- }
- break;
-
- default: {
- model = nil;
- }
- break;
- }
- [model setValuesForKeysWithDictionary:item];
-
- return model;
- }
- - (RAEditBaseModel *)addModelAtIndex:(NSUInteger)idx withJsonItem:(NSDictionary *)item {
-
- RAEditBaseModel *model = [self createModelWithJsonItem:item];
- if (model) {
- NSMutableArray *arr = [self.items mutableCopy];
- [arr insertObject:model atIndex:idx];
- _items = arr;
- }
- return model;
- }
- @end
- #pragma mark - View Controller
- @interface RAOrderEditViewController ()
- @property (nonatomic,strong) IBOutlet UITableView *orderEditTableView;
- @property (nonatomic,strong) NSMutableArray *sectionArray;
- @property (nonatomic,copy) NSString *photoDir;
- @property (nonatomic,strong) UIRefreshControl *refreshControl;
- @end
- @implementation RAOrderEditViewController
- + (instancetype)viewControllerFromStoryboard {
- RAOrderEditViewController *editVC = [[UIStoryboard storyboardWithName:@"Edit" bundle:nil] instantiateViewControllerWithIdentifier:[self storyboardID]];
- return editVC;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view.
- [self configureTable];
- [self configureNavigationBar];
- [self loadData];
- }
- - (void)viewWillAppear:(BOOL)animated {
- [super viewWillAppear:animated];
-
- [self registKeyboardListener];
- }
- - (void)viewWillDisappear:(BOOL)animated {
- [super viewWillDisappear:animated];
-
- [self unregistKeyboardListener];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- #pragma mark - Configure
- - (void)configureTable {
-
- if (@available(iOS 11.0, *)) {
- self.orderEditTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
- } else {
- self.automaticallyAdjustsScrollViewInsets = NO;
- }
-
- self.orderEditTableView.tableFooterView = [UIView new];
- self.orderEditTableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
-
- UIRefreshControl *refresh = [[UIRefreshControl alloc] init];
- [refresh addTarget:self action:@selector(refreshControlValueChanged:) forControlEvents:UIControlEventValueChanged];
- [self.orderEditTableView addSubview:refresh];
- self.refreshControl = refresh;
-
- // empty
- __weak typeof(self) weakSelf = self;
- self.orderEditTableView.emptyView = [RAEmptyView emptyViewWithTapBlk:^(id sender) {
- [weakSelf loadData];
- }];
-
- [RAPhotoCell regist2TableView:self.orderEditTableView];
- }
- - (void)configureNavigationBar {
-
- UIBarButtonItem *updateItem = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Update", nil) style:UIBarButtonItemStylePlain target:self action:@selector(updateBtnClick:)];
- self.navigationItem.rightBarButtonItem = updateItem;
- }
- - (void)registKeyboardListener {
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
- }
- - (void)unregistKeyboardListener {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- #pragma mark - Action
- - (void)refreshControlValueChanged:(UIRefreshControl *)refresh {
- [self loadData];
- }
- #pragma mark - Getter
- - (UITableView *)tableView {
- return self.orderEditTableView;
- }
- - (NSMutableArray *)sectionArray {
- if (!_sectionArray) {
- _sectionArray = [NSMutableArray array];
- }
- return _sectionArray;
- }
- - (NSUInteger)editSectionCount {
-
- return self.sectionArray.count;
- }
- - (NSInteger)itemCountForSection:(NSInteger)section {
- return [[self.sectionArray objectAtIndex:section] itemCount];
- }
- - (RAEditBaseModel *)modelForIndexPath:(NSIndexPath *)indexPath {
- return [[self.sectionArray objectAtIndex:indexPath.section] itemModelForIndex:indexPath.row];
- }
- - (NSString *)titleForSection:(NSInteger)section {
- return [[self.sectionArray objectAtIndex:section] title];
- }
- - (NSIndexPath *)indexPathForCell:(UITableViewCell *)cell {
- return [self.orderEditTableView indexPathForCell:cell];
- }
- #pragma mark - Setter
- - (void)addModelAtIndex:(NSIndexPath *)indexPath withJsonItem:(NSDictionary *)item {
- RAEditSectionModel *section = [self.sectionArray objectAtIndex:indexPath.section];
- [section addModelAtIndex:indexPath.row withJsonItem:item];
- [self.orderEditTableView reloadData];
- }
- - (void)expandIndexPath:(NSIndexPath *)indexPath withJsonItem:(NSDictionary *)item {
-
- RAEditSectionModel *section = [self.sectionArray objectAtIndex:indexPath.section];
- RAEditBaseModel *curM = [self modelForIndexPath:indexPath];
- RAEditBaseModel *expand = [section addModelAtIndex:(indexPath.row + 1) withJsonItem:item];
- curM.expand = expand;
- [self.orderEditTableView reloadData];
- }
- #pragma mark - Data
- - (void)loadData {
-
- if (self.loading) {
- return;
- }
- self.loading = YES;
-
- [self.orderEditTableView hideEmpty];
-
- // show progress
- RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
-
- NSString *orderID = self.orderID;
- NSInteger actionID = self.actionID;
- NSInteger actionIdx = self.actionIdx;
- __weak typeof(self) weakSelf = self;
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
-
- NSDictionary *json = [RADataProvider requestUpdateOrder:orderID driverAction:actionID index:actionIdx];
-
- dispatch_async(dispatch_get_main_queue(), ^{
-
- // dismiss progress
- [hud dismiss];
- if (weakSelf.refreshControl.isRefreshing) {
- [weakSelf.refreshControl endRefreshing];
- }
-
- if (weakSelf) {
- __strong typeof(weakSelf) strongSelf = weakSelf;
-
- int result = [[json objectForKey:@"result"] intValue];
- if (result == RESULT_TRUE) {
-
- NSArray *sectionArray = [json objectForKey:@"sections"];
- [strongSelf.sectionArray removeAllObjects];
- for (int i = 0; i < sectionArray.count; i++) {
- NSDictionary *section = [sectionArray objectAtIndex:i];
- RAEditSectionModel *model = [RAEditSectionModel new];
- [model setValuesForKeysWithDictionary:section];
- [strongSelf.sectionArray addObject:model];
- }
-
- [strongSelf.orderEditTableView reloadData];
-
- } else {
-
- [strongSelf.sectionArray removeAllObjects];
- strongSelf.orderEditTableView.contentOffset = CGPointZero;
- [strongSelf.orderEditTableView reloadData];
-
- // process error
- NSString *msg = [json objectForKey:@"err_msg"];
- // [strongSelf showAlert:msg];
- [strongSelf showAlertTilte:NSLocalizedString(@"Warning", nil) message:msg];
- }
- }
- weakSelf.loading = NO;
-
- if (weakSelf.editSectionCount == 0) {
- [weakSelf.orderEditTableView showEmpty];
- } else {
- [weakSelf.orderEditTableView hideEmpty];
- }
- });
-
- });
- }
- #pragma mark - Private
- - (void)submitSuccessWithPhoto:(BOOL)photo {
-
- NSString *msg = NSLocalizedString(@"update_success", nil);
- if (photo) {
- msg = NSLocalizedString(@"update_photo_success", nil);
- }
-
- UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:msg preferredStyle:UIAlertControllerStyleAlert];
-
- __weak typeof(self) weakSelf = self;
- UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Ok", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- if (weakSelf) {
- __strong typeof(weakSelf) strongSelf = weakSelf;
- [strongSelf gobackHome];
- }
- }];
-
- [alertVC addAction:okAction];
-
- [self presentViewController:alertVC animated:YES completion:nil];
- }
- #pragma mark - Tap Action
- - (IBAction)tapToResignFirstResponder:(UITapGestureRecognizer *)sender {
-
- [self.view endEditing:YES];
- }
- - (void)updateBtnClick:(UIBarButtonItem *)sender {
-
- NSMutableDictionary *params = [NSMutableDictionary dictionary];
- if (self.orderType2) {
- [params setObject:self.orderType2 forKey:@"orderType2"];
- }
- if (RASingleton.sharedInstance.requiredLocation) {
- CLLocation *location = RASingleton.sharedInstance.currentLocation;
- NSString *latLon = nil;
- if (location) {
- latLon = [NSString stringWithFormat:@"%f,%f",location.coordinate.latitude,location.coordinate.longitude];
- } else {
- latLon = @"-999,-999";
- }
- [params setObject:latLon forKey:@"location"];
- }
- NSMutableArray *emptyArr = [NSMutableArray array];
-
- NSMutableArray <RAEditImageBaseModel *> *photoArr = [self prepareParams:params checkRequired:emptyArr];
-
- if (emptyArr.count > 0) {
-
-
- RAEditRequiredAlert *alertVC = [RAEditRequiredAlert alertWithTile:NSLocalizedString(@"Warning", nil) message:[NSString localizedStringWithFormat:NSLocalizedString(@"please complete missing field:\n%@", nil),[emptyArr componentsJoinedByString:@"\n"]]];
-
- [self presentViewController:alertVC animated:YES completion:nil];
- return;
- }
-
- [params setValue:@(photoArr.count) forKey:@"photoCount"];
-
- // show progress
- RAProgressHUD *hud = [RAProgressHUD showHUDOnView:self.view];
-
- __weak typeof(self) weakSelf = self;
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
-
- if (RASingleton.sharedInstance.offline) { // 离线
-
- NSDictionary *json = [weakSelf offlineUpdate:params photos:photoArr];
-
- [hud dismiss:^{
-
- NSString *msg = [json objectForKey:@"err_msg"];
- [weakSelf showAlertTilte:NSLocalizedString(@"Warning", nil) message:msg];
- }];
- }
- else { // 在线
-
- NSDictionary *json = [RADataProvider submitEditOrder:params];
-
- if (weakSelf) {
- __strong typeof(weakSelf) strongSelf = weakSelf;
-
- int result = [[json objectForKey:@"result"] intValue];
- if (result == RESULT_TRUE) {
-
- BOOL requiredLocation = [[json objectForKey:@"requiredLocation"] boolValue];
- [RASingleton sharedInstance].requiredLocation = requiredLocation;
-
- dispatch_async(dispatch_get_main_queue(), ^{
-
- // if (photoArr.count > 0) {
- // [strongSelf syncUploadPhotos:photoArr Json:json HUD:hud];
- // } else {
- // [strongSelf gobackHome];
- // }
-
- // 全部转为后台上传
- if (photoArr.count > 0) {
- [strongSelf backgroundUploadPhoto:photoArr Json:json];
- }
-
- [hud dismiss:^{
- [strongSelf submitSuccessWithPhoto:photoArr.count > 0];
- }];
-
- });
-
- } else {
- // process error
- dispatch_async(dispatch_get_main_queue(), ^{
-
- // dismiss progress
- [hud dismiss:^{
-
- NSString *msg = [json objectForKey:@"err_msg"];
- [strongSelf showAlertTilte:NSLocalizedString(@"Warning", nil) message:msg];
- }];
-
- });
-
- }
- }
-
- }
-
-
- });
- }
- - (void)backgroundUploadPhoto:(NSArray<RAEditImageBaseModel *> *)photos Json:(NSDictionary *)json {
-
- AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
-
- NSDate *date = [NSDate date];
- NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
- formatter.dateFormat = @"MM/dd/YYYY HH:mm";
- NSString *time = [formatter stringFromDate:date];
-
- NSString *serial = [json objectForKey:@"serial"];
- for (RAEditImageBaseModel *model in photos) {
-
-
- if (serial.length) {
-
- NSMutableDictionary *params = [NSMutableDictionary dictionary];
- [params setObject:serial forKey:@"serial"];
- if (self.orderType2) {
- [params setObject:self.orderType2 forKey:@"orderType2"];
- }
- if (model.key) {
- [params setObject:model.key forKey:@"key"];
- }
- [params setObject:@(model.type) forKey:@"type"];
-
- [params setObject:[RASingleton.sharedInstance encryptUser] forKey:@"name"];
- [params setObject:[RASingleton.sharedInstance encryptPassword] forKey:@"password"];
- [params setObject:@"iOS" forKey:@"platform"];
- NSString *photoPath = [self.photoDir.lastPathComponent stringByAppendingPathComponent:model.imageName];
-
- NSMutableDictionary *task = [@{
- @"order" : self.orderID,
- @"action" : self.actionTitle,
- @"name" : model.title,
- @"time" : time,
- @"url" : URL_UPLOAD,
- @"file" : photoPath,
- @"params" : params
- } mutableCopy];
- [appDelegate.uploadManager addTask:task];
- }
- }
- }
- - (void)syncUploadPhotos:(NSMutableArray<RAEditImageBaseModel *> *)photoArr Json:(NSDictionary *)json HUD:(RAProgressHUD *)hud {
-
- dispatch_async(dispatch_get_main_queue(), ^{
-
- RAProgressHUD *innerHUD = hud;
- if (!innerHUD) {
- innerHUD = [RAProgressHUD showHUDOnView:self.view]; // main queue
- }
-
-
- dispatch_async(dispatch_get_global_queue(0, 0), ^{ // network
-
- int retryCount = 0;
- NSMutableArray *completArr = [NSMutableArray array];
- NSString *serial = [json objectForKey:@"serial"];
-
- // 同步上传照片
- for (int i = 0; i < photoArr.count; i++) {
- RAEditImageBaseModel *model = [photoArr objectAtIndex:i];
-
- if (serial.length) {
-
- NSMutableDictionary *fileParams = [NSMutableDictionary dictionary];
- [fileParams setObject:serial forKey:@"serial"];
- if (self.orderType2) {
- [fileParams setObject:self.orderType2 forKey:@"orderType2"];
- }
- if (model.key) {
- [fileParams setObject:model.key forKey:@"key"];
- }
- [fileParams setObject:@(model.type) forKey:@"type"];
-
- NSString *photoPath = [self.photoDir stringByAppendingPathComponent:model.imageName];
- NSDictionary *uploadJson = [RADataProvider uploadFile:photoPath parameters:fileParams];
- int uploadResult = [[uploadJson objectForKey:@"result"] intValue];
-
- if (uploadResult != RESULT_TRUE) { // 失败重试
-
- i--;
- retryCount++;
- if (retryCount >= 3) {
- break;
- }
- } else {
-
- [completArr addObject:model];
- retryCount = 0;
- // 删除文件
- if ([[NSFileManager defaultManager] fileExistsAtPath:photoPath]) {
- [[NSFileManager defaultManager] removeItemAtPath:photoPath error:nil];
- }
- }
- } // serial
- } // for
-
- // 将完成的model移除
- [photoArr removeObjectsInArray:completArr];
-
- __weak typeof(self) weakSelf = self;
- dispatch_async(dispatch_get_main_queue(), ^{
-
- [innerHUD dismiss:^{
-
- if (photoArr.count > 0) {
- // 上传失败,询问是否丢到后台线程上传,否则重启上传
-
- UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Warning", nil) message:NSLocalizedString(@"update_upload_failed_tips", nil) preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *backgroundAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Background", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- // 开启后台上传
- [weakSelf backgroundUploadPhoto:photoArr Json:json];
- // 返回首页
- [weakSelf gobackHome];
- }];
- UIAlertAction *retryAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"Retry", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
-
- dispatch_async(dispatch_get_global_queue(0, 0), ^{
- [weakSelf syncUploadPhotos:photoArr Json:json HUD:nil];
- });
- }];
-
- [alertVC addAction:backgroundAction];
- [alertVC addAction:retryAction];
-
- [weakSelf presentViewController:alertVC animated:YES completion:nil];
-
-
- } else {
- // 上传完成,返回到首页
- [weakSelf gobackHome];
- }
-
- }];
- });
-
- });
-
- });
-
-
-
- }
- - (void)gobackHome {
-
- [[NSNotificationCenter defaultCenter] postNotificationName:RANotificationReloadHome object:nil];
- [self.navigationController popToRootViewControllerAnimated:YES];
- }
- #pragma mark - Keyboard Listener
- - (void)keyboardWillChangeFrame:(NSNotification *)notification {
-
- CGRect end = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
- CGFloat screenHeight = CGRectGetHeight([UIScreen mainScreen].bounds);
- CGFloat keyboardHeight = screenHeight - CGRectGetMinY(end);
-
- UIEdgeInsets insets = UIEdgeInsetsMake(0, 0, keyboardHeight, 0);
- self.orderEditTableView.contentInset = insets;
-
- if (self.editingIndexPath) {
- [self.orderEditTableView scrollToRowAtIndexPath:self.editingIndexPath atScrollPosition:UITableViewScrollPositionBottom animated:NO];
- }
- }
- #pragma mark - Package Update Data
- - (NSString *)photoDir {
-
- if (!_photoDir) {
-
- NSString *cacheDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
- NSString *photoDir = [cacheDir stringByAppendingPathComponent:[NSString stringWithFormat:@"%@_%ld_%@",self.orderID,(long)self.actionID,[NSUUID UUID].UUIDString]];
-
- NSError *error;
- // BOOL dirExist = YES;
- // for (int i = 0; i < INT_MAX; i++) {
- // if (i != 0) {
- // photoDir = [photoDir stringByAppendingString:[NSString stringWithFormat:@"(%d)",i]];
- // }
- // if (![[NSFileManager defaultManager] fileExistsAtPath:photoDir]) {
- // dirExist = NO;
- // break;
- // }
- // }
-
- [[NSFileManager defaultManager] createDirectoryAtPath:photoDir withIntermediateDirectories:YES attributes:nil error:&error];
- if (error) {
- NSLog(@"create dir %@ failed %@",photoDir,error.localizedDescription);
- return nil;
- }
- _photoDir = photoDir;
- }
- return _photoDir;
- }
- - (NSMutableArray <RAEditImageBaseModel *> *)prepareParams:(NSMutableDictionary *)params checkRequired:(NSMutableArray *)emptyArr {
-
- if (params == nil) {
- return nil;;
- }
-
- NSMutableArray <RAEditImageBaseModel *> *photoArr = [NSMutableArray array];
-
- NSString *photoDir = [self photoDir];
-
- [params setObject:self.orderID forKey:@"orderID"];
- [params setObject:@(self.actionID) forKey:@"actionID"];
-
- NSString* encryptu = [RASingleton sharedInstance].encryptUser;
- NSString* encryptp = [RASingleton sharedInstance].encryptPassword;
- [params setObject:encryptu forKey:@"name"];
- [params setObject:encryptp forKey:@"password"];
- [params setObject:@"iOS" forKey:@"platform"];
-
- for (RAEditSectionModel *section in self.sectionArray) {
- for (int i = 0; i < [section itemCount]; i++) {
- RAEditBaseModel *model = [section itemModelForIndex:i];
- switch (model.type) {
- case RAEditTypeLabel: {
-
- }
- break;
- case RAEditTypeInput: {
-
- RAEditInputModel *inputModel = (RAEditInputModel *)model;
- if (model.required && inputModel.value.length == 0) {
- [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
- continue;
- }
-
- if (inputModel.key && inputModel.value.length > 0) {
- [params setObject:inputModel.value forKey:inputModel.key];
- }
- }
- break;
- case RAEditTypeMultInput: {
-
- RAEditMultInputModel *multInputModel = (RAEditMultInputModel *)model;
- if (model.required && multInputModel.value.length == 0) {
- [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
- continue;
- }
-
- if (multInputModel.key && multInputModel.value.length > 0) {
- [params setObject:multInputModel.value forKey:multInputModel.key];
- }
- }
- break;
- case RAEditTypePhoto:
- case RAEditTypeSignature: {
-
- if (photoDir) {
-
- RAEditImageBaseModel *imageModel = (RAEditImageBaseModel *)model;
- if (model.required && imageModel.image == nil) {
- [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
- continue;
- }
-
- if (imageModel.image) {
-
- NSString *photoPath = [photoDir stringByAppendingPathComponent:imageModel.imageName];
- NSData *imgData = UIImageJPEGRepresentation(imageModel.image, 1.0f);
- if (imgData) {
-
- [imgData writeToFile:photoPath atomically:NO];
- [params setObject:imageModel.imageName forKey:imageModel.key];
-
- [photoArr addObject:imageModel];
- }
- }
-
- }
- }
- break;
- case RAEditTypeDate: {
- RAEditDateModel *dateModel = (RAEditDateModel *)model;
- if (model.required && dateModel.value.length == 0) {
- [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
- continue;
- }
- if (model.key && dateModel.value) {
- [params setObject:dateModel.value forKey:model.key];
- }
- }
- break;
- case RAEditTypeMultPhoto: {
-
- if (photoDir) {
-
- RAEditMultPhotoModel *photoModel = (RAEditMultPhotoModel *)model;
- NSUInteger count = photoModel.photoCount;
- if (photoModel.required && count == 0) {
- [emptyArr addObject:[NSString stringWithFormat:@"%ld.%@",emptyArr.count + 1,model.title]];
- continue;
- }
-
- for (int i = 0; i < count; i++) {
-
- UIImage *img = [photoModel photoForIndex:i];
- if (img) {
-
- // 临时创建PhotoModel,用于上传
- RAEditPhotoModel *tmpPhotoModel = [RAEditPhotoModel new];
- tmpPhotoModel.type = RAEditTypePhoto;
- tmpPhotoModel.key = [NSString stringWithFormat:@"%@_%d",photoModel.key,i];
- tmpPhotoModel.photo = img;
-
- NSString *photoPath = [photoDir stringByAppendingPathComponent:tmpPhotoModel.imageName];
- NSData *imgData = UIImageJPEGRepresentation(tmpPhotoModel.photo, 1.0f);
- if (imgData) {
-
- [imgData writeToFile:photoPath atomically:NO];
- [params setObject:tmpPhotoModel.imageName forKey:tmpPhotoModel.key];
-
- [photoArr addObject:tmpPhotoModel];
- }
- }
- }
- }
- }
- break;
-
- default:
- break;
- }
- }
- }
-
- return photoArr;
- }
- //- (NSMutableDictionary *)preparePackage {
- //
- // NSMutableDictionary *params = [NSMutableDictionary dictionary];
- // NSInteger photoCount = [self prepareParams:params].count;
- // NSString *photoDir = [self photoDir];
- //
- // NSMutableDictionary *task = [@{
- // @"order" : self.orderID,
- // @"action" : self.actionTitle,
- // @"url" : URL_HOST,
- // @"noFile" : @(YES)
- // } mutableCopy];
- //
- // if (photoCount > 0) {
- //
- // // 压缩文件
- // NSString *zipPath = [photoDir stringByAppendingPathExtension:@".zip"];
- // ZipArchive *zip = [[ZipArchive alloc] init];
- // [zip CreateZipFile2:zipPath];
- // NSArray *subPaths = [[NSFileManager defaultManager] subpathsAtPath:photoDir];
- // for (NSString *subPath in subPaths) {
- // NSString *fullPath = [photoDir stringByAppendingPathComponent:subPath];
- // BOOL isDir;
- // if([[NSFileManager defaultManager] fileExistsAtPath:fullPath isDirectory:&isDir]) {
- // if (!isDir) {
- // [zip addFileToZip:fullPath newname:subPath];
- // }
- // }
- // }
- // [zip CloseZipFile2];
- //
- // [[NSFileManager defaultManager] removeItemAtPath:photoDir error:nil];
- //
- // NSString *md5 = [RAUtils md5WithFile:zipPath];
- //
- // [params setObject:md5 forKey:@"md5"];
- // [task setObject:@"file" forKey:zipPath.lastPathComponent];
- // }
- //
- // [task setObject:params forKey:@"params"];
- //
- // return task;
- //}
- #pragma mark - Offline
- - (NSDictionary *)offlineUpdate:(NSDictionary *)params photos:(NSArray *)photos {
-
- return [RADataProvider offlineSubmitOrder:self.orderID action:self.actionID title:self.actionTitle index:self.actionIdx withParams:params photos:photos cacheDir:self.photoDir];
- }
- @end
|