BaseViewController.m 24.5 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>
13
#import <Photos/Photos.h>
14
#import "PerfectInformationViewController.h"
15
#import "ResellerViewController.h"
曹云霄's avatar
曹云霄 committed
16
#import "LocationViewController.h"
17

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

曹云霄's avatar
曹云霄 committed
20 21

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

勾芒's avatar
勾芒 committed
23 24 25 26 27
/**
 *  提示框
 */
@property (nonatomic,strong) PromptinformationView *promptView;

28
@property (nonatomic,strong) UIPrintInteractionController *printControl;
曹云霄's avatar
曹云霄 committed
29 30


曹云霄's avatar
曹云霄 committed
31 32 33 34 35
@end

@implementation BaseViewController


勾芒's avatar
勾芒 committed
36

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


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

曹云霄's avatar
曹云霄 committed
60 61
- (void)viewDidLoad {
    [super viewDidLoad];
曹云霄's avatar
曹云霄 committed
62
    
勾芒's avatar
勾芒 committed
63
    self.automaticallyAdjustsScrollViewInsets = NO;
64 65 66 67
    if ([self respondsToSelector:@selector(setNeedsStatusBarAppearanceUpdate)])
    {
        [self prefersStatusBarHidden];
    }
68
    //检测登陆超时通知
69
    [Notification addObserver:self selector:@selector(logintimeoutNotification:) name:LOGINTIMEOUT object:nil];
70 71
}

72 73 74 75 76
#pragma mark - 隐藏状态栏
- (BOOL)prefersStatusBarHidden
{
    return YES;
}
77

78 79 80
#pragma mark -完善导购个人信息
- (void)perfectGuideInformation
{
曹云霄's avatar
曹云霄 committed
81 82 83 84 85 86 87 88
    PerfectInformationViewController *perfectInformation = [[[self class] getMainStoryboardClass] instantiateViewControllerWithIdentifier:@"PerfectInformationViewController"];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:perfectInformation];
    nav.preferredContentSize = CGSizeMake(ScreenHeight-300, ScreenHeight-300);
    nav.modalPresentationStyle = UIModalPresentationFormSheet;
    UIPopoverPresentationController *pop = nav.popoverPresentationController;
    pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
    pop.sourceView = nav.view;
    [self presentViewController:nav animated:YES completion:nil];
89 90
}

91
#pragma mark -完善导购经销商信息
92
- (void)perfectResellerInformation:(void(^)(ResellerViewController *resellerVc))completed
93 94
{
    ResellerViewController *resellerVc = [[[self class] getMainStoryboardClass] instantiateViewControllerWithIdentifier:@"ResellerViewController"];
95 96
    resellerVc.preferredContentSize = CGSizeMake(400, 400);
    resellerVc.modalPresentationStyle = UIModalPresentationFormSheet;
曹云霄's avatar
曹云霄 committed
97 98 99
    UIPopoverPresentationController *pop = resellerVc.popoverPresentationController;
    pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
    pop.sourceView = resellerVc.view;
100 101 102
    if (completed) {
        completed(resellerVc);
    }
曹云霄's avatar
曹云霄 committed
103 104 105 106 107 108 109 110
    [self presentViewController:resellerVc animated:YES completion:nil];
}

#pragma mark -完善门店位置信息
- (void)perfectStoreLocationInformation
{
    LocationViewController *resellerVc = [[[self class] getMainStoryboardClass] instantiateViewControllerWithIdentifier:@"LocationViewController"];
    resellerVc.modalPresentationStyle = UIModalPresentationFormSheet;
111
    resellerVc.preferredContentSize = CGSizeMake(400, 400);
112
    UIPopoverPresentationController *pop = resellerVc.popoverPresentationController;
113
    pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
114 115
    pop.sourceView = resellerVc.view;
    [self presentViewController:resellerVc animated:YES completion:nil];
116 117 118
}


119 120 121
#pragma mark -登陆超时回调
- (void)logintimeoutNotification:(NSNotification *)not
{
122
    WS(weakSelf);
123 124 125 126 127
    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;
128
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"OppleMain" bundle:nil];
129
        LoginViewController *loginVC = [storyboard instantiateViewControllerWithIdentifier:@"Login"];
