InspectNotUploadViewController.m 14.6 KB
Newer Older
1
//
admin's avatar
admin committed
2
//  InspectNotUploadViewController.m
3 4
//  redstar
//
admin's avatar
admin committed
5
//  Created by admin on 15/11/24.
6 7 8
//  Copyright © 2015年 ZWF. All rights reserved.
//

admin's avatar
admin committed
9 10
#import "InspectNotUploadViewController.h"
#import "InspectNotUpLoadCell.h"
admin's avatar
admin committed
11
#import "CheckPicViewController.h"
admin's avatar
admin committed
12
#import "InspectDetailFooterView.h"
admin's avatar
admin committed
13 14
#import <MBProgressHUD.h>
#import "CommonFunc.h"
15
#import "HttpClient.h"
admin's avatar
admin committed
16 17 18

#import "InspectTaskViewController.h"

admin's avatar
admin committed
19
#define kInspectNotUploadCell @"InspectNotUploadCell"
20

admin's avatar
admin committed
21
@interface InspectNotUploadViewController () <UITableViewDelegate, UITableViewDataSource, UINavigationControllerDelegate, UIImagePickerControllerDelegate , TakePhotoViewDelegate, UIScrollViewDelegate, UIAlertViewDelegate>
22 23 24

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) InspectDetailFooterView *footerView;
admin's avatar
admin committed
25
@property (nonatomic, strong) NSMutableArray *imageNameArray;
26

admin's avatar
admin committed
27

28 29
@end

admin's avatar
admin committed
30 31 32 33
@implementation InspectNotUploadViewController

- (void)viewDidLoad
{
34 35
    [super viewDidLoad];
    
36 37 38
    NSLog(@"11111self.tableView.frame = %@, self.contentSize =  %@, self.contentOff = %@", NSStringFromCGRect(self.tableView.frame),NSStringFromCGSize(self.tableView.contentSize), NSStringFromCGPoint(self.tableView.contentOffset));

    
admin's avatar
admin committed
39
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
40
    [customLab setTextColor:[UIColor whiteColor]];
admin's avatar
admin committed
41 42
    customLab.textAlignment = NSTextAlignmentCenter;
    [customLab setText:[NSString stringWithFormat:@"检查点--%@(%@/%d)", self.taskModel.category, self.taskModel.lineNo, self.questionCount]];
43 44 45
    customLab.font = [UIFont boldSystemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    
46 47 48 49 50 51 52 53
    
    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    backBtn.frame = CGRectMake(0, 0, 30, 44);
    [backBtn setImage:[UIImage imageNamed:@"back_btn"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(doBack:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithCustomView:backBtn];
    self.navigationItem.leftBarButtonItem = backItem;
    
admin's avatar
admin committed
54
    [self.tableView registerClass:[InspectNotUpLoadCell class] forCellReuseIdentifier:kInspectNotUploadCell];
55
    self.tableView.tableFooterView = self.footerView;
admin's avatar
admin committed
56
    self.footerView.takePhotoView.delegate = self;
admin's avatar
admin committed
57 58 59
    [self.footerView.reportBtn addTarget:self action:@selector(uploadTaskInfo:) forControlEvents:UIControlEventTouchUpInside];
    
    self.imageNameArray = [NSMutableArray array];
admin's avatar
admin committed
60

61 62 63 64 65 66 67
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

68
#pragma mark - private Methods
admin's avatar
admin committed
69 70 71

- (void)uploadTaskInfo:(UIButton *)sender
{
admin's avatar
admin committed
72 73 74 75 76 77 78
    int impose = _taskModel.attachmentNumber;
    if ((int)_imageNameArray.count < impose) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:[NSString stringWithFormat:@"要求图片%d张才能上传!", impose] delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        return;
    }
    
admin's avatar
admin committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    NSMutableArray *imageArray = [NSMutableArray array];
    for (int i = 0; i < _imageNameArray.count; i++) {
        NSString *imageName = _imageNameArray[i];
        NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
        UIImage *image = [UIImage imageWithContentsOfFile:fullPath];
        NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
        NSString *imageBase64 = [imageData base64EncodedStringWithOptions:0];
        NSMutableDictionary *dict = [NSMutableDictionary dictionary];
        [dict setObject:imageBase64 forKey:@"fileContent"];
        [dict setObject:imageName forKey:@"fileName"];
        [dict setObject:@(i) forKey:@"index"];
        [imageArray addObject:dict];
    }

    NSDate *date = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd+hh:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:date];
    
    NSString *operId = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_code"];
    NSString *operName = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_name"];
    
    NSString *url = [NSString stringWithFormat:@"%@%@%@?time=%@&operId=%@&operName=%@",kRedStarURL, kUpLoadInspectResult, self.praiseUuid, dateString, operId, operName];
    url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];
    NSLog(@"上传检查点url = %@", url);
    HttpClient *httpClient = [[HttpClient alloc] initWithUrl:url];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSDictionary *parameters = @{@"questionUuid":self.questionUuid,
                                 @"storeUuid":self.store_uuid,
                                 @"remark":self.footerView.contentTextView.text,
                                 @"attachments":imageArray
                                 };
    NSLog(@"上传检查点parameters = %@", parameters);
    [httpClient uploadInspectResultWithParameters:parameters completion:^(id response, NSError *error) {
        
admin's avatar
admin committed
115 116 117 118 119 120 121
        NSLog(@"上传检查点  respose  =%@  error = %@", response, error);
        if (response[@"success"]) {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"检查点上报成功!" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
            alert.delegate = self;
            alert.tag = 39429234;
            [alert show];
        }
admin's avatar
admin committed
122 123 124 125 126
        [MBProgressHUD hideHUDForView:self.view animated:YES];

    }];
}

127 128 129 130 131
// 返回上一页面
- (void)doBack:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}
admin's avatar
admin committed
132 133

