BaseViewController.m 15.7 KB
Newer Older
曹云霄's avatar
曹云霄 committed
1 2 3 4 5 6 7 8 9
//
//  BaseViewController.m
//  Lighting
//
//  Created by 曹云霄 on 16/4/27.
//  Copyright © 2016年 上海勾芒科技有限公司. All rights reserved.
//

#import "BaseViewController.h"
10
#import "LoginViewController.h"
曹云霄's avatar
曹云霄 committed
11
#import <WebKit/WebKit.h>
12
#import <AVFoundation/AVFoundation.h>
曹云霄's avatar
曹云霄 committed
13

曹云霄's avatar
曹云霄 committed
14
@interface BaseViewController ()<UIPrintInteractionControllerDelegate,UIWebViewDelegate,WKNavigationDelegate>
15

曹云霄's avatar
曹云霄 committed
16 17

@property (nonatomic,strong) WKWebView *webView;
曹云霄's avatar
曹云霄 committed
18

勾芒's avatar
勾芒 committed
19 20 21 22 23
/**
 *  提示框
 */
@property (nonatomic,strong) PromptinformationView *promptView;

24
@property (nonatomic,strong) UIPrintInteractionController *printControl;
曹云霄's avatar
曹云霄 committed
25 26


曹云霄's avatar
曹云霄 committed
27 28 29 30 31
@end

@implementation BaseViewController


勾芒's avatar
勾芒 committed
32

勾芒's avatar
勾芒 committed
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/**
 *  初始化提示框
 *
 *  @return PromptinformationView
 */
- (PromptinformationView *)promptView
{
    if (_promptView == nil) {
        
        _promptView = [[[NSBundle mainBundle] loadNibNamed:@"PromptinformationView" owner:self options:nil] firstObject];
    }
    return _promptView;
}


曹云霄's avatar
曹云霄 committed
48
- (WKWebView *)webView
勾芒's avatar
勾芒 committed
49 50 51
{
    if (_webView == nil) {
        
曹云霄's avatar
曹云霄 committed
52
        _webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
勾芒's avatar
勾芒 committed
53 54 55 56
    }
    return _webView;
}

57

曹云霄's avatar
曹云霄 committed
58 59
- (void)viewDidLoad {
    [super viewDidLoad];
曹云霄's avatar
曹云霄 committed
60
    
勾芒's avatar
勾芒 committed
61
    self.automaticallyAdjustsScrollViewInsets = NO;
曹云霄's avatar
曹云霄 committed
62
    self.navigationController.navigationBarHidden = YES;
63 64 65 66
    //检测登陆超时通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(logintimeoutNotification:) name:LOGINTIMEOUT object:nil];
}

67

68 69 70
#pragma mark -登陆超时回调
- (void)logintimeoutNotification:(NSNotification *)not
{
71
    WS(weakSelf);
72 73 74 75 76
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"登陆超时,请重新登陆" preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        [weakSelf dismissViewControllerAnimated:YES completion:nil];
        [Shoppersmanager manager].currentCustomer = NO;
        [Customermanager manager].model = nil;
曹云霄's avatar
曹云霄 committed
77
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"StoryboardwithCYX" bundle:nil];
78 79 80 81 82 83
        LoginViewController *loginVC = [storyboard instantiateViewControllerWithIdentifier:@"Login"];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:USERNAME];
        [[NSUserDefaults standardUserDefaults] removeObjectForKey:PASSWORD];
        [weakSelf restoreRootViewController:loginVC];
    }]];
    dispatch_async(dispatch_get_main_queue(), ^{
84
        [weakSelf presentViewController:alert animated:YES completion:nil];
85
    });
曹云霄's avatar
曹云霄 committed
86 87
}

88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106

#pragma mark -切换rootViewcontroller
- (void)restoreRootViewController:(UIViewController *)rootViewController
{
    typedef void (^Animation)(void);
    UIWindow* window = SHARED_APPDELEGATE.window;
    rootViewController.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    Animation animation = ^{
        BOOL oldState = [UIView areAnimationsEnabled];
        [UIView setAnimationsEnabled:NO];
        window.rootViewController = rootViewController;
        [UIView setAnimationsEnabled:oldState];
    };
    
    [UIView transitionWithView:window
                      duration:0.5f
                       options:UIViewAnimationOptionTransitionFlipFromBottom
                    animations:animation
                    completion:nil];
曹云霄's avatar
曹云霄 committed
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
}

