RAUtils.m 61 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468
  1. //
  2. // RAUtils.m
  3. // RedAnt ERP Mobile
  4. //
  5. // Created by Ray on 9/17/15.
  6. // Copyright (c) 2015 United Software Applications, Inc. All rights reserved.
  7. //
  8. #import "RAUtils.h"
  9. #import "LoginViewController.h"
  10. #import "MainViewController.h"
  11. #import <sys/param.h>
  12. #import <sys/mount.h>
  13. #import "ZipArchive.h"
  14. @implementation RAUtils
  15. +(NSString*) getdbzip
  16. {
  17. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  18. NSString *documents = /*[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];*/[paths objectAtIndex:0];
  19. NSString *database_path = [documents stringByAppendingPathComponent:DBNAME];
  20. NSString* temp = NSTemporaryDirectory();
  21. NSString* uuid=[[NSUUID UUID] UUIDString];
  22. NSString *tempfile = [temp stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.zip",uuid]];
  23. ZipArchive* zip = [[ZipArchive alloc] init];
  24. BOOL result = [zip CreateZipFile2:tempfile Password:@"usai2010"];
  25. result = [zip addFileToZip:database_path newname:[database_path lastPathComponent]];
  26. return tempfile;
  27. }
  28. +(NSData*) getdbfile
  29. {
  30. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
  31. NSString *documents = /*[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];*/[paths objectAtIndex:0];
  32. NSString *database_path = [documents stringByAppendingPathComponent:DBNAME];
  33. return [NSData dataWithContentsOfFile:database_path];
  34. }
  35. +(NSDictionary*) dictfromfile:(NSString*) path
  36. {
  37. NSData *filedata = [NSData dataWithContentsOfFile:path];
  38. NSError *error = nil;
  39. NSDictionary *string2dic = [NSJSONSerialization JSONObjectWithData: filedata
  40. options: NSJSONReadingMutableContainers
  41. error: &error];
  42. // NSLog(@"%@",string2dic);
  43. return string2dic;
  44. }
  45. + (NSArray*) allFilesAtPath:(NSString*) dirString
  46. {
  47. NSMutableArray* array = [NSMutableArray arrayWithCapacity:10];
  48. NSFileManager* fileMgr = [NSFileManager defaultManager];
  49. NSArray* tempArray = [fileMgr contentsOfDirectoryAtPath:dirString error:nil];
  50. for (NSString* fileName in tempArray) {
  51. BOOL flag = YES;
  52. NSString* fullPath = [dirString stringByAppendingPathComponent:fileName];
  53. if ([fileMgr fileExistsAtPath:fullPath isDirectory:&flag]) {
  54. if (!flag) {
  55. [array addObject:fullPath];
  56. }
  57. }
  58. }
  59. return array;
  60. }
  61. + (bool)mergeContentsOfPath:(NSString *)srcDir intoPath:(NSString *)dstDir error:(NSError**)err {
  62. NSLog(@"- mergeContentsOfPath: %@\n intoPath: %@", srcDir, dstDir);
  63. NSFileManager *fm = [NSFileManager defaultManager];
  64. NSDirectoryEnumerator *srcDirEnum = [fm enumeratorAtPath:srcDir];
  65. NSString *subPath;
  66. while ((subPath = [srcDirEnum nextObject])) {
  67. NSLog(@" subPath: %@", subPath);
  68. NSString *srcFullPath = [srcDir stringByAppendingPathComponent:subPath];
  69. NSString *potentialDstPath = [dstDir stringByAppendingPathComponent:subPath];
  70. // Need to also check if file exists because if it doesn't, value of `isDirectory` is undefined.
  71. BOOL isDirectory = ([[NSFileManager defaultManager] fileExistsAtPath:srcFullPath isDirectory:&isDirectory] && isDirectory);
  72. // Create directory, or delete existing file and move file to destination
  73. if (isDirectory) {
  74. NSLog(@" create directory");
  75. [fm createDirectoryAtPath:potentialDstPath withIntermediateDirectories:YES attributes:nil error:err];
  76. if (err && *err) {
  77. NSLog(@"ERROR: %@", *err);
  78. return false;
  79. }
  80. }
  81. else {
  82. if ([fm fileExistsAtPath:potentialDstPath]) {
  83. NSLog(@" removeItemAtPath");
  84. [fm removeItemAtPath:potentialDstPath error:err];
  85. if (err && *err) {
  86. NSLog(@"ERROR: %@", *err);
  87. return false;
  88. }
  89. }
  90. NSLog(@" moveItemAtPath");
  91. [fm moveItemAtPath:srcFullPath toPath:potentialDstPath error:err];
  92. if (err && *err) {
  93. NSLog(@"ERROR: %@", *err);
  94. return false;
  95. }
  96. }
  97. }
  98. [fm removeItemAtPath:srcDir error:err];
  99. if (err && *err) {
  100. NSLog(@"ERROR: %@", *err);
  101. return false;
  102. }
  103. return true;
  104. }
  105. +(void) enum_font
  106. {
  107. return;
  108. NSArray *familys = [UIFont familyNames];
  109. for (int i = 0; i < familys.count; i++)
  110. {
  111. NSString *family = [familys objectAtIndex:i];
  112. NSLog(@"=====Fontfamily:%@", family);
  113. NSArray *fonts = [UIFont fontNamesForFamilyName:family];
  114. for(int j = 0; j < fonts.count; j++)
  115. {
  116. NSLog(@"***FontName:%@", [fonts objectAtIndex:j]);
  117. }
  118. }
  119. }
  120. + (CGRect)rectAlign:(CGRect )parent rect:(CGRect)rect hAlign:(NSString*)hAlign vAlign:(NSString*)vAlign
  121. {
  122. // double cx=parent.origin.x+parent.size.width/2;
  123. // double cy=parent.origin.y+parent.size.height/2;
  124. CGPoint centerpoint= CGPointMake(parent.origin.x+parent.size.width/2,parent.origin.y+parent.size.height/2);
  125. if([hAlign.lowercaseString isEqualToString:@"center"])
  126. {
  127. rect=CGRectMake(centerpoint.x-rect.size.width/2, rect.origin.y, rect.size.width, rect.size.height);
  128. }
  129. if([vAlign.lowercaseString isEqualToString:@"middle"])
  130. {
  131. rect=CGRectMake(rect.origin.x, centerpoint.y-rect.size.height/2, rect.size.width, rect.size.height);
  132. }
  133. return rect;
  134. }
  135. + (CGRect)rectVAlign:(CGRect )parent rect:(CGRect)rect vAlign:(NSString*)vAlign
  136. {
  137. // double cx=parent.origin.x+parent.size.width/2;
  138. // double cy=parent.origin.y+parent.size.height/2;
  139. CGPoint centerpoint= CGPointMake(parent.origin.x+parent.size.width/2,parent.origin.y+parent.size.height/2);
  140. if([vAlign.lowercaseString isEqualToString:@"middle"])
  141. {
  142. rect=CGRectMake(rect.origin.x, centerpoint.y-rect.size.height/2, rect.size.width, rect.size.height);
  143. }
  144. return rect;
  145. }
  146. + (CGRect)scaleToSize:(CGRect )from to:(CGSize)to
  147. {
  148. if(from.size.width/from.size.height>to.width/to.height)
  149. {
  150. return CGRectMake(from.origin.x, from.origin.y, to.width, to.width*from.size.height/from.size.width);
  151. }
  152. else
  153. {
  154. return CGRectMake(from.origin.x, from.origin.y, to.height*from.size.width/from.size.height, to.height);
  155. }
  156. // // 创建一个bitmap的context
  157. // // 并把它设置成为当前正在使用的context
  158. // UIGraphicsBeginImageContext(size);
  159. // // 绘制改变大小的图片
  160. // [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
  161. // // 从当前context中创建一个改变大小后的图片
  162. // UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  163. // // 使当前的context出堆栈
  164. // UIGraphicsEndImageContext();
  165. // // 返回新的改变大小后的图片
  166. //
  167. // // NSData *imageData=UIImageJPEGRepresentation(scaledImage, 1.f);
  168. // return scaledImage;
  169. }
  170. + (UIImage *)scaleToSize:(UIImage *)img size:(CGSize)size{
  171. // 创建一个bitmap的context
  172. // 并把它设置成为当前正在使用的context
  173. UIGraphicsBeginImageContext(size);
  174. // 绘制改变大小的图片
  175. [img drawInRect:CGRectMake(0, 0, size.width, size.height)];
  176. // 从当前context中创建一个改变大小后的图片
  177. UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext();
  178. // 使当前的context出堆栈
  179. UIGraphicsEndImageContext();
  180. // 返回新的改变大小后的图片
  181. // NSData *imageData=UIImageJPEGRepresentation(scaledImage, 1.f);
  182. return scaledImage;
  183. }
  184. +(UIImage*)img_compress:(UIImage*)image kbsize:(float) size
  185. {
  186. //UIImage *image=[UIImage imageNamed:@"xxoo.jpeg"];
  187. NSData *imageData=UIImageJPEGRepresentation(image, 1.f);
  188. if(size>imageData.length/1024)
  189. return image;
  190. // CGFloat size=40.f;// kb
  191. CGFloat scale=size/(imageData.length/1024);
  192. scale = sqrt (scale);
  193. CGSize newsize=image.size;
  194. newsize.height = newsize.height*scale;
  195. newsize.width = newsize.width*scale;
  196. return [RAUtils scaleToSize:image size:newsize];
  197. // NSData *newData=UIImageJPEGRepresentation(image, scale);
  198. // UIImage* ret= [[UIImage alloc] initWithData:newData];
  199. //
  200. // return ret;
  201. }
  202. +(NSString*) FloatFormat:(float)value
  203. {
  204. if (fmodf(value, 1)==0)
  205. {
  206. return [NSString stringWithFormat:@"%.0f",value];
  207. } else if (fmodf(value*10, 1)==0)
  208. {
  209. return [NSString stringWithFormat:@"%.1f",value];
  210. }
  211. else if (fmodf(value*100, 1)==0)
  212. {
  213. return [NSString stringWithFormat:@"%.2f",value];
  214. }
  215. else if (fmodf(value*1000, 1)==0)
  216. {
  217. return [NSString stringWithFormat:@"%.3f",value];
  218. }
  219. else
  220. {
  221. return [NSString stringWithFormat:@"%.4f",value];
  222. }
  223. return nil;
  224. }
  225. +(UIViewController*) getViewController:(UIView*) view
  226. {
  227. for (UIView* next = [view superview]; next; next = next.superview) {
  228. UIResponder* nextResponder = [next nextResponder];
  229. if ([nextResponder isKindOfClass:[UIViewController class]]) {
  230. return (UIViewController*)nextResponder;
  231. }
  232. }
  233. return nil;
  234. }
  235. + (float)fileSizeForDir:(NSString*)path//计算文件夹下文件的总大小
  236. {
  237. NSFileManager *fileManager = [[NSFileManager alloc] init];
  238. float size =0;
  239. NSArray* array = [fileManager contentsOfDirectoryAtPath:path error:nil];
  240. for(int i = 0; i<[array count]; i++)
  241. {
  242. NSString *fullPath = [path stringByAppendingPathComponent:[array objectAtIndex:i]];
  243. BOOL isDir;
  244. if ( !([fileManager fileExistsAtPath:fullPath isDirectory:&isDir] && isDir) )
  245. {
  246. NSDictionary *fileAttributeDic=[fileManager attributesOfItemAtPath:fullPath error:nil];
  247. size+= fileAttributeDic.fileSize/ 1024.0/1024.0;
  248. }
  249. else
  250. {
  251. size+=[self fileSizeForDir:fullPath];
  252. }
  253. }
  254. return size;
  255. }
  256. +(NSDictionary*) error_dict:(NSError*)error
  257. {
  258. if(error==nil)
  259. return nil;
  260. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  261. [ret setValue:[NSString stringWithFormat:@"%d",error.code] forKey:@"error_code"];
  262. [ret setValue:error.domain forKey:@"err_domain"];
  263. [ret setValue:[error localizedDescription] forKey:@"err_message"];
  264. // [ret setObject:error.userInfo forKey:@"user_info"];
  265. return ret;
  266. }
  267. +(NSString*) current_date
  268. {
  269. NSDate * date = [NSDate date];
  270. NSTimeInterval sec = [date timeIntervalSinceNow];
  271. NSDate * currentDate = [[NSDate alloc] initWithTimeIntervalSinceNow:sec];
  272. NSDateFormatter * df = [[NSDateFormatter alloc] init ];
  273. [df setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
  274. NSString * na = [df stringFromDate:currentDate];
  275. return na;
  276. }
  277. +(void) message_alert :(NSString*) msg title:(NSString*) title controller:(UIViewController*) vc
  278. {
  279. if(title==nil)
  280. title = @"Message";
  281. return [self alert_view:msg title:title];
  282. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:title message:nil preferredStyle:UIAlertControllerStyleAlert];
  283. //block代码块取代了delegate
  284. // [alertControl addTextFieldWithConfigurationHandler:^(UITextField *textField) {
  285. // textField.text = self.save_name;
  286. //
  287. //
  288. // }];
  289. // UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Yes" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  290. //
  291. // UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Delete Order"];
  292. // dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  293. //
  294. // NSDictionary* return_json = [iSalesNetwork delete_Order:orderid];
  295. //
  296. // dispatch_async(dispatch_get_main_queue(), ^{
  297. // [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  298. //
  299. //
  300. // if([[return_json valueForKey:@"result"] intValue]==2)
  301. // {
  302. //
  303. // [RAUtils error_alert:nil title:@"Order Delete"] ;
  304. // }
  305. // else
  306. // {
  307. // [RAUtils error_alert:[return_json valueForKey:@"err_msg"] title:@"Delete Order Failed."] ;
  308. // }
  309. //
  310. //
  311. //
  312. //
  313. // });
  314. // });
  315. //
  316. //
  317. // }];
  318. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  319. //NSLog(@"Cancel");
  320. }];
  321. // [alertControl addAction:actionOne];
  322. [alertControl addAction:alertthree];
  323. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  324. }];
  325. [alertControl addAction:alertcancel];
  326. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  327. [vc presentViewController:alertControl animated:YES completion:nil];
  328. return;
  329. }
  330. +(void) neworder:(UIViewController*) vc selectorholder:(id)holder selector:(SEL)addtocart
  331. {
  332. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait" title:@"Create Order"];
  333. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  334. NSDictionary* return_json = [iSalesNetwork new_Order];
  335. dispatch_async(dispatch_get_main_queue(), ^{
  336. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  337. if([[return_json valueForKey:@"result"] intValue]==2)
  338. {
  339. int result=[[return_json valueForKey:@"result"] intValue];
  340. if(result==2)
  341. {
  342. //successed.
  343. NSString* order_code = [return_json valueForKey:@"orderCode"];
  344. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  345. appDelegate.order_code = order_code;
  346. appDelegate.order_status = [[return_json valueForKey:@"orderStatus"] intValue];
  347. [holder performSelector:addtocart];
  348. // [self ReloadData];
  349. // if(self.shopCartBlock!=nil)
  350. // {
  351. // UIImage* img=[self photoStackView:self.photoStack photoForIndex:0];
  352. //
  353. //
  354. // CGRect iv_rect = CGRectMake(self.btnaddCart.center.x-50, self.btnaddCart.center.y-50, 100, 100);
  355. // UIImageView* iv = [[UIImageView alloc] initWithFrame:iv_rect];
  356. // iv.image = img;
  357. // // [self.contentView addSubview:iv];
  358. // self.shopCartBlock(iv);
  359. //
  360. // }
  361. }
  362. }
  363. else
  364. {
  365. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Create Order" controller:vc] ;
  366. }
  367. });
  368. });
  369. }
  370. +(void) add_to_cart:(UIViewController*) vc selectorholder:(id)holder selector:(SEL)addtocart
  371. {
  372. UIApplication * app = [UIApplication sharedApplication];
  373. AppDelegate *appDelegate = (AppDelegate *)[app delegate];
  374. MainViewController* main_vc=(MainViewController*)appDelegate.main_vc;
  375. if(appDelegate.bLogin==false)
  376. {
  377. LoginViewController * loginvc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
  378. loginvc.returnValue = ^(bool blogin){
  379. if(blogin)
  380. {
  381. if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE*/ true)
  382. {
  383. [main_vc checklogin:false];
  384. NSString* msg =@"";
  385. // if(appDelegate.contact_id.length>0)
  386. // {
  387. // msg = [msg stringByAppendingString:@"Customer:"];
  388. // msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  389. //
  390. // }
  391. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  392. //block代码块取代了delegate
  393. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Check for saved order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  394. // vc.disable_refresh = true;
  395. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  396. ovc.showNavibar = true;
  397. //ovc.customer_id = appDelegate.contact_id;
  398. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  399. [holder performSelector:addtocart];
  400. };
  401. ovc.init_style = OL_OPEN;
  402. ovc.onCancel = ^(){
  403. // self.disable_refresh = false;
  404. };
  405. [vc.navigationController pushViewController:ovc animated:true];
  406. }];
  407. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Create new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  408. {
  409. //create new;
  410. // self.disable_refresh = true;
  411. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  412. {
  413. ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  414. cvc.showNavibar = true;
  415. cvc.contact_type = @"Sales_Order_Customer";
  416. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  417. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  418. appDelegate.customerInfo = value;
  419. if(appDelegate.order_code==nil)
  420. [self neworder:vc selectorholder:holder selector:addtocart];
  421. // neworder();
  422. // [main_vc checklogin:true];
  423. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  424. //
  425. // if(self.returnValue)
  426. // self.returnValue(value);
  427. };
  428. cvc.onCancel = ^(){
  429. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc];
  430. // self.disable_refresh = false;
  431. };
  432. cvc.onReset = ^(){
  433. // [main_vc checklogin:true];
  434. };
  435. [vc.navigationController pushViewController:cvc animated:true];
  436. }
  437. else
  438. {
  439. [self neworder:vc selectorholder:holder selector:addtocart];
  440. }
  441. }
  442. NSLog(@"No");
  443. }];
  444. [alertControl addAction:actionOne];
  445. [alertControl addAction:alertthree];
  446. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  447. }];
  448. [alertControl addAction:alertcancel];
  449. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  450. [vc presentViewController:alertControl animated:YES completion:nil];
  451. }
  452. else
  453. {
  454. //customer login;
  455. [main_vc checklogin:false];
  456. //[self addtocart];
  457. [holder performSelector:addtocart];
  458. }
  459. }
  460. };
  461. UINavigationController* navi = [[UINavigationController alloc] initWithRootViewController:loginvc] ;
  462. // [self hackModalSheetSize:CGSizeMake(450, 200) ofVC:navi];
  463. navi.modalPresentationStyle = UIModalTransitionStyleCrossDissolve;//有三种状态,自己看看是哪种
  464. [vc presentViewController:navi animated:YES completion:^{
  465. // navi.view.superview.bounds = CGRectMake(0, 0, 480, 320);
  466. NSLog(@"LoginViewController present.........");
  467. // self.btop = false;
  468. // <#code#>
  469. }];
  470. }
  471. else
  472. {
  473. if(/*appDelegate.user_type==USER_ROLE_EMPLOYEE&&/*appDelegate.contact_id==nil&&*/appDelegate.order_code==nil)
  474. {
  475. if(appDelegate.contact_id.length==0)
  476. {
  477. NSString* msg =@"";
  478. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  479. //block代码块取代了delegate
  480. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Check for saved order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  481. // vc.disable_refresh = true;
  482. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  483. ovc.showNavibar = true;
  484. //ovc.customer_id = appDelegate.contact_id;
  485. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  486. [holder performSelector:addtocart];
  487. };
  488. ovc.init_style = OL_OPEN;
  489. ovc.onCancel = ^(){
  490. // self.disable_refresh = false;
  491. };
  492. [vc.navigationController pushViewController:ovc animated:true];
  493. }];
  494. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Create new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  495. {
  496. //create new;
  497. // self.disable_refresh = true;
  498. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  499. {
  500. ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  501. cvc.showNavibar = true;
  502. cvc.contact_type = @"Sales_Order_Customer";
  503. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  504. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  505. appDelegate.customerInfo = value;
  506. if(appDelegate.order_code==nil)
  507. [self neworder:vc selectorholder:holder selector:addtocart];
  508. // neworder();
  509. // [main_vc checklogin:true];
  510. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  511. //
  512. // if(self.returnValue)
  513. // self.returnValue(value);
  514. };
  515. cvc.onCancel = ^(){
  516. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc];
  517. // self.disable_refresh = false;
  518. };
  519. cvc.onReset = ^(){
  520. // [main_vc checklogin:true];
  521. };
  522. [vc.navigationController pushViewController:cvc animated:true];
  523. }
  524. else
  525. {
  526. [self neworder:vc selectorholder:holder selector:addtocart];
  527. }
  528. }
  529. NSLog(@"No");
  530. }];
  531. [alertControl addAction:actionOne];
  532. [alertControl addAction:alertthree];
  533. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  534. }];
  535. [alertControl addAction:alertcancel];
  536. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  537. [vc presentViewController:alertControl animated:YES completion:nil];
  538. }
  539. else
  540. {
  541. UIAlertView * waitalert = [RAUtils waiting_alert:@"Please wait..." title:@"Checking Pending Order"];
  542. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  543. NSDictionary* return_json = [iSalesNetwork request_PendingOrder:appDelegate.contact_id];
  544. dispatch_async(dispatch_get_main_queue(), ^{
  545. [waitalert dismissWithClickedButtonIndex:0 animated:FALSE];
  546. if([[return_json valueForKey:@"result"] intValue]==2)
  547. {
  548. bool openPendingOrder= [[return_json valueForKey:@"hasPending"] boolValue];
  549. bool createNewOrder=appDelegate.can_create_order;
  550. if(openPendingOrder&&createNewOrder)
  551. {
  552. NSString* msg =@"";
  553. if(appDelegate.contact_id.length>0)
  554. {
  555. msg = [msg stringByAppendingString:@"Customer:"];
  556. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  557. }
  558. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  559. //block代码块取代了delegate
  560. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Check for saved order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  561. // vc.disable_refresh = true;
  562. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  563. ovc.showNavibar = true;
  564. ovc.customer_id = appDelegate.contact_id;
  565. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  566. [holder performSelector:addtocart];
  567. };
  568. ovc.init_style = OL_OPEN;
  569. ovc.onCancel = ^(){
  570. // self.disable_refresh = false;
  571. };
  572. [vc.navigationController pushViewController:ovc animated:true];
  573. }];
  574. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Create new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  575. {
  576. //create new;
  577. // self.disable_refresh = true;
  578. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  579. {
  580. ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  581. cvc.showNavibar = true;
  582. cvc.contact_type = @"Sales_Order_Customer";
  583. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  584. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  585. appDelegate.customerInfo = value;
  586. if(appDelegate.order_code==nil)
  587. [self neworder:vc selectorholder:holder selector:addtocart];
  588. // neworder();
  589. // [main_vc checklogin:true];
  590. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  591. //
  592. // if(self.returnValue)
  593. // self.returnValue(value);
  594. };
  595. cvc.onCancel = ^(){
  596. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc];
  597. // self.disable_refresh = false;
  598. };
  599. cvc.onReset = ^(){
  600. // [main_vc checklogin:true];
  601. };
  602. [vc.navigationController pushViewController:cvc animated:true];
  603. }
  604. else
  605. {
  606. [self neworder:vc selectorholder:holder selector:addtocart];
  607. }
  608. }
  609. NSLog(@"No");
  610. }];
  611. [alertControl addAction:actionOne];
  612. [alertControl addAction:alertthree];
  613. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  614. }];
  615. [alertControl addAction:alertcancel];
  616. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  617. [vc presentViewController:alertControl animated:YES completion:nil];
  618. }
  619. else
  620. {
  621. if(openPendingOrder)
  622. {
  623. NSString* msg =@"";
  624. if(appDelegate.contact_id.length>0)
  625. {
  626. msg = [msg stringByAppendingString:@"Customer:"];
  627. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  628. }
  629. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  630. //block代码块取代了delegate
  631. UIAlertAction *actionOne = [UIAlertAction actionWithTitle:@"Check for saved order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  632. // vc.disable_refresh = true;
  633. OrderListViewController* ovc =[ vc.storyboard instantiateViewControllerWithIdentifier:@"OrderListViewController"];
  634. ovc.showNavibar = true;
  635. ovc.customer_id = appDelegate.contact_id;
  636. ovc.selectOrder = ^(NSMutableDictionary* order_detail){
  637. [holder performSelector:addtocart];
  638. };
  639. ovc.init_style = OL_OPEN;
  640. ovc.onCancel = ^(){
  641. // self.disable_refresh = false;
  642. };
  643. [vc.navigationController pushViewController:ovc animated:true];
  644. }];
  645. [alertControl addAction:actionOne];
  646. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  647. }];
  648. [alertControl addAction:alertcancel];
  649. // [alertControl addAction:alertthree];
  650. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  651. [vc presentViewController:alertControl animated:YES completion:nil];
  652. }
  653. else if(createNewOrder)
  654. {
  655. NSString* msg =@"";
  656. if(appDelegate.contact_id.length>0)
  657. {
  658. msg = [msg stringByAppendingString:@"Customer:"];
  659. msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  660. }
  661. UIAlertController *alertControl = [UIAlertController alertControllerWithTitle:@"Add to cart" message:msg preferredStyle:UIAlertControllerStyleAlert];
  662. //block代码块取代了delegate
  663. UIAlertAction *alertthree = [UIAlertAction actionWithTitle:@"Create new order" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  664. {
  665. //create new;
  666. // self.disable_refresh = true;
  667. if(appDelegate.customerInfo==nil)// select contact if current contact not exist
  668. {
  669. ContactListViewController* cvc = [vc.storyboard instantiateViewControllerWithIdentifier:@"ContactListViewController" ];
  670. cvc.showNavibar = true;
  671. cvc.contact_type = @"Sales_Order_Customer";
  672. cvc.returnValue = ^(NSMutableDictionary* value,NSIndexPath* source){
  673. appDelegate.contact_id=[value valueForKey:@"customer_cid"];
  674. appDelegate.customerInfo = value;
  675. if(appDelegate.order_code==nil)
  676. [self neworder:vc selectorholder:holder selector:addtocart];
  677. // neworder();
  678. // [main_vc checklogin:true];
  679. // [self handle_action_return:value indexPath:indexPath action:ACTION_FILL_SECTION];
  680. //
  681. // if(self.returnValue)
  682. // self.returnValue(value);
  683. };
  684. cvc.onCancel = ^(){
  685. [RAUtils message_alert:@"Cannot create order without cursomer infomation." title:@"New Order Error" controller:vc];
  686. // self.disable_refresh = false;
  687. };
  688. cvc.onReset = ^(){
  689. // [main_vc checklogin:true];
  690. };
  691. [vc.navigationController pushViewController:cvc animated:true];
  692. }
  693. else
  694. {
  695. [self neworder:vc selectorholder:holder selector:addtocart];
  696. }
  697. }
  698. NSLog(@"No");
  699. }];
  700. // [alertControl addAction:actionOne];
  701. [alertControl addAction:alertthree];
  702. UIAlertAction *alertcancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  703. }];
  704. [alertControl addAction:alertcancel];
  705. //UIAlertControllerStyle类型为UIAlertControllerStyleAlert可以添加addTextFieldWithConfigurationHandler:^(UITextField *textField)
  706. [vc presentViewController:alertControl animated:YES completion:nil];
  707. }
  708. else
  709. {
  710. [RAUtils message_alert:[return_json valueForKey:@"You donot have permission to access order"] title:@"Add To Cart" controller:vc];
  711. }
  712. }
  713. }
  714. else
  715. {
  716. [RAUtils message_alert:[return_json valueForKey:@"err_msg"] title:@"Check Pending Order" controller:vc] ;
  717. }
  718. });
  719. });
  720. }
  721. // // [main_vc checklogin:false];
  722. //
  723. // if(appDelegate.can_create_order)
  724. // {
  725. // NSString* msg =@"";
  726. // if(appDelegate.contact_id.length>0)
  727. // {
  728. // msg = [msg stringByAppendingString:@"\n\nCustomer:"];
  729. // msg = [msg stringByAppendingString:appDelegate.customerInfo[@"customer_name"]];
  730. //
  731. // }
  732. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:msg delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Check for saved order", nil),NSLocalizedString(@"Create new order", nil), nil];
  733. //
  734. // // alert.
  735. // [alert show];
  736. // }
  737. // else
  738. // {
  739. // UIAlertView * alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Add to cart", nil) message:NSLocalizedString(@"", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"Cancel", nil) otherButtonTitles:NSLocalizedString(@"Check for saved order", nil), nil];
  740. //
  741. // // alert.
  742. // [alert show];
  743. // }
  744. }
  745. else
  746. {
  747. // if(appDelegate.order_code==nil)
  748. // [ self neworder];
  749. // else
  750. [holder performSelector:addtocart];
  751. }
  752. }
  753. }
  754. +(void) add_recent_model:(NSDictionary*) model
  755. {
  756. NSMutableDictionary * newdict = [[NSMutableDictionary alloc]init];
  757. [newdict setObject:model forKey:@"item_0"];
  758. AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
  759. int count=[appDelegate.recent_model[@"count"] intValue];
  760. for(int i=0;i<count;i++)
  761. {
  762. NSMutableDictionary * mitem= appDelegate.recent_model[[NSString stringWithFormat:@"item_%d",i]];
  763. if([mitem[@"product_id"] isEqualToString:model[@"product_id"]])
  764. continue;
  765. [newdict setObject:mitem forKey:[NSString stringWithFormat:@"item_%lu",(unsigned long)newdict.allKeys.count]];
  766. }
  767. newdict[@"count"]=[NSString stringWithFormat:@"%lu",(unsigned long)newdict.allKeys.count];
  768. appDelegate.recent_model = newdict;
  769. //
  770. //
  771. // if(!bexist)
  772. // {
  773. // [appDelegate.recent_model setObject:item forKey:[NSString stringWithFormat:@"item_%d",count]];
  774. // appDelegate.recent_model[@"count"]=[NSString stringWithFormat:@"%d",count+1];
  775. // }
  776. }
  777. +(void) alert_view :(NSString*) msg title:(NSString*) title
  778. {
  779. if(title==nil)
  780. title = @"Message";
  781. if(msg.length>0)
  782. {
  783. title=[NSString stringWithFormat:@"%@\n\n%@",title,msg];
  784. }
  785. UIAlertView * alert = [[UIAlertView alloc] initWithTitle: title message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
  786. [alert show];
  787. }
  788. + (long long) freeDiskSpaceInMegaBytes{
  789. struct statfs buf;
  790. long long freespace = -1;
  791. if(statfs("/var", &buf) >= 0){
  792. freespace = (long long)(buf.f_bsize * buf.f_bfree);
  793. }
  794. NSLog([NSString stringWithFormat:@"手机剩余存储空间为:%qi MB" ,freespace/1024/1024]);
  795. return freespace/1024/1024;
  796. }
  797. +(UIAlertView * ) waiting_alert :(NSString*) msg title:(NSString*) title
  798. {
  799. if(title==nil)
  800. title = @"Please Wait";
  801. if(msg==nil)
  802. msg= @"Waiting...";
  803. NSAssert(msg!=nil, @"error message from json is nil");
  804. UIAlertView * alert = [[UIAlertView alloc] initWithTitle:title message:msg delegate:nil cancelButtonTitle:nil otherButtonTitles:nil, nil];
  805. [alert show];
  806. //
  807. //
  808. // UIActivityIndicatorView *aiView = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(125.0, 80.0, 30.0, 30.0)];
  809. // aiView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge;
  810. // // check if os version is 7 or above. ios7.0及以上UIAlertView弃用了addSubview方法
  811. //// if ([[[UIDevice currentDevice] systemVersion] compare:@"7.0"] != NSOrderedAscending) {
  812. //// [alert setValue:aiView forKey:@"accessoryView"];
  813. //// }else{
  814. //// [alert addSubview:aiView];
  815. //// }
  816. //
  817. // aiView.hidden = false;
  818. // aiView.hidesWhenStopped = false;
  819. // [aiView startAnimating];
  820. //
  821. //[alert addSubview:aiView];
  822. return alert;
  823. //return nil;
  824. // return alert;
  825. // UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Error!" message:@"User&Password can not be empty!" delegate:nil cancelButtonTitle:NSLocalizedString(@"ok", nil) , nil];
  826. //[alert show];
  827. }
  828. +(NSDictionary*) device_info
  829. {
  830. NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
  831. NSString* build =[infoDict objectForKey:@"CFBundleVersion"];
  832. NSString* version =[infoDict objectForKey:@"CFBundleShortVersionString"];
  833. NSString* versionNum = [NSString stringWithFormat:@"Version: %@ Build %@",version,build];
  834. NSMutableDictionary * info = [[NSMutableDictionary alloc]init];
  835. [info setValue:[[UIDevice currentDevice] name] forKey:@"name"];
  836. [info setValue:[[UIDevice currentDevice] systemVersion] forKey:@"systemVersion"];
  837. [info setValue:[[UIDevice currentDevice] model] forKey:@"model"];
  838. [info setValue:versionNum forKey:@"ver"];
  839. [info setValue:[[UIDevice currentDevice] localizedModel] forKey:@"localizedModel"];
  840. return info;
  841. // [info setValue:[[UIDevice currentDevice] name] forKey:@"name"];
  842. // [info setValue:[[UIDevice currentDevice] name] forKey:@"name"];
  843. // [info setValue:[[UIDevice currentDevice] name] forKey:@"name"];
  844. }
  845. +(NSArray*) string2arr:(NSString*) string separator:(NSString*)separator
  846. {
  847. NSArray *stringArray = [string componentsSeparatedByString:separator];
  848. return stringArray;
  849. }
  850. +(NSString*) arr2string:(NSArray *) arr separator:(NSString*)separator trim:(bool) btrim
  851. {
  852. if(arr.count==0)
  853. return nil;
  854. NSMutableArray * marr = [arr mutableCopy];
  855. begin:
  856. for (NSString* item in marr) {
  857. if(item.length==0 )
  858. if( btrim)
  859. {
  860. [marr removeObject:item];
  861. goto begin;
  862. }
  863. }
  864. NSString * ret = [marr componentsJoinedByString:separator];
  865. return ret;
  866. }
  867. +(NSString*) arr2string:(NSArray *) arr separator:(NSString*)separator trim:(bool) btrim brackets:(NSString*)brackets
  868. {
  869. if(brackets!=nil)
  870. separator = [NSString stringWithFormat:@"%@%@%@",brackets,separator,brackets];
  871. NSMutableArray * marr = [arr mutableCopy];
  872. begin:
  873. for (NSString* item in marr) {
  874. if(item.length==0 )
  875. if( btrim)
  876. {
  877. [marr removeObject:item];
  878. goto begin;
  879. }
  880. }
  881. NSString * ret = [marr componentsJoinedByString:separator];
  882. if(brackets!=nil)
  883. ret = [NSString stringWithFormat:@"%@%@%@",brackets,ret,brackets];
  884. return ret;
  885. }
  886. +(NSDictionary*) string2dict:(NSString*) str
  887. {
  888. if(str==nil)
  889. return nil;
  890. NSError *error = nil;
  891. NSDictionary *string2dic = [NSJSONSerialization JSONObjectWithData: [str dataUsingEncoding:NSUTF8StringEncoding]
  892. options: NSJSONReadingMutableContainers
  893. error: &error];
  894. NSLog(@"%@",string2dic);
  895. return string2dic;
  896. }
  897. +(UIColor*) strColor:(NSString*) color
  898. {
  899. if([color.lowercaseString isEqualToString:@"red"])
  900. return [UIColor redColor];
  901. return [UIColor blackColor];
  902. }
  903. +(NSData*) dict2data:(NSDictionary*) dict
  904. {
  905. if(dict==nil)
  906. return nil;
  907. // 将NSDictionary转化为NSData
  908. NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil];
  909. return data;
  910. }
  911. +(NSString*) dict2string:(NSDictionary*) dict
  912. {
  913. if(dict==nil)
  914. return nil;
  915. // 将NSDictionary转化为NSData
  916. NSData *data = [NSJSONSerialization dataWithJSONObject:dict options:kNilOptions error:nil];
  917. // 再将NSData转为字符串
  918. NSString *jsonStr = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  919. return jsonStr;
  920. }
  921. +(NSString*) base64en:(NSString*) string
  922. {
  923. if(string == nil)
  924. return nil;
  925. NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
  926. NSString *stringBase64 = [data base64EncodedStringWithOptions:0]; // base64格式的字符串
  927. return stringBase64;
  928. }
  929. +(NSString*) base64de:(NSString*) stringBase64
  930. {
  931. if(stringBase64==nil)
  932. return nil;
  933. NSData *data = [[NSData alloc] initWithBase64EncodedString:stringBase64 options:NSDataBase64DecodingIgnoreUnknownCharacters];
  934. NSString *string =[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
  935. return string;
  936. }
  937. +(void) deletefiles :(NSString*) path
  938. {
  939. // NSString *extension = @"m4r";
  940. NSFileManager *fileManager = [NSFileManager defaultManager];
  941. // NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  942. // NSString *documentsDirectory = [paths objectAtIndex:0];
  943. NSArray *contents = [fileManager contentsOfDirectoryAtPath:path error:NULL];
  944. NSEnumerator *e = [contents objectEnumerator];
  945. NSString *filename;
  946. while ((filename = [e nextObject])) {
  947. bool result= [fileManager removeItemAtPath:[path stringByAppendingPathComponent:filename] error:NULL];
  948. if(!result)
  949. NSLog(@"delete file failed %@------%@",path,filename);
  950. }
  951. }
  952. +(NSMutableArray*)dictionary2array:(NSDictionary*)json count_fields:(NSString*) count_fields item_mark:(NSString*) item_mark items_mark:(NSString* )items_mark
  953. {
  954. if(json==nil)
  955. return nil;
  956. NSMutableArray* ret = [[NSMutableArray alloc] init];
  957. int count = [[json valueForKey:count_fields] intValue];
  958. NSDictionary* items = nil;
  959. if(items_mark==nil)
  960. items = json;
  961. else
  962. items = [json objectForKey:items_mark];
  963. for(int i=0;i<count;i++)
  964. {
  965. NSDictionary* obj = [items objectForKey:[NSString stringWithFormat:@"%@%d",item_mark,i]];
  966. [ret addObject:obj];
  967. }
  968. return ret;
  969. }
  970. +(NSDictionary*) error_json :(int)code err_msg:(NSString*)msg
  971. {
  972. NSMutableDictionary* ret = [[NSMutableDictionary alloc] init];
  973. //#define RESULT_FALSE 0
  974. //#define RESULT_TRUE 2
  975. //#define RESULT_NET_ERROR -3
  976. //#define RESULT_NET_NOTAVAILABLE -4
  977. //#define RESULT_ERROR -5
  978. //#define RESULT_LOCALFILE_ERROR -7
  979. //#define RESULT_USERAUTH_ERROR -9
  980. //#define RESULT_UPDATE_USERAUTH_ERROR -11
  981. //#define RESULT_SESSION_EXPIRED -13
  982. //#define RESULT_VER_LOW
  983. if(msg.length<=0)
  984. {
  985. switch (code) {
  986. case RESULT_NET_NOTAVAILABLE:
  987. msg= MSG_NET_NOTAVAILABLE;
  988. break;
  989. default:
  990. // assert(@"UNDEFINE ERROR CODE!");
  991. break;
  992. }
  993. }
  994. // if(code==RESULT_NET_NOTAVAILABLE)
  995. // [ret setValue:[NSString stringWithFormat:@"%d",RESULT_NET_ERROR] forKey:@"result"];
  996. // else
  997. [ret setValue:[NSString stringWithFormat:@"%d",code] forKey:@"result"];
  998. [ret setValue:msg forKey:@"err_msg"];
  999. // NSData *jsonData = [NSJSONSerialization dataWithJSONObject:ret
  1000. // options:0
  1001. // error:nil];
  1002. return ret;
  1003. }
  1004. + (CGRect)relativeFrameForScreenWithView:(UIView *)v
  1005. {
  1006. UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
  1007. CGRect rect=[v convertRect: v.bounds toView:window];
  1008. return rect;
  1009. // BOOL iOS7 = [[[UIDevice currentDevice] systemVersion] floatValue] >= 7;
  1010. //
  1011. // CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
  1012. // if (!iOS7) {
  1013. // screenHeight -= 20;
  1014. // }
  1015. // UIView *view = v;
  1016. // CGFloat x = .0;
  1017. // CGFloat y = .0;
  1018. // while (view.frame.size.width != 320 || view.frame.size.height != screenHeight) {
  1019. // x += view.frame.origin.x;
  1020. // y += view.frame.origin.y;
  1021. // view = view.superview;
  1022. // if ([view isKindOfClass:[UIScrollView class]]) {
  1023. // x -= ((UIScrollView *) view).contentOffset.x;
  1024. // y -= ((UIScrollView *) view).contentOffset.y;
  1025. // }
  1026. // }
  1027. // return CGRectMake(x, y, v.frame.size.width, v.frame.size.height);
  1028. }
  1029. @end