#pragma mark - TakePhoto Delegate
134 135
- (void)deletePhoto:(UIButton *)button
{
admin's avatar
admin committed
136
    NSLog(@"button -.tag  === %ld", (long)button.superview.tag);
137 138 139 140 141
    // 从存放所有Image的数组里移除当前点击的Image
    [self.footerView.takePhotoView.allImages removeObject:[(UIButton *)button.superview imageForState:UIControlStateNormal]];
    // 移除显示Image的Button
    [button.superview removeFromSuperview];
    
admin's avatar
admin committed
142 143 144 145 146 147 148 149 150 151 152 153
    CGFloat btnH = 110;
    CGFloat marginY = 10;
    int count = (int)self.footerView.takePhotoView.allImages.count + 1;
    int k ;
    if (count % 2 == 0) {
        k = count / 2;
    } else {
        k = (count + 1) / 2;
    }
    self.footerView.takePhotoView.frame = CGRectMake(0, 40, kScreenWidth, k * btnH + (k + 1) * marginY);
    self.footerView.height = self.footerView.takePhotoView.frame.size.height + 220;
    
admin's avatar
admin committed
154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    
    NSString *imageName = _imageNameArray[button.superview.tag - 1];
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    NSFileManager* fileManager = [NSFileManager defaultManager];
    BOOL blHave=[[NSFileManager defaultManager] fileExistsAtPath:fullPath];
    if (!blHave) {
        return ;
    } else {
        BOOL blDele= [fileManager removeItemAtPath:fullPath error:nil];
        if (blDele) {
            NSLog(@"dele success");
        }else {
            NSLog(@"dele fail");
        }
    }
169
    
170 171 172 173
}

- (void)createImagePicker
{
admin's avatar
admin committed
174 175 176 177 178 179 180 181 182 183 184
    if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"Unable to find the camera" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
        [alert show];
    } else {
        UIImagePickerController *picker = [[UIImagePickerController alloc] init];
        picker.delegate = self;
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
        picker.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
        picker.videoQuality = UIImagePickerControllerQualityTypeLow;
        [self presentViewController:picker animated:YES completion:nil];
    }
185 186
}

admin's avatar
admin committed
187 188 189 190 191 192 193
- (void)takePhotoShowPicture:(UIButton *)btn
{
    CheckPicViewController *checkVC = [[CheckPicViewController alloc] init];
    checkVC.checkImage = btn.imageView.image;
    [self.navigationController pushViewController:checkVC animated:YES];
}

admin's avatar
admin committed
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
#pragma mark - 保存图片至沙盒
- (void)saveImage:(UIImage *)currentImage {
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    
    NSString *imageName = [CommonFunc md5Data:imageData];
    [_imageNameArray addObject:imageName];
    
    
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}


209 210
#pragma mark - UIImagePickerController 代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
211
{
admin's avatar
admin committed
212
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
admin's avatar
admin committed
213
    [self saveImage:image];
214
    // 创建一个新的控件
admin's avatar
admin committed
215 216
    UIButton *newButton = [self.footerView.takePhotoView createButtonWithImage:image];
    [newButton addTarget:self action:@selector(takePhotoShowPicture:) forControlEvents:UIControlEventTouchUpInside];
admin's avatar
admin committed
217
    
218 219 220 221 222 223 224 225 226
    [self.footerView.takePhotoView insertSubview:newButton atIndex:self.footerView.takePhotoView.subviews.count - 1];
    [self.footerView.takePhotoView.allImages addObject:image];
    
    int count = (int)self.footerView.takePhotoView.allImages.count + 1;
    int k ;
    if (count % 2 == 0) {
        k = count / 2;
    } else {
        k = (count + 1) / 2;
227
    }
228 229 230 231 232 233 234
    
    CGFloat btnH = 110;
    CGFloat marginY = 10;
    self.footerView.takePhotoView.frame = CGRectMake(0, 40, kScreenWidth, k * btnH + (k + 1) * marginY);
    self.footerView.height = self.footerView.takePhotoView.frame.size.height + 220;
    // 退出图片选择控制器
    [picker dismissViewControllerAnimated:YES completion:nil];
235 236 237
    
    NSLog(@"222222self.tableView.frame = %@, self.contentSize =  %@, self.contentOff = %@", NSStringFromCGRect(self.tableView.frame),NSStringFromCGSize(self.tableView.contentSize), NSStringFromCGPoint(self.tableView.contentOffset));

238 239
}