#pragma mark -结束MJRfresh刷新
- (void)endRefreshingForTableView:(UIScrollView *)TableView
{
    if (TableView.mj_header.isRefreshing) {
        
        [TableView.mj_header endRefreshing];
    }
    else if (TableView.mj_footer.isRefreshing)
    {
        [TableView.mj_footer endRefreshing];
    }
}

曹云霄's avatar
曹云霄 committed
122
#pragma mark -获得我的storyboard对象
123
+ (UIStoryboard *)getMainStoryboardClass
曹云霄's avatar
曹云霄 committed
124
{
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"OppleMain" bundle:nil];
    return storyboard;
}

#pragma mark - 获得公告storyboard对象
+ (UIStoryboard *)getAnnouncementStoryboardClass
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Announcement" bundle:nil];
    return storyboard;
}

#pragma mark - 获得学习中心storyboard对象
+ (UIStoryboard *)getLearningCenterStoryboardClass
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"LearningCenter" bundle:nil];
    return storyboard;
}

#pragma mark -获得积分storyboard对象
+ (UIStoryboard *)getGuideIntegralStoryboardClass
{
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"GuideIntegral" bundle:nil];
曹云霄's avatar
曹云霄 committed
147 148 149
    return storyboard;
}

150 151 152 153 154 155 156 157 158 159 160 161
#pragma mark - 判断相机权限
+ (BOOL)determineCameraPermissions
{
    NSString *mediaType = AVMediaTypeVideo;//读取媒体类型
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
        return NO;
    }else {
        return YES;
    }
}

162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
#pragma mark -  考核题型转义
+ (NSString *)returnTopicTypeTitle:(NSString *)typeCode
{
    if ([typeCode isEqualToString:STUDYTOPICTYPE_SINGLE]) {
        return @"单选";
    }
    if ([typeCode isEqualToString:STUDYTOPICTYPE_MULTIPLE]) {
        return @"多选";
    }
    if ([typeCode isEqualToString:STUDYTOPICTYPE_TRUEORFALSE]) {
        return @"判断";
    }
    if ([typeCode isEqualToString:PASSLEVELTOPICTYPE_GAPFILLING]) {
        return @"填空";
    }
    if ([typeCode isEqualToString:PASSLEVELTOPICTYPE_SHORTANSWER]) {
        return @"简答";
    }
    return nil;
}


#pragma mark -查询兑奖单state对应文字
+ (id)returnPrizeBillStateTitleColor:(NSString *)stateCode
{
    NSDictionary *dict;
    if ([stateCode isEqualToString:@"requested"]) {
        dict = @{@"title":@"已申请",@"color":RGB(127, 192, 62, 1)};
    }else if ([stateCode isEqualToString:@"requested"]) {
        dict = @{@"title":@"已发货",@"color":RGB(245, 185, 74, 1)};
    }
    return dict;
}

#pragma mark - 获取当前时间之前或者之后的时间(之前传入负数)
+ (NSString *)getTimeby:(NSInteger)day
{
    NSDate*nowDate = [NSDate date];
    NSDate* theDate;
    if(day!=0){
        NSTimeInterval  oneDay = 24*60*60*1;  //1天的长度
        theDate = [nowDate initWithTimeIntervalSinceNow: oneDay*day];
    }else{
        theDate = nowDate;
    }
    NSDateFormatter *date_formatter = [[NSDateFormatter alloc] init];
    [date_formatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *the_date_str = [date_formatter stringFromDate:theDate];
    return the_date_str;
}

曹云霄's avatar
曹云霄 committed
213
#pragma mark 调用airPrint无线打印机
勾芒's avatar
勾芒 committed
214
- (void)callAirprintWithdata:(NSString *)PDFpath SuccessBlock:(void(^)())success ErrorBlock:(void(^)())failed
曹云霄's avatar
曹云霄 committed
215
{
勾芒's avatar
勾芒 committed
216
    NSData *mypdfdata = [NSData dataWithContentsOfFile:PDFpath];
曹云霄's avatar
曹云霄 committed
217 218 219 220 221 222 223 224 225 226
    UIPrintInteractionController *pic = [UIPrintInteractionController sharedPrintController];
    if  (pic && [UIPrintInteractionController canPrintData: mypdfdata] ) {
        pic.delegate = self;
        UIPrintInfo *printInfo = [UIPrintInfo printInfo];
        printInfo.outputType = UIPrintInfoOutputGeneral;
        printInfo.jobName = @"订单明细";
        printInfo.duplex = UIPrintInfoDuplexLongEdge;
        pic.printInfo = printInfo;
        pic.showsPageRange = YES;
        pic.printingItem = mypdfdata;
勾芒's avatar
勾芒 committed
227 228 229 230 231 232 233 234 235 236 237
        void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
            if (completed)
            {
                // 执行成功后的处理
                success();
            }
            else if (!completed && error)
            {
                // 执行失败后的处理
                failed();
            }
曹云霄's avatar
曹云霄 committed
238
        };
勾芒's avatar
勾芒 committed
239
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
曹云霄's avatar
曹云霄 committed
240 241 242 243 244
            [pic presentAnimated:YES completionHandler:completionHandler];
        }
    }
}

