Files
midi_ios/QXLive/Tools/QXProjectTools.m
2025-08-14 10:07:49 +08:00

477 lines
20 KiB
Objective-C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// QXProjectTools.m
// QXLive
//
// Created by 启星 on 2025/4/27.
//
#import "QXProjectTools.h"
#import <sys/utsname.h>
#import <CoreTelephony/CTCarrier.h>
#import <CoreTelephony/CTTelephonyNetworkInfo.h>
#import "NSDate+QX.h"
//#import <WXApi.h>
//#import <AlipaySDK/AlipaySDK.h>
@implementation QXProjectTools
+(NSString *)getDispalyTimeWithType:(DataFormatType)type dateStr:(NSString *)dateStr{
// 实例化NSDateFormatter
NSDateFormatter *formatter1 = [[NSDateFormatter alloc] init];
// 设置日期格式
[formatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
// NSDate形式的日期
NSDate *date =[formatter1 dateFromString:dateStr];
NSDateFormatter *formatter2 = [[NSDateFormatter alloc] init];
switch (type) {
case DataFormatTypeMMDDHHMM:
[formatter2 setDateFormat:@"MM-dd HH:mm"];
break;
case DataFormatTypeZHMMDDHHMM:
[formatter2 setDateFormat:@"MM月dd日 HH:mm"];
break;
case DataFormatTypeHHMM:
[formatter2 setDateFormat:@"HH:mm"];
break;
case DataFormatTypeYYYYMMDD:
[formatter2 setDateFormat:@"yyyy-MM-dd"];
break;
default:
break;
}
NSString *dateString2 = [formatter2 stringFromDate:date];
return dateString2;
}
#pragma mark - 获取显示时间
+(NSString *)getDayDisplayWithDate:(NSString *)dateStr{
NSString* time = [self getTimestampFromTime:dateStr];
NSString *str = [NSDate timeStringWithTimeInterval:time];
return str;
}
//时间转时间戳
+ (NSString *)getTimestampFromTime:(NSString*)dateStr{
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateStyle:NSDateFormatterMediumStyle];
[formatter setTimeStyle:NSDateFormatterShortStyle];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];// ----------设置你想要的格式,hh与HH的区别:分别表示12小时制,24小时制
//设置时区,这个对于时间的处理有时很重要
//例如你在国内发布信息,用户在国外的另一个时区,你想让用户看到正确的发布时间就得注意时区设置,时间的换算.
//例如你发布的时间为2010-01-26 17:40:50,那么在英国爱尔兰那边用户看到的时间应该是多少呢?
//他们与我们有7个小时的时差,所以他们那还没到这个时间呢...那就是把未来的事做了
NSTimeZone* timeZone = [NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
[formatter setTimeZone:timeZone];
NSDate *date =[formatter dateFromString:dateStr];
NSString *nowtimeStr = [formatter stringFromDate:date];//----------将nsdate按formatter格式转成nsstring
NSLog(@"%@", nowtimeStr);
// 时间转时间戳的方法:
NSString *timeSp = [NSString stringWithFormat:@"%ld", (long)[date timeIntervalSince1970]];
NSLog(@"timeSp:%@",timeSp);//时间戳的值
return timeSp;
}
//把字符串替换成星号
+(NSString *)replaceStringWithAsterisk:(NSString *)originalStr startLocation:(NSInteger)startLocation lenght:(NSInteger)lenght{
NSString *newStr = originalStr;
for (int i = 0; i < lenght; i++) {
NSRange range = NSMakeRange(startLocation, 1);
newStr = [newStr stringByReplacingCharactersInRange:range withString:@"*"];
startLocation ++;
}
return newStr;
}
#pragma mark ---- 将时间戳转换成时间
+ (NSString *)getTimeFromTimestampWithTime:(long)time{
NSDate * myDate=[NSDate dateWithTimeIntervalSince1970:time];
//设置时间格式
NSDateFormatter * formatter=[[NSDateFormatter alloc]init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
//将时间转换为字符串
NSString *timeStr=[formatter stringFromDate:myDate];
return timeStr;
}
+(void)contactWithMobile:(NSString *)mobile{
NSMutableString * string = [[NSMutableString alloc] initWithFormat:@"tel:%@",mobile];
if (@available(iOS 10, *)) {
/// 大于等于10.0系统使用此openURL方法
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:string] options:@{} completionHandler:nil];
});
}else{
dispatch_async(dispatch_get_main_queue(), ^{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:string]];
});
}
}
+ (void)shakeView:(UIView*)viewToShake {
viewToShake.backgroundColor = [UIColor redColor];
// // 偏移值
// CGFloat t = 5.0;
// // 左摇
// CGAffineTransform translateLeft =CGAffineTransformTranslate(CGAffineTransformIdentity,-t,0.0);
// // 右晃
// CGAffineTransform translateRight =CGAffineTransformTranslate(CGAffineTransformIdentity, t,0.0);
// // 执行动画 重复动画且执行动画回路
// viewToShake.transform = translateLeft;
// [UIView animateWithDuration:0.07 delay:0.0 options:UIViewAnimationOptionAutoreverse | UIViewAnimationOptionRepeat animations:^{
// // 设置重复次数 repeatCount为float
// [UIView setAnimationRepeatCount:2.0];
// viewToShake.transform = translateRight;
//
// } completion:^(BOOL finished){
// if(finished){
// // 从视图当前状态回到初始状态
// [UIView animateWithDuration:0.05 delay:0.0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
// viewToShake.transform =CGAffineTransformIdentity;
// } completion:^(BOOL finished) {
// viewToShake.backgroundColor = [UIColor whiteColor];
// }];
// }
// }];
[UIView animateWithDuration:0.1 animations:^{
viewToShake.backgroundColor = [UIColor whiteColor];
} completion:^(BOOL finished) {
[UIView animateWithDuration:0.1 animations:^{
viewToShake.backgroundColor = [UIColor redColor];
} completion:^(BOOL finished) {
viewToShake.backgroundColor = [UIColor whiteColor];
}];
}];
}
+(UIAlertController *)getInstalledMapAppWithEndLocation:(CLLocationCoordinate2D)coord
address:(NSString*)address
currentLocation:(CLLocationCoordinate2D)currentCoord
{
//    调用地图
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:@"前往导航" preferredStyle:UIAlertControllerStyleActionSheet];
//百度地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"baidumap://"]]) {
UIAlertAction *baiduMapAction = [UIAlertAction actionWithTitle:@"百度地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *baiduParameterFormat = @"baidumap://map/direction?origin=latlng:%f,%f|name:我的位置&destination=latlng:%f,%f|name:终点&mode=driving";
NSString *urlString = [[NSString stringWithFormat:
baiduParameterFormat,
currentCoord.latitude,//用户当前的位置
currentCoord.longitude,//用户当前的位置
coord.latitude,
coord.longitude]
stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
if (@available(iOS 10, *)) {
/// 大于等于10.0系统使用此openURL方法
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
}else{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
}];
[actionSheet addAction:baiduMapAction];
}
//高德地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"iosamap://"]]) {
UIAlertAction *gaodeMapAction = [UIAlertAction actionWithTitle:@"高德地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *gaodeParameterFormat = @"iosamap://path?sourceApplication=%@&sid=&slat=&slon=&sname=&did=&dlat=%f&dlon=%f&dname=&dev=0&t=0";
NSString *urlString = [[NSString stringWithFormat:
gaodeParameterFormat,
@"运是滴车主",
coord.latitude,
coord.longitude]
stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
if (@available(iOS 10, *)) {
/// 大于等于10.0系统使用此openURL方法
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
}else{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
}];
[actionSheet addAction:gaodeMapAction];
}
//苹果地图
[actionSheet addAction:[UIAlertAction actionWithTitle:@"苹果地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
//起点
MKMapItem *currentLocation = [MKMapItem mapItemForCurrentLocation];
CLLocationCoordinate2D desCorrdinate = CLLocationCoordinate2DMake(coord.latitude, coord.longitude);
//终点
MKMapItem *toLocation = [[MKMapItem alloc] initWithPlacemark:[[MKPlacemark alloc] initWithCoordinate:desCorrdinate addressDictionary:nil]];
toLocation.name = address;
//默认驾车
[MKMapItem openMapsWithItems:@[currentLocation, toLocation]
launchOptions:@{MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving,
MKLaunchOptionsMapTypeKey:[NSNumber numberWithInteger:MKMapTypeStandard],
MKLaunchOptionsShowsTrafficKey:[NSNumber numberWithBool:YES]}];
}]];
//腾讯地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"qqmap://"]]) {
[actionSheet addAction:[UIAlertAction actionWithTitle:@"腾讯地图" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *QQParameterFormat = @"qqmap://map/routeplan?type=drive&fromcoord=%f, %f&tocoord=%f,%f&coord_type=1&policy=0&refer=%@";
NSString *urlString = [[NSString stringWithFormat:
QQParameterFormat,
currentCoord.latitude,//用户当前的位置
currentCoord.longitude,//用户当前的位置
coord.latitude,
coord.longitude,
@"运是滴车主"]
stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
if (@available(iOS 10, *)) {
/// 大于等于10.0系统使用此openURL方法
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
}else{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
}]];
}
//谷歌地图
if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"comgooglemaps://"]]) {
[actionSheet addAction:[UIAlertAction actionWithTitle:@"谷歌地图"style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
NSString *urlString = [[NSString stringWithFormat:@"comgooglemaps://?x-source=%@&x-success=%@&saddr=&daddr=%f,%f&directionsmode=driving",
@"运是滴车主",
@"wufengTrucks",
coord.latitude,
coord.longitude]
stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
if (@available(iOS 10, *)) {
/// 大于等于10.0系统使用此openURL方法
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString] options:@{} completionHandler:nil];
}else{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlString]];
}
}]];
}
//取消按钮
UIAlertAction *action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
[actionSheet dismissViewControllerAnimated:YES completion:nil];
}];
[actionSheet addAction:action3];
return actionSheet;
}
+(void)contactServices{
// TCallServicesView *v = [[TCallServicesView alloc] initWithFrame:CGRectMake(0, 0, 267, 255)];
// [[TGlobal sharedInstance] handleLayoutAlertViewBlockAndAnimation:v type:(YSDPopViewTypePopFromCenter)];
// [TGlobal sharedInstance].alertViewController.modal = YES;
}
//振动反馈
+(void)vibrationFeedback{
UIImpactFeedbackGenerator*impactLight = [[UIImpactFeedbackGenerator alloc]initWithStyle:UIImpactFeedbackStyleLight];
[impactLight impactOccurred];
}
+(void)showMessage:(NSString *)text{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:KEYWINDOW animated:YES];
hud.mode = MBProgressHUDModeText;
hud.bezelView.backgroundColor = [UIColor blackColor];
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.bezelView.color = [UIColor blackColor];
hud.label.textColor = [UIColor whiteColor];
hud.label.text = text;
[hud hideAnimated:YES afterDelay:1];
}
+(void)showMessage:(NSString *)text view:(UIView *)view{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.mode = MBProgressHUDModeText;
hud.bezelView.backgroundColor = [UIColor blackColor];
hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
hud.bezelView.color = [UIColor blackColor];
hud.label.textColor = [UIColor whiteColor];
hud.label.text = text;
[hud hideAnimated:YES afterDelay:1];
}
+(void)showLoadingInView:(UIView*)view{
MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
hud.mode = MBProgressHUDModeIndeterminate;
}
+(void)hideLoadingInView:(UIView*)view{
[MBProgressHUD hideHUDForView:view animated:YES];
}
+(void)jumpToControllerWithType:(NSInteger)type parameterId:(NSString *)parameterId{
}
+(NSString *)showDoubleNumberWithType:(NSInteger)type number:(double)number{
NSString *str = @"0.00";
if (type == 0) {
long result = (long)roundf(number*100);
str = [NSString stringWithFormat:@"%.2f",result/100.0];
}else if (type == 1){
long result = (long)ceilf(number*100);
str = [NSString stringWithFormat:@"%.2f",result/100.0];
}else if (type == 2){
long price = number*100;
long result = (long)floor(price);
str = [NSString stringWithFormat:@"%.2f",result/100.0];
}
return str;
}
+(void)toSetting{
NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
if([[UIApplication sharedApplication] canOpenURL:url]) {
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
}
}
+ (UIViewController*)topViewController{
return [self topViewControllerWithRootViewController:[UIApplication sharedApplication].keyWindow.rootViewController];
}
+(UIViewController*)topViewControllerWithRootViewController:(UIViewController*)rootViewController{
if ([rootViewController isKindOfClass:[UITabBarController class]]) {
UITabBarController *tabBarController = (UITabBarController *)rootViewController;
return [self topViewControllerWithRootViewController:tabBarController.selectedViewController];
} else if ([rootViewController isKindOfClass:[UINavigationController class]]) {
UINavigationController* navigationController = (UINavigationController*)rootViewController;
return [self topViewControllerWithRootViewController:navigationController.visibleViewController];
} else if (rootViewController.presentedViewController) {
UIViewController* presentedViewController = rootViewController.presentedViewController;
return [self topViewControllerWithRootViewController:presentedViewController];
} else {
return rootViewController;
}
}
//获取父控制器
+(UIViewController *)getViewControllerWithChildVC:(UIViewController*)childVC{
for (UIView* next = [childVC.view superview]; next; next = next.superview) {
UIResponder *nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]]) {
return (UIViewController *)nextResponder;
}
}
return nil;
}
//+(void)aliPayWithOrderNum:(NSString*)orderString
// resultBlcok:(void(^)(NSDictionary*dict))resultBlock{
// [[AlipaySDK defaultService] payOrder:orderString fromScheme:@"QXLive" callback:^(NSDictionary *resultDic) {
// NSLog(@"reslut = %@",resultDic);
// resultBlock(resultDic);
// }];
//}
//+(void)wexinPayWithDict:(NSDictionary*)dict{
//// [WXApi registerApp:dict[@"appid"] enableMTA:YES];
//// [WXApi registerApp:dict[@"appid"] universalLink:@"https://"]
// //需要创建这个支付对象
// PayReq *req = [[PayReq alloc] init];
// //由用户微信号和AppID组成的唯一标识用于校验微信用户
// req.openID = dict[@"appid"];
// // 商家id在注册的时候给的
// req.partnerId = dict[@"partnerid"];
// // 预支付订单这个是后台跟微信服务器交互后,微信服务器传给你们服务器的,你们服务器再传给你
// req.prepayId = dict[@"prepayid"];
// req.package = dict[@"package"];
// // 随机编码,为了防止重复的,在后台生成
// req.nonceStr = dict[@"noncestr"];
// // 这个是时间戳,也是在后台生成的,为了验证支付的
// req.timeStamp = [dict[@"timestamp"] intValue];
// // 这个签名也是后台做的
// req.sign = dict[@"sign"];
// [WXApi sendReq:req completion:^(BOOL success) {
//
// }];
//}
@end