CommonFun.m 5.64 KB
//
//  CommonFun.m
//  B2CMall
//
//  Created by 钱鋆 on 2019/4/8.
//  Copyright © 2019 Facebook. All rights reserved.
//

#import "CommonFun.h"

// CC_MD5
#import <CommonCrypto/CommonDigest.h>

#import <netdb.h>
#include <arpa/inet.h>

@implementation CommonFun

+ (instancetype)sharedInstance {
  static id sharedInstance = nil;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    sharedInstance = [[self alloc] init];
  });
  return sharedInstance;
}

- (NSString *)f_toJSONString:(id)theData
{
  NSString *_str_json = nil;
  if (theData) {
    if ([theData isKindOfClass:[NSDictionary class]] || [theData isKindOfClass:[NSMutableDictionary class]] || [theData isKindOfClass:[NSArray class]] || [theData isKindOfClass:[NSMutableArray class]]) {
      NSData *_data_json = [NSJSONSerialization dataWithJSONObject:theData
                                                           options:NSJSONWritingPrettyPrinted
                                                             error:nil];
      if (_data_json) {
        _str_json = [[NSString alloc] initWithData:_data_json
                                          encoding:NSUTF8StringEncoding];
      }
    }
  }
  
  return _str_json;
}

- (NSDictionary *)f_jsonStringToDic:(NSString *)jsonString {
  if (jsonString == nil) {
    return nil;
  }
  NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
  NSError *err;
  NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:jsonData
                                                      options:NSJSONReadingMutableContainers
                                                        error:&err];
  if(err) {
    NSLog(@"json解析失败:%@",err);
    return nil;
  }
  return dic;
}

- (NSMutableDictionary *)f_jsonDataToDic:(NSData *)jsonData option:(NSJSONReadingOptions)option {
  NSError *_err_temp;
  if (jsonData) {
    NSMutableDictionary *_muDic_json = [NSJSONSerialization
                                        JSONObjectWithData:jsonData
                                        options:option error:&_err_temp];
    
    if (_muDic_json && _muDic_json.count > 0) {
      return _muDic_json;
    }
  }
  return nil;
}

#pragma mark - type 1:空白 2:换行 默认 空白+换行
+(NSString *)deleteWhitespaceNewline:(NSString *)str type:(int)type
{
  switch (type) {
    case 1:
      return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
      break;
    case 2:
      return [str stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
      break;
    default:
      return [str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
      break;
  }
}

#pragma mark - string是否为空(null也算)
+ (BOOL) isBlankOrNullString:(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;
}

#pragma mark - dic/array转为jsonString
/**
 *  @author qianjun
 *
 *  @brief  dic/array转为jsonString
 *
 *  @param theData dic/array
 *
 *  @return jsonString
 */
- (NSString *)event_toJSONString:(id)theData
{
    
    if(theData)
    {
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:theData
                                                           options:NSJSONWritingPrettyPrinted
                                                             error:nil];
        
        NSString *jsonString = [[NSString alloc] initWithData:jsonData
                                                     encoding:NSUTF8StringEncoding];
        
        return jsonString;
    }
    return nil;
}

#pragma mark - 当前视图
- (UIViewController *)topViewController {
    UIViewController *resultVC;
    resultVC = [self _topViewController:[[UIApplication sharedApplication].keyWindow rootViewController]];
    
    if ([resultVC isKindOfClass:[UIViewController class]]) {
        return resultVC;
    }
    
    while (resultVC.presentedViewController) {
        // 如果是UIAlertController忽略掉
        if([[self _topViewController:resultVC.presentedViewController] isKindOfClass:[UIAlertController class]]){
            break;
        }else{
            resultVC = [self _topViewController:resultVC.presentedViewController];
        }
    }
    return resultVC;
}

- (UIViewController *)_topViewController:(UIViewController *)vc {
    if ([vc isKindOfClass:[UINavigationController class]]) {
        return [self _topViewController:[(UINavigationController *)vc topViewController]];
    } else if ([vc isKindOfClass:[UITabBarController class]]) {
        return [self _topViewController:[(UITabBarController *)vc selectedViewController]];
    } else {
        return vc;
    }
    return nil;
}


#pragma mark - MD5 32位加密
+(NSString *)md5_32:(NSString *)str bool_isUpper:(BOOL)bool_isUpper
{
    const char *cStr = [str UTF8String];
    unsigned char result[32];
    CC_MD5(cStr, (CC_LONG)strlen(cStr), result);
    NSMutableString *hash = [NSMutableString string];
    for (int i = 0; i < 16; i++)
    {
        [hash appendFormat:@"%02X", result[i]];
    }
    if (bool_isUpper) {
        return [hash uppercaseString];
    }else{
        return [hash lowercaseString];
    }
}

#pragma mark -获取字符串形式的当前时间(默认yyyy-MM-dd HH:mm:ss)
+(NSString *)getDateTimeString:(NSDateFormatter *)dateFormatter
{
    if (dateFormatter == nil) {
        dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss.SSS"];
    }
    NSDate *dateNow = [NSDate date];
    return [dateFormatter stringFromDate:dateNow];
}

@end