曹云霄's avatar
曹云霄 committed
245

246
#pragma mark - 提示框
247
- (void)promptCustomerTitle:(NSString *)title withMessage:(NSString *)message finish:(void (^)())complete
248
{
249 250
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:nil message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
251 252 253 254 255 256
        if (complete) {
            complete();
        }
    }]];
    [self presentViewController:alertVC animated:YES completion:nil];
}
257

勾芒's avatar
勾芒 committed
258 259 260 261 262 263
#pragma mark -删除提示文本、图片
- (void)RemovePromptinformationView
{
    [self.promptView removeFromSuperview];
    self.promptView = nil;
}
勾芒's avatar
勾芒 committed
264

勾芒's avatar
勾芒 committed
265 266 267
#pragma mark -传入订单状态,返回当前状态图片
+ (UIImage *)ReturnOrderStateImageWithStateCode:(NSInteger)ordercode
{
268
    const NSArray *imageArray = [NSArray arrayWithObjects:@"已撤销",@"已完成",@"已退货", nil];
勾芒's avatar
勾芒 committed
269
    switch (ordercode) {
勾芒's avatar
勾芒 committed
270
        case 4://退货
勾芒's avatar
勾芒 committed
271 272 273 274
        {
            return TCImage([imageArray objectAtIndex_opple:2]);
        }
            break;
勾芒's avatar
勾芒 committed
275
        case 5://作废
勾芒's avatar
勾芒 committed
276 277 278 279
        {
            return TCImage([imageArray objectAtIndex_opple:0]);
        }
            break;
勾芒's avatar
勾芒 committed
280
        case 6://完成
勾芒's avatar
勾芒 committed
281 282 283 284 285 286 287 288 289 290
        {
            return TCImage([imageArray objectAtIndex_opple:1]);
        }
            break;
            
        default:
            break;
    }
    return nil;
}
勾芒's avatar
勾芒 committed
291 292


勾芒's avatar
勾芒 committed
293 294 295
#pragma mark -传入订单状态,返回订单文本信息,字体颜色
+ (id)ReturnOrderStateTitleWithStateCode:(NSInteger)ordercode withPoint:(CGPoint)point WithCode:(NSInteger)code
{
296
    
勾芒's avatar
勾芒 committed
297 298 299 300 301 302
    switch (ordercode) {
        case 001://未付款
        {
            if (code) {
                return kTCColor(241, 81, 83);
            }
曹云霄's avatar
曹云霄 committed
303
            return @"待支付";
勾芒's avatar
勾芒 committed
304 305 306 307
        }
            break;
        case 002://已付款
        {
308
            
勾芒's avatar
勾芒 committed
309 310 311
            if (code) {
                return kTCColor(126, 191, 53);
            }
312
            return @"已支付";
勾芒's avatar
勾芒 committed
313 314 315 316
        }
            break;
        case 003://已发货
        {
317
            
勾芒's avatar
勾芒 committed
318 319 320 321 322 323 324 325
            if (code) {
                return kTCColor(240, 180, 51);
            }
            return @"已发货";
        }
            break;
        case 004://已退货
        {
326
            
勾芒's avatar
勾芒 committed
327 328 329 330 331 332 333 334
            if (code) {
                return kTCColor(153, 153, 153);
            }
            return @"已退货";
        }
            break;
        case 005://已作废
        {
335
            
勾芒's avatar
勾芒 committed
336 337 338
            if (code) {
                return kTCColor(153, 153, 153);
            }
勾芒's avatar
勾芒 committed
339
            return @"已撤销";
勾芒's avatar
勾芒 committed
340 341 342 343
        }
            break;
        case 006://已完成
        {
344
            
勾芒's avatar
勾芒 committed
345 346 347 348 349 350 351 352 353 354 355 356
            if (code) {
                return kTCColor(89, 172, 220);
            }
            return @"已完成";
        }
            break;
            
        default:
            break;
    }
    return nil;
}
勾芒's avatar
勾芒 committed
357