130 131
        [UserDefault removeObjectForKey:USERNAME];
        [UserDefault removeObjectForKey:PASSWORD];
132 133 134
        [weakSelf restoreRootViewController:loginVC];
    }]];
    dispatch_async(dispatch_get_main_queue(), ^{
135
        [weakSelf presentViewController:alert animated:YES completion:nil];
136
    });
曹云霄's avatar
曹云霄 committed
137 138
}

139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
#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
156 157 158 159 160 161 162 163 164 165 166 167 168 169
}

#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
170
#pragma mark -获得我的storyboard对象
171
+ (UIStoryboard *)getMainStoryboardClass
曹云霄's avatar
曹云霄 committed
172
{
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    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
195 196 197
    return storyboard;
}

198 199 200 201 202 203
#pragma mark - 判断相机权限
+ (BOOL)determineCameraPermissions
{
    NSString *mediaType = AVMediaTypeVideo;//读取媒体类型
    AVAuthorizationStatus authStatus = [AVCaptureDevice authorizationStatusForMediaType:mediaType];//读取设备授权状态
    if(authStatus == AVAuthorizationStatusRestricted || authStatus == AVAuthorizationStatusDenied){
204
        ShowAlertView(@"提示", @"请在iPad的“设置-隐私-相机”选项中,允许欧立方访问你的相机", @[@"我知道了"], UIAlertControllerStyleAlert, nil);
205
        return NO;
206

207 208 209 210 211
    }else {
        return YES;
    }
}

212 213 214 215 216 217 218 219 220 221 222 223
#pragma mark - 判断相册的权限
+ (BOOL)determinePhotosPermissions
{
    PHAuthorizationStatus status = [PHPhotoLibrary authorizationStatus];
    if (status == PHAuthorizationStatusRestricted ||
        status == PHAuthorizationStatusDenied) {
        //无权限
        return NO;
    }
    return YES;
}

224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
#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)};
251
    }else if ([stateCode isEqualToString:@"shipped"]) {
252
        dict = @{@"title":@"已发货",@"color":RGB(245, 185, 74, 1)};
253 254
    }else if ([stateCode isEqualToString:@"done"]) {
        dict = @{@"title":@"已完成",@"color":kMainBlueColor};
255 256 257 258
    }
    return dict;
}

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275
#pragma mark - 设置提现状态
+ (NSDictionary *)setupLabelColor:(NSString *)text
{
    UIColor *color = kMainBlueColor;
    NSString *string;
    if ([text isEqualToString:@"submit"]) {
        color = RGB(128, 189, 51, 1);
        string = @"已提交";
    }else if ([text isEqualToString:@"paying"]) {
        color = kMainBlueColor;
        string = @"支付中";
    }else if ([text isEqualToString:@"accomplished"]) {
        color = kMainBlueColor;
        string = @"已完成";
    }else if ([text isEqualToString:@"failure"]) {
        color = [UIColor redColor];
        string = @"提现失败";
曹云霄's avatar
曹云霄 committed
276 277 278
    }else if ([text isEqualToString:@"reject"]) {
        color = [UIColor redColor];
        string = @"驳回";
279 280
    }else if ([text isEqualToString:@"commission"]) {
        color = [UIColor blackColor];
曹云霄's avatar
曹云霄 committed
281
        string = @"【产品返利】";
282 283
    }else if ([text isEqualToString:@"redPackage"]) {
        color = [UIColor blackColor];
曹云霄's avatar
曹云霄 committed
284
        string = @"【欧普奖励】";
285
    }
曹云霄's avatar
曹云霄 committed
286 287
    text = [[self class] isBlankString:text]?@"未知状态":text;
    return @{@"text":[[self class] isBlankString:string]?text:string,@"color":color};
288 289
}

