AddQuestionViewController.m 12.3 KB
Newer Older
admin's avatar
admin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14
//
//  AddQuestionViewController.m
//  redstar
//
//  Created by admin on 15/11/6.
//  Copyright © 2015年 ZWF. All rights reserved.
//

#import "AddQuestionViewController.h"
#import "OnLineTableViewCell.h"

#import "AddQuestionFooterView.h"
#import "CheckPicViewController.h"

admin's avatar
admin committed
15 16 17 18 19
#import "CommonFunc.h"
#import "HttpClient.h"
#import "RedStarTools.h"
#import <MBProgressHUD.h>

admin's avatar
admin committed
20 21
#define kAddQuestionCell @"addQuestionTableViewCell"

22
@interface AddQuestionViewController () <UITableViewDelegate, UITableViewDataSource, TakePhotoViewDelegate, UINavigationControllerDelegate, UIImagePickerControllerDelegate>
admin's avatar
admin committed
23 24 25 26 27
@property (nonatomic, strong) UITableView *tableView;

@property (nonatomic, strong) AddQuestionFooterView *footerView;

@property (nonatomic, strong) NSArray *titleArray;
admin's avatar
admin committed
28 29

@property (nonatomic, strong) NSString *imageMD5;
admin's avatar
admin committed
30 31 32 33
@end

@implementation AddQuestionViewController

admin's avatar
admin committed
34 35 36



admin's avatar
admin committed
37 38 39 40
- (void)viewDidLoad
{
    [super viewDidLoad];
    
41 42
    self.view.backgroundColor = kFootViewBackGroundColor;
    
admin's avatar
admin committed
43 44 45 46 47 48
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 40, 30)];
    [customLab setTextColor:[UIColor whiteColor]];
    [customLab setText:@"新问题提报"];
    customLab.font = [UIFont boldSystemFontOfSize:19];
    self.navigationItem.titleView = customLab;
    
49 50 51 52 53 54 55 56 57 58
    
    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
59 60 61 62
    self.titleArray = [NSArray arrayWithObjects:@"选择对应专业组", @"选择问题分类", nil];
    
    [self addTableView];
    
63
    // takePhoto代理
admin's avatar
admin committed
64
    self.footerView.takePhotoView.delegate = self;
admin's avatar
admin committed
65
    [self.footerView.reportBtn addTarget:self action:@selector(upLoadQuestionPic:) forControlEvents:UIControlEventTouchUpInside];
admin's avatar
admin committed
66 67 68 69 70 71 72 73
}

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

#pragma mark - Private Methods
74

