AddQuestionViewController.m 17.8 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
#import "CommonFunc.h"
#import "HttpClient.h"
#import <MBProgressHUD.h>

admin's avatar
admin committed
19 20
#define CATEGORYLIST @"服务", @"环境企划", @"环境物业", nil
#define QUESTIONLIST @"15分钟退单", @"便民服务", @"基础管理",@"轻松购系统",@"全员服务",@"人员信息",@"营运物流及服务硬件", nil
admin's avatar
admin committed
21

admin's avatar
admin committed
22 23 24 25

#define kAddQuestionCell @"UITableViewCellIdentifier"

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

@property (nonatomic, strong) AddQuestionFooterView *footerView;
admin's avatar
admin committed
29
@property (nonatomic, strong) NSMutableArray *imageNameArray;
admin's avatar
admin committed
30
@property (nonatomic, strong) NSArray *titleArray;
admin's avatar
admin committed
31 32

@property (nonatomic, strong) NSString *imageMD5;
admin's avatar
admin committed
33 34 35 36 37 38

@property (nonatomic, strong) UIActionSheet *categoryActionSheet;
@property (nonatomic, strong) UIActionSheet *questionActionSheet;

@property (nonatomic ,strong) UILabel *selectLabel;
@property (nonatomic ,strong) UILabel *selectLabel1;
admin's avatar
admin committed
39 40 41 42
@end

@implementation AddQuestionViewController

admin's avatar
admin committed
43 44 45



admin's avatar
admin committed
46 47 48 49
- (void)viewDidLoad
{
    [super viewDidLoad];
    
50 51
    self.view.backgroundColor = kFootViewBackGroundColor;
    
admin's avatar
admin committed
52 53 54 55 56 57
    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;
    
58 59 60 61 62 63 64 65
    
    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
66
    self.imageNameArray = [NSMutableArray array];
67

admin's avatar
admin committed
68 69 70 71
    self.titleArray = [NSArray arrayWithObjects:@"选择对应专业组", @"选择问题分类", nil];
    
    [self addTableView];
    
72
    // takePhoto代理
admin's avatar
admin committed
73
    self.footerView.takePhotoView.delegate = self;
admin's avatar
admin committed
74
    [self.footerView.reportBtn addTarget:self action:@selector(upLoadQuestionPic:) forControlEvents:UIControlEventTouchUpInside];
admin's avatar
admin committed
75 76 77 78 79 80 81 82
}

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

#pragma mark - Private Methods
83

admin's avatar
admin committed
84 85
- (void)upLoadQuestionPic:(UIButton *)sender
{
admin's avatar
admin committed
86 87 88 89 90 91 92 93 94 95 96
    if ([_selectLabel.text isEqualToString:@"请选择"]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"请选择对应专业组" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        return;
    }
    
    if ([_selectLabel1.text isEqualToString:@"请选择"]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"警告" message:@"请选择问题分类" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alert show];
        return;
    }
admin's avatar
admin committed
97 98 99
    
    
    
admin's avatar
admin committed
100
    NSDictionary *parameters;
admin's avatar
admin committed
101
    if (_imageNameArray.count == 0) {
admin's avatar
admin committed
102 103 104
        parameters = @{@"category":_selectLabel.text,
                       @"group":_selectLabel1.text,
                       @"title":self.footerView.contentTextView.text,
admin's avatar
admin committed
105 106 107 108
                       @"content":self.footerView.titleTextView.text
                       };
    } else {
        NSMutableArray *imageArray = [NSMutableArray array];
admin's avatar
admin committed
109 110
        for (int i = 0; i < _imageNameArray.count; i++) {
            NSString *imageName = _imageNameArray[i];
admin's avatar
admin committed
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
            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};
    }
    
admin's avatar
admin committed
129 130
    NSLog(@"par = %@", parameters);
    
admin's avatar
admin committed
131 132 133 134
    
    NSDate *date = [NSDate date];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"YYYY-MM-dd+hh:mm:ss"];
admin's avatar
admin committed
135
    NSString *dateString = [dateFormatter stringFromDate:date];
admin's avatar
admin committed
136 137 138 139 140 141 142 143 144 145 146 147 148
    
    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"]) {
admin's avatar
admin committed
149
            
admin's avatar
admin committed
150 151 152 153 154 155 156
            [MBProgressHUD hideHUDForView:self.view animated:YES];
        }
    }];
    
    
}

157 158 159 160 161 162 163
// 返回上一页面
- (void)doBack:(UIBarButtonItem *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}


admin's avatar
admin committed
164 165
- (void)addTableView
{
admin's avatar
admin committed
166
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:kAddQuestionCell];
admin's avatar
admin committed
167 168 169
    self.tableView.tableFooterView = self.footerView;
}

170 171 172 173


#pragma mark - TakePhoto Delegate
- (void)deletePhoto:(UIButton *)button
admin's avatar
admin committed
174
{
175 176 177 178 179 180 181 182 183 184 185 186 187
    // 从存放所有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
188
    }
189 190 191
    self.footerView.takePhotoView.frame = CGRectMake(0, 170, kScreenWidth, k * btnH + (k + 1) * marginY);
    self.footerView.height = self.footerView.takePhotoView.frame.size.height + 220;
    
admin's avatar
admin committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    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");
        }
    }
    
207 208 209 210
}

- (void)createImagePicker
{
admin's avatar
admin committed
211 212 213 214 215 216 217 218 219 220 221
    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
222 223 224 225 226 227 228 229 230
}