曹云霄's avatar
曹云霄 committed
290 291 292 293 294 295 296 297
#pragma mark - 区分提现还是返佣
+ (NSString *)separatePaymentsAndWithfrawal:(NSNumber *)number withType:(NSString *)type
{
    CGFloat money = [number floatValue];
    NSString *moneyType = [[self class] setupLabelColor:type][@"text"];
    return [NSString stringWithFormat:@"%@%@",moneyType,(money>0)?@"返佣":@"提现"];
}

298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
#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
315
#pragma mark 调用airPrint无线打印机
曹云霄's avatar
曹云霄 committed
316
- (void)callAirprintWithdata:(NSString *)PDFpath successBlock:(void(^)())success errorBlock:(void(^)())failed
曹云霄's avatar
曹云霄 committed
317
{
勾芒's avatar
勾芒 committed
318
    NSData *mypdfdata = [NSData dataWithContentsOfFile:PDFpath];
曹云霄's avatar
曹云霄 committed
319 320 321 322 323 324 325 326 327 328
    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
329 330 331 332 333 334 335 336 337 338 339
        void (^completionHandler)(UIPrintInteractionController *, BOOL, NSError *) = ^(UIPrintInteractionController *pic, BOOL completed, NSError *error) {
            if (completed)
            {
                // 执行成功后的处理
                success();
            }
            else if (!completed && error)
            {
                // 执行失败后的处理
                failed();
            }
曹云霄's avatar
曹云霄 committed
340
        };
勾芒's avatar
勾芒 committed
341
        if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
曹云霄's avatar
曹云霄 committed
342 343 344 345 346
            [pic presentAnimated:YES completionHandler:completionHandler];
        }
    }
}

曹云霄's avatar
曹云霄 committed
347

348

勾芒's avatar
勾芒 committed
349
#pragma mark -传入订单状态,返回当前状态图片
350
+ (UIImage *)returnOrderStateImageWithStateCode:(NSInteger)ordercode
勾芒's avatar
勾芒 committed
351
{
352
    const NSArray *imageArray = [NSArray arrayWithObjects:@"已撤销",@"已完成",@"已退货", nil];
勾芒's avatar
勾芒 committed
353
    switch (ordercode) {
勾芒's avatar
勾芒 committed
354
        case 4://退货
勾芒's avatar
勾芒 committed
355 356 357 358
        {
            return TCImage([imageArray objectAtIndex_opple:2]);
        }
            break;
勾芒's avatar
勾芒 committed
359
        case 5://作废
勾芒's avatar
勾芒 committed
360 361 362 363
        {
            return TCImage([imageArray objectAtIndex_opple:0]);
        }
            break;
勾芒's avatar
勾芒 committed
364
        case 6://完成
勾芒's avatar
勾芒 committed
365 366 367 368 369 370 371 372 373 374
        {
            return TCImage([imageArray objectAtIndex_opple:1]);
        }
            break;
            
        default:
            break;
    }
    return nil;
}
勾芒's avatar
勾芒 committed
375 376