勾芒's avatar
勾芒 committed
358 359 360
#pragma mark -查询购物车数量
- (void)QueryShoppingCarNumber
{
361
    [[NetworkRequestClassManager Manager] NetworkWithDictionaryRequestWithURL:[NSString stringWithFormat:@"%@%@",SERVERREQUESTURL(QUERYSHOPPINGBAGNUMBER),[Customermanager manager].model.fid]  WithRequestType:ONE WithParameter:nil WithReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
362 363 364
        
        if ([returnValue[@"code"] isEqualToNumber:@0]) {
            NSLog(@"更新购物车数量成功");
365
            [[NSNotificationCenter defaultCenter]postNotificationName:ADDSHOPPINGCAR object:returnValue[@"data"]];
勾芒's avatar
勾芒 committed
366
        }
367
    } WithFailureBlock:^(NSError *error) {
勾芒's avatar
勾芒 committed
368
        NSLog(@"更新购物车数量失败");
曹云霄's avatar
曹云霄 committed
369
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
勾芒's avatar
勾芒 committed
370 371 372
    }];
}

373 374 375 376 377
#pragma mark - 查询购物车商品
- (void)queryShoppingCarGoods
{
    [[NSNotificationCenter defaultCenter] postNotificationName:SHOPPINGCARGOODS object:nil];
}
勾芒's avatar
勾芒 committed
378

勾芒's avatar
勾芒 committed
379 380 381 382 383 384 385 386
#pragma mark -系统提示框
- (void)promptCustomerWithString:(NSString *)message
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"我知道了" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        [self dismissViewControllerAnimated:YES completion:nil];
    }]];
曹云霄's avatar
曹云霄 committed
387 388 389
    dispatch_async(dispatch_get_main_queue(), ^{
        [self presentViewController:alertVC animated:YES completion:nil];
    });
勾芒's avatar
勾芒 committed
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 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
#pragma mark -友好化时间
+ (NSString *) compareCurrentTime:(NSString *)str
{
    
    //把字符串转为NSdate
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    NSDate *timeDate = [dateFormatter dateFromString:str];
    
    //得到与当前时间差
    NSTimeInterval  timeInterval = [timeDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    //标准时间和北京时间差8个小时
    timeInterval = timeInterval - 8*60*60;
    long temp = 0;
    NSString *result;
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分钟前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
}
勾芒's avatar
勾芒 committed
433

434 435 436 437 438 439 440
#pragma mark - 获取版本号
- (NSString*)getAppVersion
{
    NSString *ver = [[[NSBundle mainBundle] infoDictionary]
                     objectForKey:@"CFBundleShortVersionString"];
    return [NSString stringWithFormat:@"Version %@",ver];
}
勾芒's avatar
勾芒 committed
441

442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
#pragma mark - 通过面额获取相应的图片
+ (UIImage *)getCorrespondingPictures:(NSInteger)total
{
    UIImage *image = nil;
    switch (total) {
        case 50:
        {
            image = TCImage(@"fifty");
        }
            break;
        case 100:
        {
            image = TCImage(@"onehundred");
        }
            break;
        case 200:
        {
            image = TCImage(@"twohundred");
        }
            break;
            
        default:
            break;
    }
    return image;
}

+ (BOOL)isBlankString:(NSString *)string
{
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505
/**
 选择框
 
 @param message 文本
 @param cancel  取消
 @param sure    确认
 */
- (void)promptBoxWithMessage:(NSString *)message cancelBlock:(void(^)())cancel sureBlock:(void(^)())sure
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        if (cancel) {
            cancel();
        }
    }]];
    [alertVC addAction:[UIAlertAction actionWithTitle:@"确认" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        if (sure) {
            sure();
        }
    }]];
    [self presentViewController:alertVC animated:YES completion:nil];
}

506

507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532
/**
 闯关结果状态
 
 @param passResult 闯关结果
 
 @return 状态图片
 */
- (UIImage *)emigratedState:(NSInteger)passResult
{
    switch (passResult) {
            case 0:
            return TCImage(@"failure");
            break;
            case 1:
            return TCImage(@"success");
            break;
            case 2:
            return TCImage(@"submited");
            break;
            
        default:
            break;
    }
    return nil;
}

533 534
- (void)dealloc
{
535
    NSLog(@"释放%@控制器",self);
536 537
}

勾芒's avatar
勾芒 committed
538

勾芒's avatar
勾芒 committed
539 540 541



曹云霄's avatar
曹云霄 committed
542
@end