admin's avatar
admin committed
240 241


242 243 244 245 246 247 248 249 250
#pragma mark - TableView Delegate/DataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

// cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
admin's avatar
admin committed
251
    
admin's avatar
admin committed
252
    InspectNotUpLoadCell *cell=[tableView dequeueReusableCellWithIdentifier:kInspectNotUploadCell];
253
    if (!cell) {
admin's avatar
admin committed
254
        cell = [[InspectNotUpLoadCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kInspectNotUploadCell];
255
    }
admin's avatar
admin committed
256 257
    cell.titleLabel.text = [NSString stringWithFormat:@"%@", _taskModel.title];
    cell.titleDetailLabel.text = [NSString stringWithFormat:@"要求图片%d张", _taskModel.attachmentNumber];
258
    cell.introLabel.text = @"参考说明";
admin's avatar
admin committed
259
    cell.introDetailLabel.text = [NSString stringWithFormat:@"%@", _taskModel.reference];
admin's avatar
admin committed
260
    
admin's avatar
admin committed
261 262 263 264 265 266 267 268 269
    NSString *refrer = [NSString stringWithFormat:@"%@", _taskModel.reference];
    if (refrer == nil || refrer == NULL || [refrer isEqual:[NSNull null]] || refrer == Nil || [refrer isEqualToString:@"(null)"]) {
        cell.introDetailLabel.text = @"";
    } else {
        NSString *str = [NSString stringWithFormat:@"<style> html{ font-size: 15px; color: #444444 } </style>%@", _taskModel.reference];
        NSAttributedString * attrStr = [[NSAttributedString alloc] initWithData:[str dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType ,NSForegroundColorAttributeName:kLightBlack,NSFontAttributeName:[UIFont systemFontOfSize:15.0f]} documentAttributes:nil error:nil];
        
        cell.introDetailLabel.attributedText = attrStr;
    }
270 271 272
    return cell;
}

admin's avatar
admin committed
273

admin's avatar
admin committed
274

275 276
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
admin's avatar
admin committed
277
    
278 279 280 281 282 283 284 285 286 287 288 289 290 291
}


// section高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return CGFLOAT_MIN;
}

admin's avatar
admin committed
292 293 294 295 296 297 298 299 300 301 302
#pragma mark - UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0 && alertView.tag == 39429234) {
        [self.navigationController popViewControllerAnimated:YES];
        NSNotificationCenter *notification = [NSNotificationCenter defaultCenter];
        [notification postNotificationName:@"kReloadTableView" object:self];
    }
}


303 304 305 306 307 308 309 310 311 312

#pragma mark - lazy loading
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        _tableView.translatesAutoresizingMaskIntoConstraints = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = UITableViewAutomaticDimension;
admin's avatar
admin committed
313
        _tableView.estimatedRowHeight = 300.0;
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
        [self.view addSubview:_tableView];
        
        NSLayoutConstraint *tableTop = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        [self.view addConstraint:tableTop];
        
        NSLayoutConstraint *tableLeft = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0];
        [self.view addConstraint:tableLeft];
        
        NSLayoutConstraint *tableRight = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        [self.view addConstraint:tableRight];
        
        NSLayoutConstraint *tableBottom = [NSLayoutConstraint constraintWithItem:_tableView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        [self.view addConstraint:tableBottom];
    }
    return _tableView;
}

- (InspectDetailFooterView *)footerView
{
    if (!_footerView) {
        _footerView = [[InspectDetailFooterView alloc] init];
    }
    return _footerView;
}

admin's avatar
admin committed
339

admin's avatar
admin committed
340
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
admin's avatar
admin committed
341 342 343
    if (![_footerView.contentTextView isExclusiveTouch]) {
        [self.footerView.contentTextView resignFirstResponder];
    }
admin's avatar
admin committed
344
}
admin's avatar
admin committed
345 346 347 348 349 350 351 352 353 354
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

355
@end