勾芒's avatar
勾芒 committed
377
#pragma mark -传入订单状态,返回订单文本信息,字体颜色
曹云霄's avatar
曹云霄 committed
378
+ (id)returnOrderStateTitleWithStateCode:(NSInteger)ordercode withPoint:(CGPoint)point withCode:(NSInteger)code
勾芒's avatar
勾芒 committed
379
{
380
    
勾芒's avatar
勾芒 committed
381 382 383 384
    switch (ordercode) {
        case 001://未付款
        {
            if (code) {
曹云霄's avatar
曹云霄 committed
385
                return RGB(241, 81, 83,1);
勾芒's avatar
勾芒 committed
386
            }
曹云霄's avatar
曹云霄 committed
387
            return @"待支付";
勾芒's avatar
勾芒 committed
388 389 390 391
        }
            break;
        case 002://已付款
        {
392
            
勾芒's avatar
勾芒 committed
393
            if (code) {
曹云霄's avatar
曹云霄 committed
394
                return RGB(126, 191, 53,1);
勾芒's avatar
勾芒 committed
395
            }
396
            return @"已支付";
勾芒's avatar
勾芒 committed
397 398 399 400
        }
            break;
        case 003://已发货
        {
401
            
勾芒's avatar
勾芒 committed
402
            if (code) {
曹云霄's avatar
曹云霄 committed
403
                return RGB(240, 180, 51,1);
勾芒's avatar
勾芒 committed
404 405 406 407 408 409
            }
            return @"已发货";
        }
            break;
        case 004://已退货
        {
410
            
勾芒's avatar
勾芒 committed
411
            if (code) {
曹云霄's avatar
曹云霄 committed
412
                return RGB(153, 153, 153,1);
勾芒's avatar
勾芒 committed
413 414 415 416 417 418
            }
            return @"已退货";
        }
            break;
        case 005://已作废
        {
419
            
勾芒's avatar
勾芒 committed
420
            if (code) {
曹云霄's avatar
曹云霄 committed
421
                return RGB(153, 153, 153,1);
勾芒's avatar
勾芒 committed
422
            }
勾芒's avatar
勾芒 committed
423
            return @"已撤销";
勾芒's avatar
勾芒 committed
424 425 426 427
        }
            break;
        case 006://已完成
        {
428
            
勾芒's avatar
勾芒 committed
429
            if (code) {
曹云霄's avatar
曹云霄 committed
430
                return RGB(89, 172, 220,1);
勾芒's avatar
勾芒 committed
431 432 433 434 435 436 437 438 439 440
            }
            return @"已完成";
        }
            break;
            
        default:
            break;
    }
    return nil;
}
勾芒's avatar
勾芒 committed
441