admin's avatar
admin committed
75 76 77 78 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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
- (void)upLoadQuestionPic:(UIButton *)sender
{
    [[RedStarTools sharedInstance] loadLocalData];
    NSMutableArray *localData = [RedStarTools sharedInstance].localDatas;
    NSDictionary *parameters;
    if (localData.count == 0) {
        parameters = @{@"category":@"环境物业",
                       @"group":@"基础环境",
                       @"title":@"测试数据",
                       @"content":self.footerView.titleTextView.text
                       };
    } else {
        NSMutableArray *imageArray = [NSMutableArray array];
        for (int i = 0; i < localData.count; i++) {
            NSString *imageName = [NSString stringWithFormat:@"%@", localData[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];
        }
        
        parameters = @{@"category":@"环境物业",
                       @"group":@"基础环境",
                       @"title":@"测试数据",
                       @"content":self.footerView.titleTextView.text,
                       @"attachments":imageArray};
    }
    
    
    NSDate *date = [NSDate date];
    NSTimeZone *zone = [NSTimeZone systemTimeZone];
    NSInteger interval = [zone secondsFromGMTForDate:date];
    NSDate *localeDate = [date  dateByAddingTimeInterval:interval];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd+hh:mm:ss"];
    NSString *dateString = [dateFormatter stringFromDate:localeDate];
    
    NSString *operId = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_code"];
    NSString *operName = [[NSUserDefaults standardUserDefaults] objectForKey:@"user_name"];

    
    NSString *url = [NSString stringWithFormat:@"%@%@?time=%@&operId=%@&operName=%@", kRedStarURL, kUpLoadQuestionURL, dateString, operId, operName];
    url = [url stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];

    HttpClient *client = [[HttpClient alloc] initWithUrl:url];
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [client uploadNewQuestionWithParameters:parameters completion:^(id response, NSError *error) {
        NSLog(@"上传 response= %@, error = %@", response, error);
        if (response[@"success"]) {
            [RedStarTools sharedInstance].localDatas = [NSMutableArray array];
            [[RedStarTools sharedInstance] saveLocalData];
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }
    }];
    
    
}

139 140 141
// 返回上一页面
- (void)doBack:(UIBarButtonItem *)sender
{
admin's avatar
admin committed
142 143
    [RedStarTools sharedInstance].localDatas = [NSMutableArray array];
    [[RedStarTools sharedInstance] saveLocalData];
144 145 146 147
    [self.navigationController popViewControllerAnimated:YES];
}


admin's avatar
admin committed
148 149 150 151 152 153
- (void)addTableView
{
    [self.tableView registerClass:[OnLineTableViewCell class] forCellReuseIdentifier:kAddQuestionCell];
    self.tableView.tableFooterView = self.footerView;
}

154 155 156 157


#pragma mark - TakePhoto Delegate
- (void)deletePhoto:(UIButton *)button
admin's avatar
admin committed
158
{
159 160 161 162 163 164 165 166 167 168 169 170 171
    // 从存放所有Image的数组里移除当前点击的Image
    [self.footerView.takePhotoView.allImages removeObject:[(UIButton *)button.superview imageForState:UIControlStateNormal]];
    // 移除显示Image的Button
    [button.superview removeFromSuperview];
    
    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;
admin's avatar
admin committed
172
    }
173 174 175 176 177 178 179
    self.footerView.takePhotoView.frame = CGRectMake(0, 170, kScreenWidth, k * btnH + (k + 1) * marginY);
    self.footerView.height = self.footerView.takePhotoView.frame.size.height + 220;
    
}

- (void)createImagePicker
{
admin's avatar
admin committed
180 181 182 183 184 185 186 187 188 189 190
    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];
    }
admin's avatar
admin committed
191 192 193 194 195 196 197 198 199
}

- (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
200 201 202 203 204 205 206 207 208 209 210 211 212 213
// 保存图片至沙盒
- (void)saveImage:(UIButton *)button
{
    UIImage *currentImage = button.imageView.image;
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    self.imageMD5 = [CommonFunc md5Data:imageData];
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:_imageMD5];
    
    [[RedStarTools sharedInstance].localDatas addObject:_imageMD5];
    [[RedStarTools sharedInstance] saveLocalData];
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}
214 215 216
#pragma mark - UIImagePickerController 代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
admin's avatar
admin committed
217
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
218 219
    
    // 创建一个新的控件
admin's avatar
admin committed
220 221 222 223 224 225
    UIButton *newButton = [self.footerView.takePhotoView createButtonWithImage:image];
    [newButton addTarget:self action:@selector(takePhotoShowPicture:) forControlEvents:UIControlEventTouchUpInside];
    
    // 保存图片至沙盒
    [self saveImage:newButton];
    
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    [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;
    }
    
    CGFloat btnH = 110;
    CGFloat marginY = 10;
    self.footerView.takePhotoView.frame = CGRectMake(0, 170, kScreenWidth, k * btnH + (k + 1) * marginY);
    self.footerView.height = self.footerView.takePhotoView.frame.size.height + 220;
    // 退出图片选择控制器
    [picker dismissViewControllerAnimated:YES completion:nil];

}

admin's avatar
admin committed
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323
#pragma mark - TableView Delegate/DateSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleArray.count;
}

// cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    OnLineTableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:kAddQuestionCell];
    if (!cell) {
        cell = [[OnLineTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kAddQuestionCell];
    }
    cell.titleLabel.text = [NSString stringWithFormat:@"%@", _titleArray[indexPath.row]];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
    
    
}

// cell的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}


#pragma mark - lazy loading
- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero];
        _tableView.translatesAutoresizingMaskIntoConstraints = NO;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsHorizontalScrollIndicator = NO;
        [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;
}

- (AddQuestionFooterView *)footerView
{
    if (!_footerView) {
        if (kScreenHeight == 480) {
            _footerView = [[AddQuestionFooterView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 400)];
        } else {
            _footerView = [[AddQuestionFooterView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, (kScreenHeight - 100 - 64))];
        }
    }
    return _footerView;
}

/*
#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.
}
*/

@end