- (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
231 232
#pragma mark - 保存图片至沙盒
- (void)saveImage:(UIImage *)currentImage {
admin's avatar
admin committed
233 234
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    
admin's avatar
admin committed
235 236 237 238 239 240
    NSString *imageName = [CommonFunc md5Data:imageData];
    [_imageNameArray addObject:imageName];
    
    
    // 获取沙盒目录
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
admin's avatar
admin committed
241 242 243
    // 将图片写入文件
    [imageData writeToFile:fullPath atomically:NO];
}
244 245 246
#pragma mark - UIImagePickerController 代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
admin's avatar
admin committed
247
    UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
admin's avatar
admin committed
248
    [self saveImage:image];
249
    // 创建一个新的控件
admin's avatar
admin committed
250 251 252
    UIButton *newButton = [self.footerView.takePhotoView createButtonWithImage:image];
    [newButton addTarget:self action:@selector(takePhotoShowPicture:) forControlEvents:UIControlEventTouchUpInside];
    
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
    [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
273 274 275 276 277 278 279 280 281 282
#pragma mark - TableView Delegate/DateSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _titleArray.count;
}

// cell显示的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
admin's avatar
admin committed
283
    UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:kAddQuestionCell];
admin's avatar
admin committed
284
    if (!cell) {
admin's avatar
admin committed
285
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:kAddQuestionCell];
admin's avatar
admin committed
286
    }
admin's avatar
admin committed
287 288 289 290
    cell.textLabel.text = [NSString stringWithFormat:@"%@", _titleArray[indexPath.row]];
    cell.textLabel.font = [UIFont systemFontOfSize:16.0];
    cell.textLabel.textColor = kOnLineCellTitleColor;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
admin's avatar
admin committed
291
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
admin's avatar
admin committed
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 324 325 326 327 328 329 330 331 332 333 334 335 336
    
    if (indexPath.row == 0) {
        self.selectLabel = [[UILabel alloc] init];
        _selectLabel.text = @"请选择";
        _selectLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _selectLabel.textColor = kOnLineCellDetailColor;
        _selectLabel.textAlignment = NSTextAlignmentRight;
        _selectLabel.translatesAutoresizingMaskIntoConstraints = NO;
        _selectLabel.font = [UIFont systemFontOfSize:15.0];
        [cell.contentView addSubview:_selectLabel];
        // 顶端
        NSLayoutConstraint *selectTop = [NSLayoutConstraint constraintWithItem:_selectLabel attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        [cell.contentView addConstraint:selectTop];
        
        // 左边
        NSLayoutConstraint *selectRight = [NSLayoutConstraint constraintWithItem:_selectLabel attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        [cell.contentView addConstraint:selectRight];
        
        // 高度
        NSLayoutConstraint *selectBottom = [NSLayoutConstraint constraintWithItem:_selectLabel attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        [cell.contentView addConstraint:selectBottom];
    } else {
        self.selectLabel1 = [[UILabel alloc] init];
        _selectLabel1.text = @"请选择";
        _selectLabel1.translatesAutoresizingMaskIntoConstraints = NO;
        _selectLabel1.textColor = kOnLineCellDetailColor;
        _selectLabel1.textAlignment = NSTextAlignmentRight;
        _selectLabel1.translatesAutoresizingMaskIntoConstraints = NO;
        _selectLabel1.font = [UIFont systemFontOfSize:15.0];
        [cell.contentView addSubview:_selectLabel1];
        // 顶端
        NSLayoutConstraint *selectTop = [NSLayoutConstraint constraintWithItem:_selectLabel1 attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeTop multiplier:1.0 constant:0];
        [cell.contentView addConstraint:selectTop];
        
        // 左边
        NSLayoutConstraint *selectRight = [NSLayoutConstraint constraintWithItem:_selectLabel1 attribute:NSLayoutAttributeRight relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeRight multiplier:1.0 constant:0];
        [cell.contentView addConstraint:selectRight];
        
        // 高度
        NSLayoutConstraint *selectBottom = [NSLayoutConstraint constraintWithItem:_selectLabel1 attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:cell.contentView attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0];
        [cell.contentView addConstraint:selectBottom];
    }
    
    

admin's avatar
admin committed
337 338 339 340 341 342 343 344 345 346 347
    return cell;
    
    
}

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

admin's avatar
admin committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        self.categoryActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:CATEGORYLIST];
        _categoryActionSheet.delegate = self;
        [_categoryActionSheet showInView:self.view];

    } else {
        self.questionActionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:QUESTIONLIST];
        _questionActionSheet.delegate = self;
        [_questionActionSheet showInView:self.view];
    }
}

#pragma mark - UIActionSheetDelegate

- (void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (actionSheet == _categoryActionSheet) {
        
        if (buttonIndex < [NSArray arrayWithObjects:CATEGORYLIST].count) {
            _selectLabel.text = [[NSArray arrayWithObjects:CATEGORYLIST] objectAtIndex:buttonIndex];
        }
    } else {
        if (buttonIndex < [NSArray arrayWithObjects:QUESTIONLIST].count) {
            _selectLabel1.text = [[NSArray arrayWithObjects:QUESTIONLIST] objectAtIndex:buttonIndex];
        }
    }
}

admin's avatar
admin committed
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417

#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;
}

admin's avatar
admin committed
418 419 420 421
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.tableView endEditing:YES];
}

admin's avatar
admin committed
422 423 424 425 426 427 428 429 430 431 432
/*
#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