勾芒's avatar
勾芒 committed
442
#pragma mark -查询购物车数量
443
- (void)queryShoppingCarNumber
勾芒's avatar
勾芒 committed
444
{
445 446
    NSString *urlString = [NSString stringWithFormat:SERVERREQUESTURL(QUERYSHOPPINGBAGNUMBER),[Customermanager manager].model.fid];
    [HTTP networkWithDictionaryRequestWithURL:urlString  withRequestType:ONE withParameter:nil withReturnValueBlock:^(id returnValue) {
勾芒's avatar
勾芒 committed
447
        
曹云霄's avatar
曹云霄 committed
448
        if (RESULT(returnValue)) {
勾芒's avatar
勾芒 committed
449
            NSLog(@"更新购物车数量成功");
450
            [Notification postNotificationName:REFRESHSHOPPINGCAR object:RESPONSE(returnValue)];
勾芒's avatar
勾芒 committed
451
        }
452
    } withFailureBlock:^(NSError *error) {
勾芒's avatar
勾芒 committed
453
        NSLog(@"更新购物车数量失败");
曹云霄's avatar
曹云霄 committed
454
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
勾芒's avatar
勾芒 committed
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 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504

#pragma mark -通过经销商ID获取省市区
- (void)getStoreAddress:(void(^)())completed
{
    [HTTP networkRequestWithURL:[NSString stringWithFormat:SERVERREQUESTURL(STOREADDRESS),[Shoppersmanager manager].shoppers.employee.currentDepart.fid] withRequestType:POST withParameter:nil withReturnValueBlock:^(id returnValue) {
        
        if (RESULT(returnValue)) {
            ToStoreAddress *address = [[ToStoreAddress alloc] initWithDictionary:RESPONSE(returnValue) error:nil];
            [Shoppersmanager manager].storeAddress = address;
            if (completed) {
                completed();
            }
        }else {
            [XBLoadingView showHUDViewWithText:MESSAGE(returnValue)];
        }
        
    } withFailureBlock:^(NSError *error) {
        [XBLoadingView showHUDViewWithText:error.localizedDescription];
    }];
}

#pragma mark -直辖市处理
+ (NSString *)isMunicipality:(NSString *)province withCity:(NSString *)city withCountry:(NSString *)country withDetailAddress:(NSString *)address
{
    NSString *string = nil;
    if (![BaseViewController isBlankString:province] && ![BaseViewController isBlankString:city] && ![BaseViewController isBlankString:country]) {
        if (![province isEqualToString:city]) {
            if (![BaseViewController isBlankString:address]) {
                string = [NSString stringWithFormat:@"%@%@%@%@",province,city,country,address];
            }else {
                string = [NSString stringWithFormat:@"%@%@%@",province,city,country];
            }
        }else
        {
            if (![BaseViewController isBlankString:address]) {
                string = [NSString stringWithFormat:@"%@%@%@",city,country,address];
            }else {
                string = [NSString stringWithFormat:@"%@%@",city,country];
            }
        }
    }else
    {
        string = [NSString stringWithFormat:@"%@",address];
    }
    return string;
}

505 506 507
#pragma mark - 查询购物车商品
- (void)queryShoppingCarGoods
{
508
    [Notification postNotificationName:SHOPPINGCARGOODS object:nil];
509
}
勾芒's avatar
勾芒 committed
510

511 512 513 514 515 516 517
#pragma mark - 获取版本号
- (NSString*)getAppVersion
{
    NSString *ver = [[[NSBundle mainBundle] infoDictionary]
                     objectForKey:@"CFBundleShortVersionString"];
    return [NSString stringWithFormat:@"Version %@",ver];
}
勾芒's avatar
勾芒 committed
518

519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
#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;
}

560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
/**
 选择框
 
 @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];
}

583

584 585 586 587 588 589 590
/**
 闯关结果状态
 
 @param passResult 闯关结果
 
 @return 状态图片
 */
591
+ (UIImage *)emigratedState:(NSString *)passResult
592
{
593 594 595 596 597 598
    if ([passResult isEqualToString:@"0"]) {
        return TCImage(@"failure");
    }else if ([passResult isEqualToString:@"1"]) {
        return TCImage(@"success");
    }else if ([passResult isEqualToString:@"2"]) {
        return TCImage(@"submited");
599 600 601 602
    }
    return nil;
}

603 604 605 606

#pragma mark - 保存账号密码
+ (void)saveAccountAndPassWord:(NSString *)account AndPassword:(NSString *)passWord
{
607 608 609
    [UserDefault setValue:account forKey:SAVEACCOUNT];
    [UserDefault setValue:passWord forKey:SAVEPASSWORD];
    [UserDefault synchronize];
610 611 612 613 614
}

#pragma mark - 删除账号密码
+ (void)deleteAccountAndPassWord
{
615 616
    [UserDefault removeObjectForKey:SAVEACCOUNT];
    [UserDefault removeObjectForKey:SAVEPASSWORD];
617 618 619
}


620 621
#pragma mark -比较两个NSDate的大小
- (int)compareOneDay:(NSDate *)oneDay withAnotherDay:(NSDate *)anotherDay
曹云霄's avatar
曹云霄 committed
622 623
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
624 625 626 627 628 629 630 631
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *oneDayStr = [dateFormatter stringFromDate:oneDay];
    NSString *anotherDayStr = [dateFormatter stringFromDate:anotherDay];
    NSDate *dateA = [dateFormatter dateFromString:oneDayStr];
    NSDate *dateB = [dateFormatter dateFromString:anotherDayStr];
    NSComparisonResult result = [dateA compare:dateB];
    if (result == NSOrderedDescending) {
        return 1;
曹云霄's avatar
曹云霄 committed
632
    }
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
    else if (result == NSOrderedAscending){
        return -1;
    }
    return 0;
}

/**
 转换秒钟

 @param totalSeconds 秒数

 */
- (NSString *)timeFormatted:(NSInteger)totalSeconds
{
    NSInteger seconds = totalSeconds % 60;
    NSInteger minutes = (totalSeconds / 60) % 60;
    NSInteger hours = totalSeconds / 3600;
    if (!hours && !minutes && seconds) {
        return [NSString stringWithFormat:@"%02ld秒", seconds];
    }else if (!hours && minutes){
        return [NSString stringWithFormat:@"%02ld分:%02ld秒",minutes,seconds];
    }else if (hours && minutes) {
        return [NSString stringWithFormat:@"%02ld时:%02ld分:%02ld秒",hours, minutes, seconds];
    }
    return nil;
曹云霄's avatar
曹云霄 committed
658
}
659

660

曹云霄's avatar
曹云霄 committed
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727
/**
 格式化时间字符串

 @param dateString 时间字符串
 @return 中文标示
 */
+ (NSString *)formateDate:(NSString *)dateString
{
    @try {
        // ------实例化一个NSDateFormatter对象
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];//这里的格式必须和DateString格式一致
        NSDate * nowDate = [NSDate date];
        // ------将需要转换的时间转换成 NSDate 对象
        NSDate * needFormatDate = [dateFormatter dateFromString:dateString];
        // ------取当前时间和转换时间两个日期对象的时间间隔
        NSTimeInterval time = [nowDate timeIntervalSinceDate:needFormatDate];
        // ------再然后,把间隔的秒数折算成天数和小时数:
        
        NSString *dateStr = [[NSString alloc] init];
        
        if (time<=60) {  //1分钟以内的
            
            dateStr = @"刚刚";
            
        }else if(time<=60*60){  //一个小时以内的
            
            int mins = time/60;
            dateStr = [NSString stringWithFormat:@"%d分钟前",mins];
            
        }else if(time<=60*60*24){  //在两天内的
            
            [dateFormatter setDateFormat:@"YYYY-MM-dd"];
            NSString * need_yMd = [dateFormatter stringFromDate:needFormatDate];
            NSString *now_yMd = [dateFormatter stringFromDate:nowDate];
            
            [dateFormatter setDateFormat:@"HH:mm"];
            if ([need_yMd isEqualToString:now_yMd]) {
                //在同一天
                dateStr = [NSString stringWithFormat:@"今天 %@",[dateFormatter stringFromDate:needFormatDate]];
            }else{
                //昨天
                dateStr = [NSString stringWithFormat:@"昨天 %@",[dateFormatter stringFromDate:needFormatDate]];
            }
        }else {
            
            [dateFormatter setDateFormat:@"yyyy"];
            NSString * yearStr = [dateFormatter stringFromDate:needFormatDate];
            NSString *nowYear = [dateFormatter stringFromDate:nowDate];
            
            if ([yearStr isEqualToString:nowYear]) {
                //在同一年
                [dateFormatter setDateFormat:@"MM-dd"];
                dateStr = [dateFormatter stringFromDate:needFormatDate];
            }else{
                [dateFormatter setDateFormat:@"yyyy-MM-dd"];
                dateStr = [dateFormatter stringFromDate:needFormatDate];
            }
        }
        return dateStr;
    }
    @catch (NSException *exception) {
        return @"";
    }
}


曹云霄's avatar
曹云霄 committed
728 729 730 731 732 733 734
#pragma mark -汉字转码
- (NSString *)returnUrlString:(NSString *)ChineseString
{
    NSString* encodedString = [ChineseString stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    return encodedString;
}

曹云霄's avatar
曹云霄 committed
735 736 737 738 739 740 741
/**
 UIPopoverPresentationController弹出框
 
 @param viewController 需要弹出的控制器
 @param size size
 @param barbuttonitem 箭头按钮
 */
742
- (void)popoverPresentationController:(UIViewController *)viewController withPreferredContentSize:(CGSize)size  withBarButtonItem:(UIBarButtonItem *)barbuttonitem
曹云霄's avatar
曹云霄 committed
743 744 745 746 747 748 749 750 751 752 753
{
    viewController.preferredContentSize = size;
    viewController.modalPresentationStyle = UIModalPresentationPopover;
    UIPopoverPresentationController *pop = viewController.popoverPresentationController;
    pop.permittedArrowDirections = UIPopoverArrowDirectionAny;
    pop.sourceView = viewController.view;
    pop.barButtonItem = barbuttonitem;
    [self presentViewController:viewController animated:YES completion:nil];
}


曹云霄's avatar
曹云霄 committed
754 755


756 757
- (void)dealloc
{
758
    NSLog(@"释放%@控制器",self);
759 760
}

勾芒's avatar
勾芒 committed
761

勾芒's avatar
勾芒 committed
762 763 764



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